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

42 lines
1.7 KiB
JavaScript
Raw Normal View History

2018-11-13 10:26:36 +00:00
const LoopBackContext = require('loopback-context');
2021-02-05 07:05:57 +00:00
const dateUtil = require('vn-loopback/util/date');
2018-11-13 10:26:36 +00:00
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';
2021-02-05 07:05:57 +00:00
const shipped = dateUtil.toString(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
}
});
};