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

60 lines
1.7 KiB
JavaScript
Raw Normal View History

2022-10-07 08:47:54 +00:00
module.exports = Self => {
Self.remoteMethod('moveExpeditions', {
description: 'Move the selected expeditions to another ticket',
accessType: 'WRITE',
accepts: [{
2022-10-18 09:47:37 +00:00
arg: 'expeditionIds',
2022-10-07 08:47:54 +00:00
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'
}
});
2022-10-18 09:47:37 +00:00
Self.moveExpeditions = async(expeditionIds, ticketId, options) => {
2022-10-07 08:47:54 +00:00
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 = [];
2022-10-18 09:47:37 +00:00
for (let expeditionsId of expeditionIds) {
2022-10-07 08:47:54 +00:00
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;
}
};
};