Skip to content
This repository has been archived by the owner on Sep 18, 2020. It is now read-only.

Commit

Permalink
- Simplify checktout process by always scanning Game first
Browse files Browse the repository at this point in the history
  • Loading branch information
Mebibyte committed Aug 8, 2018
1 parent 5f192e8 commit a4ec317
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 114 deletions.
152 changes: 82 additions & 70 deletions app/assets/javascripts/checkouts.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,65 @@
$(document).ready(function(){

// Make a call to /return when a new barcode is entered.
$('#g-barcode').change(function(){
var barcode_val = $(this).val();

if(!bc_regex.test(barcode_val)){
$.notify('Invalid barcode format! Barcode should be at least 3 characters long and only contain alphanumeric characters.', 'warning', 5000);
$(this).val('');
return;
}
gameBarcode(false);

$.post('/return', { barcode: barcode_val }).success(function(response){
if(response.errors){
$.each(response.errors, function(k, v){
$.notify(v, 'danger');
});
gameBarcode(true);
}else if(response.time){
$.notify('Successfully returned ' + response.game + '!', 5000);
resetCheckout();
}else{
$('#g-name').text('Checking out: ' + response.game)
$('#a-row').show();
$('#a-barcode').focus();
}
}).error(function(){
$.notify(DEFAULT_ERROR, 'danger');
gameBarcode(true);
});
});

// Make a call to /attendee/status when a new barcode is entered.
$('#a-barcode').change(function(){
var barcode_val = $(this).val();

if(!bc_regex.test(barcode_val)){
$.notify('Invalid barcode format! Barcode should be at least 7 characters long and only contain alphanumeric characters.', 'warning', 5000);
$.notify('Invalid barcode format! Barcode should be at least 3 characters long and only contain alphanumeric characters.', 'warning', 5000);
$(this).val('');
return;
}
attendeeBarcode(false);

$.get('attendee/status', { barcode: barcode_val }).success(function(response){
$('#a-name').text(response.attendee.name);
$('#g-row').show();
$('#g-barcode').focus();
displayCheckouts(response.checkouts);
$.post('checkout/new', { g_barcode: $('#g-barcode').val(), a_barcode: barcode_val }).success(function(response){
if(response.errors){
$.each(response.errors, function(k, v){
$.notify(v, 'danger');
});
}else{
$.notify('Successfully checked out ' + response.game + '!');
resetCheckout();
}
if(response.approval){
$.notify(response.approval, 'success', 8000);
}
}).error(function(){
$.notify(DEFAULT_ERROR, 'danger');
}).complete(function(){
attendeeBarcode(true);
});
}).error(function(response){
if(response.status == 400){
$('#a-form').modal();
Expand Down Expand Up @@ -55,10 +99,24 @@ $(document).ready(function(){
$.post('attendee/new', data).success(function(response){
if(response.attendee){
$('#a-form').modal('hide');
$('#a-name').text(response.attendee.name);
$('#g-row').show();
$('#g-barcode').focus();
}else{
$.post('checkout/new', { g_barcode: $('#g-barcode').val(), a_barcode: $('#a-barcode').val() }).success(function(response){
if(response.errors){
$.each(response.errors, function(k, v){
$.notify(v, 'danger');
});
}else{
$.notify('Successfully checked out ' + response.game + '!');
resetCheckout();
}
if(response.approval){
$.notify(response.approval, 'success', 8000);
}
}).error(function(){
$.notify(DEFAULT_ERROR, 'danger');
}).complete(function(){
attendeeBarcode(true);
});
} else {
// got errors
$.each(response.errors, function(k, v){
var input = $('[name="' + k + '"]');
Expand All @@ -79,67 +137,31 @@ $(document).ready(function(){
$('#a-form-save').click(saveAttendee);
$('#a-form').find('input[type="text"]').keypress(saveAttendeeByEnter);

$('#g-barcode').change(function(){
var barcode_val = $(this).val();

if(!bc_regex.test(barcode_val)){
$.notify('Invalid barcode format! Barcode should be at least 7 characters long and only contain alphanumeric characters.', 'warning', 5000);
$(this).val('');
return;
}

$.post('checkout/new', { g_barcode: barcode_val, a_barcode: $('#a-barcode').val() }).success(function(response){
if(response.errors){
$.each(response.errors, function(k, v){
$.notify(v, 'danger');
});
}else if(response.checkouts.length <= 1){
$.notify(response.message);
resetCheckout();
}else{
$.notify(response.message);
displayCheckouts(response.checkouts);
}
if(response.approval){
$.notify(response.approval, 'success', 8000);
}
}).error(function(){
$.notify(DEFAULT_ERROR, 'danger');
}).complete(function(){
$('#g-barcode').val('');
});
});

$('#games-container').delegate('.return-game', 'click', function(){
var _me = $(this);
$.post('/return', { co_id: _me.data('checkout-id') }).success(function(){
$.notify('Returned game successfully!');
_me.closest('.row').remove();
if($('#games-container').children().length <= 0){
resetCheckout();
}
}).error(function(){
$.notify(DEFAULT_ERROR, 'danger');
});
$('#find-barcode').change(function(){
$.get('/find', $(this).serialize(), null, 'script');
});

$('#found-div').delegate('.return-game', 'click', function(){
var _me = $(this);
$.post('/return', { co_id: _me.data('checkout-id') }).success(function(response){
$.notify('Returned game successfully!');
$.notify('Successfully returned ' + response.game + '!', 5000);
var cell = _me.closest('.col-xs-2');
cell.html(response.time);
cell.next().html("RETURNED");
}).error(function(){
$.notify(DEFAULT_ERROR, 'danger');
});
});

$('#find-barcode').change(function(){
$.get('/find', $(this).serialize(), null, 'script');
});

});
function gameBarcode(bool){
var barcode = $('#g-barcode');

barcode.prop('disabled', !bool);
if(bool){
barcode.val('').focus();
}
$('#checkouts-x-btn').toggle(!bool);
}

function attendeeBarcode(bool){
var barcode = $('#a-barcode');
Expand All @@ -148,21 +170,11 @@ function attendeeBarcode(bool){
if(bool){
barcode.val('').focus();
}
$('#checkouts-x-btn').toggle(!bool);
}

function resetCheckout(){
attendeeBarcode(true);
$('#a-name').text('');
gameBarcode(true);
$('#g-name').text('');
$('#g-barcode').val('');
$('#g-row').hide();
$('#games-container').html('');
}

function displayCheckouts(checkouts){
var container = $('#games-container');
container.html('');
$.each(checkouts, function(o, v){
container.append(v);
});
$('#a-row').hide();
}
34 changes: 23 additions & 11 deletions app/controllers/checkouts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,40 @@ def index
end

def new
result = Checkout.checkout_or_return_game(params.permit(:a_barcode, :g_barcode))
checkout = Checkout.new_checkout(params.permit(:a_barcode, :g_barcode))

if result[:checkout].errors.messages.blank?
if checkout.errors.messages.blank?
render json: {
approval: result[:checkout].approval_tag,
checkouts: result[:checkout].attendee.open_co.order(check_out_time: :desc).map do |co|
render_to_string('games/checked_out_template', locals: { checkout: co }, layout: false)
end,
message: result[:message]
approval: checkout.approval_tag,
game: checkout.game.name
}
else
render json: {
errors: result[:checkout].errors
errors: checkout.errors.messages
}
end
end

def return
checkout = Checkout.find(params[:co_id])
checkout.return
if params[:barcode]
game = Game.get(params[:barcode])
if !game
render json: { errors: ['Game not found!'] }
return
end
checkout = game.open_checkout
if checkout
checkout.return
render json: { time: ct(checkout.return_time).strftime('%m/%d %I:%M%P'), game: game.name }
else
render json: { game: game.name }
end
elsif params[:co_id]
checkout = Checkout.find(params[:co_id])
checkout.return

render json: { time: ct(checkout.return_time).strftime('%m/%d %I:%M%P') }
render json: { time: ct(checkout.return_time).strftime('%m/%d %I:%M%P'), game: checkout.game.name }
end
end

def ct(datetime)
Expand Down
26 changes: 0 additions & 26 deletions app/models/checkout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,32 +60,6 @@ def self.new_checkout(params)
Checkout.create(game: Game.get(params[:g_barcode]), attendee: Attendee.get(params[:a_barcode]))
end

def self.checkout_or_return_game(params)
checkout = Checkout.create(game: Game.get(params[:g_barcode]), attendee: Attendee.get(params[:a_barcode]))

if checkout.errors.messages.blank?
return {
message: 'Game successfully checked out!',
checkout: checkout
}
else
if !checkout.game.nil? && checkout.game.checked_out?
check = checkout.game.open_checkout
if check.attendee.barcode == params[:a_barcode]
check.return
return {
message: 'Game successfully returned!',
checkout: check
}
end
end
end

return {
checkout: checkout
}
end

def return
self.return_time = Time.now.utc
self.closed = true
Expand Down
13 changes: 6 additions & 7 deletions app/views/checkouts/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
<div class="row">
<div class="col-xs-3"></div>
<div class="col-xs-6 glyphicon-custom-left">
<input type="text" id="a-barcode" class="form-control" placeholder="Enter ATTENDEE's barcode here." autofocus />
<span class="glyphicon glyphicon-user form-control-feedback"></span>
<h3 id="a-name"></h3>
<input type="text" id="g-barcode" class="form-control" placeholder="Enter GAME barcode to checkout or return." autofocus />
<span class="glyphicon glyphicon-tower form-control-feedback"></span>
<h3 id="g-name"></h3>
</div>
<div class="col-xs-1">
<input id="checkouts-x-btn" type="button" class="btn btn-default" style="display:none;" value="X" />
</div>
<div class="col-xs-2"></div>
</div>

<div class="row" id="g-row" style="display:none;">
<div class="row" id="a-row" style="display:none;">
<div class="col-xs-3"></div>
<div class="col-xs-6 glyphicon-custom-left">
<input type="text" id="g-barcode" class="form-control" placeholder="Enter GAME barcode to checkout or return game." />
<span class="glyphicon glyphicon-tower form-control-feedback"></span>
<div id="games-container" style="padding: 15px 15px;"></div>
<input type="text" id="a-barcode" class="form-control" placeholder="Enter ATTENDEE's barcode to checkout game." />
<span class="glyphicon glyphicon-user form-control-feedback"></span>
</div>
<div class="col-xs-1"></div>
<div class="col-xs-2"></div>
Expand Down

0 comments on commit a4ec317

Please sign in to comment.