diff --git a/services/order/common/methods/order-row/removes.js b/services/order/common/methods/order-row/removes.js index c08ac9a8f..0f8aaa86e 100644 --- a/services/order/common/methods/order-row/removes.js +++ b/services/order/common/methods/order-row/removes.js @@ -23,9 +23,12 @@ module.exports = Self => { Self.removes = async params => { if (!params.rows || !params.rows.length) - throw new UserError('There is nothing delete'); + throw new UserError('There is nothing to delete'); - await Self.app.models.Order.isEditable(params.actualOrderId); + let isEditable = await Self.app.models.Order.isEditable(params.actualOrderId); + + if (!isEditable) + throw new UserError('This order is not editable'); let promises = []; for (let i = 0; i < params.rows.length; i++) { diff --git a/services/order/common/methods/order-row/specs/addToOrder.spec.js b/services/order/common/methods/order-row/specs/addToOrder.spec.js index c9c1a53cd..9cef506d6 100644 --- a/services/order/common/methods/order-row/specs/addToOrder.spec.js +++ b/services/order/common/methods/order-row/specs/addToOrder.spec.js @@ -1,6 +1,11 @@ const app = require(`../../../../server/server`); describe('order addToOrder()', () => { + let rowToDelete; + afterAll(async() => { + await app.models.OrderRow.removes({rows: [rowToDelete], actualOrderId: 16}); + }); + it('should add a row to a given order', async() => { let unmodifiedRows = await app.models.OrderRow.find({where: {orderFk: 16}}); @@ -19,7 +24,8 @@ describe('order addToOrder()', () => { let modifiedRows = await app.models.OrderRow.find({where: {orderFk: 16}}); + rowToDelete = modifiedRows[modifiedRows.length - 1].id; + expect(modifiedRows.length).toBe(5); - await app.models.OrderRow.removes({rows: [modifiedRows[modifiedRows.length - 1].id]}); }); }); diff --git a/services/order/common/methods/order-row/specs/removes.spec.js b/services/order/common/methods/order-row/specs/removes.spec.js index b6dafae24..f8a90e2c0 100644 --- a/services/order/common/methods/order-row/specs/removes.spec.js +++ b/services/order/common/methods/order-row/specs/removes.spec.js @@ -1,6 +1,15 @@ const app = require(`../../../../server/server`); -xdescribe('order removes()', () => { +describe('order removes()', () => { + let row; + let newRow; + + beforeAll(async() => { + row = await app.models.OrderRow.findOne({where: {id: 12}}); + row.id = null; + newRow = await app.models.OrderRow.create(row); + }); + it('should throw an error if rows property is empty', async() => { let error; try { @@ -9,7 +18,7 @@ xdescribe('order removes()', () => { error = e; } - expect(error).toEqual(new Error('There is nothing delete')); + expect(error).toEqual(new Error('There is nothing to delete')); }); it('should throw an error if the row selected is not editable', async() => { @@ -22,4 +31,15 @@ xdescribe('order removes()', () => { expect(error).toEqual(new Error('This order is not editable')); }); + + it('should delete the row', async() => { + let params = { + rows: [newRow.id], + actualOrderId: 16 + }; + + let res = await app.models.OrderRow.removes(params); + + expect(res).toEqual([{count: 1}]); + }); });