diff --git a/modules/ticket/back/methods/expedition-state/addExpeditionState.js b/modules/ticket/back/methods/expedition-state/addExpeditionState.js index 16b7a4c75..cc70cbb8d 100644 --- a/modules/ticket/back/methods/expedition-state/addExpeditionState.js +++ b/modules/ticket/back/methods/expedition-state/addExpeditionState.js @@ -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; } }; }; diff --git a/modules/ticket/back/methods/expedition-state/specs/addExpeditionState.spec.js b/modules/ticket/back/methods/expedition-state/specs/addExpeditionState.spec.js new file mode 100644 index 000000000..c9c40ccd9 --- /dev/null +++ b/modules/ticket/back/methods/expedition-state/specs/addExpeditionState.spec.js @@ -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.'); + }); +});