From 5273e90a30a093dced38988d639d726fc7b2d61b Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Thu, 26 Dec 2019 07:12:34 +0100 Subject: [PATCH 1/2] Added params to ticket method & updated unit tests --- .../back/methods/claim/regularizeClaim.js | 35 +++--- modules/claim/front/dms/index/index.html | 2 +- modules/item/back/methods/item/regularize.js | 35 +++--- modules/ticket/back/methods/ticket/new.js | 107 +++++++++++------- .../back/methods/ticket/specs/new.spec.js | 70 ++++++++---- modules/ticket/front/create/card.html | 19 ++-- modules/ticket/front/create/card.js | 73 +++++++----- 7 files changed, 209 insertions(+), 132 deletions(-) diff --git a/modules/claim/back/methods/claim/regularizeClaim.js b/modules/claim/back/methods/claim/regularizeClaim.js index 136016b1e..e1e95728b 100644 --- a/modules/claim/back/methods/claim/regularizeClaim.js +++ b/modules/claim/back/methods/claim/regularizeClaim.js @@ -18,7 +18,6 @@ module.exports = Self => { }); Self.regularizeClaim = async(ctx, params) => { - const userId = ctx.req.accessToken.userId; const models = Self.app.models; const resolvedState = 3; @@ -55,11 +54,10 @@ module.exports = Self => { if (!ticketFk) { ticketFk = await createTicket(ctx, { - clientFk: address.clientFk, - addressFk: addressFk, - warehouseFk: sale.ticket().warehouseFk, - companyFk: sale.ticket().companyFk, - userId: userId + clientId: address.clientFk, + warehouseId: sale.ticket().warehouseFk, + companyId: sale.ticket().companyFk, + addressId: addressFk }, options); } @@ -140,16 +138,21 @@ module.exports = Self => { } async function createTicket(ctx, params, options) { - let ticket = await Self.app.models.Ticket.new(ctx, - { - shipped: new Date(), - landed: new Date(), - clientFk: params.clientFk, - warehouseFk: params.warehouseFk, - companyFk: params.companyFk, - addressFk: params.addressFk, - userId: params.userId - }, options); + params.shipped = new Date(); + params.landed = new Date(); + params.agencyModeId = null; + params.routeId = null; + + const ticket = await Self.app.models.Ticket.new(ctx, + params.clientId, + params.shipped, + params.landed, + params.warehouseId, + params.companyId, + params.addressId, + params.agencyModeId, + params.routeId, + options); return ticket.id; } diff --git a/modules/claim/front/dms/index/index.html b/modules/claim/front/dms/index/index.html index e3cd5da5b..bd6b9a21d 100644 --- a/modules/claim/front/dms/index/index.html +++ b/modules/claim/front/dms/index/index.html @@ -11,7 +11,7 @@ + class="vn-w-xl">
{ }); Self.regularize = async(ctx, itemFk, quantity, warehouseFk) => { - const userId = ctx.req.accessToken.userId; const models = Self.app.models; const itemDestination = await models.ClaimDestination.findOne({ @@ -56,10 +55,9 @@ module.exports = Self => { if (!ticketFk) { ticketFk = await createTicket(ctx, { - clientFk: itemDestination.address().clientFk, - addressFk: itemDestination.addressFk, - warehouseFk: warehouseFk, - userId: userId + clientId: itemDestination.address().clientFk, + warehouseId: warehouseFk, + addressId: itemDestination.addressFk }, options); } @@ -88,17 +86,22 @@ module.exports = Self => { } async function createTicket(ctx, params, options) { - let ticket = await Self.app.models.Ticket.new( - ctx, - { - shipped: new Date(), - landed: new Date(), - clientFk: params.clientFk, - warehouseFk: params.warehouseFk, - companyFk: params.companyFk, - addressFk: params.addressFk, - userId: params.userId - }, options); + params.shipped = new Date(); + params.landed = new Date(); + params.companyId = null; + params.agencyModeId = null; + params.routeId = null; + + const ticket = await Self.app.models.Ticket.new(ctx, + params.clientId, + params.shipped, + params.landed, + params.warehouseId, + params.companyId, + params.addressId, + params.agencyModeId, + params.routeId, + options); return ticket.id; } diff --git a/modules/ticket/back/methods/ticket/new.js b/modules/ticket/back/methods/ticket/new.js index 7ddde8785..db5310f7c 100644 --- a/modules/ticket/back/methods/ticket/new.js +++ b/modules/ticket/back/methods/ticket/new.js @@ -2,14 +2,50 @@ let UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethodCtx('new', { - description: 'Create a newticket and returns the new ID', + description: 'Creates a new ticket and returns the id', accessType: 'WRITE', accepts: [{ - arg: 'params', - type: 'object', - required: true, - description: 'ClientFk, Shipped, WarehouseFk, CompanyFk, AddressFk, AgencyModeFk, RouteFk, Landed, userId', - http: {source: 'body'} + arg: 'clientId', + type: 'Number', + description: `The client id filter`, + required: true + }, + { + arg: 'shipped', + type: 'Date', + description: `The shipment date filter` + }, + { + arg: 'landed', + type: 'Date', + description: `The landing date filter` + }, + { + arg: 'warehouseId', + type: 'Number', + description: `The warehouse id filter`, + required: true + }, + { + arg: 'companyId', + type: 'Number', + description: `The company id filter` + }, + { + arg: 'addressId', + type: 'Number', + description: `The address id filter`, + required: true + }, + { + arg: 'agencyModeId', + type: 'Number', + description: `The agencyMode id filter` + }, + { + arg: 'routeId', + type: 'Number', + description: `The route id filter` }], returns: { type: 'number', @@ -21,10 +57,12 @@ module.exports = Self => { } }); - Self.new = async(ctx, params, options) => { - let models = Self.app.models; - let address = await models.Address.findOne({ - where: {id: params.addressFk}, + Self.new = async(ctx, clientId, shipped, landed, warehouseId, + companyId, addressId, agencyModeId, routeId, options) => { + const myUserId = ctx.req.accessToken.userId; + const models = Self.app.models; + const address = await models.Address.findOne({ + where: {id: addressId}, fields: ['id', 'clientFk'], include: { relation: 'client', @@ -36,18 +74,12 @@ module.exports = Self => { } }); - // FIXME: #1953 Params should be declared with it's correct argument type - if (params.shipped) - params.shipped = new Date(params.shipped); - if (params.landed) - params.landed = new Date(params.landed); - if (!address) throw new UserError(`This address doesn't exist`); let agencyMode; - if (params && params.agencyModeFk) - agencyMode = await models.AgencyMode.findById(params.agencyModeFk); + if (agencyModeId) + agencyMode = await models.AgencyMode.findById(agencyModeId); if (address.client().type().code === 'normal' && (!agencyMode || agencyMode.code != 'refund')) { if (!address.client().isActive) @@ -65,33 +97,30 @@ module.exports = Self => { } try { - if (!params.shipped && params.landed) { - const shippedResult = await models.Agency.getShipped(params.landed, - address.id, params.agencyModeFk, params.warehouseFk); - params.shipped = shippedResult && shippedResult.shipped; + if (!shipped && landed) { + const shippedResult = await models.Agency.getShipped(landed, + address.id, agencyModeId, warehouseId); + shipped = shippedResult && shippedResult.shipped; } - if (params.shipped && !params.landed) { - const landedResult = await models.Agency.getLanded(params.shipped, - address.id, params.agencyModeFk, params.warehouseFk); - params.landed = landedResult && landedResult.landed; + if (shipped && !landed) { + const landedResult = await models.Agency.getLanded(shipped, + address.id, agencyModeId, warehouseId); + landed = landedResult && landedResult.landed; } - if (!params.userId && ctx.req && ctx.req.accessToken.userId) - params.userId = ctx.req.accessToken.userId; - query = `CALL vn.ticketCreateWithUser(?, ?, ?, ?, ?, ?, ?, ?, ?, @result); SELECT @result newTicketId;`; let result = await Self.rawSql(query, [ - params.clientFk, - params.shipped, - params.warehouseFk, - params.companyFk || 442, - params.addressFk, - params.agencyModeFk || null, - params.routeFk || null, - params.landed, - params.userId + clientId, + shipped, + warehouseId, + companyId || 442, + addressId, + agencyModeId || null, + routeId || null, + landed, + myUserId ], options); let ticket = await models.Ticket.findById(result[1][0].newTicketId, null, options); @@ -99,7 +128,7 @@ module.exports = Self => { let logRecord = { originFk: cleanInstance.id, - userFk: params.userId, + userFk: myUserId, action: 'create', changedModel: 'Ticket', changedModelId: cleanInstance.id, diff --git a/modules/ticket/back/methods/ticket/specs/new.spec.js b/modules/ticket/back/methods/ticket/specs/new.spec.js index 80147c2f2..87bc89b12 100644 --- a/modules/ticket/back/methods/ticket/specs/new.spec.js +++ b/modules/ticket/back/methods/ticket/specs/new.spec.js @@ -15,42 +15,74 @@ describe('ticket new()', () => { it('should throw an error if the client isnt frozen and isnt active', async() => { let error; - let params = {addressFk: 6}; + let params = { + clientId: 106, + shipped: today, + landed: null, + warehouseId: 1, + companyId: 442, + addressId: 6 + }; - await app.models.Ticket.new(ctx, params) - .catch(e => { - error = e; - }); + await app.models.Ticket.new(ctx, + params.clientId, + params.shipped, + params.landed, + params.warehouseId, + params.companyFk, + params.addressId + ).catch(e => { + error = e; + }); expect(error).toEqual(new UserError(`You can't create a ticket for a inactive client`)); }); it('should throw an error if the address doesnt exist', async() => { let error; - let params = {addressFk: 'invalid address', clientFk: 104}; + let params = { + clientId: 104, + shipped: today, + landed: null, + warehouseId: 1, + companyId: 442, + addressId: 'invalid address' + }; - await app.models.Ticket.new(ctx, params) - .catch(response => { - expect(response.message).toEqual(`This address doesn't exist`); - error = response; - }); + await app.models.Ticket.new(ctx, + params.clientId, + params.shipped, + params.landed, + params.warehouseId, + params.companyFk, + params.addressId + ).catch(response => { + expect(response.message).toEqual(`This address doesn't exist`); + error = response; + }); expect(error).toBeDefined(); }); it('should return the id of the created ticket', async() => { let params = { - warehouseFk: 1, - clientFk: 104, - companyFk: 442, - addressFk: 4, - agencyModeFk: 1, - userId: 9, + clientId: 104, shipped: today, - landed: today + landed: today, + warehouseId: 1, + companyId: 442, + addressId: 4, + agencyModeId: 1 }; - ticket = await app.models.Ticket.new(ctx, params); + ticket = await app.models.Ticket.new(ctx, + params.clientId, + params.shipped, + params.landed, + params.warehouseId, + params.companyFk, + params.addressId, + params.agencyModeId); let newestTicketIdInFixtures = 21; diff --git a/modules/ticket/front/create/card.html b/modules/ticket/front/create/card.html index cd26f21e7..6e6b5de62 100644 --- a/modules/ticket/front/create/card.html +++ b/modules/ticket/front/create/card.html @@ -1,20 +1,19 @@ - {{id}}: {{name}} @@ -25,18 +24,18 @@ ng-model="$ctrl.landed"> + ng-model="$ctrl.agencyModeId"> \ No newline at end of file diff --git a/modules/ticket/front/create/card.js b/modules/ticket/front/create/card.js index 629647416..48790ed10 100644 --- a/modules/ticket/front/create/card.js +++ b/modules/ticket/front/create/card.js @@ -19,16 +19,20 @@ class Controller { this.warehouseFk = this.vnConfig.warehouseFk; } + get ticket() { + return this._ticket; + } + set ticket(value) { if (value) this._ticket = value; } - get ticket() { - return this._ticket; + get clientId() { + return this.ticket.clientFk; } - set clientFk(value) { + set clientId(value) { this.ticket.clientFk = value; if (value) { @@ -36,7 +40,7 @@ class Controller { include: { relation: 'defaultAddress', scope: { - fields: 'id' + fields: ['id', 'agencyModeFk'] } }, where: {id: value} @@ -44,32 +48,21 @@ class Controller { filter = encodeURIComponent(JSON.stringify(filter)); let query = `Clients?filter=${filter}`; this.$http.get(query).then(res => { - if (res.data) { - let client = res.data[0]; - let defaultAddress = client.defaultAddress; - this.addressFk = defaultAddress.id; - } + const [client] = res.data; + this.defaultAddress = client.defaultAddress; + this.addressId = this.defaultAddress.id; }); } else - this.addressFk = null; + this.addressId = null; this.getAvailableAgencies(); } - get clientFk() { - return this.ticket.clientFk; - } - - set addressFk(value) { - this.ticket.addressFk = value; - this.getAvailableAgencies(); - } - - get addressFk() { + get addressId() { return this.ticket.addressFk; } - set landed(value) { - this.ticket.landed = value; + set addressId(value) { + this.ticket.addressFk = value; this.getAvailableAgencies(); } @@ -77,12 +70,26 @@ class Controller { return this.ticket.landed; } - set warehouseFk(value) { + set landed(value) { + this.ticket.landed = value; + this.getAvailableAgencies(); + } + + get warehouseId() { + return this.ticket.warehouseFk; + } + + set warehouseId(value) { this.ticket.warehouseFk = value; this.getAvailableAgencies(); } - get warehouseFk() { - return this.ticket.warehouseFk; + + get agencyModeId() { + return this.ticket.agencyModeFk; + } + + set agencyModeId(value) { + this.ticket.agencyModeFk = value; } getAvailableAgencies() { @@ -96,8 +103,12 @@ class Controller { if (params.warehouseFk && params.addressFk && params.landed) { ticket.agencyModeFk = null; - this.$http.get(`Agencies/getAgenciesWithWarehouse`, {params}) - .then(res => this._availableAgencies = res.data); + this.$http.get(`Agencies/getAgenciesWithWarehouse`, {params}).then(res => { + this._availableAgencies = res.data; + + this.agencyModeId = this.defaultAddress.agencyModeFk; + console.log(this.agencyModeId); + }); } } @@ -107,11 +118,11 @@ class Controller { createTicket() { let params = { - clientFk: this.ticket.clientFk, + clientId: this.ticket.clientFk, landed: this.ticket.landed, - addressFk: this.ticket.addressFk, - agencyModeFk: this.ticket.agencyModeFk, - warehouseFk: this.ticket.warehouseFk, + addressId: this.ticket.addressFk, + agencyModeId: this.ticket.agencyModeFk, + warehouseId: this.ticket.warehouseFk, }; this.$http.post(`Tickets/new`, params).then(res => { this.vnApp.showSuccess(this.translate.instant('Data saved!')); From fd8eeb626dcc5559ccbe823daba665efe8fdf200 Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Thu, 26 Dec 2019 08:45:55 +0100 Subject: [PATCH 2/2] removed console.log --- modules/ticket/front/create/card.js | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/ticket/front/create/card.js b/modules/ticket/front/create/card.js index 48790ed10..20def7b72 100644 --- a/modules/ticket/front/create/card.js +++ b/modules/ticket/front/create/card.js @@ -107,7 +107,6 @@ class Controller { this._availableAgencies = res.data; this.agencyModeId = this.defaultAddress.agencyModeFk; - console.log(this.agencyModeId); }); } }