This commit is contained in:
parent
0f3b633de4
commit
bda3937417
|
@ -5,10 +5,7 @@ module.exports = Self => {
|
||||||
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'
|
||||||
}
|
}
|
||||||
|
@ -18,7 +15,7 @@ module.exports = Self => {
|
||||||
root: true
|
root: true
|
||||||
},
|
},
|
||||||
http: {
|
http: {
|
||||||
path: `/addState`,
|
path: `/addExpeditionState`,
|
||||||
verb: 'post'
|
verb: 'post'
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -44,11 +41,18 @@ module.exports = Self => {
|
||||||
where: {code: expedition.stateCode}
|
where: {code: expedition.stateCode}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
if (!expeditionStateType)
|
||||||
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 = expeditionStateType.id;
|
const typeFk = expeditionStateType.id;
|
||||||
|
|
||||||
|
const existsExpedition = await models.Expedition.findOne({
|
||||||
|
fields: ['id'],
|
||||||
|
where: {id: expedition.expeditionFk}
|
||||||
|
});
|
||||||
|
if (!existsExpedition)
|
||||||
|
throw new Error(`The expedition with id: ${expedition.expeditionFk} does not exist.`);
|
||||||
|
|
||||||
const newExpeditionState = models.ExpeditionState.create({
|
const newExpeditionState = models.ExpeditionState.create({
|
||||||
expeditionFk: expedition.expeditionFk,
|
expeditionFk: expedition.expeditionFk,
|
||||||
typeFk,
|
typeFk,
|
||||||
|
@ -59,10 +63,9 @@ module.exports = Self => {
|
||||||
await Promise.all(promises);
|
await Promise.all(promises);
|
||||||
|
|
||||||
if (tx) await tx.commit();
|
if (tx) await tx.commit();
|
||||||
return true;
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (tx) await tx.rollback();
|
if (tx) await tx.rollback();
|
||||||
return false;
|
throw e;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -0,0 +1,75 @@
|
||||||
|
const models = require('vn-loopback/server/server').models;
|
||||||
|
|
||||||
|
describe('expeditionState addExpeditionState()', () => {
|
||||||
|
it('should update the expedition states', async() => {
|
||||||
|
const tx = await models.ExpeditionState.beginTransaction({});
|
||||||
|
|
||||||
|
try {
|
||||||
|
const options = {transaction: tx};
|
||||||
|
const payload = [
|
||||||
|
{
|
||||||
|
expeditionFk: 5,
|
||||||
|
stateCode: 'ON DELIVERY'
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
await models.ExpeditionState.addExpeditionState(payload, options);
|
||||||
|
|
||||||
|
const expeditionState = await models.ExpeditionState.findOne({
|
||||||
|
where: {id: 5}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(expeditionState.typeFk).toEqual(1);
|
||||||
|
await tx.rollback();
|
||||||
|
} catch (e) {
|
||||||
|
await tx.rollback();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw an error an error when an stateCode does not exist', async() => {
|
||||||
|
const tx = await models.ExpeditionState.beginTransaction({});
|
||||||
|
let error;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const options = {transaction: tx};
|
||||||
|
const payload = [
|
||||||
|
{
|
||||||
|
expeditionFk: 2,
|
||||||
|
stateCode: 'DUMMY'
|
||||||
|
}
|
||||||
|
];
|
||||||
|
await models.ExpeditionState.addExpeditionState(payload, options);
|
||||||
|
|
||||||
|
await tx.rollback();
|
||||||
|
} catch (e) {
|
||||||
|
error = e;
|
||||||
|
await tx.rollback();
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(error.message).toContain('The state code: DUMMY does not exist.');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw an error when expeditionFk does not exist', async() => {
|
||||||
|
const tx = await models.ExpeditionState.beginTransaction({});
|
||||||
|
let error;
|
||||||
|
try {
|
||||||
|
const options = {transaction: tx};
|
||||||
|
const payload = [
|
||||||
|
{
|
||||||
|
expeditionFk: 50,
|
||||||
|
stateCode: 'LOST'
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
await models.ExpeditionState.addExpeditionState(payload, options);
|
||||||
|
|
||||||
|
await tx.rollback();
|
||||||
|
} catch (e) {
|
||||||
|
error = e;
|
||||||
|
await tx.rollback();
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(error.message).toContain('The expedition with id: 50 does not exist.');
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue