Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated parties to support account information. #50

Merged
merged 3 commits into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions src/audit-resolve.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
{
"1300|nyc>istanbul-reports>handlebars": {
"fix": 1
"decisions": {
"1300|nyc>istanbul-reports>handlebars": {
"madeAt": 0,
"decision": "fix"
},
"1316|nyc>istanbul-reports>handlebars": {
"madeAt": 0,
"decision": "fix"
},
"1324|nyc>istanbul-reports>handlebars": {
"madeAt": 0,
"decision": "fix"
},
"1325|nyc>istanbul-reports>handlebars": {
"madeAt": 0,
"decision": "fix"
},
"1500|npm-audit-resolver>yargs-unparser>yargs>yargs-parser": {
"decision": "ignore",
"madeAt": 1590450688208,
"expiresAt": 1593042654156
},
"1500|npm-audit-resolver>audit-resolve-core>yargs-parser": {
"decision": "ignore",
"madeAt": 1590450692169,
"expiresAt": 1593042654156
}
},
"1316|nyc>istanbul-reports>handlebars": {
"fix": 1
},
"1324|nyc>istanbul-reports>handlebars": {
"fix": 1
},
"1325|nyc>istanbul-reports>handlebars": {
"fix": 1
}
"rules": {},
"version": 1
}
2 changes: 1 addition & 1 deletion src/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ services:
- "3500:3000"
- "3501:4000"
depends_on:
- redis
- redis
13 changes: 13 additions & 0 deletions src/models/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
******/
'use strict';

const partyAccountsTable = 'account';
const partyTable = 'party';
const quoteTable = 'quote';
const transactionRequestTable = 'transactionRequest';
Expand Down Expand Up @@ -78,6 +79,16 @@ CREATE TABLE IF NOT EXISTS ${transferTable} (
)
`;

const createAccountTable = `
CREATE TABLE IF NOT EXISTS ${partyAccountsTable} (
address TEXT NOT NULL PRIMARY KEY,
currency TEXT NOT NULL,
description TEXT NOT NULL,
idValue TEXT NOT NULL,
FOREIGN KEY (idValue) REFERENCES party(idValue) ON DELETE CASCADE
)
`;

module.exports = {
partyTable,
quoteTable,
Expand All @@ -89,4 +100,6 @@ module.exports = {
transferTable,
partyExtensionTable,
createPartyExtensionTable,
partyAccountsTable,
createAccountTable,
};
2 changes: 2 additions & 0 deletions src/models/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const {
createQuoteTable,
createTransactionRequestTable,
createPartyExtensionTable,
createAccountTable,
} = require('./constants');

/**
Expand Down Expand Up @@ -89,6 +90,7 @@ module.exports = class Model {
await this.db.run(createTransactionRequestTable);
await this.db.run(createTransferTable);
await this.db.run(createPartyExtensionTable);
await this.db.run(createAccountTable);

this.party = new Party(this.db);
this.quote = new Quote(this.db);
Expand Down
49 changes: 46 additions & 3 deletions src/models/party.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* @description Defines the party model structure and operations within the simulator.
*/

const { partyTable, partyExtensionTable } = require('./constants');
const { partyTable, partyExtensionTable, partyAccountsTable } = require('./constants');


/**
Expand All @@ -51,7 +51,12 @@ module.exports = class Party {
*/

async get(idType, idValue) {
const res = await this.db.all(`SELECT p.displayName, p.firstName, p.middleName, p.lastName, p.dateOfBirth, p.idType, p.idValue, pe.key, pe.value FROM ${partyTable} p LEFT JOIN ${partyExtensionTable} pe ON p.idValue = pe.idValue WHERE p.idType = ? AND p.idValue = ?`, [idType, idValue]);
const res = await this.db.all(`
SELECT p.displayName, p.firstName, p.middleName, p.lastName, p.dateOfBirth, p.idType, p.idValue, pe.key, pe.value, pa.address, pa.currency, pa.description
FROM ${partyTable} p
LEFT JOIN ${partyExtensionTable} pe ON p.idValue = pe.idValue
LEFT JOIN ${partyAccountsTable} pa ON p.idValue = pa.idValue
WHERE p.idType = ? AND p.idValue = ?`, [idType, idValue]);
const resultMap = {};
res.forEach((row) => {
let party;
Expand All @@ -75,6 +80,16 @@ module.exports = class Party {
}
party.extensionList.push({ key: row.key, value: row.value });
}
if (row.address) {
if (!party.accounts) {
party.accounts = [];
}
party.accounts.push({
address: row.address,
currency: row.currency,
description: row.description,
});
}
});
if (res.length && res.length > 0) {
return Object.values(resultMap)[0];
Expand All @@ -89,7 +104,11 @@ module.exports = class Party {
* @returns {Promise<Object>} Party object.
*/
async getAll() {
const res = await this.db.all(`SELECT p.displayName, p.firstName, p.middleName, p.lastName, p.dateOfBirth, p.idType, p.idValue, pe.key, pe.value FROM ${partyTable} p LEFT JOIN ${partyExtensionTable} pe ON p.idValue = pe.idValue`);
const res = await this.db.all(`
SELECT p.displayName, p.firstName, p.middleName, p.lastName, p.dateOfBirth, p.idType, p.idValue, pe.key, pe.value, pa.address, pa.currency, pa.description
FROM ${partyTable} p
LEFT JOIN ${partyExtensionTable} pe ON p.idValue = pe.idValue
LEFT JOIN ${partyAccountsTable} pa ON p.idValue = pa.idValue`);
const resultMap = {};
res.forEach((row) => {
let party;
Expand All @@ -113,6 +132,16 @@ module.exports = class Party {
}
party.extensionList.push({ key: row.key, value: row.value });
}
if (row.address) {
if (!party.accounts) {
party.accounts = [];
}
party.accounts.push({
address: row.address,
currency: row.currency,
description: row.description,
});
}
});
return Object.values(resultMap);
}
Expand Down Expand Up @@ -141,6 +170,13 @@ module.exports = class Party {
[idValue, extension.key, extension.value]);
});
}
if (party.accounts) {
const { accounts } = party;
await Promise.all(accounts.map(async (account) => this.db.get(`
INSERT INTO ${partyAccountsTable} (idValue, address, currency, description)
VALUES (?, ?, ?, ?)`,
[idValue, account.address, account.currency, account.description])));
}
}


Expand Down Expand Up @@ -181,6 +217,13 @@ module.exports = class Party {
[idValue, extension.key, extension.value]);
});
}
if (newParty.accounts) {
const { accounts } = newParty;
await Promise.all(accounts.map(async (account) => this.db.run(`
INSERT OR IGNORE INTO ${partyAccountsTable} (idValue, address, currency, description)
VALUES (?, ?, ?, ?);`,
[idValue, account.address, account.currency, account.description])));
}
}

/**
Expand Down
26 changes: 26 additions & 0 deletions src/test-api/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ components:
maxLength: 128
extensionList:
$ref: '#/components/schemas/ExtensionList'
accounts:
$ref: '#/components/schemas/AccountList'
Extension:
type: object
required:
Expand All @@ -193,6 +195,30 @@ components:
$ref: '#/components/schemas/Extension'
title: ExtensionList
description: Data model for the complex type ExtensionList
Account:
type: object
required:
- address
- currency
- description
properties:
address:
type: string
description: The routable address of this account.
currency:
type: string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be the CurrencyCode enum, but I'm happy to leave it as is for now, since that enum would double the size of this file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

description: The currency of the account.
description:
type: string
description: The name of the account.
title: Account
description: Data model for the complex type Account
AccountList:
type: array
items:
$ref: '#/components/schemas/Account'
title: AccountList
description: Data model for the complex type accountList
idType:
type: string
enum:
Expand Down
12 changes: 12 additions & 0 deletions src/test/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ const partyCreate = {
value: '12345343',
},
],
accounts: [
{
currency: 'USD',
description: 'savings',
address: 'moja.blue.8f027046-b82a-4fa9-838b-514514543785',
},
{
currency: 'USD',
description: 'checking',
address: 'moja.blue.8f027046-b82a-4fa9-838b-70210fcf8137',
},
],
};


Expand Down
12 changes: 12 additions & 0 deletions src/test/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ test('create and update a party', async (t) => {
value: '12345343',
},
],
accounts: [
{
currency: 'USD',
description: 'savings',
address: 'moja.blue.8f027046-b82a-4fa9-838b-100000000000',
},
{
currency: 'USD',
description: 'savings',
address: 'moja.blue.8f027046-b82a-4fa9-838b-200000000000',
},
],
};
await model.party.create(partyCreate);
const orig = await model.party.get(idType, idValue);
Expand Down