Skip to content

Commit

Permalink
fix(db): allow posting default references
Browse files Browse the repository at this point in the history
This commit allows the references to be rebuilt from previous data.  In
the old database, the references were re-calculated every time a record
was written, resulting in some strange behavior (like PA.TPA.1 being
Test Patient 2 despite the test data.sql reversing the references). I
have fixed this bug to allow the database to be rebuilt correctly.
  • Loading branch information
Jonathan Niles authored and jniles committed Feb 2, 2017
1 parent bb255a9 commit fd15a86
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 16 deletions.
3 changes: 0 additions & 3 deletions server/controllers/finance/cash.create.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ const db = require('../../lib/db');
const BadRequest = require('../../lib/errors/BadRequest');
const util = require('../../lib/util');

// @fixme - only for testing purposes
const q = require('q');

module.exports = create;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ function processInvoice(invoiceUuid, invoice) {
delete invoice.items;
delete invoice.billingServices;
delete invoice.subsidies;
delete invoice.reference;

const keys = [
'date', 'cost', 'description', 'service_id',
Expand Down
1 change: 1 addition & 0 deletions server/controllers/finance/purchases.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ function create(req, res, next) {

// delete the purchase order items
delete data.items;
delete data.reference;

const transaction = db.transaction();

Expand Down
1 change: 1 addition & 0 deletions server/controllers/finance/vouchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ function create(req, res, next) {
// remove the voucher items from the request before insertion into the
// database
delete voucher.items;
delete voucher.reference;

// convert dates to a date objects
voucher.date = voucher.date ? new Date(voucher.date) : new Date();
Expand Down
8 changes: 4 additions & 4 deletions server/models/test/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,10 @@ SET @first_voucher = HUID('a5a5f950-a4c9-47f0-9a9a-2bfc3123e534');
SET @second_voucher = HUID('304cfa94-0249-466c-9870-95eb3c221b0a');
SET @third_voucher = HUID('3688e9ce-85ea-4b5c-9144-688177edcb63');

INSERT INTO `voucher` (uuid, `date`, project_id, currency_id, amount, description, user_id) VALUES
(@first_voucher, CURRENT_TIMESTAMP, 1, 2, 100, 'Sample voucher data one', 1),
(@second_voucher, CURRENT_TIMESTAMP, 1, 2, 200, 'Sample voucher data two', 1),
(@third_voucher, CURRENT_TIMESTAMP, 1, 2, 300, 'Sample voucher data three', 1);
INSERT INTO `voucher` (uuid, `date`, project_id, currency_id, amount, description, user_id) VALUES
(@first_voucher, CURRENT_TIMESTAMP, 1, 2, 100, 'Sample voucher data one', 1),
(@second_voucher, CURRENT_TIMESTAMP, 2, 2, 200, 'Sample voucher data two', 1),
(@third_voucher, CURRENT_TIMESTAMP, 3, 1, 300, 'Sample voucher data three', 1);

-- voucher items sample data
INSERT INTO `voucher_item` VALUES
Expand Down
21 changes: 14 additions & 7 deletions server/models/triggers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ DELIMITER $$
-- Patient Triggers

CREATE TRIGGER patient_reference BEFORE INSERT ON patient
FOR EACH ROW SET NEW.reference = (SELECT IFNULL(MAX(reference) + 1, 1) FROM patient WHERE patient.project_id = new.project_id)$$
FOR EACH ROW
SET NEW.reference = (SELECT IF(NEW.reference, NEW.reference, IFNULL(MAX(patient.reference) + 1, 1)) FROM patient WHERE patient.project_id = new.project_id);$$

CREATE TRIGGER patient_entity_map AFTER INSERT ON patient
FOR EACH ROW BEGIN
Expand All @@ -21,7 +22,8 @@ END$$
-- Purchase Triggers

CREATE TRIGGER purchase_reference BEFORE INSERT ON purchase
FOR EACH ROW SET NEW.reference = (SELECT IFNULL(MAX(reference) + 1, 1) FROM purchase WHERE purchase.project_id = new.project_id)$$
FOR EACH ROW
SET NEW.reference = (SELECT IF(NEW.reference, NEW.reference, IFNULL(MAX(purchase.reference) + 1, 1)) FROM purchase WHERE purchase.project_id = new.project_id);$$

CREATE TRIGGER purchase_document_map AFTER INSERT ON purchase
FOR EACH ROW BEGIN
Expand All @@ -33,7 +35,8 @@ END$$
-- Invoice Triggers

CREATE TRIGGER invoice_reference BEFORE INSERT ON invoice
FOR EACH ROW SET NEW.reference = (SELECT IFNULL(MAX(reference) + 1, 1) FROM invoice WHERE invoice.project_id = new.project_id)$$
FOR EACH ROW
SET NEW.reference = (SELECT IF(NEW.reference, NEW.reference, IFNULL(MAX(invoice.reference) + 1, 1)) FROM invoice WHERE invoice.project_id = new.project_id);$$

CREATE TRIGGER invoice_document_map AFTER INSERT ON invoice
FOR EACH ROW BEGIN
Expand All @@ -45,7 +48,8 @@ END$$
-- Cash Payment Triggers

CREATE TRIGGER cash_before_insert BEFORE INSERT ON cash
FOR EACH ROW SET NEW.reference = (SELECT IFNULL(MAX(reference) + 1, 1) FROM cash WHERE cash.project_id = new.project_id)$$
FOR EACH ROW
SET NEW.reference = (SELECT IF(NEW.reference, NEW.reference, IFNULL(MAX(cash.reference) + 1, 1)) FROM cash WHERE cash.project_id = new.project_id);$$

CREATE TRIGGER cash_document_map AFTER INSERT ON cash
FOR EACH ROW BEGIN
Expand All @@ -57,7 +61,8 @@ END$$
-- Credit Note Triggers
-- @FIXME - why are we still using a credit note table?
CREATE TRIGGER credit_note_before_insert BEFORE INSERT ON credit_note
FOR EACH ROW SET NEW.reference = (SELECT IFNULL(MAX(reference) + 1, 1) FROM credit_note WHERE credit_note.project_id = new.project_id)$$
FOR EACH ROW
SET NEW.reference = (SELECT IF(NEW.reference, NEW.reference, IFNULL(MAX(credit_note.reference) + 1, 1)) FROM credit_note WHERE credit_note.project_id = new.project_id);$$

CREATE TRIGGER credit_note_document_map AFTER INSERT ON credit_note
FOR EACH ROW BEGIN
Expand All @@ -69,7 +74,8 @@ END$$
-- Voucher Triggers

CREATE TRIGGER voucher_before_insert BEFORE INSERT ON voucher
FOR EACH ROW SET NEW.reference = (SELECT IFNULL(MAX(reference) + 1, 1) FROM voucher WHERE voucher.project_id = NEW.project_id)$$
FOR EACH ROW
SET NEW.reference = (SELECT IF(NEW.reference, NEW.reference, IFNULL(MAX(voucher.reference) + 1, 1)) FROM voucher WHERE voucher.project_id = new.project_id);$$

CREATE TRIGGER voucher_document_map AFTER INSERT ON voucher
FOR EACH ROW BEGIN
Expand All @@ -92,7 +98,8 @@ END$$
-- Supplier Triggers

CREATE TRIGGER supplier_before_insert BEFORE INSERT ON supplier
FOR EACH ROW SET NEW.reference = (SELECT IFNULL(MAX(supplier.reference) + 1, 1) FROM supplier)$$
FOR EACH ROW
SET NEW.reference = (SELECT IF(NEW.reference, NEW.reference, IFNULL(MAX(supplier.reference) + 1, 1)) FROM supplier);$$

CREATE TRIGGER supplier_entity_map AFTER INSERT ON supplier
FOR EACH ROW BEGIN
Expand Down
2 changes: 1 addition & 1 deletion test/end-to-end/cash/cash.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ describe('Cash Payments', function () {

// This payment against patient invoices should succeed
var mockInvoicesPayment = {
patientId: 'PA.TPA.1',
patientId: 'PA.TPA.2',
date : new Date('2016-03-01'),
amount : 5.12
};
Expand Down
2 changes: 1 addition & 1 deletion test/end-to-end/patient/record.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('Patient Record', function () {

const patient = {
name : 'Test 2 Patient',
id : 'PA.TPA.1',
id : 'PA.TPA.2',
hospital_no : '110',
age : '26',
gender : 'M'
Expand Down

0 comments on commit fd15a86

Please sign in to comment.