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

91 lines
2.8 KiB
JavaScript

module.exports = Self => {
Self.remoteMethodCtx('deny', {
description: 'Sets a ticket request to denied and returns the changes',
accessType: 'WRITE',
accepts: [
{
arg: 'id',
type: 'number',
required: true,
description: 'The request ID',
},
{
arg: 'observation',
type: 'string',
required: true,
description: 'The request observation',
}
],
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;
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;
const worker = await models.Worker.findById(userId, myOptions);
if (!worker) throw new Error('Worker not found');
const params = {
isOk: false,
attenderFk: worker.id,
response: ctx.args.observation,
};
const request = await models.TicketRequest.findById(ctx.args.id, {
include: {
relation: 'ticket',
scope: {
include: {
relation: 'client',
scope: {
fields: ['id', 'name', 'salesPersonFk']
}
}
}
}
}, myOptions);
if (!request || !request.ticket() || !request.ticket().client())
throw new Error('Invalid request or related ticket/client data');
const url = await models.Url.getUrl();
const requesterId = request.ticket().client().salesPersonFk;
const message = $t('Deny buy request', {
ticketId: request.ticketFk,
url: `${url}ticket/${request.ticketFk}/request/index`,
observation: params.response
});
await models.Chat.sendCheckingPresence(ctx, requesterId, message, myOptions);
await request.updateAttributes(params, myOptions);
if (tx) await tx.commit();
return request;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};