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

91 lines
2.8 KiB
JavaScript
Raw Normal View History

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-12 08:30:22 +00:00
const worker = await models.Worker.findById({where: {id: userId}}, myOptions);
2024-12-10 11:04:03 +00:00
if (!worker) throw new Error('Worker not found');
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
2024-12-10 11:04:03 +00:00
if (!request || !request.ticket() || !request.ticket().client())
throw new Error('Invalid request or related ticket/client data');
2024-12-10 11:04:03 +00:00
const url = await models.Url.getUrl();
const requesterId = request.ticket().client().salesPersonFk;
const message = $t('Deny buy request', {
ticketId: request.ticketFk,
2023-10-16 16:24:58 +00:00
url: `${url}ticket/${request.ticketFk}/request/index`,
observation: params.response
});
2021-10-27 10:22:32 +00:00
2024-12-12 08:30:22 +00:00
await models.Chat.sendCheckingPresence(ctx, requesterId, message, myOptions);
2024-12-10 11:04:03 +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
};
};