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

55 lines
2.3 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);
2022-09-22 06:32:18 +00:00
require('../methods/ticket-request/getItemTypeWorker')(Self);
2019-04-05 13:20:12 +00:00
2018-11-13 10:26:36 +00:00
Self.observe('before save', async function(ctx) {
2024-04-24 08:31:26 +00:00
const loopBackContext = LoopBackContext.getCurrentContext();
const filter = {where: {id: loopBackContext.active.accessToken.userId}};
const models = Self.app.models;
const worker = await models.Worker.findOne(filter);
const instance = ctx.instance;
const attenderFk = instance?.attenderFk;
2018-11-13 10:26:36 +00:00
2024-04-24 08:31:26 +00:00
if (ctx.isNewInstance) {
instance.requesterFk = worker.id;
const httpCtx = {req: loopBackContext.active};
const httpRequest = httpCtx.req.http .req;
const $t = httpRequest.__;
2024-04-24 08:31:26 +00:00
if (attenderFk) {
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
});
2024-04-24 08:31:26 +00:00
await models.Chat.sendCheckingPresence(httpCtx, attenderFk, message);
} else {
const {defaultAttenderFk} = await models.TicketConfig.findOne();
Object.assign(instance, {attenderFk: defaultAttenderFk});
}
2018-11-13 10:26:36 +00:00
}
});
2024-04-24 08:31:26 +00:00
Self.observe('after save', async function(ctx) {
const models = Self.app.models;
const instance = ctx.instance;
if (instance?.attenderFk === null && !ctx.isNewInstance) {
const {defaultAttenderFk} = await models.TicketConfig.findOne();
await models.TicketRequest.updateAll({id: instance.id}, {attenderFk: defaultAttenderFk});
}
});
2018-11-13 10:26:36 +00:00
};