salix/modules/ticket/back/methods/expedition-state/addExpeditionState.js

68 lines
2.0 KiB
JavaScript
Raw Normal View History

module.exports = Self => {
2023-07-26 10:36:30 +00:00
Self.remoteMethod('addExpeditionState', {
description: 'Update an expedition state',
2023-07-26 10:36:30 +00:00
accessType: 'WRITE',
accepts: [
{
arg: 'expeditions',
2023-07-26 10:36:30 +00:00
type: ['object'],
required: true,
description: 'Array of objects containing expeditionFk and stateCode'
}
],
http: {
path: `/addExpeditionState`,
verb: 'post'
}
});
Self.updateExpedtionState = async(expeditions, options) => {
2023-07-26 10:36:30 +00:00
console.log('Expeditions!!!!!!!!', expeditions);
const models = Self.app.models;
let tx;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
const promises = [];
try {
const conn = Self.dataSource.connector;
for (const expedition of expeditions) {
2023-07-26 10:36:30 +00:00
const expeditionStateType = await models.expeditionStateType.findOne(
{
fields: ['id'],
where: {code: expedition.stateCode}
}
);
2023-07-26 10:36:30 +00:00
const result = await conn.query(stmt);
2023-07-26 10:36:30 +00:00
if (!expeditionStateType.id)
throw new Error(`The state code '${expedition.stateCode}' does not exist.`);
2023-07-26 10:36:30 +00:00
const typeFk = result.id;
const newExpeditionState = models.Self.create({
expeditionFk: expedition.expeditionFk,
typeFk,
});
promises.push(newExpeditionState);
}
await Promise.all(promises);
if (tx) await tx.commit();
return true;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};