salix/modules/ticket/back/methods/ticket-request/deny.js

90 lines
2.7 KiB
JavaScript
Raw Normal View History

2024-12-12 10:11:22 +00:00
const UserError = require('vn-loopback/util/user-error');
2019-04-05 13:20:12 +00:00
module.exports = Self => {
Self.remoteMethodCtx('deny', {
2024-12-10 11:04:03 +00:00
description: 'Sets a ticket request to denied and returns the changes',
2019-04-05 13:20:12 +00:00
accessType: 'WRITE',
2024-12-10 11:04:03 +00:00
accepts: [
{
arg: 'id',
type: 'number',
required: true,
description: 'The request ID',
},
{
arg: 'observation',
type: 'string',
required: true,
description: 'The request observation',
}
],
2019-04-05 13:20:12 +00:00
returns: {
type: 'number',
root: true
},
http: {
path: `/:id/deny`,
verb: 'post'
}
});
Self.deny = async(ctx, options) => {
const models = Self.app.models;
const $t = ctx.req.__; // $translate
const myOptions = {};
let tx;
2024-12-10 11:04:03 +00:00
if (typeof options === 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const userId = ctx.req.accessToken.userId;
2024-12-16 10:55:01 +00:00
const worker = await models.Worker.findById(userId, {fields: ['id']}, myOptions);
const params = {
isOk: false,
attenderFk: worker.id,
response: ctx.args.observation,
};
2019-04-05 13:20:12 +00:00
2024-12-10 11:04:03 +00:00
const request = await models.TicketRequest.findById(ctx.args.id, {
include: {
relation: 'ticket',
scope: {
include: {
relation: 'client',
scope: {
fields: ['id', 'name', 'salesPersonFk']
}
}
}
}
}, myOptions);
2019-04-05 13:20:12 +00:00
const salesPerson = request.ticket().client().salesPersonFk;
if (salesPerson) {
2024-12-16 09:25:33 +00:00
const url = await models.Url.getUrl();
const message = $t('Deny buy request', {
ticketId: request.ticketFk,
url: `${url}ticket/${request.ticketFk}/request/index`,
observation: params.response
});
2021-10-27 10:22:32 +00:00
await models.Chat.sendCheckingPresence(ctx, salesPerson, message, myOptions);
2024-12-16 09:25:33 +00:00
await request.updateAttributes(params, myOptions);
}
if (tx) await tx.commit();
return request;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
2019-04-05 13:20:12 +00:00
};
};