Skip to content

Commit

Permalink
Make OR clause for transfers with same from/to
Browse files Browse the repository at this point in the history
If the same account is specified for both `from` and `to`, we
return all the incoming and outgoing transfers for that account.
  • Loading branch information
0237h committed Jun 21, 2024
1 parent 077cd9c commit 1d4ff66
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export async function makeUsageQuery(ctx: Context, endpoint: UsageEndpoints, use
page = 1;

let filters = "";
for (const k of Object.keys(query_params).filter(k => k !== "limit")) // Don't add limit to WHERE clause
filters += ` (${k} == {${k}: String}) AND`;
for (const k of Object.keys(query_params).filter(k => k !== "limit")) // Don't add `limit` to WHERE clause
filters += ` (${k} = {${k}: String}) AND`;
filters = filters.substring(0, filters.lastIndexOf(' ')); // Remove last item ` AND`

if (filters.length)
Expand Down Expand Up @@ -48,6 +48,13 @@ export async function makeUsageQuery(ctx: Context, endpoint: UsageEndpoints, use

const q = query_params as ValidUserParams<typeof endpoint>;
if (q.from) {
// Find all incoming and outgoing transfers from single account
if (q.to && q.to === q.from)
filters = filters.replace(
"(from = {from: String}) AND (to = {to: String})",
"((from = {from: String}) OR (to = {to: String}))",
)

query += `transfers_from`;
} else if (q.to) {
query += `transfers_to`;
Expand Down

0 comments on commit 1d4ff66

Please sign in to comment.