From e5b13267e2e1d1fb71d286210c1659be94ee4862 Mon Sep 17 00:00:00 2001 From: joan Date: Tue, 5 Jan 2021 15:03:35 +0100 Subject: [PATCH] 2605 - Ticket request send chat notification --- loopback/locale/en.json | 4 ++- loopback/locale/es.json | 4 ++- loopback/util/log.js | 6 ++-- modules/ticket/back/models/ticket-request.js | 34 ++++++++++++++++++-- 4 files changed, 40 insertions(+), 8 deletions(-) diff --git a/loopback/locale/en.json b/loopback/locale/en.json index 1f85356dd..0ac49d5b5 100644 --- a/loopback/locale/en.json +++ b/loopback/locale/en.json @@ -86,5 +86,7 @@ "The social name cannot be empty": "The social name cannot be empty", "The nif cannot be empty": "The nif cannot be empty", "A travel with this data already exists": "A travel with this data already exists", - "The observation type can't be repeated": "The observation type can't be repeated" + "The observation type can't be repeated": "The observation type can't be repeated", + "New ticket request has been created with price": "New ticket request has been created '{{description}}' for day {{shipped}}, with a quantity of {{quantity}} and a price of {{price}} €", + "New ticket request has been created": "New ticket request has been created '{{description}}' for day {{shipped}}, with a quantity of {{quantity}}" } \ No newline at end of file diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 70c46c110..b54c0cc67 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -164,5 +164,7 @@ "You can not select this payment method without a registered bankery account": "No se puede utilizar este método de pago si no has registrado una cuenta bancaria", "You can't upload images on the test environment": "No puedes subir imágenes en el entorno de pruebas", "The selected ticket is not suitable for this route": "El ticket seleccionado no es apto para esta ruta", - "Sorts whole route": "Reordena ruta entera" + "Sorts whole route": "Reordena ruta entera", + "New ticket request has been created with price": "Se ha creado una nueva petición de compra '{{description}}' para el día {{shipped}}, con una cantidad de {{quantity}} y un precio de {{price}} €", + "New ticket request has been created": "Se ha creado una nueva petición de compra '{{description}}' para el día {{shipped}}, con una cantidad de {{quantity}}" } \ No newline at end of file diff --git a/loopback/util/log.js b/loopback/util/log.js index d81fc39a0..b491b97d0 100644 --- a/loopback/util/log.js +++ b/loopback/util/log.js @@ -24,9 +24,9 @@ exports.translateValues = async(instance, changes) => { function formatDate(date) { return new Intl.DateTimeFormat('es', { - year: '2-digit', - month: '2-digit', - day: '2-digit', + year: 'numeric', + month: 'numeric', + day: 'numeric', hour: '2-digit', minute: '2-digit', second: '2-digit' diff --git a/modules/ticket/back/models/ticket-request.js b/modules/ticket/back/models/ticket-request.js index f711ed23e..814f47bae 100644 --- a/modules/ticket/back/models/ticket-request.js +++ b/modules/ticket/back/models/ticket-request.js @@ -8,10 +8,38 @@ module.exports = function(Self) { Self.observe('before save', async function(ctx) { if (ctx.isNewInstance) { const loopBackContext = LoopBackContext.getCurrentContext(); - let filter = {where: {userFk: loopBackContext.active.accessToken.userId}}; - let worker = await Self.app.models.Worker.findOne(filter); + const filter = {where: {userFk: loopBackContext.active.accessToken.userId}}; + const models = Self.app.models; + const worker = await models.Worker.findOne(filter); - ctx.instance.requesterFk = worker.id; + const instance = ctx.instance; + instance.requesterFk = worker.id; + + const httpCtx = {req: loopBackContext.active}; + const httpRequest = httpCtx.req.http .req; + const $t = httpRequest.__; + + const attenderId = instance.attenderFk; + if (attenderId) { + const ticket = await models.Ticket.findById(instance.ticketFk); + let messageText = 'New ticket request has been created'; + if (instance.price) + messageText = 'New ticket request has been created with price'; + + const shipped = new Intl.DateTimeFormat('es', { + year: 'numeric', + month: 'numeric', + day: 'numeric' + }).format(ticket.shipped); + + const message = $t(messageText, { + description: instance.description, + shipped: shipped, + quantity: instance.quantity, + price: instance.price + }); + await models.Chat.sendCheckingPresence(httpCtx, attenderId, message); + } } }); };