-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit implements the server-side logic and code for deleting cash payments. An integration tests shows that the functionality is intact. This goes halfway to addressing #2159.
- Loading branch information
Showing
9 changed files
with
255 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,4 @@ | ||
{ | ||
"directory": "client/vendor", | ||
"analytics": false, | ||
"resolvers": [ | ||
"bower-npm-resolver" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
/** | ||
* @overview transactions.js | ||
* | ||
* @description | ||
* This module contains helper functions for operating on transactions. These | ||
* helper functions | ||
* | ||
* @requires lib/db | ||
* @requires lib/errors/BadRequest | ||
* | ||
* @requires controllers/finance/cash | ||
* @requires controllers/finance/vouchers | ||
* @requires controllers/finance/patientInvoice | ||
*/ | ||
|
||
const db = require('../../lib/db'); | ||
const BadRequest = require('../../lib/errors/BadRequest'); | ||
|
||
const Cash = require('./cash'); | ||
|
||
// 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) | ||
}; | ||
|
||
exports.deleteTransaction = deleteTransaction; | ||
|
||
/** | ||
* @function getTransactionReferences | ||
* | ||
* @description | ||
* This function will find the uuids of any transactions that reference the | ||
* provided transaction's uuid. | ||
* | ||
* @param {String} transactionUuid - the record_uuid of the transaction | ||
*/ | ||
function getTransactionReferences(transactionUuid) { | ||
const sql = ` | ||
SELECT DISTINCT uuid, text FROM ( | ||
SELECT dm.uuid, dm.text | ||
FROM posting_journal AS j JOIN document_map AS dm ON | ||
j.reference_uuid = dm.uuid | ||
WHERE j.reference_uuid = ? | ||
UNION ALL | ||
SELECT dm.uuid, dm.text | ||
FROM general_ledger AS g JOIN document_map AS dm ON | ||
g.reference_uuid = dm.uuid | ||
WHERE g.reference_uuid = ? | ||
)c; | ||
`; | ||
|
||
const buid = db.bid(transactionUuid); | ||
|
||
return db.exec(sql, [buid, buid]); | ||
} | ||
|
||
/** | ||
* @function parseDocumentMapString | ||
* | ||
* @description | ||
* This function parses the document map identifier and returns the safe | ||
* deletion method associated with this document. | ||
* | ||
* @param {String} text - the text of the document map | ||
* | ||
* @returns {Function} the safe deletion method associated with the module | ||
*/ | ||
function parseDocumentMapString(text) { | ||
const key = text.split('.').shift(); | ||
return safeDeletionMethods[key]; | ||
} | ||
|
||
/** | ||
* @function getTransactionRecords | ||
* | ||
* @description | ||
* Returns the transaction from the posting journal and general_ledger. | ||
*/ | ||
function getTransactionRecords(uuid) { | ||
const sql = ` | ||
SELECT BUID(j.uuid) AS uuid, trans_id, BUID(record_uuid) AS record_uuid, | ||
trans_date, debit_equiv, credit_equiv, currency_id, | ||
BUID(reference_uuid) AS reference_uuid, | ||
BUID(entity_uuid) AS entity_uuid, 0 AS posted, | ||
document_map.text AS identifier | ||
FROM posting_journal AS j JOIN document_map ON | ||
j.record_uuid = document_map.uuid | ||
WHERE record_uuid = ? | ||
UNION ALL | ||
SELECT BUID(j.uuid) AS uuid, trans_id, BUID(record_uuid) AS record_uuid, | ||
trans_date, debit_equiv, credit_equiv, currency_id, | ||
BUID(reference_uuid) AS reference_uuid, | ||
BUID(entity_uuid) AS entity_uuid, 0 AS posted, | ||
document_map.text AS identifier | ||
FROM general_ledger AS j JOIN document_map ON | ||
j.record_uuid = document_map.uuid | ||
WHERE record_uuid = ? | ||
`; | ||
|
||
return db.exec(sql, [db.bid(uuid), db.bid(uuid)]); | ||
} | ||
|
||
|
||
/** | ||
* @function deleteTransation | ||
* | ||
* @description | ||
* This function is the HTTP handler for the delete transactions route. | ||
* | ||
* DELETE /transactions/:uuid | ||
*/ | ||
function deleteTransaction(req, res, next) { | ||
const { uuid } = req.params; | ||
let transaction; | ||
|
||
// get all the rows of the transaction | ||
getTransactionRecords(uuid) | ||
.then(rows => { | ||
transaction = rows; | ||
|
||
// TODO(@jniles) - i18n | ||
const isPosted = transaction[0].posted; | ||
if (isPosted) { | ||
throw new BadRequest('This transaction is already posted'); | ||
} | ||
|
||
// check if the transaction has references elsewhere | ||
return getTransactionReferences(uuid); | ||
}) | ||
.then(references => { | ||
// TODO(@jniles) - i18n | ||
const isReferenced = references.length > 0; | ||
if (isReferenced) { | ||
throw new BadRequest('This transaction is referenced'); | ||
} | ||
|
||
const documentMapText = transaction[0].identifier; | ||
|
||
// route to do the correct safe deletion method. | ||
const safeDeleteFn = parseDocumentMapString(documentMapText); | ||
|
||
// run the safe deletion method | ||
return safeDeleteFn(uuid); | ||
}) | ||
.then(() => { | ||
res.sendStatus(201); | ||
}) | ||
.catch(next) | ||
.done(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
-- UPDATE SERVICES WHICH DOESN'T HAVE UUID | ||
UPDATE `service` SET `uuid` = HUID(UUID()) WHERE `uuid` IS NULL; | ||
UPDATE `service` SET `uuid` = HUID(UUID()) WHERE `uuid` IS NULL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.