2023-07-24 11:32:28 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
|
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethod('updateExpedtionState', {
|
|
|
|
description: 'Update an expedition state',
|
|
|
|
accepts: [
|
|
|
|
{
|
|
|
|
arg: 'expeditions',
|
|
|
|
type: [
|
|
|
|
{
|
|
|
|
expeditionFk: 'number',
|
|
|
|
stateCode: 'string'
|
|
|
|
}
|
|
|
|
],
|
|
|
|
required: true,
|
|
|
|
description: 'Array of objects containing expeditionFk and stateCode'
|
|
|
|
}
|
|
|
|
],
|
|
|
|
http: {
|
2023-07-25 14:32:38 +00:00
|
|
|
path: `/addExpeditionState`,
|
2023-07-24 11:32:28 +00:00
|
|
|
verb: 'post'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Self.updateExpedtionState = async(expeditions, options) => {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (expeditions.length === 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const promises = [];
|
|
|
|
try {
|
|
|
|
const conn = Self.dataSource.connector;
|
|
|
|
for (const expedition of expeditions) {
|
|
|
|
const stmt = new ParameterizedSQL(
|
|
|
|
`SELECT id FROM vn.expeditionStateType WHERE code = ?`,
|
|
|
|
[expedition.stateCode]
|
|
|
|
);
|
|
|
|
const result = await conn.query(stmt);
|
|
|
|
if (result.length === 0)
|
|
|
|
throw new Error(`The state code '${exp.stateCode}' does not exist.`);
|
|
|
|
|
|
|
|
const typeFk = result[0].id;
|
|
|
|
|
2023-07-25 14:32:38 +00:00
|
|
|
const newExpeditionState = models.Self.create({
|
2023-07-24 11:32:28 +00:00
|
|
|
expeditionFk: expedition.expeditionFk,
|
|
|
|
typeFk,
|
|
|
|
});
|
2023-07-25 14:32:38 +00:00
|
|
|
promises.push(newExpeditionState);
|
2023-07-24 11:32:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all(promises);
|
|
|
|
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
if (tx) await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|