51 lines
1.4 KiB
JavaScript
51 lines
1.4 KiB
JavaScript
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',
|
|
type: ['Number'],
|
|
required: true,
|
|
http: {source: 'body'}
|
|
}
|
|
],
|
|
returns: {
|
|
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;
|
|
|
|
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 id of ticketIds) {
|
|
let promise = models.TicketTracking.changeState(ctx, {
|
|
stateFk: state.id,
|
|
workerFk: worker.id,
|
|
ticketFk: id
|
|
});
|
|
promises.push(promise);
|
|
}
|
|
await Promise.all(promises);
|
|
|
|
return state;
|
|
};
|
|
};
|