From 4aea2847039d1ba944723eda2a1ba5ffd273def8 Mon Sep 17 00:00:00 2001 From: jgallego Date: Tue, 20 Oct 2020 14:42:23 +0200 Subject: [PATCH] WIP: a falta de e2e --- .../10230-oktoberFest/00-observationType.sql | 9 +++ loopback/common/models/loggable.js | 3 +- .../back/methods/client/createAddress.js | 37 ++++------- .../client/specs/createAddress.spec.js | 62 ++++++++++++++----- .../client/front/address/create/index.html | 8 +-- modules/client/front/address/create/index.js | 10 +-- .../client/front/address/create/index.spec.js | 12 ++-- .../order/back/methods/order/catalogFilter.js | 2 - .../back/methods/ticket/componentUpdate.js | 45 +++++++++++++- 9 files changed, 128 insertions(+), 60 deletions(-) create mode 100644 db/changes/10230-oktoberFest/00-observationType.sql diff --git a/db/changes/10230-oktoberFest/00-observationType.sql b/db/changes/10230-oktoberFest/00-observationType.sql new file mode 100644 index 0000000000..426fa1b559 --- /dev/null +++ b/db/changes/10230-oktoberFest/00-observationType.sql @@ -0,0 +1,9 @@ +ALTER TABLE `vn`.`observationType` +ADD COLUMN `code` VARCHAR(45) NOT NULL AFTER `description`; + +UPDATE `vn`.`observationType` SET `code` = 'itemPicker' WHERE (`id` = '1'); +UPDATE `vn`.`observationType` SET `code` = 'packager' WHERE (`id` = '2'); +UPDATE `vn`.`observationType` SET `code` = 'salesPerson' WHERE (`id` = '4'); +UPDATE `vn`.`observationType` SET `code` = 'administrative' WHERE (`id` = '5'); +UPDATE `vn`.`observationType` SET `code` = 'weight' WHERE (`id` = '6'); +UPDATE `vn`.`observationType` SET `code` = 'delivery' WHERE (`id` = '3'); diff --git a/loopback/common/models/loggable.js b/loopback/common/models/loggable.js index eaa44f9ba0..258fff4ff2 100644 --- a/loopback/common/models/loggable.js +++ b/loopback/common/models/loggable.js @@ -154,7 +154,8 @@ module.exports = function(Self) { const showFieldNames = [ 'name', 'description', - 'code' + 'code', + 'nickname' ]; for (field of showFieldNames) { const propField = properties && properties[field]; diff --git a/modules/client/back/methods/client/createAddress.js b/modules/client/back/methods/client/createAddress.js index 1a086b124c..450b167224 100644 --- a/modules/client/back/methods/client/createAddress.js +++ b/modules/client/back/methods/client/createAddress.js @@ -4,7 +4,7 @@ module.exports = function(Self) { Self.remoteMethodCtx('createAddress', { description: 'Creates client address updating default address', accepts: [{ - arg: 'id', + arg: 'clientFk', type: 'number', description: 'The client id', http: {source: 'path'} @@ -37,19 +37,19 @@ module.exports = function(Self) { type: 'string' }, { - arg: 'provinceId', + arg: 'provinceFk', type: 'number' }, { - arg: 'agencyModeId', + arg: 'agencyModeFk', type: 'number' }, { - arg: 'incotermsId', + arg: 'incotermsFk', type: 'string' }, { - arg: 'customsAgentId', + arg: 'customsAgentFk', type: 'number' }, { @@ -66,44 +66,33 @@ module.exports = function(Self) { }, http: { verb: 'post', - path: '/:id/createAddress' + path: '/:clientFk/createAddress' } }); - Self.createAddress = async(ctx, clientId) => { + Self.createAddress = async(ctx, clientFk) => { const models = Self.app.models; const args = ctx.args; const tx = await models.Address.beginTransaction({}); try { const options = {transaction: tx}; - const province = await models.Province.findById(args.provinceId, { + const province = await models.Province.findById(args.provinceFk, { include: { relation: 'country' } }, options); const isUeeMember = province.country().isUeeMember; - if (!isUeeMember && !args.incotermsId) + if (!isUeeMember && !args.incotermsFk) throw new UserError(`Incoterms is required for a non UEE member`); - if (!isUeeMember && !args.customsAgentId) + if (!isUeeMember && !args.customsAgentFk) throw new UserError(`Customs agent is required for a non UEE member`); - const newAddress = await models.Address.create({ - clientFk: clientId, - nickname: args.nickname, - incotermsFk: args.incotermsId, - customsAgentFk: args.customsAgentId, - city: args.city, - street: args.street, - phone: args.phone, - postalCode: args.postalCode, - provinceFk: args.provinceId, - agencyModeFk: args.agencyModeId, - isActive: args.isActive - }, options); - const client = await Self.findById(clientId, null, options); + delete args.ctx; // Remove unwanted properties + const newAddress = await models.Address.create(args, options); + const client = await Self.findById(clientFk, null, options); if (args.isDefaultAddress) { await client.updateAttributes({ diff --git a/modules/client/back/methods/client/specs/createAddress.spec.js b/modules/client/back/methods/client/specs/createAddress.spec.js index 8b2d1f5e2f..be80fb1db6 100644 --- a/modules/client/back/methods/client/specs/createAddress.spec.js +++ b/modules/client/back/methods/client/specs/createAddress.spec.js @@ -1,25 +1,25 @@ const app = require('vn-loopback/server/server'); describe('Address createAddress', () => { - const clientId = 101; - const provinceId = 5; - const incotermsId = 'FAS'; + const clientFk = 101; + const provinceFk = 5; + const incotermsFk = 'FAS'; const customAgentOneId = 1; it('should throw a non uee member error if no incoterms is defined', async() => { const expectedResult = 'My edited address'; const ctx = { args: { - provinceId: provinceId, + provinceFk: provinceFk, nickname: expectedResult, street: 'Wall Street', city: 'New York', - customsAgentId: customAgentOneId + customsAgentFk: customAgentOneId } }; try { - await app.models.Client.createAddress(ctx, clientId); + await app.models.Client.createAddress(ctx, clientFk); } catch (e) { err = e; } @@ -32,16 +32,16 @@ describe('Address createAddress', () => { const expectedResult = 'My edited address'; const ctx = { args: { - provinceId: provinceId, + provinceFk: provinceFk, nickname: expectedResult, street: 'Wall Street', city: 'New York', - incotermsId: incotermsId + incotermsFk: incotermsFk } }; try { - await app.models.Client.createAddress(ctx, clientId); + await app.models.Client.createAddress(ctx, clientFk); } catch (e) { err = e; } @@ -51,7 +51,7 @@ describe('Address createAddress', () => { }); it('should verify that client defaultAddressFk is untainted', async() => { - const client = await app.models.Client.findById(clientId); + const client = await app.models.Client.findById(clientFk); expect(client.defaultAddressFk).toEqual(1); }); @@ -59,18 +59,23 @@ describe('Address createAddress', () => { it('should create a new address and set as a client default address', async() => { const ctx = { args: { - provinceId: 1, + clientFk: 101, + provinceFk: 1, nickname: 'My address', street: 'Wall Street', city: 'New York', - incotermsId: incotermsId, - customsAgentId: customAgentOneId, + phone: 678678678, + mobile: 678678678, + postalCode: 46680, + agencyModeFk: 1, + incotermsFk: incotermsFk, + customsAgentFk: customAgentOneId, isDefaultAddress: true } }; - const address = await app.models.Client.createAddress(ctx, clientId); - const client = await app.models.Client.findById(clientId); + const address = await app.models.Client.createAddress(ctx, clientFk); + const client = await app.models.Client.findById(clientFk); expect(client.defaultAddressFk).toEqual(address.id); @@ -78,4 +83,31 @@ describe('Address createAddress', () => { await client.updateAttributes({defaultAddressFk: 1}); await address.destroy(); }); + + it('should create a new address and set all properties', async() => { + const ctx = { + args: { + clientFk: 101, + provinceFk: 1, + nickname: 'My address', + street: 'Wall Street', + city: 'New York', + phone: '678678678', + mobile: '678678678', + postalCode: '46680', + agencyModeFk: 1, + incotermsFk: incotermsFk, + customsAgentFk: customAgentOneId, + isDefaultAddress: true + } + }; + + address = await app.models.Client.createAddress(ctx, clientFk); + + expect(address.__data).toEqual(jasmine.objectContaining(ctx.args)); + // restores + const client = await app.models.Client.findById(clientFk); + await client.updateAttributes({defaultAddressFk: 1}); + await address.destroy(); + }); }); diff --git a/modules/client/front/address/create/index.html b/modules/client/front/address/create/index.html index d6b5bcf8e8..8e882adc78 100644 --- a/modules/client/front/address/create/index.html +++ b/modules/client/front/address/create/index.html @@ -88,7 +88,7 @@ this.address.customsAgentId = res.data.id); + .then(res => this.address.customsAgentFk = res.data.id); } get town() { @@ -45,8 +45,8 @@ export default class Controller extends Section { const province = selection.province; const postcodes = selection.postcodes; - if (!this.address.provinceI) - this.address.provinceId = province.id; + if (!this.address.provinceFk) + this.address.provinceFk = province.id; if (postcodes.length === 1) this.address.postalCode = postcodes[0].code; @@ -68,8 +68,8 @@ export default class Controller extends Section { if (!this.address.city) this.address.city = town.name; - if (!this.address.provinceId) - this.address.provinceId = province.id; + if (!this.address.provinceFk) + this.address.provinceFk = province.id; } onResponse(response) { diff --git a/modules/client/front/address/create/index.spec.js b/modules/client/front/address/create/index.spec.js index 1a4340115f..b12bc183b1 100644 --- a/modules/client/front/address/create/index.spec.js +++ b/modules/client/front/address/create/index.spec.js @@ -54,7 +54,7 @@ describe('Client', () => { }); describe('town() setter', () => { - it(`should set provinceId property`, () => { + it(`should set provinceFk property`, () => { controller.town = { provinceFk: 1, code: 46001, @@ -69,10 +69,10 @@ describe('Client', () => { postcodes: [] }; - expect(controller.address.provinceId).toEqual(1); + expect(controller.address.provinceFk).toEqual(1); }); - it(`should set provinceId property and fill the postalCode if there's just one`, () => { + it(`should set provinceFk property and fill the postalCode if there's just one`, () => { controller.town = { provinceFk: 1, code: 46001, @@ -87,7 +87,7 @@ describe('Client', () => { postcodes: [{code: '46001'}] }; - expect(controller.address.provinceId).toEqual(1); + expect(controller.address.provinceFk).toEqual(1); expect(controller.address.postalCode).toEqual('46001'); }); }); @@ -112,7 +112,7 @@ describe('Client', () => { }; expect(controller.address.city).toEqual('New York'); - expect(controller.address.provinceId).toEqual(1); + expect(controller.address.provinceFk).toEqual(1); }); }); @@ -123,7 +123,7 @@ describe('Client', () => { controller.onCustomAgentAccept(); $httpBackend.flush(); - expect(controller.address.customsAgentId).toEqual(1); + expect(controller.address.customsAgentFk).toEqual(1); }); }); }); diff --git a/modules/order/back/methods/order/catalogFilter.js b/modules/order/back/methods/order/catalogFilter.js index 8c98317bda..23414372fe 100644 --- a/modules/order/back/methods/order/catalogFilter.js +++ b/modules/order/back/methods/order/catalogFilter.js @@ -190,8 +190,6 @@ module.exports = Self => { item.prices.push(price); else item.prices = [price]; - - item.available = price.grouping; } }); }); diff --git a/modules/ticket/back/methods/ticket/componentUpdate.js b/modules/ticket/back/methods/ticket/componentUpdate.js index 15cfa05975..b295315c89 100644 --- a/modules/ticket/back/methods/ticket/componentUpdate.js +++ b/modules/ticket/back/methods/ticket/componentUpdate.js @@ -88,9 +88,29 @@ module.exports = Self => { if (!zoneShipped || zoneShipped.zoneFk != zoneFk) throw new UserError(`You don't have privileges to change the zone`); } - const originalTicket = await models.Ticket.findById(id, {fields: - ['id', 'clientFk', 'agencyModeFk', 'addressFk', 'zoneFk', - 'warehouseFk', 'companyFk', 'shipped', 'landed', 'isDeleted'] + const observationTypeDelivery = await models.ObservationType.findOne({ + where: {code: 'delivery'} + }); + const originalTicket = await models.Ticket.findOne({ + where: {id: id}, + fields: ['id', 'clientFk', 'agencyModeFk', 'addressFk', 'zoneFk', + 'warehouseFk', 'companyFk', 'shipped', 'landed', 'isDeleted'], + include: [ + { + relation: 'address', + scope: { + include: { + relation: 'observations', + scope: { + where: {observationTypeFk: observationTypeDelivery.id}, + include: { + relation: 'observationType' + + } + } + } + } + }] }); const updatedTicket = Object.assign({}, ctx.args); delete updatedTicket.ctx; @@ -125,6 +145,25 @@ module.exports = Self => { option ]); + if (originalTicket.addressFk != updatedTicket.addressFk) { + const ticketObservation = await models.TicketObservation.findOne({ + where: { + ticketFk: id, + observationTypeFk: observationTypeDelivery.id} + }); + + if (ticketObservation) + await ticketObservation.destroy(); + + const [observation] = originalTicket.address().observations(); + if (observation) { + await models.TicketObservation.upsert({ + ticketFk: id, + observationTypeFk: observation.observationTypeFk, + description: observation.description + }); + } + } await models.TicketLog.create(logRecord); return res; };