2020-06-17 13:38:37 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('setDelivered', {
|
|
|
|
description: 'Changes the state of the received ticket ids to delivered',
|
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [
|
|
|
|
{
|
|
|
|
arg: 'ticketIds',
|
|
|
|
description: 'the array of ticket ids to set as delivered',
|
2021-10-07 07:57:07 +00:00
|
|
|
type: ['number'],
|
2020-06-17 13:38:37 +00:00
|
|
|
required: true,
|
|
|
|
http: {source: 'body'}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
returns: {
|
2021-10-07 07:57:07 +00:00
|
|
|
type: 'object',
|
2020-06-17 13:38:37 +00:00
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/setDelivered`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-10-07 07:57:07 +00:00
|
|
|
Self.setDelivered = async(ctx, ticketIds, options) => {
|
|
|
|
const userId = ctx.req.accessToken.userId;
|
|
|
|
const models = Self.app.models;
|
|
|
|
const myOptions = {};
|
|
|
|
let tx;
|
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
|
|
|
if (!myOptions.transaction) {
|
|
|
|
tx = await Self.beginTransaction({});
|
|
|
|
myOptions.transaction = tx;
|
2020-06-17 13:38:37 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 07:57:07 +00:00
|
|
|
try {
|
|
|
|
const state = await models.State.findOne({
|
|
|
|
where: {
|
|
|
|
code: 'delivered'
|
|
|
|
},
|
|
|
|
fields: ['id', 'name', 'alertLevel', 'code']
|
|
|
|
}, myOptions);
|
|
|
|
|
|
|
|
const worker = await models.Worker.findOne({where: {userFk: userId}}, myOptions);
|
|
|
|
|
|
|
|
const promises = [];
|
|
|
|
for (const id of ticketIds) {
|
|
|
|
const promise = models.TicketTracking.changeState(ctx, {
|
|
|
|
stateFk: state.id,
|
|
|
|
workerFk: worker.id,
|
|
|
|
ticketFk: id
|
|
|
|
}, myOptions);
|
|
|
|
promises.push(promise);
|
|
|
|
}
|
|
|
|
await Promise.all(promises);
|
|
|
|
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
|
|
|
|
return state;
|
|
|
|
} catch (e) {
|
|
|
|
if (tx) await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2020-06-17 13:38:37 +00:00
|
|
|
};
|
|
|
|
};
|