From 1c6bddbfb67cae03424734e5af74183500c15dbe Mon Sep 17 00:00:00 2001 From: Jorge Padawan Date: Wed, 10 Feb 2021 08:47:58 +0100 Subject: [PATCH] Added functionality to autocomplete the inputs --- .../back/methods/travel/getTravelData.js | 36 +++++++++++++++ .../methods/travel/getTravelDaysDuration.js | 44 ------------------- modules/travel/back/models/travel.js | 1 + modules/travel/front/create/index.html | 2 + modules/travel/front/create/index.js | 24 ++++++++++ modules/travel/front/create/index.spec.js | 39 +++++++++++++++- 6 files changed, 101 insertions(+), 45 deletions(-) create mode 100644 modules/travel/back/methods/travel/getTravelData.js delete mode 100644 modules/travel/back/methods/travel/getTravelDaysDuration.js diff --git a/modules/travel/back/methods/travel/getTravelData.js b/modules/travel/back/methods/travel/getTravelData.js new file mode 100644 index 000000000..7e70c454b --- /dev/null +++ b/modules/travel/back/methods/travel/getTravelData.js @@ -0,0 +1,36 @@ +module.exports = Self => { + Self.remoteMethod('getTravelData', { + description: 'Returns the days of travel duration', + accessType: 'READ', + accepts: [{ + arg: 'agencyModeFk', + type: 'number', + required: true + }], + returns: { + type: 'number', + root: true + }, + http: { + path: `/getTravelData`, + verb: 'GET' + } + }); + + Self.getTravelData = async agencyModeFk => { + const query = ` + SELECT t.warehouseInFk as warehouseIn, + t.warehouseOutFk as warehouseOut, + dayDuration + FROM travel t + JOIN ( + SELECT ROUND(AVG(DATEDIFF(landed, shipped))) as dayDuration + FROM travel + WHERE agencyFk = 4) AS t2 + WHERE t.agencyFk = 4 + ORDER BY t.id DESC LIMIT 1;`; + + const [result] = await Self.rawSql(query, [agencyModeFk, agencyModeFk]); + return result; + }; +}; diff --git a/modules/travel/back/methods/travel/getTravelDaysDuration.js b/modules/travel/back/methods/travel/getTravelDaysDuration.js deleted file mode 100644 index 12a190e19..000000000 --- a/modules/travel/back/methods/travel/getTravelDaysDuration.js +++ /dev/null @@ -1,44 +0,0 @@ - -const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; -module.exports = Self => { - Self.remoteMethod('getTravelDaysDuration', { - description: 'Return the total days of travel', - accessType: 'READ', - accepts: { - arg: 'id', - type: 'number', - required: true, - description: 'The travel id', - http: {source: 'path'} - }, - returns: { - type: 'number', - root: true - }, - http: { - path: `/:id/getTravelDaysDuration`, - verb: 'GET' - } - }); - - Self.getTravelDaysDuration = async id => { - let stmt; - - stmt = new ParameterizedSQL(` - SELECT - ROUND( - AVG( - DATEDIFF(landed , shipped) - ) - ) - FROM travel - WHERE agencyFk = ? - GROUP BY agencyFK`, [ - id - ]); - - let result = await Self.rawStmt(stmt); - - return result; - }; -}; diff --git a/modules/travel/back/models/travel.js b/modules/travel/back/models/travel.js index 46d33b305..e2bb94c87 100644 --- a/modules/travel/back/models/travel.js +++ b/modules/travel/back/models/travel.js @@ -8,6 +8,7 @@ module.exports = Self => { require('../methods/travel/deleteThermograph')(Self); require('../methods/travel/updateThermograph')(Self); require('../methods/travel/extraCommunityFilter')(Self); + require('../methods/travel/getTravelData')(Self); require('../methods/travel/cloneWithEntries')(Self); Self.rewriteDbError(function(err) { diff --git a/modules/travel/front/create/index.html b/modules/travel/front/create/index.html index 0931c322e..6db662a54 100644 --- a/modules/travel/front/create/index.html +++ b/modules/travel/front/create/index.html @@ -20,7 +20,9 @@ { + const landed = new Date(value); + const warehouseIn = res.data.warehouseIn; + const warehouseOut = res.data.warehouseOut; + + const futureDate = landed.getDate() + res.data.dayDuration; + landed.setDate(futureDate); + + this.travel.landed = landed; + this.travel.warehouseInFk = warehouseIn; + this.travel.warehouseOutFk = warehouseOut; + }); + } + onSubmit() { return this.$.watcher.submit().then( res => this.$state.go('travel.card.basicData', {id: res.data.id}) diff --git a/modules/travel/front/create/index.spec.js b/modules/travel/front/create/index.spec.js index 99f52b322..a00a3fb79 100644 --- a/modules/travel/front/create/index.spec.js +++ b/modules/travel/front/create/index.spec.js @@ -5,10 +5,12 @@ describe('Travel Component vnTravelCreate', () => { let $scope; let $state; let controller; + let $httpBackend; beforeEach(ngModule('travel')); - beforeEach(inject(($componentController, $rootScope, _$state_) => { + beforeEach(inject(($componentController, $rootScope, _$state_, _$httpBackend_) => { + $httpBackend = _$httpBackend_; $scope = $rootScope.$new(); $state = _$state_; $scope.watcher = watcher; @@ -38,4 +40,39 @@ describe('Travel Component vnTravelCreate', () => { expect(controller.travel).toEqual(json); }); }); + + fdescribe('onShippedChange()', () => { + it(`should do nothing if there's no agencyMode or the travel has filled properties.`, () => { + controller.agencyModeFk = {}; + controller.landed = {landed: 'January 30,2021, 00:00:00'}; + controller.warehouseInFk = {warehouseInFk: 4}; + controller.warehouseOutFk = {warehouseOutFk: 4}; + + const landed = {landed: 'January 30,2021, 00:00:00'}; + const warehouseIn = {warehouseInFk: 4}; + const warehouseOut = {warehouseOutFk: 4}; + const agencyModeFk = {}; + + expect(controller.agencyModeFk).toEqual(agencyModeFk); + expect(controller.landed).toEqual(landed); + expect(controller.warehouseInFk).toEqual(warehouseIn); + expect(controller.warehouseOutFk).toEqual(warehouseOut); + }); + + it(`should do fill the fields when it's selected a date and agency.`, () => { + controller.travel = { + agencyModeFk: 4, + landed: 'January 30,2021, 00:00:00', + warehouseInFk: 4, + warehouseOutFk: 4 + }; + const params = {agencyModeFk: 4}; + + $httpBackend.expectGET(`travels/getTravelData`, params).respond({agencyModeFk: 4}); + controller.onShippedChange(); + // $httpBackend.flush(); + + expect(controller.travel.agencyModeFk).toEqual(params.agencyModeFk); + }); + }); });