refs #5216 addExpeditionState refactored
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Jorge Penadés 2023-07-26 13:33:56 +02:00
parent bbc26d8ba4
commit 10361ba78e
2 changed files with 14 additions and 16 deletions

View File

@ -5,23 +5,28 @@ module.exports = Self => {
accepts: [
{
arg: 'expeditions',
type: ['object'],
type: [{
expeditionFk: 'number',
stateCode: 'string',
}],
required: true,
description: 'Array of objects containing expeditionFk and stateCode'
}
],
returns: {
type: 'boolean',
root: true
},
http: {
path: `/addExpeditionState`,
path: `/addState`,
verb: 'post'
}
});
Self.updateExpedtionState = async(expeditions, options) => {
console.log('Expeditions!!!!!!!!', expeditions);
Self.addExpeditionState = async(expeditions, options) => {
const models = Self.app.models;
let tx;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
@ -32,22 +37,19 @@ module.exports = Self => {
const promises = [];
try {
const conn = Self.dataSource.connector;
for (const expedition of expeditions) {
const expeditionStateType = await models.expeditionStateType.findOne(
const expeditionStateType = await models.ExpeditionStateType.findOne(
{
fields: ['id'],
where: {code: expedition.stateCode}
}
);
const result = await conn.query(stmt);
if (!expeditionStateType.id)
throw new Error(`The state code '${expedition.stateCode}' does not exist.`);
const typeFk = result.id;
const newExpeditionState = models.Self.create({
const typeFk = expeditionStateType.id;
const newExpeditionState = models.ExpeditionState.create({
expeditionFk: expedition.expeditionFk,
typeFk,
});
@ -57,11 +59,10 @@ module.exports = Self => {
await Promise.all(promises);
if (tx) await tx.commit();
return true;
} catch (e) {
if (tx) await tx.rollback();
throw e;
return false;
}
};
};

View File

@ -1,3 +0,0 @@
module.exports = function(Self) {
};