From 6f2a7a5cef9c0cc1460b242978cbae90d230ea7c Mon Sep 17 00:00:00 2001 From: vicent Date: Wed, 23 Nov 2022 15:15:40 +0100 Subject: [PATCH 01/11] =?UTF-8?q?feat:=20enviar=20sms=20con=20cambios=20en?= =?UTF-8?q?=20las=20l=C3=ADneas?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/dump/fixtures.sql | 5 ++ .../back/methods/ticket-log/getChanges.js | 52 +++++++++++++++++++ .../ticket-log/specs/getChanges.spec.js | 46 ++++++++++++++++ modules/ticket/back/models/ticket-log.js | 3 ++ .../ticket/front/descriptor-menu/index.html | 30 ++++++----- modules/ticket/front/descriptor-menu/index.js | 6 +++ .../front/descriptor-menu/locale/es.yml | 3 +- modules/ticket/front/descriptor/locale/en.yml | 1 + 8 files changed, 133 insertions(+), 13 deletions(-) create mode 100644 modules/ticket/back/methods/ticket-log/getChanges.js create mode 100644 modules/ticket/back/methods/ticket-log/specs/getChanges.spec.js create mode 100644 modules/ticket/back/models/ticket-log.js diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index 41cd84638..77fd573cd 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -2728,6 +2728,11 @@ UPDATE `account`.`user` SET `hasGrant` = 1 WHERE `id` = 66; +INSERT INTO `vn`.`ticketLog` (`originFk`, userFk, `action`, changedModel, oldInstance, newInstance, changedModelId) + VALUES + (7, 18, 'update', 'Sale', '{"quantity":1}', '{"quantity":10}', 22); + + INSERT INTO `vn`.`osTicketConfig` (`id`, `host`, `user`, `password`, `oldStatus`, `newStatusId`, `day`, `comment`, `hostDb`, `userDb`, `passwordDb`, `portDb`, `responseType`, `fromEmailId`, `replyTo`) VALUES (0, 'http://localhost:56596/scp', 'ostadmin', 'Admin1', 'open', 3, 60, 'Este CAU se ha cerrado automáticamente. Si el problema persiste responda a este mensaje.', 'localhost', 'osticket', 'osticket', 40003, 'reply', 1, 'all'); diff --git a/modules/ticket/back/methods/ticket-log/getChanges.js b/modules/ticket/back/methods/ticket-log/getChanges.js new file mode 100644 index 000000000..31677f158 --- /dev/null +++ b/modules/ticket/back/methods/ticket-log/getChanges.js @@ -0,0 +1,52 @@ +module.exports = Self => { + Self.remoteMethodCtx('getChanges', { + description: 'Get item id and quantity changes on the sales of a ticket', + accessType: 'WRITE', + accepts: [ + { + arg: 'ticketId', + description: 'the ticket id', + type: 'number', + required: true, + http: {source: 'body'} + } + ], + returns: { + type: 'object', + root: true + }, + http: { + path: `/:id/getChanges`, + verb: 'POST' + } + }); + + Self.getChanges = async(ctx, ticketId, options) => { + const models = Self.app.models; + const myOptions = {}; + let tx; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + try { + const ticketLogs = await models.TicketLog.find({where: {originFk: ticketId}}, myOptions); + + for (const ticketLog of ticketLogs) { + if (ticketLog) + } + + if (tx) await tx.commit(); + + return state; + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } + }; +}; diff --git a/modules/ticket/back/methods/ticket-log/specs/getChanges.spec.js b/modules/ticket/back/methods/ticket-log/specs/getChanges.spec.js new file mode 100644 index 000000000..3d37221c4 --- /dev/null +++ b/modules/ticket/back/methods/ticket-log/specs/getChanges.spec.js @@ -0,0 +1,46 @@ +const models = require('vn-loopback/server/server').models; +const LoopBackContext = require('loopback-context'); + +describe('ticket setDelivered()', () => { + const userId = 50; + const activeCtx = { + accessToken: {userId: userId}, + }; + + beforeAll(async() => { + spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ + active: activeCtx + }); + }); + + it('should return the state which has been applied to the given tickets', async() => { + const tx = await models.TicketTracking.beginTransaction({}); + + try { + const options = {transaction: tx}; + + const ctx = {req: {accessToken: {userId: 49}}}; + + const originalTicketOne = await models.Ticket.findById(8, null, options); + const originalTicketTwo = await models.Ticket.findById(10, null, options); + + originalTicketOne.id = null; + originalTicketTwo.id = null; + + const ticketOne = await models.Ticket.create(originalTicketOne, options); + const ticketTwo = await models.Ticket.create(originalTicketTwo, options); + + const delivered = await models.State.findOne({where: {code: 'delivered'}, fields: ['id']}, options); + + const params = [ticketOne.id, ticketTwo.id]; + const state = await models.TicketTracking.setDelivered(ctx, params, options); + + expect(state.id).toEqual(delivered.id); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); +}); diff --git a/modules/ticket/back/models/ticket-log.js b/modules/ticket/back/models/ticket-log.js new file mode 100644 index 000000000..81855ada7 --- /dev/null +++ b/modules/ticket/back/models/ticket-log.js @@ -0,0 +1,3 @@ +module.exports = function(Self) { + require('../methods/ticket-log/getChanges')(Self); +}; diff --git a/modules/ticket/front/descriptor-menu/index.html b/modules/ticket/front/descriptor-menu/index.html index 0c04b42fb..052659b85 100644 --- a/modules/ticket/front/descriptor-menu/index.html +++ b/modules/ticket/front/descriptor-menu/index.html @@ -43,7 +43,7 @@ ng-if="!$ctrl.hasDocuwareFile" ng-click="$ctrl.showPdfDeliveryNote('withoutPrices')" translate> - as PDF without prices + as PDF without prices SMS Minimum import + + SMS Notify changes + @@ -251,13 +257,13 @@ - - - \ 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 e0e7ea849..0265dd1d1 100644 --- a/modules/ticket/front/descriptor-menu/index.js +++ b/modules/ticket/front/descriptor-menu/index.js @@ -224,6 +224,12 @@ class Controller extends Section { }); } + sendChangesSms() { + this.showSMSDialog({ + message: this.$t('Send changes') + }); + } + showSMSDialog(params) { const address = this.ticket.address; const client = this.ticket.client; diff --git a/modules/ticket/front/descriptor-menu/locale/es.yml b/modules/ticket/front/descriptor-menu/locale/es.yml index 968c61f84..7621b8b3b 100644 --- a/modules/ticket/front/descriptor-menu/locale/es.yml +++ b/modules/ticket/front/descriptor-menu/locale/es.yml @@ -10,4 +10,5 @@ Send PDF Delivery Note: Enviar albarán en PDF Show Proforma: Ver proforma Refund all: Abonar todo The following refund ticket have been created: "Se ha creado siguiente ticket de abono: {{ticketId}}" -Transfer client: Transferir cliente \ No newline at end of file +Transfer client: Transferir cliente +SMS Notify changes: SMS Notificar cambios diff --git a/modules/ticket/front/descriptor/locale/en.yml b/modules/ticket/front/descriptor/locale/en.yml index 64075c7ef..8eed2265d 100644 --- a/modules/ticket/front/descriptor/locale/en.yml +++ b/modules/ticket/front/descriptor/locale/en.yml @@ -1,2 +1,3 @@ Make a payment: "Verdnatura communicates:\rYour order is pending of payment.\rPlease, enter the web page and make the payment with card.\rThank you." Minimum is needed: "Verdnatura communicates:\rA minimum import of 50€ (Without BAT) is needed for your order {{ticketId}} from date {{created | date: 'dd/MM/yyyy'}} to receive it with no extra fees." +Send changes: "Verdnatura communicates:\rOrder {{ticketId}} date {{created | date: 'dd/MM/yyyy'}}\r{{changes}}" From 84c6604dd9c4785125c2747bcf97625b7183f738 Mon Sep 17 00:00:00 2001 From: vicent Date: Thu, 24 Nov 2022 15:04:15 +0100 Subject: [PATCH 02/11] feat: enviar sms con cambios en el pedido --- db/changes/10503-november/00-aclTicketLog.sql | 3 + db/dump/fixtures.sql | 7 ++- .../back/methods/ticket-log/getChanges.js | 55 +++++++++---------- modules/ticket/front/descriptor-menu/index.js | 12 +++- modules/ticket/front/descriptor/locale/es.yml | 1 + 5 files changed, 45 insertions(+), 33 deletions(-) create mode 100644 db/changes/10503-november/00-aclTicketLog.sql diff --git a/db/changes/10503-november/00-aclTicketLog.sql b/db/changes/10503-november/00-aclTicketLog.sql new file mode 100644 index 000000000..edba17ab4 --- /dev/null +++ b/db/changes/10503-november/00-aclTicketLog.sql @@ -0,0 +1,3 @@ +INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) + VALUES + ('TicketLog', 'getChanges', 'READ', 'ALLOW', 'ROLE', 'employee'); diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index 77fd573cd..4ba95dd8c 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -2728,9 +2728,12 @@ UPDATE `account`.`user` SET `hasGrant` = 1 WHERE `id` = 66; -INSERT INTO `vn`.`ticketLog` (`originFk`, userFk, `action`, changedModel, oldInstance, newInstance, changedModelId) +INSERT INTO `vn`.`ticketLog` (`originFk`, userFk, `action`, changedModel, oldInstance, newInstance, changedModelId, `description`) VALUES - (7, 18, 'update', 'Sale', '{"quantity":1}', '{"quantity":10}', 22); + (7, 18, 'update', 'Sale', '{"quantity":1}', '{"quantity":10}', 1, NULL), + (7, 18, 'update', 'Ticket', '{"quantity":1,"concept":"Chest ammo box"}', '{"quantity":10,"concept":"Chest ammo box"}', 1, NULL), + (7, 18, 'update', 'Sale', '{"price":3}', '{"price":5}', 1, NULL), + (7, 18, 'update', NULL, NULL, NULL, NULL, "Cambio cantidad Melee weapon heavy shield 1x0.5m de '5' a '10'"); INSERT INTO `vn`.`osTicketConfig` (`id`, `host`, `user`, `password`, `oldStatus`, `newStatusId`, `day`, `comment`, `hostDb`, `userDb`, `passwordDb`, `portDb`, `responseType`, `fromEmailId`, `replyTo`) diff --git a/modules/ticket/back/methods/ticket-log/getChanges.js b/modules/ticket/back/methods/ticket-log/getChanges.js index 31677f158..a025bac05 100644 --- a/modules/ticket/back/methods/ticket-log/getChanges.js +++ b/modules/ticket/back/methods/ticket-log/getChanges.js @@ -1,52 +1,51 @@ module.exports = Self => { Self.remoteMethodCtx('getChanges', { - description: 'Get item id and quantity changes on the sales of a ticket', - accessType: 'WRITE', - accepts: [ - { - arg: 'ticketId', - description: 'the ticket id', - type: 'number', - required: true, - http: {source: 'body'} - } - ], + description: 'Check if a ticket is editable', + accessType: 'READ', + accepts: [{ + arg: 'id', + type: 'number', + required: true, + description: 'the ticket id', + http: {source: 'path'} + }], returns: { type: 'object', root: true }, http: { path: `/:id/getChanges`, - verb: 'POST' + verb: 'get' } }); - Self.getChanges = async(ctx, ticketId, options) => { + Self.getChanges = async(ctx, id, options) => { const models = Self.app.models; const myOptions = {}; - let tx; if (typeof options == 'object') Object.assign(myOptions, options); - if (!myOptions.transaction) { - tx = await Self.beginTransaction({}); - myOptions.transaction = tx; - } + const ticketLogs = await models.TicketLog.find({where: {originFk: id}}, myOptions); - try { - const ticketLogs = await models.TicketLog.find({where: {originFk: ticketId}}, myOptions); + const changes = []; + for (const ticketLog of ticketLogs) { + const isUpdate = ticketLog.action == 'update'; - for (const ticketLog of ticketLogs) { - if (ticketLog) + if (ticketLog.description && isUpdate) + changes.push(ticketLog.description); + + const oldQuantity = ticketLog.oldInstance ? ticketLog.oldInstance.quantity : null; + const newQuantity = ticketLog.newInstance ? ticketLog.newInstance.quantity : null; + + if (ticketLog.changedModel == 'Sale' && isUpdate && ticketLog.newInstance.quantity) { + const item = await models.Item.findById(ticketLog.changedModelId, null, myOptions); + changes.push(`${item.name} cambia de ${oldQuantity} a ${newQuantity}`); } - if (tx) await tx.commit(); - - return state; - } catch (e) { - if (tx) await tx.rollback(); - throw e; + if (ticketLog.changedModel == 'Ticket' && isUpdate && ticketLog.newInstance.quantity) + changes.push(`${ticketLog.oldInstance.concept} cambia de ${oldQuantity} a ${newQuantity}`); } + return changes.join('\n'); }; }; diff --git a/modules/ticket/front/descriptor-menu/index.js b/modules/ticket/front/descriptor-menu/index.js index 0265dd1d1..b309ba965 100644 --- a/modules/ticket/front/descriptor-menu/index.js +++ b/modules/ticket/front/descriptor-menu/index.js @@ -225,9 +225,15 @@ class Controller extends Section { } sendChangesSms() { - this.showSMSDialog({ - message: this.$t('Send changes') - }); + return this.$http.get(`TicketLogs/${this.id}/getChanges`) + .then(res => { + const params = { + ticketId: this.id, + created: this.ticket.updated, + changes: res.data + }; + this.showSMSDialog({message: this.$t('Send changes', params)}); + }); } showSMSDialog(params) { diff --git a/modules/ticket/front/descriptor/locale/es.yml b/modules/ticket/front/descriptor/locale/es.yml index bce9e62d7..d921b5dc2 100644 --- a/modules/ticket/front/descriptor/locale/es.yml +++ b/modules/ticket/front/descriptor/locale/es.yml @@ -23,3 +23,4 @@ Restore ticket: Restaurar ticket You are going to restore this ticket: Vas a restaurar este ticket Are you sure you want to restore this ticket?: ¿Seguro que quieres restaurar el ticket? Are you sure you want to refund all?: ¿Seguro que quieres abonar todo? +Send changes: "Verdnatura le recuerda:\rPedido {{ticketId}} día {{created | date: 'dd/MM/yyyy'}}\r{{changes}}" From e540bf7b31f99444ae33e94510f83644944a664b Mon Sep 17 00:00:00 2001 From: vicent Date: Fri, 25 Nov 2022 09:38:34 +0100 Subject: [PATCH 03/11] feat: inserta en la nueva tabla ticketSms --- db/changes/10503-november/00-ticketSms.sql | 10 +++++++ modules/client/back/model-config.json | 3 ++ modules/client/back/models/ticket-sms.json | 28 +++++++++++++++++++ modules/ticket/back/methods/ticket/sendSms.js | 12 ++++++-- 4 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 db/changes/10503-november/00-ticketSms.sql create mode 100644 modules/client/back/models/ticket-sms.json diff --git a/db/changes/10503-november/00-ticketSms.sql b/db/changes/10503-november/00-ticketSms.sql new file mode 100644 index 000000000..8ebaedb30 --- /dev/null +++ b/db/changes/10503-november/00-ticketSms.sql @@ -0,0 +1,10 @@ +CREATE TABLE `vn`.`ticketSms` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `ticketFk` int(11) DEFAULT NULL, + `smsFk` mediumint(8) unsigned DEFAULT NULL, + PRIMARY KEY (`id`), + KEY `ticketSms_FK` (`ticketFk`), + KEY `ticketSms_FK_1` (`smsFk`), + CONSTRAINT `ticketSms_FK` FOREIGN KEY (`ticketFk`) REFERENCES `ticket` (`id`) ON UPDATE CASCADE, + CONSTRAINT `ticketSms_FK_1` FOREIGN KEY (`smsFk`) REFERENCES `sms` (`id`) ON UPDATE CASCADE +) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci diff --git a/modules/client/back/model-config.json b/modules/client/back/model-config.json index 4ef34ca3a..b466aa5a1 100644 --- a/modules/client/back/model-config.json +++ b/modules/client/back/model-config.json @@ -104,6 +104,9 @@ "SageTransactionType": { "dataSource": "vn" }, + "TicketSms": { + "dataSource": "vn" + }, "TpvError": { "dataSource": "vn" }, diff --git a/modules/client/back/models/ticket-sms.json b/modules/client/back/models/ticket-sms.json new file mode 100644 index 000000000..1787ba17c --- /dev/null +++ b/modules/client/back/models/ticket-sms.json @@ -0,0 +1,28 @@ +{ + "name": "TicketSms", + "base": "VnModel", + "options": { + "mysql": { + "table": "ticketSms" + } + }, + "properties": { + "id": { + "type": "number", + "id": true, + "description": "Identifier" + } + }, + "relations": { + "ticket": { + "type": "belongsTo", + "model": "Ticket", + "foreignKey": "ticketFk" + }, + "sms": { + "type": "belongsTo", + "model": "Sms", + "foreignKey": "smsFk" + } + } +} diff --git a/modules/ticket/back/methods/ticket/sendSms.js b/modules/ticket/back/methods/ticket/sendSms.js index a0adcae07..2336ae859 100644 --- a/modules/ticket/back/methods/ticket/sendSms.js +++ b/modules/ticket/back/methods/ticket/sendSms.js @@ -31,6 +31,7 @@ module.exports = Self => { }); Self.sendSms = async(ctx, id, destination, message, options) => { + const models = Self.app.models; const myOptions = {}; let tx; @@ -45,7 +46,14 @@ module.exports = Self => { const userId = ctx.req.accessToken.userId; try { - const sms = await Self.app.models.Sms.send(ctx, destination, message); + const sms = await models.Sms.send(ctx, destination, message); + + const newTicketSms = { + ticketFk: id, + smsFk: sms.id + }; + await models.TicketSms.create(newTicketSms); + const logRecord = { originFk: id, userFk: userId, @@ -60,7 +68,7 @@ module.exports = Self => { } }; - const ticketLog = await Self.app.models.TicketLog.create(logRecord, myOptions); + const ticketLog = await models.TicketLog.create(logRecord, myOptions); sms.logId = ticketLog.id; From bd91d4a6494a4adb87bc592805d6f80ca281a4c8 Mon Sep 17 00:00:00 2001 From: vicent Date: Fri, 25 Nov 2022 11:50:24 +0100 Subject: [PATCH 04/11] =?UTF-8?q?fix:=20a=C3=B1adida=20transacci=C3=B3n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/methods/collection/setSaleQuantity.js | 29 +++++++++++++++---- .../collection/spec/setSaleQuantity.spec.js | 25 +++++++++++----- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/back/methods/collection/setSaleQuantity.js b/back/methods/collection/setSaleQuantity.js index 644c44a60..b6c56ddc4 100644 --- a/back/methods/collection/setSaleQuantity.js +++ b/back/methods/collection/setSaleQuantity.js @@ -26,11 +26,30 @@ module.exports = Self => { Self.setSaleQuantity = async(saleId, quantity) => { const models = Self.app.models; + const myOptions = {}; + let tx; - const sale = await models.Sale.findById(saleId); - return await sale.updateAttributes({ - originalQuantity: sale.quantity, - quantity: quantity - }); + if (typeof options == 'object') + Object.assign(myOptions, options); + + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + try { + const sale = await models.Sale.findById(saleId, null, myOptions); + const saleUpdated = await sale.updateAttributes({ + originalQuantity: sale.quantity, + quantity: quantity + }, myOptions); + + if (tx) await tx.commit(); + + return saleUpdated; + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } }; }; diff --git a/back/methods/collection/spec/setSaleQuantity.spec.js b/back/methods/collection/spec/setSaleQuantity.spec.js index 5d06a4383..63dc3bd2d 100644 --- a/back/methods/collection/spec/setSaleQuantity.spec.js +++ b/back/methods/collection/spec/setSaleQuantity.spec.js @@ -2,15 +2,26 @@ const models = require('vn-loopback/server/server').models; describe('setSaleQuantity()', () => { it('should change quantity sale', async() => { - const saleId = 30; - const newQuantity = 10; + const tx = await models.Ticket.beginTransaction({}); - const originalSale = await models.Sale.findById(saleId); + try { + const options = {transaction: tx}; - await models.Collection.setSaleQuantity(saleId, newQuantity); - const updateSale = await models.Sale.findById(saleId); + const saleId = 30; + const newQuantity = 10; - expect(updateSale.originalQuantity).toEqual(originalSale.quantity); - expect(updateSale.quantity).toEqual(newQuantity); + const originalSale = await models.Sale.findById(saleId, null, options); + + await models.Collection.setSaleQuantity(saleId, newQuantity, options); + const updateSale = await models.Sale.findById(saleId, null, options); + + expect(updateSale.originalQuantity).toEqual(originalSale.quantity); + expect(updateSale.quantity).toEqual(newQuantity); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); }); From bca79e1240af79a14f6d1c2f99b437c60edec42b Mon Sep 17 00:00:00 2001 From: vicent Date: Fri, 25 Nov 2022 11:50:54 +0100 Subject: [PATCH 05/11] feat: add backTest --- .../ticket-log/specs/getChanges.spec.js | 45 +++---------------- .../back/methods/ticket/specs/sendSms.spec.js | 7 +++ 2 files changed, 12 insertions(+), 40 deletions(-) diff --git a/modules/ticket/back/methods/ticket-log/specs/getChanges.spec.js b/modules/ticket/back/methods/ticket-log/specs/getChanges.spec.js index 3d37221c4..cdbebf9b3 100644 --- a/modules/ticket/back/methods/ticket-log/specs/getChanges.spec.js +++ b/modules/ticket/back/methods/ticket-log/specs/getChanges.spec.js @@ -1,46 +1,11 @@ const models = require('vn-loopback/server/server').models; -const LoopBackContext = require('loopback-context'); -describe('ticket setDelivered()', () => { - const userId = 50; - const activeCtx = { - accessToken: {userId: userId}, - }; +describe('ticketLog getChanges()', () => { + it('should return the changes in the sales of a ticket', async() => { + const ticketId = 1; - beforeAll(async() => { - spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ - active: activeCtx - }); - }); + const changues = await models.TicketLog.getChanges({id: ticketId}); - it('should return the state which has been applied to the given tickets', async() => { - const tx = await models.TicketTracking.beginTransaction({}); - - try { - const options = {transaction: tx}; - - const ctx = {req: {accessToken: {userId: 49}}}; - - const originalTicketOne = await models.Ticket.findById(8, null, options); - const originalTicketTwo = await models.Ticket.findById(10, null, options); - - originalTicketOne.id = null; - originalTicketTwo.id = null; - - const ticketOne = await models.Ticket.create(originalTicketOne, options); - const ticketTwo = await models.Ticket.create(originalTicketTwo, options); - - const delivered = await models.State.findOne({where: {code: 'delivered'}, fields: ['id']}, options); - - const params = [ticketOne.id, ticketTwo.id]; - const state = await models.TicketTracking.setDelivered(ctx, params, options); - - expect(state.id).toEqual(delivered.id); - - await tx.rollback(); - } catch (e) { - await tx.rollback(); - throw e; - } + expect(changues).toContain(`cambia de 1 a 10`); }); }); diff --git a/modules/ticket/back/methods/ticket/specs/sendSms.spec.js b/modules/ticket/back/methods/ticket/specs/sendSms.spec.js index f50253b10..f94b8be2a 100644 --- a/modules/ticket/back/methods/ticket/specs/sendSms.spec.js +++ b/modules/ticket/back/methods/ticket/specs/sendSms.spec.js @@ -15,9 +15,16 @@ describe('ticket sendSms()', () => { const sms = await models.Ticket.sendSms(ctx, id, destination, message, options); const createdLog = await models.TicketLog.findById(sms.logId, null, options); + + const filter = { + ticketFk: createdLog.originFk + }; + const ticketSms = await models.TicketSms.findOne(filter, options); + const json = JSON.parse(JSON.stringify(createdLog.newInstance)); expect(json.message).toEqual(message); + expect(ticketSms.ticketFk).toEqual(createdLog.originFk); await tx.rollback(); } catch (e) { From af87fc2d53df1ae642556144d5026ea2e900c716 Mon Sep 17 00:00:00 2001 From: vicent Date: Fri, 25 Nov 2022 11:51:07 +0100 Subject: [PATCH 06/11] fix: modelo incorrecto --- modules/ticket/back/methods/ticket-log/getChanges.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/ticket/back/methods/ticket-log/getChanges.js b/modules/ticket/back/methods/ticket-log/getChanges.js index a025bac05..bb40a66d8 100644 --- a/modules/ticket/back/methods/ticket-log/getChanges.js +++ b/modules/ticket/back/methods/ticket-log/getChanges.js @@ -1,6 +1,6 @@ module.exports = Self => { Self.remoteMethodCtx('getChanges', { - description: 'Check if a ticket is editable', + description: 'Get changues in the sales of a ticket', accessType: 'READ', accepts: [{ arg: 'id', @@ -39,8 +39,8 @@ module.exports = Self => { const newQuantity = ticketLog.newInstance ? ticketLog.newInstance.quantity : null; if (ticketLog.changedModel == 'Sale' && isUpdate && ticketLog.newInstance.quantity) { - const item = await models.Item.findById(ticketLog.changedModelId, null, myOptions); - changes.push(`${item.name} cambia de ${oldQuantity} a ${newQuantity}`); + const sale = await models.Sale.findById(ticketLog.changedModelId, null, myOptions); + changes.push(`${sale.concept} cambia de ${oldQuantity} a ${newQuantity}`); } if (ticketLog.changedModel == 'Ticket' && isUpdate && ticketLog.newInstance.quantity) From d15e2551a2662cefb808c4e21b5cfb9fe1c242a5 Mon Sep 17 00:00:00 2001 From: vicent Date: Fri, 25 Nov 2022 12:16:01 +0100 Subject: [PATCH 07/11] feat: add frontTest --- modules/ticket/front/descriptor-menu/index.spec.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/modules/ticket/front/descriptor-menu/index.spec.js b/modules/ticket/front/descriptor-menu/index.spec.js index 1716e36f6..94298b099 100644 --- a/modules/ticket/front/descriptor-menu/index.spec.js +++ b/modules/ticket/front/descriptor-menu/index.spec.js @@ -258,6 +258,19 @@ describe('Ticket Component vnTicketDescriptorMenu', () => { }); }); + describe('sendChangesSms()', () => { + it('should make a query and open the sms dialog', () => { + controller.$.sms = {open: () => {}}; + jest.spyOn(controller.$.sms, 'open'); + + $httpBackend.expectGET(`TicketLogs/${ticket.id}/getChanges`).respond(); + controller.sendChangesSms(); + $httpBackend.flush(); + + expect(controller.$.sms.open).toHaveBeenCalledWith(); + }); + }); + describe('showSMSDialog()', () => { it('should set the destionationFk and destination properties and then call the sms open() method', () => { controller.$.sms = {open: () => {}}; From 1588492f9abacf1aec82fcedb2d79b3b670958d0 Mon Sep 17 00:00:00 2001 From: vicent Date: Wed, 30 Nov 2022 13:29:39 +0100 Subject: [PATCH 08/11] =?UTF-8?q?feat:=20solo=20a=C3=B1ade=20los=20logs=20?= =?UTF-8?q?de=20salix=20con=20el=20model=20'Sale'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- loopback/locale/en.json | 3 +- loopback/locale/es.json | 3 +- .../back/methods/ticket-log/getChanges.js | 39 ++++++++++++------- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/loopback/locale/en.json b/loopback/locale/en.json index 36da9f661..acc4ba39d 100644 --- a/loopback/locale/en.json +++ b/loopback/locale/en.json @@ -66,7 +66,8 @@ "MESSAGE_INSURANCE_CHANGE": "I have changed the insurence credit of client [{{clientName}} ({{clientId}})]({{{url}}}) to *{{credit}} €*", "Changed client paymethod": "I have changed the pay method for client [{{clientName}} ({{clientId}})]({{{url}}})", "Sent units from ticket": "I sent *{{quantity}}* units of [{{concept}} ({{itemId}})]({{{itemUrl}}}) to *\"{{nickname}}\"* coming from ticket id [{{ticketId}}]({{{ticketUrl}}})", - "Claim will be picked": "The product from the claim [({{claimId}})]({{{claimUrl}}}) from the client *{{clientName}}* will be picked", + "Change quantity": "{{concept}} change of {{oldQuantity}} to {{newQuantity}}", + "Claim will be picked": "The product from the claim [({{claimId}})]({{{claimUrl}}}) from the client *{{clientName}}* will be picked", "Claim state has changed to incomplete": "The state of the claim [({{claimId}})]({{{claimUrl}}}) from client *{{clientName}}* has changed to *incomplete*", "Claim state has changed to canceled": "The state of the claim [({{claimId}})]({{{claimUrl}}}) from client *{{clientName}}* has changed to *canceled*", "Customs agent is required for a non UEE member": "Customs agent is required for a non UEE member", diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 747007713..98fc9a3c6 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -134,7 +134,8 @@ "MESSAGE_INSURANCE_CHANGE": "He cambiado el crédito asegurado del cliente [{{clientName}} ({{clientId}})]({{{url}}}) a *{{credit}} €*", "Changed client paymethod": "He cambiado la forma de pago del cliente [{{clientName}} ({{clientId}})]({{{url}}})", "Sent units from ticket": "Envio *{{quantity}}* unidades de [{{concept}} ({{itemId}})]({{{itemUrl}}}) a *\"{{nickname}}\"* provenientes del ticket id [{{ticketId}}]({{{ticketUrl}}})", - "Claim will be picked": "Se recogerá el género de la reclamación [({{claimId}})]({{{claimUrl}}}) del cliente *{{clientName}}*", + "Change quantity": "{{concept}} cambia de {{oldQuantity}} a {{newQuantity}}", + "Claim will be picked": "Se recogerá el género de la reclamación [({{claimId}})]({{{claimUrl}}}) del cliente *{{clientName}}*", "Claim state has changed to incomplete": "Se ha cambiado el estado de la reclamación [({{claimId}})]({{{claimUrl}}}) del cliente *{{clientName}}* a *incompleta*", "Claim state has changed to canceled": "Se ha cambiado el estado de la reclamación [({{claimId}})]({{{claimUrl}}}) del cliente *{{clientName}}* a *anulado*", "Client checked as validated despite of duplication": "Cliente comprobado a pesar de que existe el cliente id {{clientId}}", diff --git a/modules/ticket/back/methods/ticket-log/getChanges.js b/modules/ticket/back/methods/ticket-log/getChanges.js index bb40a66d8..d020a0957 100644 --- a/modules/ticket/back/methods/ticket-log/getChanges.js +++ b/modules/ticket/back/methods/ticket-log/getChanges.js @@ -22,29 +22,42 @@ module.exports = Self => { Self.getChanges = async(ctx, id, options) => { const models = Self.app.models; const myOptions = {}; + const $t = ctx.req.__; // $translate if (typeof options == 'object') Object.assign(myOptions, options); - const ticketLogs = await models.TicketLog.find({where: {originFk: id}}, myOptions); + const ticketLogs = await models.TicketLog.find( + { + where: { + and: [ + {originFk: id}, + {action: 'update'}, + {changedModel: 'Sale'} + ] + }, + fields: [ + 'oldInstance', + 'newInstance', + 'changedModelId' + ], + }, myOptions); const changes = []; for (const ticketLog of ticketLogs) { - const isUpdate = ticketLog.action == 'update'; + const oldQuantity = ticketLog.oldInstance.quantity; + const newQuantity = ticketLog.newInstance.quantity; - if (ticketLog.description && isUpdate) - changes.push(ticketLog.description); - - const oldQuantity = ticketLog.oldInstance ? ticketLog.oldInstance.quantity : null; - const newQuantity = ticketLog.newInstance ? ticketLog.newInstance.quantity : null; - - if (ticketLog.changedModel == 'Sale' && isUpdate && ticketLog.newInstance.quantity) { + if (oldQuantity || newQuantity) { const sale = await models.Sale.findById(ticketLog.changedModelId, null, myOptions); - changes.push(`${sale.concept} cambia de ${oldQuantity} a ${newQuantity}`); - } + const message = $t('Change quantity', { + concept: sale.concept, + oldQuantity: oldQuantity || 0, + newQuantity: newQuantity || 0, + }); - if (ticketLog.changedModel == 'Ticket' && isUpdate && ticketLog.newInstance.quantity) - changes.push(`${ticketLog.oldInstance.concept} cambia de ${oldQuantity} a ${newQuantity}`); + changes.push(message); + } } return changes.join('\n'); }; From c1089c637ac8d4730c7eb53c90fc831b567a0642 Mon Sep 17 00:00:00 2001 From: vicent Date: Wed, 30 Nov 2022 13:43:29 +0100 Subject: [PATCH 09/11] fix: backTest --- .../back/methods/ticket-log/specs/getChanges.spec.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/modules/ticket/back/methods/ticket-log/specs/getChanges.spec.js b/modules/ticket/back/methods/ticket-log/specs/getChanges.spec.js index cdbebf9b3..c0f7dde0e 100644 --- a/modules/ticket/back/methods/ticket-log/specs/getChanges.spec.js +++ b/modules/ticket/back/methods/ticket-log/specs/getChanges.spec.js @@ -1,11 +1,16 @@ const models = require('vn-loopback/server/server').models; describe('ticketLog getChanges()', () => { + const ctx = {req: {}}; + + ctx.req.__ = value => { + return value; + }; it('should return the changes in the sales of a ticket', async() => { - const ticketId = 1; + const ticketId = 7; - const changues = await models.TicketLog.getChanges({id: ticketId}); + const changues = await models.TicketLog.getChanges(ctx, ticketId); - expect(changues).toContain(`cambia de 1 a 10`); + expect(changues).toContain(`Change quantity`); }); }); From a45ef27e3fcf07d2ea603ddfb50eed93127b82ae Mon Sep 17 00:00:00 2001 From: vicent Date: Wed, 21 Dec 2022 12:15:33 +0100 Subject: [PATCH 10/11] move changes sql --- db/changes/225201/00-aclTicketLog.sql | 3 +++ db/changes/225201/00-ticketSms.sql | 8 ++++++++ 2 files changed, 11 insertions(+) create mode 100644 db/changes/225201/00-aclTicketLog.sql create mode 100644 db/changes/225201/00-ticketSms.sql diff --git a/db/changes/225201/00-aclTicketLog.sql b/db/changes/225201/00-aclTicketLog.sql new file mode 100644 index 000000000..edba17ab4 --- /dev/null +++ b/db/changes/225201/00-aclTicketLog.sql @@ -0,0 +1,3 @@ +INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) + VALUES + ('TicketLog', 'getChanges', 'READ', 'ALLOW', 'ROLE', 'employee'); diff --git a/db/changes/225201/00-ticketSms.sql b/db/changes/225201/00-ticketSms.sql new file mode 100644 index 000000000..f454f99b1 --- /dev/null +++ b/db/changes/225201/00-ticketSms.sql @@ -0,0 +1,8 @@ +CREATE TABLE `vn`.`ticketSms` ( + `smsFk` mediumint(8) unsigned NOT NULL, + `ticketFk` int(11) DEFAULT NULL, + PRIMARY KEY (`smsFk`), + KEY `ticketSms_FK_1` (`ticketFk`), + CONSTRAINT `ticketSms_FK` FOREIGN KEY (`smsFk`) REFERENCES `sms` (`id`) ON UPDATE CASCADE, + CONSTRAINT `ticketSms_FK_1` FOREIGN KEY (`ticketFk`) REFERENCES `ticket` (`id`) ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci From 630890cd0440d666de348fc49b6ded7798c09d73 Mon Sep 17 00:00:00 2001 From: vicent Date: Wed, 21 Dec 2022 12:23:16 +0100 Subject: [PATCH 11/11] fix: colum name in model --- modules/client/back/models/ticket-sms.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/client/back/models/ticket-sms.json b/modules/client/back/models/ticket-sms.json index 1787ba17c..03f592f51 100644 --- a/modules/client/back/models/ticket-sms.json +++ b/modules/client/back/models/ticket-sms.json @@ -7,7 +7,7 @@ } }, "properties": { - "id": { + "smsFk": { "type": "number", "id": true, "description": "Identifier"