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',
|
2020-06-18 11:50:52 +00:00
|
|
|
type: ['Number'],
|
2020-06-17 13:38:37 +00:00
|
|
|
required: true,
|
|
|
|
http: {source: 'body'}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
returns: {
|
2020-06-18 11:50:52 +00:00
|
|
|
type: 'Object',
|
2020-06-17 13:38:37 +00:00
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/setDelivered`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Self.setDelivered = async(ctx, ticketIds) => {
|
|
|
|
let userId = ctx.req.accessToken.userId;
|
|
|
|
let models = Self.app.models;
|
|
|
|
|
2020-06-18 11:50:52 +00:00
|
|
|
let state = await models.State.findOne({
|
|
|
|
where: {
|
|
|
|
code: 'delivered'
|
|
|
|
},
|
|
|
|
fields: ['id', 'name', 'alertLevel', 'code']
|
|
|
|
});
|
2020-06-17 13:38:37 +00:00
|
|
|
|
|
|
|
let worker = await models.Worker.findOne({where: {userFk: userId}});
|
|
|
|
|
|
|
|
let promises = [];
|
|
|
|
for (let i = 0; i < ticketIds.length; i++) {
|
|
|
|
let promise = models.TicketTracking.changeState(ctx, {
|
|
|
|
stateFk: state.id,
|
|
|
|
workerFk: worker.id,
|
|
|
|
ticketFk: ticketIds[i]
|
|
|
|
});
|
|
|
|
promises.push(promise);
|
|
|
|
}
|
2020-06-18 11:50:52 +00:00
|
|
|
await Promise.all(promises);
|
2020-06-17 13:38:37 +00:00
|
|
|
|
2020-06-18 11:50:52 +00:00
|
|
|
return state;
|
2020-06-17 13:38:37 +00:00
|
|
|
};
|
|
|
|
};
|