From c4b12211593fa9d57b4cc9343d9aa647aa49c740 Mon Sep 17 00:00:00 2001 From: trashman Date: Thu, 16 Jan 2025 11:53:29 -0500 Subject: [PATCH] feat: allow sorting accounts endpoint by balance --- api/routes/accounts/index.mjs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/api/routes/accounts/index.mjs b/api/routes/accounts/index.mjs index e27f377..b4766d6 100644 --- a/api/routes/accounts/index.mjs +++ b/api/routes/accounts/index.mjs @@ -9,7 +9,14 @@ const router = express.Router({ mergeParams: true }) router.get('/?', async (req, res) => { const { logger, cache, db } = req.app.locals try { - let { tags, accounts, limit = 100, offset = 0 } = req.query + let { + tags, + accounts, + limit = 100, + offset = 0, + order_by, + order_by_direction = 'desc' + } = req.query // Validate limit limit = parseInt(limit, 10) @@ -27,6 +34,13 @@ router.get('/?', async (req, res) => { .send({ error: 'Offset must be a positive integer' }) } + // validate order_by_direction + if (order_by_direction !== 'asc' && order_by_direction !== 'desc') { + return res + .status(400) + .send({ error: 'Order by direction must be asc or desc' }) + } + let accounts_array = accounts ? accounts.toLowerCase().split(',') : [] let tags_array = tags ? tags.toLowerCase().split(',') : [] @@ -38,7 +52,7 @@ router.get('/?', async (req, res) => { if (accounts_array.length === 0 && tags_array.length === 0) { cache_key = `/accounts/offset/${offset}/limit/${limit}` } else if (accounts_array.length === 0) { - cache_key = `/accounts//tags/${tags_array.join( + cache_key = `/accounts/tags/${tags_array.join( ',' )}/offset/${offset}/limit/${limit}` } else if (tags_array.length === 0) { @@ -74,6 +88,12 @@ router.get('/?', async (req, res) => { query.whereIn('accounts_tags.tag', tags_array) } + switch (order_by) { + case 'balance': + query.orderBy('balance', order_by_direction) + break + } + // Add a query to get all tags for each account query .select(db.raw('ARRAY_AGG(accounts_tags.tag) as tags'))