diff options
| author | Ranabir Guin <[email protected]> | 2021-05-14 23:06:03 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2021-06-09 16:05:31 -0700 |
| commit | 4f39e4a10804347e89a30201bade1a42e9df0ef0 (patch) | |
| tree | 51ef1f626674cefa0942cb146a9ac1b79c024b70 | |
| parent | ffc9d98b58e2af91bd9c6b063e6be88663f7b8c7 (diff) | |
| download | coinos-server-4f39e4a10804347e89a30201bade1a42e9df0ef0.tar.gz coinos-server-4f39e4a10804347e89a30201bade1a42e9df0ef0.zip | |
try join queries; order normalized models to enable HasMany, BelongsTo specs in applicable models (enabling all table specific logic to be in one file)
| -rw-r--r-- | db/index.js | 44 | ||||
| -rw-r--r-- | routes/admin.js | 47 |
2 files changed, 54 insertions, 37 deletions
diff --git a/db/index.js b/db/index.js index 37394ecf..658a51a0 100644 --- a/db/index.js +++ b/db/index.js @@ -21,22 +21,29 @@ require("./models/orders.js"); require("./models/users.js"); require("./models/withdrawals.js"); -// require("./normalized/accounts.js"); +// Require models in order (to enable FK relationship specs in models) + +// require("./normalized/users.js"); +// require("./normalized/user_keys.js"); +// require("./normalized/user_preferences.js"); + +require("./normalized/referrals.js"); +require("./normalized/waiting_list.js"); + +// require("./normalized/subscriptions.js"); +// require("./normalized/user_subscriptions.js"); + // require("./normalized/assets.js"); // require("./normalized/codes.js"); // require("./normalized/currencies.js"); -// require("./normalized/deposits.js"); -// require("./normalized/institutions.js"); // require("./normalized/networks.js"); +// require("./normalized/institutions.js"); + +// require("./normalized/accounts.js"); // require("./normalized/non_custodial_accounts.js"); -// require("./normalized/payments.js"); -require("./normalized/referrals.js"); -// require("./normalized/user_keys.js"); -// require("./normalized/user_preferences.js"); -// require("./normalized/user_subscriptions.js"); -// require("./normalized/users.js"); -require("./normalized/waiting_list.js"); +// require("./normalized/deposits.js"); // require("./normalized/withdrawals.js"); +// require("./normalized/payments.js"); // ** Need to add ? *** // require("./normalized/orders.js"); @@ -44,10 +51,11 @@ require("./normalized/waiting_list.js"); const { User, Account, Payment, Invoice, Key, Order } = db; -User.hasMany(Account, { - as: "accounts", - foreignKey: "user_id" -}); +// move relationships to specfic models +// User.hasMany(Account, { +// as: "accounts", +// foreignKey: "user_id" +// }); User.hasMany(Invoice, { as: "invoices", @@ -74,10 +82,10 @@ Invoice.belongsTo(Account, { foreignKey: "account_id" }); -Account.belongsTo(User, { - as: "user", - foreignKey: "user_id" -}); +// Account.belongsTo(User, { +// as: "user", +// foreignKey: "user_id" +// }); Invoice.belongsTo(User, { as: "user", diff --git a/routes/admin.js b/routes/admin.js index 0d065a4d..b17e5aa4 100644 --- a/routes/admin.js +++ b/routes/admin.js @@ -39,29 +39,38 @@ router.get( router.get( "/accounts", ah(async (req, res) => { - // var referrals = await db.User - // .findAll({ - // attributes: ['users.id', 'accounts.balance'], - // where: { - // '$accounts.user_id$' : '$users.id$' - // // user_id: users.id - // // $or: [ - // // {'$accounts.user_id$' : $users.id$}, - // // ] - // }, - // include: [ - // { - // model: Account, - // required: false - // } - // ] - // }); - - var accounts = await db.Account.findAll() + var accounts = await db.Accounts.findAll({ + include: [ db.User ] + }) debug('accounts: ' + JSON.stringify(accounts)) return res.send({accounts: accounts}) }) ); +// usage: GET '/user/transactions' +// +// Returns: +// { users: [ +// <user attributes>, +// Invoices: +// <invoice attributes>, +// Payments: +// <invoice attributes>, +// Orders: +// <invoice attributes>, +// ], ... +// } +router.get( + "/user_transactions", + ah(async (req, res) => { + var transactions = await db.User.findAll({ + include: [ db.Invoice, db.Payment, db.Order ] + }) + + debug('transactions: ' + JSON.stringify(transactions)) + return res.send({transactions: transactions}) + }) +); + module.exports = router; |
