diff --git a/server/controllers/finance/purchases.js b/server/controllers/finance/purchases.js index 59e3d1b6b3..2a592dd6c8 100644 --- a/server/controllers/finance/purchases.js +++ b/server/controllers/finance/purchases.js @@ -549,7 +549,7 @@ async function remove(req, res, next) { const WAITING_CONFIRMATION = 1; if (record.status_id !== WAITING_CONFIRMATION) { - throw new BadRequest('Cannot remove a purchase order that is confirmed'); + throw new BadRequest('Can only remove purchase orders that have been confirmed.'); } // there is no financial writings about purchase orders, so we simply need to delete it @@ -628,9 +628,7 @@ function resetPurchaseIntervall(purchaseUuid) { return db.exec(sql, [db.bid(purchaseUuid)]) .then((rows) => { const inventories = rows.map(item => item.inventory_uuid); - const transactions = purchaceIntervalleSetting(inventories); - return transactions.execute(); }) .then((items) => { diff --git a/test/server-unit/db.spec.js b/test/server-unit/db.spec.js index bdaa014fc4..07cd50b634 100644 --- a/test/server-unit/db.spec.js +++ b/test/server-unit/db.spec.js @@ -7,7 +7,6 @@ function DatabaseUnitTests() { db = require('../../server/lib/db'); }); - it('should check the connection to mysql', (done) => { db.pool.getConnection(err => { if (err) { @@ -19,7 +18,12 @@ function DatabaseUnitTests() { }); }); - it('#should try to retrieve data from a specific table (unit)', (done) => { + it('#exec() should retrieve a promise result', async () => { + const [result] = await db.exec('SELECT 1 + 1 AS two;'); + expect(result).to.deep.equal({ two : 2 }); + }); + + it('should try to retrieve data from a specific table (unit)', (done) => { db.exec('SELECT * FROM unit LIMIT 2') .then(rows => { expect(rows).to.have.lengthOf(2); diff --git a/test/server-unit/purchases/purchases.spec.js b/test/server-unit/purchases/purchases.spec.js new file mode 100644 index 0000000000..8b90162af1 --- /dev/null +++ b/test/server-unit/purchases/purchases.spec.js @@ -0,0 +1,42 @@ +/* eslint global-require:off */ +const { expect } = require('chai'); +const uuid = require('uuid').v4; + +function PurchaseUnitTests() { + let db; + before(() => { + db = require('../../../server/lib/db'); + }); + + it('should remove purchase items on purchase delete', async () => { + // test setup - create a purchase order + const puid = uuid(); + const prednisone = 'c3fd5a02-6a75-49fc-b2f3-76ee4c3fbfb7'; + + // create the purchase order + await db.exec(` + INSERT INTO purchase VALUES + (HUID('${puid}'), 2, 1, 300, 2, HUID('3ac4e83c-65f2-45a1-8357-8b025003d793'), + DATE_ADD(CURRENT_DATE, INTERVAL -1725 DAY), CURRENT_TIMESTAMP, 1, NULL, NULL, 1); + `); + + // create a purchase item linked to the above purchase order. + await db.exec(`INSERT INTO purchase_item VALUES + (HUID('${uuid()}'), HUID('${puid}'), HUID('${prednisone}'), 2000, 0.15, (2000 * 0.15));`); + + const [purchase] = await db.exec(`SELECT * FROM purchase WHERE uuid = HUID('${puid}');`); + expect(purchase.cost).to.equal(300); + + // okay, now that we know our data is built, actually test if the data is deleted + // correctly. + await db.exec(`DELETE FROM purchase WHERE uuid = HUID('${puid}');`); + + const purchases = await db.exec(`SELECT * FROM purchase WHERE uuid = HUID('${puid}');`); + expect(purchases).to.have.length(0); + const items = await db.exec(`SELECT * FROM purchase_item WHERE purchase_uuid = HUID('${puid}');`); + expect(items).to.have.length(0); + }); + +} + +describe('Purchase Orders', PurchaseUnitTests);