From 17866021874e9ef72151ab62c2cf99ab20c8bc43 Mon Sep 17 00:00:00 2001 From: Joan Date: Thu, 16 Aug 2018 13:12:18 +0200 Subject: [PATCH] Change landed on shipped change #458 --- client/ticket/src/data/step-one/index.html | 2 +- client/ticket/src/data/step-one/index.js | 25 ++++++++++ client/ticket/src/data/step-one/index.spec.js | 36 ++++++++++++++- .../common/methods/ticket/getLanded.js | 46 +++++++++++++++++++ .../common/methods/ticket/getShipped.js | 19 ++++++-- .../methods/ticket/specs/getLanded.spec.js | 15 ++++++ .../methods/ticket/specs/getShipped.spec.js | 23 ++++++++-- services/loopback/common/models/ticket.js | 1 + 8 files changed, 155 insertions(+), 12 deletions(-) create mode 100644 services/loopback/common/methods/ticket/getLanded.js create mode 100644 services/loopback/common/methods/ticket/specs/getLanded.spec.js diff --git a/client/ticket/src/data/step-one/index.html b/client/ticket/src/data/step-one/index.html index 102bf6888..0d9bd27f7 100644 --- a/client/ticket/src/data/step-one/index.html +++ b/client/ticket/src/data/step-one/index.html @@ -33,7 +33,7 @@ { + if (res.data && res.data.landed) + this.ticket.landed = res.data.landed; + }); + } + onChangeLanded(value) { let data = { landed: value, diff --git a/client/ticket/src/data/step-one/index.spec.js b/client/ticket/src/data/step-one/index.spec.js index a48eb64a9..23d889bb9 100644 --- a/client/ticket/src/data/step-one/index.spec.js +++ b/client/ticket/src/data/step-one/index.spec.js @@ -1,4 +1,5 @@ import './index.js'; +import {toJsonDate} from 'core/src/lib/date'; describe('Ticket', () => { describe('Component vnTicketDataStepOne', () => { @@ -38,6 +39,16 @@ describe('Ticket', () => { }); }); + describe('shipped() setter', () => { + it('should set shipped property and call onChangeShipped() method ', () => { + spyOn(controller, 'onChangeShipped'); + controller.ticket = {id: 1}; + controller.shipped = new Date(); + + expect(controller.onChangeShipped).toHaveBeenCalledWith(new Date()); + }); + }); + describe('landed() setter', () => { it('should set landed property and call onChangeLanded() method ', () => { spyOn(controller, 'onChangeLanded'); @@ -48,8 +59,31 @@ describe('Ticket', () => { }); }); + describe('onChangeShipped()', () => { + it('should return an available landing date', async () => { + controller._ticket = { + id: 1, + shipped: new Date(), + addressFk: 121, + agencyModeFk: 2, + warehouseFk: 1 + }; + let data = { + shipped: toJsonDate(new Date()), + addressFk: 121, + agencyModeFk: 2, + warehouseFk: 1 + }; + + $httpBackend.whenPOST(`/api/Tickets/getLanded`, data).respond(200); + $httpBackend.expectPOST(`/api/Tickets/getLanded`, data); + controller.onChangeShipped(new Date()); + $httpBackend.flush(); + }); + }); + describe('onChangeLanded()', () => { - it('should call onChangeLanded method and return an available shipment date', async () => { + it('should return an available shipment date', async () => { controller._ticket = {id: 1, landed: new Date(), addressFk: 121, agencyModeFk: 2}; let data = {landed: new Date(), addressFk: 121, agencyModeFk: 2}; diff --git a/services/loopback/common/methods/ticket/getLanded.js b/services/loopback/common/methods/ticket/getLanded.js new file mode 100644 index 000000000..a77e6166d --- /dev/null +++ b/services/loopback/common/methods/ticket/getLanded.js @@ -0,0 +1,46 @@ + +const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; + +module.exports = Self => { + Self.remoteMethod('getLanded', { + description: 'Returns ticket available landed date', + accessType: 'READ', + accepts: [{ + arg: 'data', + type: 'Object', + required: true, + description: 'shipped, addressFk, agencyModeFk, warehouseFk', + http: {source: 'body'} + }], + returns: { + type: 'object', + root: true + }, + http: { + path: `/getLanded`, + verb: 'POST' + } + }); + + Self.getLanded = async data => { + let stmts = []; + let agencyMode = await Self.app.models.AgencyMode.findById(data.agencyModeFk); + + stmts.push(new ParameterizedSQL( + `CALL vn.agencyHourGetLanded(?, ?, ?, ?)`, [ + data.shipped, + data.addressFk, + agencyMode.agencyFk, + data.warehouseFk + ] + )); + + let rsIndex = stmts.push( + `SELECT * FROM tmp.agencyHourGetLanded`) - 1; + + let sql = ParameterizedSQL.join(stmts, ';'); + let landed = await Self.rawStmt(sql); + + return landed[rsIndex][0]; + }; +}; diff --git a/services/loopback/common/methods/ticket/getShipped.js b/services/loopback/common/methods/ticket/getShipped.js index a7650f5d5..908a70678 100644 --- a/services/loopback/common/methods/ticket/getShipped.js +++ b/services/loopback/common/methods/ticket/getShipped.js @@ -23,14 +23,23 @@ module.exports = Self => { }); Self.getShipped = async data => { + let stmts = []; let agencyMode = await Self.app.models.AgencyMode.findById(data.agencyModeFk); - let params = [data.landed, data.addressFk, agencyMode.agencyFk]; - let stmt = new ParameterizedSQL(`CALL vn.agencyHourGetShipped(?, ?, ?);`, params); - stmt.merge('SELECT * FROM tmp.agencyHourGetShipped'); + stmts.push(new ParameterizedSQL( + `CALL vn.agencyHourGetShipped(?, ?, ?)`, [ + data.landed, + data.addressFk, + agencyMode.agencyFk + ] + )); - let rows = await Self.rawStmt(stmt); + let rsIndex = stmts.push(` + SELECT * FROM tmp.agencyHourGetShipped`) - 1; - return rows[1][0]; + let sql = ParameterizedSQL.join(stmts, ';'); + let shipped = await Self.rawStmt(sql); + + return shipped[rsIndex][0]; }; }; diff --git a/services/loopback/common/methods/ticket/specs/getLanded.spec.js b/services/loopback/common/methods/ticket/specs/getLanded.spec.js new file mode 100644 index 000000000..37ccaf6be --- /dev/null +++ b/services/loopback/common/methods/ticket/specs/getLanded.spec.js @@ -0,0 +1,15 @@ +const app = require(`${servicesDir}/ticket/server/server`); + +describe('ticket getLanded()', () => { + it('should return a landing date', async() => { + let data = { + shipped: new Date(), + addressFk: 121, + agencyModeFk: 7, + warehouseFk: 1 + }; + let result = await app.models.Ticket.getLanded(data); + + expect(result.landed).toBeDefined(); + }); +}); diff --git a/services/loopback/common/methods/ticket/specs/getShipped.spec.js b/services/loopback/common/methods/ticket/specs/getShipped.spec.js index af98d040c..dbcb1bc83 100644 --- a/services/loopback/common/methods/ticket/specs/getShipped.spec.js +++ b/services/loopback/common/methods/ticket/specs/getShipped.spec.js @@ -1,15 +1,28 @@ const app = require(`${servicesDir}/ticket/server/server`); describe('ticket getShipped()', () => { - it('should call the getShipped method', async() => { + it('should return a shipment date', async() => { let data = { landed: new Date(), addressFk: 121, agencyModeFk: 7 }; - await app.models.Ticket.getShipped(data) - .then(response => { - expect(response.warehouseFk).toEqual(1); - }); + let result = await app.models.Ticket.getShipped(data); + + expect(result.shipped).toBeDefined(); + }); + + it('should not return a shipment date', async() => { + let newDate = new Date(); + newDate.setMonth(newDate.getMonth() - 1); + + let data = { + landed: newDate, + addressFk: 121, + agencyModeFk: 7 + }; + let result = await app.models.Ticket.getShipped(data); + + expect(result).toBeUndefined(); }); }); diff --git a/services/loopback/common/models/ticket.js b/services/loopback/common/models/ticket.js index f86bb6544..9c5c95523 100644 --- a/services/loopback/common/models/ticket.js +++ b/services/loopback/common/models/ticket.js @@ -15,5 +15,6 @@ module.exports = Self => { require('../methods/ticket/getSales')(Self); require('../methods/ticket/getSalesPersonMana')(Self); require('../methods/ticket/getShipped')(Self); + require('../methods/ticket/getLanded')(Self); require('../methods/ticket/filter')(Self); };