Skip to content

Commit

Permalink
Some fixes to settlement window queries (#21)
Browse files Browse the repository at this point in the history
* Some fixes to settlement window queries

* Improved result for settlement window info query

* update settlements endpoint in windows model

Co-authored-by: Aarón Reynoza <ldaaron38@gmail.com>
  • Loading branch information
partiallyordered and Aarón Reynoza committed Jan 27, 2020
1 parent b23e8db commit 881eccb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 24 deletions.
57 changes: 39 additions & 18 deletions src/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ const MYSQL_MIN_DATETIME = '1000-01-01';
const MYSQL_MAX_DATETIME = '9999-12-31';

const previousSettlementWindowDataQuery = `
SELECT id, MAX(payments) AS payments, MAX(receipts) AS receipts, MAX(numPayments) AS numPayments, MAX(numReceipts) AS numReceipts, curr, ANY_VALUE(open), ANY_VALUE(close) FROM
SELECT id,
MAX(payments) AS payments,
MAX(receipts) AS receipts,
MAX(numPayments) AS numPayments,
MAX(numReceipts) AS numReceipts,
curr,
MIN(open) AS open,
MIN(close) AS close FROM
(
(
SELECT
Expand All @@ -14,8 +21,8 @@ const previousSettlementWindowDataQuery = `
0 AS numPayments,
0 AS numReceipts,
c.currencyId AS curr,
swOpen.createdDate AS open,
swClose.createdDate AS close
MIN(swOpen.createdDate) AS open,
MIN(swClose.createdDate) AS close
FROM central_ledger.settlementWindow sw
INNER JOIN central_ledger.settlementWindowStateChange AS swClose ON swClose.settlementWindowId = sw.settlementWindowId
INNER JOIN central_ledger.settlementWindowStateChange AS swOpen ON swOpen.settlementWindowId = sw.settlementWindowId
Expand All @@ -29,6 +36,7 @@ const previousSettlementWindowDataQuery = `
) AS c
WHERE swClose.settlementWindowStateId = 'CLOSED'
AND swOpen.settlementWindowStateId = 'OPEN'
GROUP BY id, curr
)
UNION
(
Expand All @@ -38,9 +46,9 @@ const previousSettlementWindowDataQuery = `
SUM(CASE WHEN tprt.name = 'PAYEE_DFSP' THEN q.amount ELSE 0 END) as receipts,
COUNT(CASE WHEN tprt.name = 'PAYER_DFSP' THEN q.amount ELSE NULL END) as numPayments,
COUNT(CASE WHEN tprt.name = 'PAYEE_DFSP' THEN q.amount ELSE NULL END) as numReceipts,
ANY_VALUE(q.currencyId) AS curr,
ANY_VALUE(swOpen.createdDate) AS open,
swClose.createdDate AS close
q.currencyId AS curr,
MIN(swOpen.createdDate) AS open,
MIN(swClose.createdDate) AS close
FROM central_ledger.settlementWindowStateChange AS swClose
INNER JOIN central_ledger.settlementWindowStateChange AS swOpen ON swOpen.settlementWindowId = swClose.settlementWindowId
INNER JOIN central_ledger.transferFulfilment AS tf ON swClose.settlementWindowId = tf.settlementWindowId
Expand All @@ -52,8 +60,7 @@ const previousSettlementWindowDataQuery = `
WHERE p.name = ?
AND swClose.settlementWindowStateId = 'CLOSED'
AND swOpen.settlementWindowStateId = 'OPEN'
GROUP BY swClose.settlementWindowId
ORDER BY swClose.createdDate DESC
GROUP BY id, curr
)
ORDER BY open
) T
Expand All @@ -67,6 +74,7 @@ const previousSettlementWindowDataQuery = `
LIMIT 1
)
GROUP BY id, curr
ORDER BY close DESC
`;


Expand Down Expand Up @@ -156,7 +164,15 @@ const positionQuery = `
`;

const historicalSettlementWindowDataQuery = `
SELECT id, MAX(payments) AS payments, MAX(receipts) AS receipts, MAX(numPayments) AS numPayments, MAX(numReceipts) AS numReceipts, curr, ANY_VALUE(open), ANY_VALUE(close) FROM
SELECT
id,
MAX(payments) AS payments,
MAX(receipts) AS receipts,
MAX(numPayments) AS numPayments,
MAX(numReceipts) AS numReceipts,
curr,
MIN(open) AS open,
MIN(close) AS close FROM
(
(
SELECT
Expand All @@ -166,8 +182,8 @@ const historicalSettlementWindowDataQuery = `
0 AS numPayments,
0 AS numReceipts,
c.currencyId AS curr,
swOpen.createdDate AS open,
swClose.createdDate AS close
MIN(swOpen.createdDate) AS open,
MIN(swClose.createdDate) AS close
FROM central_ledger.settlementWindow sw
INNER JOIN central_ledger.settlementWindowStateChange AS swClose ON swClose.settlementWindowId = sw.settlementWindowId
INNER JOIN central_ledger.settlementWindowStateChange AS swOpen ON swOpen.settlementWindowId = sw.settlementWindowId
Expand All @@ -181,6 +197,7 @@ const historicalSettlementWindowDataQuery = `
) AS c
WHERE swClose.settlementWindowStateId = 'CLOSED'
AND swOpen.settlementWindowStateId = 'OPEN'
GROUP BY id, curr
)
UNION
(
Expand All @@ -190,9 +207,9 @@ const historicalSettlementWindowDataQuery = `
SUM(CASE WHEN tprt.name = 'PAYEE_DFSP' THEN q.amount ELSE 0 END) as receipts,
COUNT(CASE WHEN tprt.name = 'PAYER_DFSP' THEN q.amount ELSE NULL END) as numPayments,
COUNT(CASE WHEN tprt.name = 'PAYEE_DFSP' THEN q.amount ELSE NULL END) as numReceipts,
ANY_VALUE(q.currencyId) AS curr,
ANY_VALUE(swOpen.createdDate) AS open,
swClose.createdDate AS close
q.currencyId AS curr,
MIN(swOpen.createdDate) AS open,
MIN(swClose.createdDate) AS close
FROM central_ledger.settlementWindowStateChange AS swClose
INNER JOIN central_ledger.settlementWindowStateChange AS swOpen ON swOpen.settlementWindowId = swClose.settlementWindowId
INNER JOIN central_ledger.transferFulfilment AS tf ON swClose.settlementWindowId = tf.settlementWindowId
Expand All @@ -204,13 +221,13 @@ const historicalSettlementWindowDataQuery = `
WHERE p.name = ?
AND swClose.settlementWindowStateId = 'CLOSED'
AND swOpen.settlementWindowStateId = 'OPEN'
GROUP BY swClose.settlementWindowId
GROUP BY id, curr
ORDER BY swClose.createdDate DESC
)
ORDER BY open
) T
) T
WHERE T.open > ? AND T.close < ?
GROUP BY id, curr
ORDER BY open
`;

const historicalParticipantPositionQuery = `
Expand Down Expand Up @@ -313,7 +330,11 @@ const historicalParticipantLimitQuery = `
`;

const settlementWindowInfoQuery = `
SELECT totals.settlementWindowId, totals.settlementWindowStateId AS settlementWindowStateId, sum(totals.amount) AS amount, GROUP_CONCAT(totals.currencyId),
SELECT
totals.settlementWindowId,
totals.settlementWindowStateId AS settlementWindowStateId,
SUM(totals.amount) AS amount,
GROUP_CONCAT(DISTINCT(totals.currencyId)) as currencyId,
DATE_FORMAT(MIN(swOpen.createdDate), '%Y-%m-%dT%T.000Z') AS settlementWindowOpen,
DATE_FORMAT(MIN(swClose.createdDate), '%Y-%m-%dT%T.000Z') AS settlementWindowClose
FROM (
Expand Down
10 changes: 4 additions & 6 deletions src/handlers/settlement-windows.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ const handler = (router, routesContext) => {
});

router.get('/settlement-windows/:settlementWindowId', async (ctx, next) => {
const currentSettlementWindowId = await routesContext.db.getCurrentSettlementWindowId();
const thisSettlementWindowId = parseInt(ctx.params.settlementWindowId);
const settlementWindow = await routesContext.db.getSettlementWindowInfo(ctx.params.settlementWindowId);

if (currentSettlementWindowId !== thisSettlementWindowId) {
const api = new Model({ endpoint: routesContext.config.centralLedgerEndpoint });
try {
const api = new Model({ endpoint: routesContext.config.settlementsEndpoint });
const settlement = await api.getSettlements({ settlementWindowId: ctx.params.settlementWindowId });
settlementWindow.settlement = (settlement.length === 1 ? settlement[0] : {});
} else {
} catch(error) {
routesContext.log(error);
settlementWindow.settlement = {};
}
ctx.response.body = settlementWindow;
Expand Down

0 comments on commit 881eccb

Please sign in to comment.