salix/modules/ticket/back/methods/expedition/moveExpeditions.js

60 lines
1.7 KiB
JavaScript

module.exports = Self => {
Self.remoteMethod('moveExpeditions', {
description: 'Move the selected expeditions to another ticket',
accessType: 'WRITE',
accepts: [{
arg: 'expeditionsIds',
type: ['number'],
required: true,
description: 'The expeditions ids to nove'
},
{
arg: 'ticketId',
type: 'number',
required: true,
description: 'the ticket id to which the expeditions are added'
}],
returns: {
type: 'object',
root: true
},
http: {
path: `/moveExpeditions`,
verb: 'POST'
}
});
Self.moveExpeditions = async(expeditionsIds, ticketId, options) => {
const models = Self.app.models;
const myOptions = {};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const promises = [];
for (let expeditionsId of expeditionsIds) {
const expeditionToUpdate = await models.Expedition.findById(expeditionsId);
const expeditionUpdated = expeditionToUpdate.updateAttribute('ticketFk', ticketId, myOptions);
promises.push(expeditionUpdated);
}
const updated = await Promise.all(promises);
if (tx) await tx.commit();
return updated;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};