This commit is contained in:
parent
0b88194f09
commit
bbc26d8ba4
|
@ -1,17 +1,11 @@
|
||||||
const UserError = require('vn-loopback/util/user-error');
|
|
||||||
|
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
Self.remoteMethod('updateExpedtionState', {
|
Self.remoteMethod('addExpeditionState', {
|
||||||
description: 'Update an expedition state',
|
description: 'Update an expedition state',
|
||||||
|
accessType: 'WRITE',
|
||||||
accepts: [
|
accepts: [
|
||||||
{
|
{
|
||||||
arg: 'expeditions',
|
arg: 'expeditions',
|
||||||
type: [
|
type: ['object'],
|
||||||
{
|
|
||||||
expeditionFk: 'number',
|
|
||||||
stateCode: 'string'
|
|
||||||
}
|
|
||||||
],
|
|
||||||
required: true,
|
required: true,
|
||||||
description: 'Array of objects containing expeditionFk and stateCode'
|
description: 'Array of objects containing expeditionFk and stateCode'
|
||||||
}
|
}
|
||||||
|
@ -23,6 +17,7 @@ module.exports = Self => {
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.updateExpedtionState = async(expeditions, options) => {
|
Self.updateExpedtionState = 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 = {};
|
||||||
|
@ -35,22 +30,22 @@ module.exports = Self => {
|
||||||
myOptions.transaction = tx;
|
myOptions.transaction = tx;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (expeditions.length === 0)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
const promises = [];
|
const promises = [];
|
||||||
try {
|
try {
|
||||||
const conn = Self.dataSource.connector;
|
const conn = Self.dataSource.connector;
|
||||||
for (const expedition of expeditions) {
|
for (const expedition of expeditions) {
|
||||||
const stmt = new ParameterizedSQL(
|
const expeditionStateType = await models.expeditionStateType.findOne(
|
||||||
`SELECT id FROM vn.expeditionStateType WHERE code = ?`,
|
{
|
||||||
[expedition.stateCode]
|
fields: ['id'],
|
||||||
|
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;
|
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 newExpeditionState = models.Self.create({
|
||||||
expeditionFk: expedition.expeditionFk,
|
expeditionFk: expedition.expeditionFk,
|
||||||
|
|
|
@ -20,6 +20,9 @@
|
||||||
"ExpeditionState": {
|
"ExpeditionState": {
|
||||||
"dataSource": "vn"
|
"dataSource": "vn"
|
||||||
},
|
},
|
||||||
|
"ExpeditionStateType": {
|
||||||
|
"dataSource": "vn"
|
||||||
|
},
|
||||||
"Packaging": {
|
"Packaging": {
|
||||||
"dataSource": "vn"
|
"dataSource": "vn"
|
||||||
},
|
},
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
module.exports = function(Self) {
|
||||||
|
|
||||||
|
};
|
|
@ -0,0 +1,22 @@
|
||||||
|
{
|
||||||
|
"name": "ExpeditionStateType",
|
||||||
|
"base": "VnModel",
|
||||||
|
"options": {
|
||||||
|
"mysql": {
|
||||||
|
"table": "expeditionStateType"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"properties": {
|
||||||
|
"id": {
|
||||||
|
"id": true,
|
||||||
|
"type": "number",
|
||||||
|
"description": "Identifier"
|
||||||
|
},
|
||||||
|
"description": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"code": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue