salix/modules/ticket/back/methods/ticket/sendSms.js

76 lines
1.9 KiB
JavaScript

module.exports = Self => {
Self.remoteMethodCtx('sendSms', {
description: 'Log the message in ticketLog and call the send method',
accessType: 'WRITE',
accepts: [{
arg: 'id',
type: 'number',
required: true,
description: 'The ticket id',
http: {source: 'path'}
},
{
arg: 'destination',
type: 'string',
required: true,
},
{
arg: 'message',
type: 'string',
required: true,
}],
returns: {
type: 'object',
root: true
},
http: {
path: `/:id/sendSms`,
verb: 'POST'
}
});
Self.sendSms = async(ctx, id, destination, message, options) => {
const myOptions = {};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
const userId = ctx.req.accessToken.userId;
try {
const sms = await Self.app.models.Sms.send(ctx, id, destination, message);
const logRecord = {
originFk: id,
userFk: userId,
action: 'insert',
changedModel: 'sms',
newInstance: {
destinationFk: id,
destination: destination,
message: message,
statusCode: sms.statusCode,
status: sms.status
}
};
const ticketLog = await Self.app.models.TicketLog.create(logRecord, myOptions);
sms.logId = ticketLog.id;
if (tx) await tx.commit();
return sms;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};