salix/modules/ticket/back/models/ticket-request.js

46 lines
1.8 KiB
JavaScript
Raw Normal View History

2018-11-13 10:26:36 +00:00
const LoopBackContext = require('loopback-context');
module.exports = function(Self) {
2019-04-05 13:20:12 +00:00
require('../methods/ticket-request/filter')(Self);
require('../methods/ticket-request/deny')(Self);
require('../methods/ticket-request/confirm')(Self);
2018-11-13 10:26:36 +00:00
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);
2018-11-13 10:26:36 +00:00
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);
}
2018-11-13 10:26:36 +00:00
}
});
};