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

73 lines
2.1 KiB
JavaScript
Raw Normal View History

2019-04-05 13:20:12 +00:00
module.exports = Self => {
Self.remoteMethodCtx('deny', {
2019-07-25 07:55:09 +00:00
description: 'sets a ticket request to denied and returns the changes',
2019-04-05 13:20:12 +00:00
accessType: 'WRITE',
accepts: [{
arg: 'id',
type: 'number',
2019-04-05 13:20:12 +00:00
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 Self.app.models.Worker.findOne({where: {userFk: userId}}, myOptions);
const params = {
isOk: false,
attenderFk: worker.id,
response: ctx.args.observation,
};
2019-04-05 13:20:12 +00:00
const request = await Self.app.models.TicketRequest.findById(ctx.args.id, null, myOptions);
await request.updateAttributes(params, myOptions);
2019-04-05 13:20:12 +00:00
const origin = ctx.req.headers.origin;
2021-10-27 10:22:32 +00:00
const requesterId = request.requesterFk;
const message = $t('Deny buy request', {
ticketId: request.ticketFk,
url: `${origin}/#!/ticket/${request.ticketFk}/request/index`,
observation: params.response
});
2021-10-27 10:22:32 +00:00
await models.Chat.sendCheckingPresence(ctx, requesterId, message, 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
};
};