From fa7b79b1c84f9d39932775c07a87cf9874fe745f Mon Sep 17 00:00:00 2001 From: Bernat Date: Mon, 3 Jun 2019 10:05:22 +0200 Subject: [PATCH] Bug/Test #1491, #1500 refactor new.js --- modules/order/back/methods/order/new.js | 33 +++++++++++++------ .../order/back/methods/order/newFromTicket.js | 10 +++--- .../back/methods/order/specs/new.spec.js | 22 +++++++------ modules/order/front/create/card.html | 1 + modules/order/front/create/card.js | 4 +-- 5 files changed, 43 insertions(+), 27 deletions(-) diff --git a/modules/order/back/methods/order/new.js b/modules/order/back/methods/order/new.js index 259fb3ea2..0d020ccd6 100644 --- a/modules/order/back/methods/order/new.js +++ b/modules/order/back/methods/order/new.js @@ -4,11 +4,24 @@ module.exports = Self => { Self.remoteMethod('new', { description: 'Create a new order and returns the new ID', accessType: 'WRITE', - accepts: [{ - arg: 'params', - type: 'object', - http: {source: 'body'} - }], + accepts: [ + { + arg: 'landed', + description: 'The landed for the order', + type: 'date', + required: true + }, { + arg: 'addressId', + description: 'The address for the order', + type: 'number', + required: true + }, { + arg: 'agencyModeId', + description: 'The agencyMode for the order', + type: 'number', + required: true + } + ], returns: { type: 'number', root: true @@ -19,9 +32,9 @@ module.exports = Self => { } }); - Self.new = async params => { + Self.new = async(landed, addressId, agencyModeId) => { let address = await Self.app.models.Address.findOne({ - where: {id: params.addressFk}, + where: {id: addressId}, fields: ['clientFk'], include: [ {relation: 'client', @@ -44,9 +57,9 @@ module.exports = Self => { query = `CALL vn.orderListCreate(?, ?, ?, ?);`; [result] = await Self.rawSql(query, [ - params.landed, - params.agencyModeFk, - params.addressFk, + landed, + agencyModeId, + addressId, 'SALIX' ]); diff --git a/modules/order/back/methods/order/newFromTicket.js b/modules/order/back/methods/order/newFromTicket.js index 533ec6ff9..285f60ee8 100644 --- a/modules/order/back/methods/order/newFromTicket.js +++ b/modules/order/back/methods/order/newFromTicket.js @@ -23,11 +23,11 @@ module.exports = Self => { where: {id: ticketFk} }); - let orderID = await Self.app.models.Order.new({ - addressFk: ticket.addressFk, - landed: ticket.landed, - agencyModeFk: ticket.agencyModeFk - }); + let landed = ticket.landed; + let addressFk = ticket.addressFk; + let agencyModeFk = ticket.agencyModeFk; + + let orderID = await Self.app.models.Order.new(landed, addressFk, agencyModeFk); return orderID; }; diff --git a/modules/order/back/methods/order/specs/new.spec.js b/modules/order/back/methods/order/specs/new.spec.js index 88f5776b6..9ed55981c 100644 --- a/modules/order/back/methods/order/specs/new.spec.js +++ b/modules/order/back/methods/order/specs/new.spec.js @@ -12,9 +12,11 @@ describe('order new()', () => { it('should throw an error if the client is frozen', async() => { let error; - let params = {addressFk: 121}; + let landed = new Date(); + let addressFk = 121; + let agencyModeFk = 1; - await app.models.Order.new(params) + await app.models.Order.new(landed, addressFk, agencyModeFk) .catch(e => { error = e; }); @@ -24,9 +26,11 @@ describe('order new()', () => { it('should throw an error if the client isnt frozen and isnt active', async() => { let error; - let params = {addressFk: 6}; + let landed = new Date(); + let addressFk = 6; + let agencyModeFk = 1; - await app.models.Order.new(params) + await app.models.Order.new(landed, addressFk, agencyModeFk) .catch(e => { error = e; }); @@ -35,13 +39,11 @@ describe('order new()', () => { }); it('should create a new order for the user with id 105 when all conditions are met', async() => { - let params = { - landed: new Date(), - agencyModeFk: 1, - addressFk: 125 - }; + let landed = new Date(); + let addressFk = 125; + let agencyModeFk = 1; - orderId = await app.models.Order.new(params); + orderId = await app.models.Order.new(landed, addressFk, agencyModeFk); let highestOrderIdInFixtures = 3; diff --git a/modules/order/front/create/card.html b/modules/order/front/create/card.html index 0b0f8822a..7ef5f0243 100644 --- a/modules/order/front/create/card.html +++ b/modules/order/front/create/card.html @@ -31,5 +31,6 @@ label="Agency" show-field="agency" value-field="id" + rule="Order.agencyModeFk" field="$ctrl.order.agencyModeFk"> diff --git a/modules/order/front/create/card.js b/modules/order/front/create/card.js index ad6317f02..e092b4be9 100644 --- a/modules/order/front/create/card.js +++ b/modules/order/front/create/card.js @@ -96,8 +96,8 @@ class Controller { createOrder() { let params = { landed: this.order.landed, - addressFk: this.order.addressFk, - agencyModeFk: this.order.agencyModeFk + addressId: this.order.addressFk, + agencyModeId: this.order.agencyModeFk }; this.$http.post(`order/api/Orders/new`, params).then(res => { this.vnApp.showSuccess(this.translate.instant('Data saved!'));