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 = [];
|
2020-06-18 13:26:15 +00:00
|
|
|
for (let id of ticketIds) {
|
2020-06-17 13:38:37 +00:00
|
|
|
let promise = models.TicketTracking.changeState(ctx, {
|
|
|
|
stateFk: state.id,
|
|
|
|
workerFk: worker.id,
|
2020-06-18 13:26:15 +00:00
|
|
|
ticketFk: id
|
2020-06-17 13:38:37 +00:00
|
|
|
});
|
|
|
|
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
|
|
|
};
|
|
|
|
};
|