-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
resolves MAN-1724
- Loading branch information
Showing
7 changed files
with
259 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,13 @@ | ||
import { millisToTs } from 'common/supabase/utils' | ||
import { type APIHandler } from './helpers/endpoint' | ||
import { createSupabaseDirectClient } from 'shared/supabase/init' | ||
import { convertTxn } from 'common/supabase/txns' | ||
import { buildArray } from 'common/util/array' | ||
import { | ||
from, | ||
limit, | ||
orderBy, | ||
renderSql, | ||
select, | ||
where, | ||
} from 'shared/supabase/sql-builder' | ||
import { getTxnsMain } from './get-txns' | ||
import { ManaPayTxn } from 'common/txn' | ||
|
||
export const getManagrams: APIHandler<'managrams'> = async (props) => { | ||
const { limit: limitValue, toId, fromId, before, after } = props | ||
const txns = await getTxnsMain({ | ||
...props, | ||
offset: 0, | ||
category: 'MANA_PAYMENT', | ||
}) | ||
|
||
const pg = createSupabaseDirectClient() | ||
|
||
const conditions = buildArray( | ||
where('category = ${category}', { category: 'MANA_PAYMENT' }), | ||
before && where('created_time < ${before}', { before: millisToTs(before) }), | ||
after && where('created_time > ${after}', { after: millisToTs(after) }), | ||
toId && where('to_id = ${toId}', { toId }), | ||
fromId && where('from_id = ${fromId}', { fromId }) | ||
) | ||
|
||
const query = renderSql( | ||
select('*'), | ||
from('txns'), | ||
...conditions, | ||
orderBy('created_time desc'), | ||
limitValue && limit(limitValue) | ||
) | ||
|
||
return (await pg.map(query, [], convertTxn)) as ManaPayTxn[] | ||
return txns as ManaPayTxn[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { APIHandler } from 'api/helpers/endpoint' | ||
import { convertTxn } from 'common/supabase/txns' | ||
import { millisToTs } from 'common/supabase/utils' | ||
import { createSupabaseDirectClient } from 'shared/supabase/init' | ||
import { | ||
from, | ||
limit, | ||
orderBy, | ||
renderSql, | ||
select, | ||
where, | ||
} from 'shared/supabase/sql-builder' | ||
|
||
export const getTxns: APIHandler<'txns'> = async (props) => { | ||
return await getTxnsMain(props) | ||
} | ||
|
||
export const getTxnsMain = async (props: { | ||
token?: string | ||
offset: number | ||
limit: number | ||
before?: number | ||
after?: number | ||
toId?: string | ||
fromId?: string | ||
category?: string | ||
}) => { | ||
const { | ||
token, | ||
offset, | ||
limit: limitValue, | ||
before, | ||
after, | ||
toId, | ||
fromId, | ||
category, | ||
} = props | ||
|
||
const pg = createSupabaseDirectClient() | ||
|
||
const query = renderSql( | ||
select('*'), | ||
from('txns'), | ||
|
||
token && where('token = ${token}', { token }), | ||
before && where('created_time < ${before}', { before: millisToTs(before) }), | ||
after && where('created_time > ${after}', { after: millisToTs(after) }), | ||
toId && where('to_id = ${toId}', { toId }), | ||
fromId && where('from_id = ${fromId}', { fromId }), | ||
category && where('category = ${category}', { category }), | ||
|
||
orderBy('created_time desc'), | ||
limit(limitValue, offset) | ||
) | ||
|
||
return await pg.map(query, [], convertTxn) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { supabaseConsoleTxnPath } from 'common/envs/constants' | ||
import { type Txn } from 'common/txn' | ||
import { formatMoney } from 'common/util/format' | ||
import { useCallback } from 'react' | ||
import { Button } from 'web/components/buttons/button' | ||
import { Col } from 'web/components/layout/col' | ||
import { Page } from 'web/components/layout/page' | ||
import { Row } from 'web/components/layout/row' | ||
import { Avatar } from 'web/components/widgets/avatar' | ||
import { Table } from 'web/components/widgets/table' | ||
import { Title } from 'web/components/widgets/title' | ||
import { UserLink } from 'web/components/widgets/user-link' | ||
import { useAdmin } from 'web/hooks/use-admin' | ||
import { usePagination } from 'web/hooks/use-pagination' | ||
import { useDisplayUserById } from 'web/hooks/use-user-supabase' | ||
import { api } from 'web/lib/api/api' | ||
import { formatTime } from 'web/lib/util/time' | ||
|
||
export default function CashTxnsPage() { | ||
const fetch = useCallback( | ||
({ limit, offset }: { limit: number; offset: number }) => | ||
api('txns', { limit, offset, token: 'CASH' }), | ||
[] | ||
) | ||
|
||
const pagination = usePagination({ | ||
pageSize: 50, | ||
prefix: [] as Txn[], | ||
q: fetch, | ||
}) | ||
|
||
// TODO: it's actually ok for anyone to see this page | ||
const isAdmin = useAdmin() | ||
if (!isAdmin) return <></> | ||
|
||
return ( | ||
<Page trackPageView={false}> | ||
<Col className="gap-4"> | ||
<Row className="items-start justify-between"> | ||
<Title>Cash Transactions</Title> | ||
<div className="flex gap-1"> | ||
<Button onClick={pagination.getPrev} disabled={pagination.isStart}> | ||
Previous | ||
</Button> | ||
<Button onClick={pagination.getNext}>Next</Button> | ||
</div> | ||
</Row> | ||
<Table className="w-full"> | ||
<thead> | ||
<tr> | ||
<th>ID</th> | ||
<th>From</th> | ||
<th>To</th> | ||
<th>Amount</th> | ||
<th>Category</th> | ||
<th>Created At</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{pagination.items.map((txn) => ( | ||
<TxnRow key={txn.id} txn={txn} /> | ||
))} | ||
</tbody> | ||
</Table> | ||
<div className="flex items-end gap-1"> | ||
<Button onClick={pagination.getPrev} disabled={pagination.isStart}> | ||
Previous | ||
</Button> | ||
<Button onClick={pagination.getNext}>Next</Button> | ||
</div> | ||
</Col> | ||
</Page> | ||
) | ||
} | ||
|
||
const TxnRow = ({ txn }: { txn: Txn }) => { | ||
const a = useDisplayUserById(txn.fromId) | ||
const b = useDisplayUserById(txn.toId) | ||
|
||
return ( | ||
<tr> | ||
<td> | ||
<a | ||
className="text-primary-700 hover-underline cursor-pointer" | ||
href={supabaseConsoleTxnPath(txn.id)} | ||
> | ||
{txn.id} | ||
</a> | ||
</td> | ||
<td> | ||
{txn.fromType === 'USER' && a ? ( | ||
<Row className="gap-1"> | ||
<Avatar username={a.username} avatarUrl={a.avatarUrl} size="xs" /> | ||
<UserLink user={a} /> | ||
</Row> | ||
) : ( | ||
txn.fromId | ||
)} | ||
</td> | ||
<td> | ||
{txn.toType === 'USER' && b ? ( | ||
<Row className="gap-1"> | ||
<Avatar username={b.username} avatarUrl={b.avatarUrl} size="xs" /> | ||
<UserLink user={b} /> | ||
</Row> | ||
) : ( | ||
txn.toId | ||
)} | ||
</td> | ||
<td>{formatMoney(txn.amount, 'CASH')}</td> | ||
<td>{txn.category}</td> | ||
<td>{formatTime(txn.createdTime)}</td> | ||
</tr> | ||
) | ||
} |