diff --git a/addons/hr_attendance/static/src/js/greeting_message.js b/addons/hr_attendance/static/src/js/greeting_message.js index 57191d0965b..16f1fc0c935 100644 --- a/addons/hr_attendance/static/src/js/greeting_message.js +++ b/addons/hr_attendance/static/src/js/greeting_message.js @@ -5,7 +5,6 @@ var AbstractAction = require('web.AbstractAction'); var core = require('web.core'); var _t = core._t; -var previously_barcode_scanned; var GreetingMessage = AbstractAction.extend({ @@ -55,17 +54,19 @@ var GreetingMessage = AbstractAction.extend({ }, willStart: function() { - if (this.attendance != undefined){ - this._rpc({ + var self = this; + var def; + if (this.attendance && this.attendance.employee_id) { + def = this._rpc({ model: 'hr.employee', method: 'read', args: [this.attendance.employee_id[0], ['barcode']], }) .then(function (employee) { - previously_barcode_scanned = employee[0].barcode; + self.attendanceBarcode = employee[0].barcode; }); } - return this._super.apply(this, arguments); + return $.when(this._super.apply(this, arguments), def); }, start: function() { @@ -151,11 +152,11 @@ var GreetingMessage = AbstractAction.extend({ _onBarcodeScanned: function(barcode) { var self = this; - if (this.return_to_main_menu) { // in case of multiple scans in the greeting message view, delete the timer, a new one will be created. - clearTimeout(this.return_to_main_menu); - } - if (previously_barcode_scanned != barcode){ - previously_barcode_scanned = barcode; + if (this.attendanceBarcode !== barcode){ + if (this.return_to_main_menu) { // in case of multiple scans in the greeting message view, delete the timer, a new one will be created. + clearTimeout(this.return_to_main_menu); + } + core.bus.off('barcode_scanned', this, this._onBarcodeScanned); this._rpc({ model: 'hr.employee', method: 'attendance_scan', diff --git a/addons/hr_attendance/static/src/js/kiosk_mode.js b/addons/hr_attendance/static/src/js/kiosk_mode.js index 361977717fe..ead0c1cec2c 100644 --- a/addons/hr_attendance/static/src/js/kiosk_mode.js +++ b/addons/hr_attendance/static/src/js/kiosk_mode.js @@ -58,6 +58,7 @@ var KioskMode = AbstractAction.extend({ }, destroy: function () { + core.bus.off('barcode_scanned', this, this._onBarcodeScanned); clearInterval(this.clock_start); clearInterval(this._interval); this._super.apply(this, arguments); diff --git a/addons/hr_attendance/static/tests/hr_attendance_tests.js b/addons/hr_attendance/static/tests/hr_attendance_tests.js index c8e7c298162..227d2af36b1 100644 --- a/addons/hr_attendance/static/tests/hr_attendance_tests.js +++ b/addons/hr_attendance/static/tests/hr_attendance_tests.js @@ -2,8 +2,11 @@ odoo.define('hr_attendance.tests', function (require) { "use strict"; var testUtils = require('web.test_utils'); +var core = require('web.core'); var MyAttendances = require('hr_attendance.my_attendances'); +var KioskMode = require('hr_attendance.kiosk_mode'); +var GreetingMessage = require('hr_attendance.greeting_message'); QUnit.module('HR Attendance', { @@ -19,12 +22,30 @@ QUnit.module('HR Attendance', { default: 1, }, user_id: {string: 'user ID', type: 'integer'}, + barcode: {string:'barcode', type: 'integer'}, }, records: [{ id: 1, name: "Employee A", attendance_state: 'checked_out', user_id: 1, + barcode: 1, + }, + { + id: 2, + name: "Employee B", + attendance_state: 'checked_out', + user_id: 2, + barcode: 2, + }], + }, + 'res.company': { + fields: { + name: {string: 'Name', type: 'char'}, + }, + records: [{ + id: 1, + name: "Company A", }], }, }; @@ -51,6 +72,111 @@ QUnit.module('HR Attendance', { clientAction.destroy(); }); + QUnit.test('Attendance Kiosk Mode Test', function (assert) { + assert.expect(2); + + var $target = $('#qunit-fixture'); + var self = this; + var rpcCount = 0; + var clientAction = new KioskMode(null); + testUtils.addMockEnvironment(clientAction, { + data: this.data, + session: { + uid: 1, + company_id: 1, + }, + mockRPC: function(route, args) { + if (args.method === 'attendance_scan' && args.model === 'hr.employee') { + + rpcCount++; + return $.when(self.data['hr.employee'].records[0]); + } + return this._super(route, args); + }, + }); + clientAction.appendTo($target); + core.bus.trigger('barcode_scanned', 1); + core.bus.trigger('barcode_scanned', 1); + assert.strictEqual(rpcCount, 1, 'RPC call should have been done only once.'); + + core.bus.trigger('barcode_scanned', 2); + assert.strictEqual(rpcCount, 1, 'RPC call should have been done only once.'); + + clientAction.destroy(); + }); + + QUnit.test('Attendance Greeting Message Test', function (assert) { + assert.expect(10); + + var $target = $('#qunit-fixture'); + var self = this; + var rpcCount = 0; + + var clientActions = []; + function createGreetingMessage (target, barcode){ + var action = { + attendance: { + check_in: "2018-09-20 13:41:13", + employee_id: [barcode], + }, + next_action: "hr_attendance.hr_attendance_action_kiosk_mode", + } + var clientAction = new GreetingMessage(null, action); + testUtils.addMockEnvironment(clientAction, { + data: self.data, + session: { + uid: 1, + company_id: 1, + }, + mockRPC: function(route, args) { + if (args.method === 'attendance_scan' && args.model === 'hr.employee') { + rpcCount++; + action.attendance.employee_id = [args.args[0], 'Employee']; + /* + if rpc have been made, a new instance is created to simulate the same behaviour + as functional flow. + */ + createGreetingMessage (target, args.args[0]); + return $.when({action: action}); + } + return this._super(route, args); + }, + }); + clientAction.appendTo(target); + + clientActions.push(clientAction); + }; + + // init - mock coming from kiosk + createGreetingMessage ($target, 1); + assert.strictEqual(clientActions.length, 1, 'Number of clientAction must = 1.'); + + core.bus.trigger('barcode_scanned', 1); + /* + As action is given when instantiate GreetingMessage, we simulate that we come from the KioskMode + So rescanning the same barcode won't lead to another RPC. + */ + assert.strictEqual(clientActions.length, 1, 'Number of clientActions must = 1.'); + assert.strictEqual(rpcCount, 0, 'RPC call should not have been done.'); + + core.bus.trigger('barcode_scanned', 2); + assert.strictEqual(clientActions.length, 2, 'Number of clientActions must = 2.'); + assert.strictEqual(rpcCount, 1, 'RPC call should have been done only once.'); + core.bus.trigger('barcode_scanned', 2); + assert.strictEqual(clientActions.length, 2, 'Number of clientActions must = 2.'); + assert.strictEqual(rpcCount, 1, 'RPC call should have been done only once.'); + + core.bus.trigger('barcode_scanned', 1); + assert.strictEqual(clientActions.length, 3, 'Number of clientActions must = 3.'); + core.bus.trigger('barcode_scanned', 1); + assert.strictEqual(clientActions.length, 3, 'Number of clientActions must = 3.'); + assert.strictEqual(rpcCount, 2, 'RPC call should have been done only twice.'); + + _.each(clientActions, function(clientAction) { + clientAction.destroy(); + }); + }); + }); });