Skip to content

Commit

Permalink
Add date filtering to balance log
Browse files Browse the repository at this point in the history
fixes MAN-2104
  • Loading branch information
sipec committed Dec 5, 2024
1 parent 78eb474 commit 656aeb2
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 116 deletions.
37 changes: 25 additions & 12 deletions backend/api/src/get-balance-changes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,36 @@ import { convertContract } from 'common/supabase/contracts'
export const getBalanceChanges: APIHandler<'get-balance-changes'> = async (
props
) => {
const { after, userId } = props
const { userId, before, after } = props
const [betBalanceChanges, txnBalanceChanges] = await Promise.all([
getBetBalanceChanges(after, userId),
getTxnBalanceChanges(after, userId),
getBetBalanceChanges(before, after, userId),
getTxnBalanceChanges(before, after, userId),
])
return orderBy(
const allChanges = orderBy(
[...betBalanceChanges, ...txnBalanceChanges],
(change) => change.createdTime,
'desc'
)
return allChanges
}

const getTxnBalanceChanges = async (after: number, userId: string) => {
const getTxnBalanceChanges = async (
before: number | undefined,
after: number,
userId: string
) => {
const pg = createSupabaseDirectClient()
const balanceChanges = [] as TxnBalanceChange[]

const txns = await pg.map(
`select *
from txns
where created_time > millis_to_ts($1)
and (to_id = $2 or from_id = $2)
where
($1 is null or created_time < millis_to_ts($1)) and
created_time >= millis_to_ts($2)
and (to_id = $3 or from_id = $3)
order by created_time`,
[after, userId],
[before, after, userId],
convertTxn
)
const contractIds = filterDefined(
Expand Down Expand Up @@ -113,7 +120,11 @@ const getContractIdFromTxn = (txn: Txn) => {
return null
}

const getBetBalanceChanges = async (after: number, userId: string) => {
const getBetBalanceChanges = async (
before: number | undefined,
after: number,
userId: string
) => {
const pg = createSupabaseDirectClient()
const contractToBets: {
[contractId: string]: {
Expand All @@ -133,11 +144,13 @@ const getBetBalanceChanges = async (after: number, userId: string) => {
from contract_bets cb
join contracts c on cb.contract_id = c.id
left join answers a on a.id = cb.answer_id
where cb.updated_time > millis_to_ts($1)
and cb.user_id = $2
where
($1 is null or cb.updated_time < millis_to_ts($1))
and cb.updated_time >= millis_to_ts($2)
and cb.user_id = $3
group by c.id;
`,
[after, userId],
[before, after, userId],
(row) => {
contractToBets[row.id] = {
bets: orderBy(row.bets, (bet) => bet.createdTime, 'asc'),
Expand Down
3 changes: 2 additions & 1 deletion common/src/api/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1342,7 +1342,8 @@ export const API = (_apiTypeCheck = {
returns: [] as AnyBalanceChangeType[],
props: z
.object({
after: z.coerce.number(),
before: z.coerce.number().optional(),
after: z.coerce.number().default(0),
userId: z.string(),
})
.strict(),
Expand Down
Loading

0 comments on commit 656aeb2

Please sign in to comment.