const LoopBackContext = require('loopback-context'); module.exports = function(Self) { require('../methods/ticket-request/filter')(Self); require('../methods/ticket-request/deny')(Self); require('../methods/ticket-request/confirm')(Self); Self.observe('before save', async function(ctx) { if (ctx.isNewInstance) { const loopBackContext = LoopBackContext.getCurrentContext(); const filter = {where: {userFk: loopBackContext.active.accessToken.userId}}; const models = Self.app.models; const worker = await models.Worker.findOne(filter); 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); } } }); };