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