salix/modules/ticket/back/methods/ticket-tracking/setDelivered.js

51 lines
1.4 KiB
JavaScript
Raw Normal View History

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'],
required: true,
http: {source: 'body'}
}
],
returns: {
2020-06-18 11:50:52 +00:00
type: 'Object',
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']
});
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-18 11:50:52 +00:00
return state;
};
};