Skip to content

Commit

Permalink
feat(invoices): implement delete server-side
Browse files Browse the repository at this point in the history
This commit implements the DELETE /transactions/:uuid route for invoices
to remove records from the database.

Closes #2060.
  • Loading branch information
jniles committed Oct 13, 2017
1 parent 2efc3f0 commit 00a1367
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 19 deletions.
56 changes: 45 additions & 11 deletions server/controllers/finance/patientInvoice.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
/**
* Patient Invoice API Controller
*
*@module controllers/finance/patientInvoice
* @module controllers/finance/patientInvoice
*
* @todo (required) major bug - Invoice items are entered based on order or attributes sent from client
* this doesn't seem to be consistent as of 2.X
* @todo GET /invoices/patient/:uuid - retrieve all patient invoices for a specific patient
* - should this be /patients/:uuid/invoices?
* @todo Factor in subsidies, this depends on price lists and billing services infrastructure
* @todo Factor in subsidies, this depends on price lists and
* billing services infrastructure
*/

const uuid = require('node-uuid');
Expand Down Expand Up @@ -37,6 +34,8 @@ exports.lookupInvoice = lookupInvoice;

exports.find = find;

exports.safelyDeleteInvoice = safelyDeleteInvoice;

/** find the balance on an invoice due the particular debtor */
exports.balance = balance;

Expand Down Expand Up @@ -170,16 +169,14 @@ function detail(req, res, next) {
}

function create(req, res, next) {
const invoice = req.body.invoice;
const { invoice } = req.body;
invoice.user_id = req.session.user.id;

const hasInvoiceItems = (invoice.items && invoice.items.length > 0);

// detect missing items early and respond with an error
if (!hasInvoiceItems) {
next(
new BadRequest(`An invoice must be submitted with invoice items.`)
);
next(new BadRequest(`An invoice must be submitted with invoice items.`));
return;
}

Expand Down Expand Up @@ -232,7 +229,8 @@ function find(options) {
);

filters.custom(
'inventory_uuid', 'invoice.uuid IN (SELECT invoice_item.invoice_uuid FROM invoice_item WHERE invoice_item.inventory_uuid = ?)'
'inventory_uuid',
'invoice.uuid IN (SELECT invoice_item.invoice_uuid FROM invoice_item WHERE invoice_item.inventory_uuid = ?)'
);

filters.period('period', 'date');
Expand Down Expand Up @@ -280,3 +278,39 @@ function lookupInvoiceCreditNote(invoiceUuid) {
return null;
});
}

/**
* @function safelyDeleteInvoice
*
* @description
* This function deletes the invoice from the system. It assumes that
* checks have already been made for referencing transactions.
*/
function safelyDeleteInvoice(guid) {
const DELETE_TRANSACTION = `
DELETE FROM posting_journal WHERE record_uuid = ?;
`;

const DELETE_INVOICE = `
DELETE FROM invoice WHERE uuid = ?;
`;

const DELETE_TRANSACTION_HISTORY = `
DELETE FROM transaction_history WHERE record_uuid = ?;
`;

const DELETE_DOCUMENT_MAP = `
DELETE FROM document_map WHERE uuid = ?;
`;

const binaryUuid = db.bid(guid);
const transaction = db.transaction();

transaction
.addQuery(DELETE_TRANSACTION, binaryUuid)
.addQuery(DELETE_TRANSACTION_HISTORY, binaryUuid)
.addQuery(DELETE_INVOICE, binaryUuid)
.addQuery(DELETE_DOCUMENT_MAP, binaryUuid);

return transaction.execute();
}
14 changes: 6 additions & 8 deletions server/controllers/finance/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@ const db = require('../../lib/db');
const BadRequest = require('../../lib/errors/BadRequest');

const Cash = require('./cash');

// const Invoices = require('./patientInvoice');
// const Vouchers = require('./vouchers');
const Invoices = require('./patientInvoice');
const Vouchers = require('./vouchers');

// this wraps up the safe deletion methods.
// TODO(@jniles) - move these to the `indentifiers` as suggested by @sfount
const safeDeletionMethods = {
CP : Cash.safelyDeleteCashPayment,
// IV : Invoices.safelyDeleteInvoice, TODO(@jniles)
// VO : Vouchers.safelyDeleteVoucher, TODO(@jniles)
IV : Invoices.safelyDeleteInvoice,
VO : Vouchers.safelyDeleteVoucher,
};

exports.deleteTransaction = deleteTransaction;
Expand Down Expand Up @@ -131,7 +130,7 @@ function deleteTransaction(req, res, next) {
// TODO(@jniles) - i18n
const isPosted = transaction[0].posted;
if (isPosted) {
throw new BadRequest('This transaction is already posted');
throw new BadRequest('This transaction is already posted.');
}

// check if the transaction has references elsewhere
Expand All @@ -141,7 +140,7 @@ function deleteTransaction(req, res, next) {
// TODO(@jniles) - i18n
const isReferenced = references.length > 0;
if (isReferenced) {
throw new BadRequest('This transaction is referenced');
throw new BadRequest('This transaction is referenced.');
}

const documentMapText = transaction[0].identifier;
Expand All @@ -158,4 +157,3 @@ function deleteTransaction(req, res, next) {
.catch(next)
.done();
}

14 changes: 14 additions & 0 deletions test/integration/patientInvoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ describe('(/invoices) Patient Invoices', function () {
const debtorUuid = '3be232f9-a4b9-4af6-984c-5d3f87d5c107';
const patientUuid = '274c51ae-efcc-4238-98c6-f402bfb39866';

const TO_DELETE_UUID = 'c44619e0-3a88-4754-a750-a414fc9567bf';

// run the 'BillingScenarios' test suite
describe('(POST /invoices)', BillingScenarios);

Expand Down Expand Up @@ -360,4 +362,16 @@ function BillingScenarios() {
})
.catch(helpers.handler);
});

it('DELETE /transactions/:uuid deletes an invoice', () => {
return agent.delete(`/transactions/${TO_DELETE_UUID}`)
.then(res => {
expect(res).to.have.status(201);
return agent.get(`/invoices/${TO_DELETE_UUID}`);
})
.then(res => {
helpers.api.errored(res, 404);
})
.catch(helpers.handler);
});
}

0 comments on commit 00a1367

Please sign in to comment.