From 391f1d8c975d8740563e6182f73c103f1fca9f14 Mon Sep 17 00:00:00 2001 From: carlossa Date: Tue, 10 Dec 2024 12:04:03 +0100 Subject: [PATCH 1/8] 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(); -- 2.40.1 From efcf4d225b5e0aae5f9f68957bd2defd0e913bf5 Mon Sep 17 00:00:00 2001 From: carlossa Date: Thu, 12 Dec 2024 09:30:22 +0100 Subject: [PATCH 2/8] fix: refs #7028 fix pr --- loopback/locale/es.json | 3 ++- modules/ticket/back/methods/ticket-request/deny.js | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 0dc8e53a8..b4b5ee7c6 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -392,6 +392,7 @@ "The raid information is not correct": "La información de la redada no es correcta", "There are tickets to be invoiced": "Hay tickets para esta zona, borralos primero", "An item type with the same code already exists": "Un tipo con el mismo código ya existe", - "Holidays to past days not available": "Las vacaciones a días pasados no están disponibles" + "Holidays to past days not available": "Las vacaciones a días pasados no están disponibles", + "Worker not found": "Trabajador no encontrado" } diff --git a/modules/ticket/back/methods/ticket-request/deny.js b/modules/ticket/back/methods/ticket-request/deny.js index cb13c2728..2f710ad3a 100644 --- a/modules/ticket/back/methods/ticket-request/deny.js +++ b/modules/ticket/back/methods/ticket-request/deny.js @@ -42,7 +42,7 @@ module.exports = Self => { try { const userId = ctx.req.accessToken.userId; - const worker = await models.Worker.findOne({where: {id: userId}}, myOptions); + const worker = await models.Worker.findById({where: {id: userId}}, myOptions); if (!worker) throw new Error('Worker not found'); const params = { @@ -76,7 +76,7 @@ module.exports = Self => { observation: params.response }); - await models.Chat.sendCheckingPresence(ctx, requesterId, message); + await models.Chat.sendCheckingPresence(ctx, requesterId, message, myOptions); await request.updateAttributes(params, myOptions); if (tx) await tx.commit(); -- 2.40.1 From 1970b9c0db6cb4c5f6285c2e9cbca9a1c83822a5 Mon Sep 17 00:00:00 2001 From: carlossa Date: Thu, 12 Dec 2024 10:04:31 +0100 Subject: [PATCH 3/8] fix: refs #7028 fix tback findById --- modules/ticket/back/methods/ticket-request/deny.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ticket/back/methods/ticket-request/deny.js b/modules/ticket/back/methods/ticket-request/deny.js index 2f710ad3a..f151afd6b 100644 --- a/modules/ticket/back/methods/ticket-request/deny.js +++ b/modules/ticket/back/methods/ticket-request/deny.js @@ -42,7 +42,7 @@ module.exports = Self => { try { const userId = ctx.req.accessToken.userId; - const worker = await models.Worker.findById({where: {id: userId}}, myOptions); + const worker = await models.Worker.findById(userId, myOptions); if (!worker) throw new Error('Worker not found'); const params = { -- 2.40.1 From cd214b3526d204581562730405e17451b7897122 Mon Sep 17 00:00:00 2001 From: carlossa Date: Thu, 12 Dec 2024 11:11:22 +0100 Subject: [PATCH 4/8] fix: refs #7028 fix userError --- loopback/locale/es.json | 4 +++- modules/ticket/back/methods/ticket-request/deny.js | 5 +++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/loopback/locale/es.json b/loopback/locale/es.json index e0b180544..d7fcb8805 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -394,5 +394,7 @@ "Price cannot be blank": "Price cannot be blank", "An item type with the same code already exists": "Un tipo con el mismo código ya existe", "Holidays to past days not available": "Las vacaciones a días pasados no están disponibles", - "Worker not found": "Trabajador no encontrado" + "Worker not found": "Trabajador no encontrado", + "Invalid request or related ticket/client data": "Solicitud no válida o datos de ticket/cliente relacionados" + } diff --git a/modules/ticket/back/methods/ticket-request/deny.js b/modules/ticket/back/methods/ticket-request/deny.js index f151afd6b..c6a1d35af 100644 --- a/modules/ticket/back/methods/ticket-request/deny.js +++ b/modules/ticket/back/methods/ticket-request/deny.js @@ -1,3 +1,4 @@ +const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethodCtx('deny', { description: 'Sets a ticket request to denied and returns the changes', @@ -43,7 +44,7 @@ module.exports = Self => { try { const userId = ctx.req.accessToken.userId; const worker = await models.Worker.findById(userId, myOptions); - if (!worker) throw new Error('Worker not found'); + if (!worker) throw new UserError('Worker not found'); const params = { isOk: false, @@ -66,7 +67,7 @@ module.exports = Self => { }, myOptions); if (!request || !request.ticket() || !request.ticket().client()) - throw new Error('Invalid request or related ticket/client data'); + throw new UserError('Invalid request or related ticket/client data'); const url = await models.Url.getUrl(); const requesterId = request.ticket().client().salesPersonFk; -- 2.40.1 From 32d9a3ace1a2b5a96ec5c74ecdce576a583bd8e3 Mon Sep 17 00:00:00 2001 From: carlossa Date: Mon, 16 Dec 2024 09:57:48 +0100 Subject: [PATCH 5/8] fix: refs #7028 remove ifs --- loopback/locale/es.json | 5 +---- modules/ticket/back/methods/ticket-request/deny.js | 4 ---- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 73efce216..a47195d07 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -398,8 +398,5 @@ "An item type with the same code already exists": "Un tipo con el mismo código ya existe", "Holidays to past days not available": "Las vacaciones a días pasados no están disponibles", "All tickets have a route order": "Todos los tickets tienen orden de ruta", - "Price cannot be blank": "Price cannot be blank", - "Worker not found": "Trabajador no encontrado", - "Invalid request or related ticket/client data": "Solicitud no válida o datos de ticket/cliente relacionados" - + "Price cannot be blank": "Price cannot be blank" } diff --git a/modules/ticket/back/methods/ticket-request/deny.js b/modules/ticket/back/methods/ticket-request/deny.js index c6a1d35af..b96d8a244 100644 --- a/modules/ticket/back/methods/ticket-request/deny.js +++ b/modules/ticket/back/methods/ticket-request/deny.js @@ -44,7 +44,6 @@ module.exports = Self => { try { const userId = ctx.req.accessToken.userId; const worker = await models.Worker.findById(userId, myOptions); - if (!worker) throw new UserError('Worker not found'); const params = { isOk: false, @@ -66,9 +65,6 @@ module.exports = Self => { } }, myOptions); - if (!request || !request.ticket() || !request.ticket().client()) - throw new UserError('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', { -- 2.40.1 From 1e471ac20f146e76405309017f609717c8cca44a Mon Sep 17 00:00:00 2001 From: carlossa Date: Mon, 16 Dec 2024 10:25:33 +0100 Subject: [PATCH 6/8] fix: refs #7028 requesterId fix --- .../back/methods/ticket-request/confirm.js | 23 ++++++++++--------- .../back/methods/ticket-request/deny.js | 18 ++++++++------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/modules/ticket/back/methods/ticket-request/confirm.js b/modules/ticket/back/methods/ticket-request/confirm.js index 56ee14a73..ab4d39d51 100644 --- a/modules/ticket/back/methods/ticket-request/confirm.js +++ b/modules/ticket/back/methods/ticket-request/confirm.js @@ -97,18 +97,19 @@ module.exports = Self => { const query = `CALL vn.sale_calculateComponent(?, NULL)`; await Self.rawSql(query, [sale.id], myOptions); - const url = await Self.app.models.Url.getUrl(); const requesterId = request.ticket().client().salesPersonFk; - const message = $t('Bought units from buy request', { - quantity: sale.quantity, - concept: sale.concept, - itemId: sale.itemFk, - ticketId: sale.ticketFk, - url: `${url}ticket/${sale.ticketFk}/summary`, - urlItem: `${url}item/${sale.itemFk}/summary` - }); - await models.Chat.sendCheckingPresence(ctx, requesterId, message, myOptions); - + if (requesterId) { + const url = await Self.app.models.Url.getUrl(); + const message = $t('Bought units from buy request', { + quantity: sale.quantity, + concept: sale.concept, + itemId: sale.itemFk, + ticketId: sale.ticketFk, + url: `${url}ticket/${sale.ticketFk}/summary`, + urlItem: `${url}item/${sale.itemFk}/summary` + }); + await models.Chat.sendCheckingPresence(ctx, requesterId, message, myOptions); + } if (tx) await tx.commit(); return sale; diff --git a/modules/ticket/back/methods/ticket-request/deny.js b/modules/ticket/back/methods/ticket-request/deny.js index b96d8a244..5c9f87dde 100644 --- a/modules/ticket/back/methods/ticket-request/deny.js +++ b/modules/ticket/back/methods/ticket-request/deny.js @@ -65,16 +65,18 @@ module.exports = Self => { } }, myOptions); - 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 - }); + if (requesterId) { + const url = await models.Url.getUrl(); + 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 request.updateAttributes(params, myOptions); + await models.Chat.sendCheckingPresence(ctx, requesterId, message, myOptions); + await request.updateAttributes(params, myOptions); + } if (tx) await tx.commit(); -- 2.40.1 From 576641d09d05ef4a9848db4c3137690930b44150 Mon Sep 17 00:00:00 2001 From: carlossa Date: Mon, 16 Dec 2024 11:55:01 +0100 Subject: [PATCH 7/8] fix: refs #7028 requesterId fix --- modules/ticket/back/methods/ticket-request/deny.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ticket/back/methods/ticket-request/deny.js b/modules/ticket/back/methods/ticket-request/deny.js index 5c9f87dde..686b9d1de 100644 --- a/modules/ticket/back/methods/ticket-request/deny.js +++ b/modules/ticket/back/methods/ticket-request/deny.js @@ -43,7 +43,7 @@ module.exports = Self => { try { const userId = ctx.req.accessToken.userId; - const worker = await models.Worker.findById(userId, myOptions); + const worker = await models.Worker.findById(userId, {fields: ['id']}, myOptions); const params = { isOk: false, -- 2.40.1 From 5f43c0440c0e0710497624993915b0ce63dec196 Mon Sep 17 00:00:00 2001 From: carlossa Date: Mon, 16 Dec 2024 11:58:21 +0100 Subject: [PATCH 8/8] fix: refs #7028 requesterId fix salesPerson --- modules/ticket/back/methods/ticket-request/confirm.js | 6 +++--- modules/ticket/back/methods/ticket-request/deny.js | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/ticket/back/methods/ticket-request/confirm.js b/modules/ticket/back/methods/ticket-request/confirm.js index ab4d39d51..45ee287de 100644 --- a/modules/ticket/back/methods/ticket-request/confirm.js +++ b/modules/ticket/back/methods/ticket-request/confirm.js @@ -97,8 +97,8 @@ module.exports = Self => { const query = `CALL vn.sale_calculateComponent(?, NULL)`; await Self.rawSql(query, [sale.id], myOptions); - const requesterId = request.ticket().client().salesPersonFk; - if (requesterId) { + const salesPerson = request.ticket().client().salesPersonFk; + if (salesPerson) { const url = await Self.app.models.Url.getUrl(); const message = $t('Bought units from buy request', { quantity: sale.quantity, @@ -108,7 +108,7 @@ module.exports = Self => { url: `${url}ticket/${sale.ticketFk}/summary`, urlItem: `${url}item/${sale.itemFk}/summary` }); - await models.Chat.sendCheckingPresence(ctx, requesterId, message, myOptions); + await models.Chat.sendCheckingPresence(ctx, salesPerson, message, myOptions); } if (tx) await tx.commit(); diff --git a/modules/ticket/back/methods/ticket-request/deny.js b/modules/ticket/back/methods/ticket-request/deny.js index 686b9d1de..26e6f63ff 100644 --- a/modules/ticket/back/methods/ticket-request/deny.js +++ b/modules/ticket/back/methods/ticket-request/deny.js @@ -65,8 +65,8 @@ module.exports = Self => { } }, myOptions); - const requesterId = request.ticket().client().salesPersonFk; - if (requesterId) { + const salesPerson = request.ticket().client().salesPersonFk; + if (salesPerson) { const url = await models.Url.getUrl(); const message = $t('Deny buy request', { ticketId: request.ticketFk, @@ -74,7 +74,7 @@ module.exports = Self => { observation: params.response }); - await models.Chat.sendCheckingPresence(ctx, requesterId, message, myOptions); + await models.Chat.sendCheckingPresence(ctx, salesPerson, message, myOptions); await request.updateAttributes(params, myOptions); } -- 2.40.1