From 391f1d8c975d8740563e6182f73c103f1fca9f14 Mon Sep 17 00:00:00 2001 From: carlossa Date: Tue, 10 Dec 2024 12:04:03 +0100 Subject: [PATCH] fix: refs #7028 fix confirm deny --- .../back/methods/ticket-request/confirm.js | 15 +++-- .../back/methods/ticket-request/deny.js | 56 ++++++++++++------- 2 files changed, 48 insertions(+), 23 deletions(-) diff --git a/modules/ticket/back/methods/ticket-request/confirm.js b/modules/ticket/back/methods/ticket-request/confirm.js index 7c17d0010..56ee14a73 100644 --- a/modules/ticket/back/methods/ticket-request/confirm.js +++ b/modules/ticket/back/methods/ticket-request/confirm.js @@ -54,9 +54,17 @@ module.exports = Self => { throw new UserError(`That item doesn't exists`); const request = await models.TicketRequest.findById(ctx.args.id, { - include: {relation: 'ticket'} + include: { + relation: 'ticket', + scope: { + include: { + relation: 'client', + scope: { + fields: ['id', 'name', 'salesPersonFk'] + } + } + }} }, myOptions); - const itemStock = await models.Item.getVisibleAvailable( ctx.args.itemFk, request.ticket().warehouseFk, @@ -90,8 +98,7 @@ module.exports = Self => { await Self.rawSql(query, [sale.id], myOptions); const url = await Self.app.models.Url.getUrl(); - const requesterId = request.requesterFk; - + const requesterId = request.ticket().client().salesPersonFk; const message = $t('Bought units from buy request', { quantity: sale.quantity, concept: sale.concept, diff --git a/modules/ticket/back/methods/ticket-request/deny.js b/modules/ticket/back/methods/ticket-request/deny.js index 44f1e48a1..cb13c2728 100644 --- a/modules/ticket/back/methods/ticket-request/deny.js +++ b/modules/ticket/back/methods/ticket-request/deny.js @@ -1,18 +1,21 @@ module.exports = Self => { Self.remoteMethodCtx('deny', { - description: 'sets a ticket request to denied and returns the changes', + description: 'Sets a ticket request to denied and returns the changes', accessType: 'WRITE', - accepts: [{ - arg: 'id', - type: 'number', - required: true, - description: 'The request ID', - }, { - arg: 'observation', - type: 'String', - required: true, - description: 'The request observation', - }], + accepts: [ + { + arg: 'id', + type: 'number', + required: true, + description: 'The request ID', + }, + { + arg: 'observation', + type: 'string', + required: true, + description: 'The request observation', + } + ], returns: { type: 'number', root: true @@ -29,7 +32,7 @@ module.exports = Self => { const myOptions = {}; let tx; - if (typeof options == 'object') + if (typeof options === 'object') Object.assign(myOptions, options); if (!myOptions.transaction) { @@ -39,7 +42,8 @@ module.exports = Self => { try { const userId = ctx.req.accessToken.userId; - const worker = await Self.app.models.Worker.findOne({where: {id: userId}}, myOptions); + const worker = await models.Worker.findOne({where: {id: userId}}, myOptions); + if (!worker) throw new Error('Worker not found'); const params = { isOk: false, @@ -47,19 +51,33 @@ module.exports = Self => { response: ctx.args.observation, }; - const request = await Self.app.models.TicketRequest.findById(ctx.args.id, null, myOptions); - await request.updateAttributes(params, myOptions); + const request = await models.TicketRequest.findById(ctx.args.id, { + include: { + relation: 'ticket', + scope: { + include: { + relation: 'client', + scope: { + fields: ['id', 'name', 'salesPersonFk'] + } + } + } + } + }, myOptions); - const url = await Self.app.models.Url.getUrl(); - const requesterId = request.requesterFk; + if (!request || !request.ticket() || !request.ticket().client()) + throw new Error('Invalid request or related ticket/client data'); + const url = await models.Url.getUrl(); + const requesterId = request.ticket().client().salesPersonFk; const message = $t('Deny buy request', { ticketId: request.ticketFk, url: `${url}ticket/${request.ticketFk}/request/index`, observation: params.response }); - await models.Chat.sendCheckingPresence(ctx, requesterId, message, myOptions); + await models.Chat.sendCheckingPresence(ctx, requesterId, message); + await request.updateAttributes(params, myOptions); if (tx) await tx.commit();