Merge pull request 'fix: refs #6731 fix ticketRequest' (!2364) from 6731-fixTicketRequestLog into dev
gitea/salix/pipeline/head This commit looks good Details

Reviewed-on: #2364
Reviewed-by: Javi Gallego <jgallego@verdnatura.es>
This commit is contained in:
Jon Elias 2024-04-30 10:19:29 +00:00
commit 2b9bfb5d85
8 changed files with 42 additions and 39 deletions

View File

@ -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 ;

View File

@ -14,7 +14,7 @@ BEGIN
END IF; END IF;
IF NEW.attenderFk IS NULL THEN 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 IF;
END$$ END$$
DELIMITER ; DELIMITER ;

View File

@ -12,9 +12,5 @@ BEGIN
IF NEW.salesPersonCode <> OLD.salesPersonCode THEN IF NEW.salesPersonCode <> OLD.salesPersonCode THEN
SET NEW.requesterFk = (SELECT w.id FROM worker w WHERE w.code = NEW.salesPersonCode); SET NEW.requesterFk = (SELECT w.id FROM worker w WHERE w.code = NEW.salesPersonCode);
END IF; 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$$ END$$
DELIMITER ; DELIMITER ;

View File

@ -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);

View File

@ -92,9 +92,7 @@ describe('ticket-request confirm()', () => {
const request = await models.TicketRequest.findById(requestId, null, options); const request = await models.TicketRequest.findById(requestId, null, options);
expect(request.saleFk).toBeNull(); expect(request.saleFk).toBeNull();
await request.updateAttributes({saleFk: 2});
await request.updateAttributes({saleFk: 2}, options);
ctx.args = { ctx.args = {
itemFk: itemId, itemFk: itemId,
id: requestId, id: requestId,
@ -102,7 +100,6 @@ describe('ticket-request confirm()', () => {
}; };
await models.TicketRequest.confirm(ctx, options); await models.TicketRequest.confirm(ctx, options);
await tx.rollback(); await tx.rollback();
} catch (e) { } catch (e) {
await tx.rollback(); await tx.rollback();

View File

@ -23,6 +23,16 @@
}, },
"daysForWarningClaim": { "daysForWarningClaim": {
"type": "number" "type": "number"
},
"defaultAttenderFk": {
"type": "number"
}
},
"relations": {
"attender": {
"type": "belongsTo",
"model": "Worker",
"foreignKey": "defaultAttenderFk"
} }
} }
} }

View File

@ -8,21 +8,21 @@ module.exports = function(Self) {
require('../methods/ticket-request/getItemTypeWorker')(Self); require('../methods/ticket-request/getItemTypeWorker')(Self);
Self.observe('before save', async function(ctx) { Self.observe('before save', async function(ctx) {
if (ctx.isNewInstance) {
const loopBackContext = LoopBackContext.getCurrentContext(); const loopBackContext = LoopBackContext.getCurrentContext();
const filter = {where: {id: loopBackContext.active.accessToken.userId}}; const filter = {where: {id: loopBackContext.active.accessToken.userId}};
const models = Self.app.models; const models = Self.app.models;
const worker = await models.Worker.findOne(filter); const worker = await models.Worker.findOne(filter);
const instance = ctx.instance; const instance = ctx.instance;
const attenderFk = instance?.attenderFk;
if (ctx.isNewInstance) {
instance.requesterFk = worker.id; instance.requesterFk = worker.id;
const httpCtx = {req: loopBackContext.active}; const httpCtx = {req: loopBackContext.active};
const httpRequest = httpCtx.req.http .req; const httpRequest = httpCtx.req.http .req;
const $t = httpRequest.__; const $t = httpRequest.__;
const attenderId = instance.attenderFk; if (attenderFk) {
if (attenderId) {
const ticket = await models.Ticket.findById(instance.ticketFk); const ticket = await models.Ticket.findById(instance.ticketFk);
let messageText = 'New ticket request has been created'; let messageText = 'New ticket request has been created';
if (instance.price) if (instance.price)
@ -35,8 +35,20 @@ module.exports = function(Self) {
quantity: instance.quantity, quantity: instance.quantity,
price: instance.price 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});
}
});
}; };