From a4ec31751f9e4ae1bab6d54d42c1dba7c5b60b62 Mon Sep 17 00:00:00 2001 From: Glenn Hollingsworth Date: Mon, 14 May 2018 15:21:19 -0700 Subject: [PATCH] - Simplify checktout process by always scanning Game first --- app/assets/javascripts/checkouts.js | 152 +++++++++++++----------- app/controllers/checkouts_controller.rb | 34 ++++-- app/models/checkout.rb | 26 ---- app/views/checkouts/index.html.erb | 13 +- 4 files changed, 111 insertions(+), 114 deletions(-) diff --git a/app/assets/javascripts/checkouts.js b/app/assets/javascripts/checkouts.js index ad92fbc..62e8745 100644 --- a/app/assets/javascripts/checkouts.js +++ b/app/assets/javascripts/checkouts.js @@ -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(); @@ -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 + '"]'); @@ -79,54 +137,14 @@ $(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"); @@ -134,12 +152,16 @@ $(document).ready(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'); @@ -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(); } \ No newline at end of file diff --git a/app/controllers/checkouts_controller.rb b/app/controllers/checkouts_controller.rb index 3ae1c49..acb6922 100644 --- a/app/controllers/checkouts_controller.rb +++ b/app/controllers/checkouts_controller.rb @@ -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) diff --git a/app/models/checkout.rb b/app/models/checkout.rb index cf58690..87eed9d 100644 --- a/app/models/checkout.rb +++ b/app/models/checkout.rb @@ -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 diff --git a/app/views/checkouts/index.html.erb b/app/views/checkouts/index.html.erb index 453dbc8..cb43845 100644 --- a/app/views/checkouts/index.html.erb +++ b/app/views/checkouts/index.html.erb @@ -1,9 +1,9 @@
- - -

+ + +

@@ -11,12 +11,11 @@
-