Skip to content

Commit

Permalink
fix: join on debtor/creditor for entity lookup
Browse files Browse the repository at this point in the history
Instead of implementing #3514 which would require massive migration
scripts, we simply JOIN the debtor/creditor on the entity_map.  It less
performant, but doesn't require a breaking change.
  • Loading branch information
jniles committed Feb 12, 2024
1 parent f770b2c commit d419ec4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 22 deletions.
32 changes: 21 additions & 11 deletions server/controllers/finance/creditors.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const q = require('q');
const moment = require('moment');
const db = require('../../lib/db');
const NotFound = require('../../lib/errors/NotFound');
const FilterParser = require('../../lib/filter');

exports.list = list;
exports.detail = detail;
Expand All @@ -26,21 +27,30 @@ exports.getFinancialActivity = getFinancialActivity;
* @todo integration tests for this function
*/
function list(req, res, next) {
const options = req.query;

db.convert(options, ['uuid', 'group_uuid']);
const filters = new FilterParser(options, { tableAlias : 'c' });

const sql = `
SELECT BUID(c.uuid) as uuid, c.text, cg.name, BUID(c.group_uuid) as group_uuid,
a.id AS account_id, a.number, map.text as hr_entity
FROM creditor AS c
JOIN creditor_group AS cg
JOIN account AS a ON c.group_uuid = cg.uuid AND cg.account_id = a.id
LEFT JOIN entity_map map ON map.uuid = c.uuid;
a.id AS account_id, a.number, em.text as hrEntity
FROM creditor AS c
JOIN creditor_group AS cg
JOIN account AS a ON c.group_uuid = cg.uuid AND cg.account_id = a.id
LEFT JOIN entity_map me ON em.uuid = c.uuid
`;

db.exec(sql)
.then((rows) => {
res.status(200).json(rows);
})
.catch(next)
.done();
filters.equals('uuid');
filters.equals('creditor_uuid');
filters.custom('hrEntity', 'em.text = ?');

const query = filters.applyQuery(sql);
const parameters = filters.parameters();

db.exec(query, parameters)
.then(rows => { res.status(200).json(rows); })
.catch(next);
}

/**
Expand Down
25 changes: 18 additions & 7 deletions server/controllers/finance/debtors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
const _ = require('lodash');
const db = require('../../../lib/db');
const NotFound = require('../../../lib/errors/NotFound');
const FilterParser = require('../../../lib/filter');

const CAUTION_LINK_TYPE_ID = 19;

Expand All @@ -34,17 +35,26 @@ exports.getFinancialActivity = getFinancialActivity;
* List of debtors
*/
function list(req, res, next) {
const options = req.query;

db.convert(options, ['uuid', 'group_uuid']);
const filters = new FilterParser(options, { tableAlias : 'd' });

const sql = `
SELECT BUID(d.uuid) AS uuid, BUID(d.group_uuid) AS group_uuid,
d.text, map.text as hr_entity
FROM debtor d
JOIN entity_map map ON map.uuid = d.uuid;
d.text, em.text as hrEntity
FROM debtor d JOIN entity_map em ON em.uuid = d.uuid
`;

db.exec(sql)
.then((rows) => {
res.status(200).send(rows);
})
filters.equals('uuid');
filters.equals('group_uuid');
filters.custom('hrEntity', 'em.text = ?');

const query = filters.applyQuery(sql);
const parameters = filters.parameters();

db.exec(query, parameters)
.then(rows => { res.status(200).json(rows); })
.catch(next);
}

Expand All @@ -68,6 +78,7 @@ async function update(req, res, next) {
const historicSQL = `INSERT INTO debtor_group_history SET ?`;
const { user } = req.session;
const data = _.clone(req.body);

// delete the uuid if it exists
delete data.uuid;

Expand Down
12 changes: 8 additions & 4 deletions server/controllers/finance/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ function getRecordUuidByTextBulk(hrRecords) {
*/
function getEntityUuidByText(hrEntity) {
const sql = `
SELECT uuid FROM entity_map WHERE text = ?;
SELECT em.uuid FROM entity_map em JOIN debtor ON em.uuid = debtor.uuid WHERE em.text = ?
UNION
SELECT em.uuid FROM entity_map em JOIN creditor ON em.uuid = creditor.uuid WHERE em.text = ?;
`;

return db.one(sql, hrEntity);
return db.one(sql, [hrEntity, hrEntity]);
}

/**
Expand All @@ -80,10 +82,12 @@ function getEntityUuidByText(hrEntity) {
*/
function getEntityUuidByTextBulk(hrEntities) {
const sql = `
SELECT uuid, text FROM entity_map WHERE text IN (?);
SELECT em.uuid FROM entity_map em JOIN debtor ON em.uuid = debtor.uuid WHERE em.text IN (?)
UNION
SELECT em.uuid FROM entity_map em JOIN creditor ON em.uuid = creditor.uuid WHERE em.text IN (?);
`;

return db.exec(sql, [hrEntities]);
return db.exec(sql, [hrEntities, hrEntities]);
}

/**
Expand Down

0 comments on commit d419ec4

Please sign in to comment.