Skip to content

Commit

Permalink
feat(journal): transaction edit history
Browse files Browse the repository at this point in the history
This commit contains the first implementation of the transaction_history
proposed in #2046.  It implements the table and triggers to update the
transaction_history after the transaction has been edited.

Closes #2046.
  • Loading branch information
jniles committed Sep 26, 2017
1 parent 6b202be commit df82035
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
24 changes: 20 additions & 4 deletions server/controllers/finance/journal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ exports.reverse = reverse;
exports.find = find;
exports.buildTransactionQuery = buildTransactionQuery;

// exports.getTransactionEditHistory = getTransactionEditHistory;

exports.editTransaction = editTransaction;
exports.count = count;
exports.commentPostingJournal = commentPostingJournal;
Expand Down Expand Up @@ -200,9 +202,10 @@ function getTransaction(req, res, next) {
// @TODO(sfount) move edit transaction code to separate server controller - split editing process
// up into smaller self contained methods
function editTransaction(req, res, next) {
const REMOVE_JOURNAL_ROW = 'DELETE FROM posting_journal WHERE uuid = ?';
const UPDATE_JOURNAL_ROW = 'UPDATE posting_journal SET ? WHERE uuid = ?';
const INSERT_JOURNAL_ROW = 'INSERT INTO posting_journal SET ?';
const REMOVE_JOURNAL_ROW = 'DELETE FROM posting_journal WHERE uuid = ?;';
const UPDATE_JOURNAL_ROW = 'UPDATE posting_journal SET ? WHERE uuid = ?;';
const INSERT_JOURNAL_ROW = 'INSERT INTO posting_journal SET ?;';
const UPDATE_TRANSACTION_HISTORY = 'INSERT INTO transaction_history SET ?;';

const transaction = db.transaction();
const recordUuid = req.params.record_uuid;
Expand Down Expand Up @@ -274,6 +277,17 @@ function editTransaction(req, res, next) {
db.convert(row, ['entity_uuid']);
transaction.addQuery(UPDATE_JOURNAL_ROW, [row, db.bid(uid)]);
});

// record the transaction history once the transaction has been updated.
const row = _transactionToEdit[0];
const transactionHistory = {
uuid : db.bid(uuid.v4()),
record_uuid : db.bid(row.record_uuid),
user_id : req.session.user.id,
};

transaction.addQuery(UPDATE_TRANSACTION_HISTORY, [transactionHistory]);

return transaction.execute();
})
.then(() => {
Expand All @@ -283,7 +297,8 @@ function editTransaction(req, res, next) {
.then((updatedRows) => {
res.status(200).json(updatedRows);
})
.catch(next);
.catch(next)
.done();

// 1. make changes with update methods ('SET ?') etc.
// 2. run changes through trial balance
Expand Down Expand Up @@ -528,6 +543,7 @@ function getTransactionDate(changedRows = {}, oldRows) {
.pop();
}


/**
* PUT /journal/comments
*
Expand Down
17 changes: 17 additions & 0 deletions server/models/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1926,3 +1926,20 @@ CREATE TABLE `stock_consumption` (
KEY `depot_uuid` (`depot_uuid`),
KEY `period_id` (`period_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


/*
The transaction_history table stores the editing history of transactions that
have gone through the posting process. The record_uuid should be the same
record_uuid as that found in the posting_journal/general_ledger.
*/
CREATE TABLE `transaction_history` (
`uuid` BINARY(16) NOT NULL,
`record_uuid` BINARY(16) NOT NULL,
`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`user_id` SMALLINT(5) UNSIGNED NOT NULL,
PRIMARY KEY (`uuid`),
KEY `record_uuid` (`record_uuid`),
KEY `user_id` (`user_id`),
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

0 comments on commit df82035

Please sign in to comment.