Skip to content

Commit

Permalink
feat: allow sorting accounts endpoint by balance
Browse files Browse the repository at this point in the history
  • Loading branch information
trashman committed Jan 16, 2025
1 parent 49ca1cf commit c4b1221
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions api/routes/accounts/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(',') : []

Expand All @@ -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) {
Expand Down Expand Up @@ -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'))
Expand Down

0 comments on commit c4b1221

Please sign in to comment.