From 67069747845d6daea1237971f73a82ac0740bced Mon Sep 17 00:00:00 2001 From: Jon Date: Wed, 24 Apr 2024 10:31:26 +0200 Subject: [PATCH] fix: refs #6731 fix ticketRequest --- .../vn/procedures/ticketRequest_Add.sql | 19 ------------ .../triggers/ticketRequest_beforeInsert.sql | 2 +- .../triggers/ticketRequest_beforeUpdate.sql | 4 --- .../11002-limeCarnation/00-firstScript.sql | 7 +++++ loopback/locale/en.json | 4 +-- .../ticket-request/specs/confirm.spec.js | 5 +--- modules/ticket/back/models/ticket-config.json | 10 +++++++ modules/ticket/back/models/ticket-request.js | 30 +++++++++++++------ 8 files changed, 42 insertions(+), 39 deletions(-) delete mode 100644 db/routines/vn/procedures/ticketRequest_Add.sql create mode 100644 db/versions/11002-limeCarnation/00-firstScript.sql diff --git a/db/routines/vn/procedures/ticketRequest_Add.sql b/db/routines/vn/procedures/ticketRequest_Add.sql deleted file mode 100644 index 5ba347fef..000000000 --- a/db/routines/vn/procedures/ticketRequest_Add.sql +++ /dev/null @@ -1,19 +0,0 @@ -DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticketRequest_Add`(vDescription VARCHAR(255), vQuantity INT, vPrice DOUBLE, vTicketFk INT, vBuyerCode VARCHAR(3)) -BEGIN - - INSERT INTO vn.ticketRequest(description, - quantity, - price, - ticketFk, - buyerCode, - requesterFk) - VALUES(vDescription, - vQuantity, - vPrice, - vTicketFk, - vBuyerCode, - vn.getUser()); - -END$$ -DELIMITER ; diff --git a/db/routines/vn/triggers/ticketRequest_beforeInsert.sql b/db/routines/vn/triggers/ticketRequest_beforeInsert.sql index d17459912..00e659abc 100644 --- a/db/routines/vn/triggers/ticketRequest_beforeInsert.sql +++ b/db/routines/vn/triggers/ticketRequest_beforeInsert.sql @@ -14,7 +14,7 @@ BEGIN END IF; IF NEW.attenderFk IS NULL THEN - SET NEW.attenderFk = (SELECT w.id FROM worker w WHERE w.code = NEW.buyerCode); + SET NEW.attenderFk = (SELECT defaultAttenderFk FROM ticketConfig LIMIT 1); END IF; END$$ DELIMITER ; diff --git a/db/routines/vn/triggers/ticketRequest_beforeUpdate.sql b/db/routines/vn/triggers/ticketRequest_beforeUpdate.sql index e5e9c307e..954df8ed3 100644 --- a/db/routines/vn/triggers/ticketRequest_beforeUpdate.sql +++ b/db/routines/vn/triggers/ticketRequest_beforeUpdate.sql @@ -12,9 +12,5 @@ BEGIN IF NEW.salesPersonCode <> OLD.salesPersonCode THEN SET NEW.requesterFk = (SELECT w.id FROM worker w WHERE w.code = NEW.salesPersonCode); END IF; - - IF NEW.buyerCode <> OLD.buyerCode THEN - SET NEW.attenderFk = (SELECT w.id FROM worker w WHERE w.code = NEW.buyerCode); - END IF; END$$ DELIMITER ; diff --git a/db/versions/11002-limeCarnation/00-firstScript.sql b/db/versions/11002-limeCarnation/00-firstScript.sql new file mode 100644 index 000000000..b1a49a309 --- /dev/null +++ b/db/versions/11002-limeCarnation/00-firstScript.sql @@ -0,0 +1,7 @@ +-- Place your SQL code here +ALTER TABLE `vn`.`ticketRequest` + CHANGE IF EXISTS `buyerCode` `buyerCode__` varchar(3) NOT NULL COMMENT '@deprecated 2024-04-23 refs #6731 field not used'; + +ALTER TABLE `vn`.`ticketConfig` ADD COLUMN IF NOT EXISTS `defaultAttenderFk` int unsigned; + +ALTER TABLE vn.ticketConfig ADD CONSTRAINT ticketConfig_worker_FK FOREIGN KEY (defaultAttenderFk) REFERENCES vn.worker(id); diff --git a/loopback/locale/en.json b/loopback/locale/en.json index 9a3a1f52a..93a54393d 100644 --- a/loopback/locale/en.json +++ b/loopback/locale/en.json @@ -223,7 +223,7 @@ "printerNotExists": "The printer does not exist", "There are not picking tickets": "There are not picking tickets", "ticketCommercial": "The ticket {{ ticket }} for the salesperson {{ salesMan }} is in preparation. (automatically generated message)", - "This password can only be changed by the user themselves": "This password can only be changed by the user themselves", - "They're not your subordinate": "They're not your subordinate", + "This password can only be changed by the user themselves": "This password can only be changed by the user themselves", + "They're not your subordinate": "They're not your subordinate", "InvoiceIn is already booked": "InvoiceIn is already booked" } \ No newline at end of file diff --git a/modules/ticket/back/methods/ticket-request/specs/confirm.spec.js b/modules/ticket/back/methods/ticket-request/specs/confirm.spec.js index de2817d87..668a991f4 100644 --- a/modules/ticket/back/methods/ticket-request/specs/confirm.spec.js +++ b/modules/ticket/back/methods/ticket-request/specs/confirm.spec.js @@ -92,9 +92,7 @@ describe('ticket-request confirm()', () => { const request = await models.TicketRequest.findById(requestId, null, options); expect(request.saleFk).toBeNull(); - - await request.updateAttributes({saleFk: 2}, options); - + await request.updateAttributes({saleFk: 2}); ctx.args = { itemFk: itemId, id: requestId, @@ -102,7 +100,6 @@ describe('ticket-request confirm()', () => { }; await models.TicketRequest.confirm(ctx, options); - await tx.rollback(); } catch (e) { await tx.rollback(); diff --git a/modules/ticket/back/models/ticket-config.json b/modules/ticket/back/models/ticket-config.json index d757fbd1a..6dd2808ea 100644 --- a/modules/ticket/back/models/ticket-config.json +++ b/modules/ticket/back/models/ticket-config.json @@ -23,6 +23,16 @@ }, "daysForWarningClaim": { "type": "number" + }, + "defaultAttenderFk": { + "type": "number" + } + }, + "relations": { + "attender": { + "type": "belongsTo", + "model": "Worker", + "foreignKey": "defaultAttenderFk" } } } diff --git a/modules/ticket/back/models/ticket-request.js b/modules/ticket/back/models/ticket-request.js index d133f85d5..622cd7696 100644 --- a/modules/ticket/back/models/ticket-request.js +++ b/modules/ticket/back/models/ticket-request.js @@ -8,21 +8,21 @@ module.exports = function(Self) { require('../methods/ticket-request/getItemTypeWorker')(Self); Self.observe('before save', async function(ctx) { - if (ctx.isNewInstance) { - const loopBackContext = LoopBackContext.getCurrentContext(); - const filter = {where: {id: loopBackContext.active.accessToken.userId}}; - const models = Self.app.models; - const worker = await models.Worker.findOne(filter); + const loopBackContext = LoopBackContext.getCurrentContext(); + const filter = {where: {id: loopBackContext.active.accessToken.userId}}; + const models = Self.app.models; + const worker = await models.Worker.findOne(filter); + const instance = ctx.instance; + const attenderFk = instance?.attenderFk; - const instance = ctx.instance; + if (ctx.isNewInstance) { instance.requesterFk = worker.id; const httpCtx = {req: loopBackContext.active}; const httpRequest = httpCtx.req.http .req; const $t = httpRequest.__; - const attenderId = instance.attenderFk; - if (attenderId) { + if (attenderFk) { const ticket = await models.Ticket.findById(instance.ticketFk); let messageText = 'New ticket request has been created'; if (instance.price) @@ -35,8 +35,20 @@ module.exports = function(Self) { quantity: instance.quantity, price: instance.price }); - await models.Chat.sendCheckingPresence(httpCtx, attenderId, message); + await models.Chat.sendCheckingPresence(httpCtx, attenderFk, message); + } else { + const {defaultAttenderFk} = await models.TicketConfig.findOne(); + Object.assign(instance, {attenderFk: defaultAttenderFk}); } } }); + + Self.observe('after save', async function(ctx) { + const models = Self.app.models; + const instance = ctx.instance; + if (instance?.attenderFk === null && !ctx.isNewInstance) { + const {defaultAttenderFk} = await models.TicketConfig.findOne(); + await models.TicketRequest.updateAll({id: instance.id}, {attenderFk: defaultAttenderFk}); + } + }); };