From 39470e837b7e8f24790064cf65242dc7d615849a Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Thu, 13 Feb 2020 13:20:31 +0100 Subject: [PATCH] 2094 Regularize claim send chat even if doesn't have destination --- back/methods/chat/sendCheckingPresence.js | 2 +- loopback/locale/en.json | 2 +- loopback/locale/es.json | 2 +- .../back/methods/claim/regularizeClaim.js | 49 ++++++++++--------- 4 files changed, 28 insertions(+), 27 deletions(-) diff --git a/back/methods/chat/sendCheckingPresence.js b/back/methods/chat/sendCheckingPresence.js index 70b4da58f4..b2a3ca725f 100644 --- a/back/methods/chat/sendCheckingPresence.js +++ b/back/methods/chat/sendCheckingPresence.js @@ -36,7 +36,7 @@ module.exports = Self => { relation: 'department' } }); - const department = workerDepartment.department(); + const department = workerDepartment && workerDepartment.department(); const channelName = department.chatName; if (channelName) diff --git a/loopback/locale/en.json b/loopback/locale/en.json index 23717ba8f3..49cd0f1719 100644 --- a/loopback/locale/en.json +++ b/loopback/locale/en.json @@ -61,7 +61,7 @@ "MESSAGE_BOUGHT_UNITS": "Bought {{quantity}} units of {{concept}} (#{{itemId}}) for the ticket id [#{{ticketId}}]({{{url}}})", "MESSAGE_INSURANCE_CHANGE": "I have changed the insurence credit of client [{{clientName}} (#{{clientId}})]({{{url}}}) to *{{credit}} €*", "MESSAGE_CHANGED_PAYMETHOD": "I have changed the pay method for client [{{clientName}} (#{{clientId}})]({{{url}}})", - "MESSAGE_CLAIM_ITEM_REGULARIZE": "I sent *{{quantity}}* units of [{{concept}} (#{{itemId}})]({{{itemUrl}}}) to {{nickname}} coming from ticket id [#{{ticketId}}]({{{ticketUrl}}})", + "Sent units from ticket": "I sent *{{quantity}}* units of [{{concept}} (#{{itemId}})]({{{itemUrl}}}) to *\"{{nickname}}\"* coming from ticket id [#{{ticketId}}]({{{ticketUrl}}})", "Customs agent is required for a non UEE member": "Customs agent is required for a non UEE member", "Incoterms is required for a non UEE member": "Incoterms is required for a non UEE member", "Client checked as validated despite of duplication": "Client checked as validated despite of duplication from client id {{clientId}}" diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 90d4f8793d..64fabd5226 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -124,7 +124,7 @@ "MESSAGE_BOUGHT_UNITS": "Se ha comprado {{quantity}} unidades de {{concept}} (#{{itemId}}) para el ticket id [#{{ticketId}}]({{{url}}})", "MESSAGE_INSURANCE_CHANGE": "He cambiado el crédito asegurado del cliente [{{clientName}} (#{{clientId}})]({{{url}}}) a *{{credit}} €*", "MESSAGE_CHANGED_PAYMETHOD": "He cambiado la forma de pago del cliente [{{clientName}} (#{{clientId}})]({{{url}}})", - "MESSAGE_CLAIM_ITEM_REGULARIZE": "Envio *{{quantity}}* unidades de [{{concept}} (#{{itemId}})]({{{itemUrl}}}) a {{nickname}} provenientes del ticket id [#{{ticketId}}]({{{ticketUrl}}})", + "Sent units from ticket": "Envio *{{quantity}}* unidades de [{{concept}} (#{{itemId}})]({{{itemUrl}}}) a *\"{{nickname}}\"* provenientes del ticket id [#{{ticketId}}]({{{ticketUrl}}})", "Client checked as validated despite of duplication": "Cliente comprobado a pesar de que existe el cliente id {{clientId}}", "ORDER_ROW_UNAVAILABLE": "No hay disponibilidad de este producto" } \ No newline at end of file diff --git a/modules/claim/back/methods/claim/regularizeClaim.js b/modules/claim/back/methods/claim/regularizeClaim.js index 8a4f6dc957..adf1623c66 100644 --- a/modules/claim/back/methods/claim/regularizeClaim.js +++ b/modules/claim/back/methods/claim/regularizeClaim.js @@ -37,27 +37,43 @@ module.exports = Self => { for (let i = 0; i < claimEnds.length; i++) { const claimEnd = claimEnds[i]; const destination = claimEnd.claimDestination(); - const addressFk = destination && destination.addressFk; + const sale = await getSale(claimEnd.saleFk, options); + const addressId = destination && destination.addressFk; - if (!addressFk) continue; + let address; + if (addressId) + address = await models.Address.findById(addressId, null, options); + + const salesPerson = sale.ticket().client().salesPerson(); + if (salesPerson) { + const nickname = address && address.nickname || destination.description; + const origin = ctx.req.headers.origin; + const message = $t('Sent units from ticket', { + quantity: sale.quantity, + concept: sale.concept, + itemId: sale.itemFk, + ticketId: sale.ticketFk, + nickname: nickname, + ticketUrl: `${origin}/#!/ticket/${sale.ticketFk}/summary`, + itemUrl: `${origin}/#!/item/${sale.itemFk}/summary` + }); + await models.Chat.sendCheckingPresence(ctx, salesPerson.id, message); + } + + if (!address) continue; - let sale = await getSale(claimEnd.saleFk, options); let ticketFk = await getTicketId({ - addressFk: addressFk, + addressFk: addressId, companyFk: sale.ticket().companyFk, warehouseFk: sale.ticket().warehouseFk }, options); - let address = await models.Address.findOne({ - where: {id: addressFk} - }, options); - if (!ticketFk) { ticketFk = await createTicket(ctx, { clientId: address.clientFk, warehouseId: sale.ticket().warehouseFk, companyId: sale.ticket().companyFk, - addressId: addressFk + addressId: addressId }, options); } @@ -69,21 +85,6 @@ module.exports = Self => { price: sale.price, discount: 100 }, options); - - const salesPerson = sale.ticket().client().salesPerson(); - if (salesPerson) { - const origin = ctx.req.headers.origin; - const message = $t('MESSAGE_CLAIM_ITEM_REGULARIZE', { - quantity: sale.quantity, - concept: sale.concept, - itemId: sale.itemFk, - ticketId: sale.ticketFk, - nickname: address.nickname, - ticketUrl: `${origin}/#!/ticket/${sale.ticketFk}/summary`, - itemUrl: `${origin}/#!/item/${sale.itemFk}/summary` - }); - await models.Chat.sendCheckingPresence(ctx, salesPerson.id, message); - } } let claim = await Self.findById(params.claimFk, null, options);