refs #5216 path replaced and test created
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Jorge Penadés 2023-08-24 10:01:07 +02:00
parent 0f3b633de4
commit bda3937417
2 changed files with 88 additions and 10 deletions

View File

@ -5,10 +5,7 @@ module.exports = Self => {
accepts: [
{
arg: 'expeditions',
type: [{
expeditionFk: 'number',
stateCode: 'string',
}],
type: ['object'],
required: true,
description: 'Array of objects containing expeditionFk and stateCode'
}
@ -18,7 +15,7 @@ module.exports = Self => {
root: true
},
http: {
path: `/addState`,
path: `/addExpeditionState`,
verb: 'post'
}
});
@ -44,11 +41,18 @@ module.exports = Self => {
where: {code: expedition.stateCode}
}
);
if (!expeditionStateType.id)
throw new Error(`The state code '${expedition.stateCode}' does not exist.`);
if (!expeditionStateType)
throw new Error(`The state code: ${expedition.stateCode} does not exist.`);
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({
expeditionFk: expedition.expeditionFk,
typeFk,
@ -59,10 +63,9 @@ module.exports = Self => {
await Promise.all(promises);
if (tx) await tx.commit();
return true;
} catch (e) {
if (tx) await tx.rollback();
return false;
throw e;
}
};
};

View File

@ -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.');
});
});