45 lines
1.3 KiB
JavaScript
45 lines
1.3 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: [],
|
|
required: true,
|
|
http: {source: 'body'}
|
|
}
|
|
],
|
|
returns: {
|
|
type: 'string',
|
|
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']});
|
|
|
|
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);
|
|
}
|
|
|
|
return Promise.all(promises);
|
|
};
|
|
};
|