From 9db8ae237fcbb9fce120af1f237d7b6496338fd7 Mon Sep 17 00:00:00 2001 From: jorgep Date: Thu, 20 Jul 2023 10:19:03 +0200 Subject: [PATCH 1/2] refs #5866 addBackTransferClient --- db/changes/233001/00-transferClient.sql | 2 + .../back/methods/ticket/transferClient.js | 69 +++++++++++++++++++ modules/ticket/back/models/ticket-methods.js | 1 + modules/ticket/front/descriptor-menu/index.js | 8 +++ 4 files changed, 80 insertions(+) create mode 100644 db/changes/233001/00-transferClient.sql create mode 100644 modules/ticket/back/methods/ticket/transferClient.js diff --git a/db/changes/233001/00-transferClient.sql b/db/changes/233001/00-transferClient.sql new file mode 100644 index 000000000..8a7ce0543 --- /dev/null +++ b/db/changes/233001/00-transferClient.sql @@ -0,0 +1,2 @@ +INSERT INTO `salix`.`ACL` (`model`,`property`,`accessType`,`permission`,`principalType`,`principalId`) + VALUES ('Ticket','transferClient','WRITE','ALLOW','ROLE','administrative'); \ No newline at end of file diff --git a/modules/ticket/back/methods/ticket/transferClient.js b/modules/ticket/back/methods/ticket/transferClient.js new file mode 100644 index 000000000..226f5dedf --- /dev/null +++ b/modules/ticket/back/methods/ticket/transferClient.js @@ -0,0 +1,69 @@ +const UserError = require('vn-loopback/util/user-error'); +module.exports = Self => { + Self.remoteMethodCtx('transferClient', { + description: 'Transfering ticket to another client', + accessType: 'WRITE', + accepts: [ + { + arg: 'id', + type: 'number', + required: true, + description: 'the ticket id', + http: {source: 'path'} + }, + { + arg: 'data', + type: 'object', + required: true, + description: 'the client id', + http: {source: 'body'} + }, + ], + returns: { + type: 'boolean', + root: true + }, + http: { + path: `/:id/transferClient`, + verb: 'PATCH' + } + }); + + Self.transferClient = async(ctx, ticketId, params) => { + const models = Self.app.models; + const args = ctx.args; + const myOptions = {}; + try { + const clientId = params.clientId; + const isEditable = await Self.isEditable(ctx, args.id, myOptions); + console.log('es editable?',isEditable) // Revisar + /* if (!isEditable){ + console.log('no es editable!') + throw new UserError(`The sales of this ticket can't be modified`); + } */ + + const ticket = await models.Ticket.findById(ticketId, myOptions); + console.log('ticket',ticket); + if(!ticket) return false; + const nparams = { + clientFk: clientId, + addressFk: ticket.addressFk, + } + + const promise = await ticket.updateAttributes(nparams); + console.log('promise', promise); + if(promise) return true; + } catch (error) { + console.log(error); + } + + /* if (typeof options == 'object') + Object.assign(myOptions, options); + + const ticket = await Self.findById(id, { + fields: ['isDeleted', 'refFk'] + }, myOptions); */ + + return false; + }; +}; \ No newline at end of file diff --git a/modules/ticket/back/models/ticket-methods.js b/modules/ticket/back/models/ticket-methods.js index f0c85ecc4..b432c9f6b 100644 --- a/modules/ticket/back/models/ticket-methods.js +++ b/modules/ticket/back/models/ticket-methods.js @@ -19,6 +19,7 @@ module.exports = function(Self) { require('../methods/ticket/uploadFile')(Self); require('../methods/ticket/addSale')(Self); require('../methods/ticket/transferSales')(Self); + require('../methods/ticket/transferClient')(Self); require('../methods/ticket/recalculateComponents')(Self); require('../methods/ticket/sendSms')(Self); require('../methods/ticket/isLocked')(Self); diff --git a/modules/ticket/front/descriptor-menu/index.js b/modules/ticket/front/descriptor-menu/index.js index 0fc8488ca..019ae4fa6 100644 --- a/modules/ticket/front/descriptor-menu/index.js +++ b/modules/ticket/front/descriptor-menu/index.js @@ -96,6 +96,14 @@ class Controller extends Section { } transferClient() { + const ticket = this.ticket; + const clientId = ticket.client.id; + console.log('ticketId',ticket.id) + console.log('clientId',clientId); + this.$http.patch(`Tickets/${ticket.id}/transferClient`, {clientId}).then(() =>{ + this.vnApp.showSuccess(this.$t('Data saved!')); + this.reload(); + }) this.$http.get(`Clients/${this.ticket.client.id}`).then(client => { const ticket = this.ticket; -- 2.40.1 From 618989052a481df85338fa54ec297c0ae57d876c Mon Sep 17 00:00:00 2001 From: jorgep Date: Thu, 20 Jul 2023 15:15:33 +0200 Subject: [PATCH 2/2] refs #5866 fix transferClient and test added --- .../{233001 => 233201}/00-transferClient.sql | 0 .../ticket/specs/transferClient.spec.js | 49 +++++++++++++++ .../back/methods/ticket/transferClient.js | 60 +++++++------------ modules/ticket/front/descriptor-menu/index.js | 18 +----- .../front/descriptor-menu/index.spec.js | 13 ---- 5 files changed, 71 insertions(+), 69 deletions(-) rename db/changes/{233001 => 233201}/00-transferClient.sql (100%) create mode 100644 modules/ticket/back/methods/ticket/specs/transferClient.spec.js diff --git a/db/changes/233001/00-transferClient.sql b/db/changes/233201/00-transferClient.sql similarity index 100% rename from db/changes/233001/00-transferClient.sql rename to db/changes/233201/00-transferClient.sql diff --git a/modules/ticket/back/methods/ticket/specs/transferClient.spec.js b/modules/ticket/back/methods/ticket/specs/transferClient.spec.js new file mode 100644 index 000000000..ed10e5159 --- /dev/null +++ b/modules/ticket/back/methods/ticket/specs/transferClient.spec.js @@ -0,0 +1,49 @@ +const models = require('vn-loopback/server/server').models; + +describe('Ticket transferClient()', () => { + const userId = 9; + const activeCtx = { + accessToken: {userId: userId}, + }; + const ctx = {req: activeCtx}; + + it('should throw an error as the ticket is not editable', async() => { + const tx = await models.Ticket.beginTransaction({}); + let error; + + try { + const options = {transaction: tx}; + const ticketId = 4; + const clientId = 1; + await models.Ticket.transferClient(ctx, ticketId, clientId, options); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + error = e; + } + + expect(error.message).toEqual(`The current ticket can't be modified`); + }); + + it('should be assigned a different clientFk', async() => { + const tx = await models.Ticket.beginTransaction({}); + let updatedTicket; + const ticketId = 10; + const clientId = 1; + + try { + const options = {transaction: tx}; + + await models.Ticket.transferClient(ctx, ticketId, clientId, options); + updatedTicket = await models.Ticket.findById(ticketId, {fields: ['clientFk']}, options); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + + expect(updatedTicket.clientFk).toEqual(clientId); + }); +}); diff --git a/modules/ticket/back/methods/ticket/transferClient.js b/modules/ticket/back/methods/ticket/transferClient.js index 226f5dedf..05ea27fa0 100644 --- a/modules/ticket/back/methods/ticket/transferClient.js +++ b/modules/ticket/back/methods/ticket/transferClient.js @@ -12,58 +12,38 @@ module.exports = Self => { http: {source: 'path'} }, { - arg: 'data', - type: 'object', + arg: 'clientId', + type: 'number', required: true, - description: 'the client id', - http: {source: 'body'} }, ], - returns: { - type: 'boolean', - root: true - }, http: { path: `/:id/transferClient`, verb: 'PATCH' } }); - Self.transferClient = async(ctx, ticketId, params) => { + Self.transferClient = async(ctx, ticketId, clientId, options) => { const models = Self.app.models; - const args = ctx.args; const myOptions = {}; - try { - const clientId = params.clientId; - const isEditable = await Self.isEditable(ctx, args.id, myOptions); - console.log('es editable?',isEditable) // Revisar - /* if (!isEditable){ - console.log('no es editable!') - throw new UserError(`The sales of this ticket can't be modified`); - } */ - - const ticket = await models.Ticket.findById(ticketId, myOptions); - console.log('ticket',ticket); - if(!ticket) return false; - const nparams = { - clientFk: clientId, - addressFk: ticket.addressFk, - } - - const promise = await ticket.updateAttributes(nparams); - console.log('promise', promise); - if(promise) return true; - } catch (error) { - console.log(error); - } - - /* if (typeof options == 'object') + if (typeof options == 'object') Object.assign(myOptions, options); - const ticket = await Self.findById(id, { - fields: ['isDeleted', 'refFk'] - }, myOptions); */ + const isEditable = await Self.isEditable(ctx, ticketId, myOptions); - return false; + if (!isEditable) + throw new UserError(`The current ticket can't be modified`); + + const ticket = await models.Ticket.findById( + ticketId, + {fields: ['id', 'shipped', 'clientFk', 'addressFk']}, + myOptions + ); + const client = await models.Client.findById(clientId, {fields: ['id', 'defaultAddressFk']}, myOptions); + + await ticket.updateAttributes({ + clientFk: clientId, + addressFk: client.defaultAddressFk, + }); }; -}; \ No newline at end of file +}; diff --git a/modules/ticket/front/descriptor-menu/index.js b/modules/ticket/front/descriptor-menu/index.js index 019ae4fa6..62a233891 100644 --- a/modules/ticket/front/descriptor-menu/index.js +++ b/modules/ticket/front/descriptor-menu/index.js @@ -98,26 +98,12 @@ class Controller extends Section { transferClient() { const ticket = this.ticket; const clientId = ticket.client.id; - console.log('ticketId',ticket.id) - console.log('clientId',clientId); - this.$http.patch(`Tickets/${ticket.id}/transferClient`, {clientId}).then(() =>{ - this.vnApp.showSuccess(this.$t('Data saved!')); - this.reload(); - }) - this.$http.get(`Clients/${this.ticket.client.id}`).then(client => { - const ticket = this.ticket; - const params = - { - clientFk: client.data.id, - addressFk: client.data.defaultAddressFk, - }; - - this.$http.patch(`Tickets/${ticket.id}`, params).then(() => { + this.$http.patch(`Tickets/${ticket.id}/transferClient`, {clientId}) + .then(() => { this.vnApp.showSuccess(this.$t('Data saved!')); this.reload(); }); - }); } isTicketEditable() { diff --git a/modules/ticket/front/descriptor-menu/index.spec.js b/modules/ticket/front/descriptor-menu/index.spec.js index 914fe486c..1bb270165 100644 --- a/modules/ticket/front/descriptor-menu/index.spec.js +++ b/modules/ticket/front/descriptor-menu/index.spec.js @@ -326,17 +326,4 @@ describe('Ticket Component vnTicketDescriptorMenu', () => { expect(controller.vnApp.showSuccess).toHaveBeenCalled(); }); }); - - describe('transferClient()', () => { - it(`should perform two queries, a get to obtain the clientData and a patch to update the ticket`, () => { - const client = - { - clientFk: 1101, - addressFk: 1, - }; - $httpBackend.expect('GET', `Clients/${ticket.client.id}`).respond(client); - $httpBackend.expect('PATCH', `Tickets/${ticket.id}`).respond(); - controller.transferClient(); - }); - }); }); -- 2.40.1