dev #1731
|
@ -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;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
module.exports = function(Self) {
|
||||
|
||||
};
|
Loading…
Reference in New Issue