games_app/backend/src/repositories/depositSweepBatchRepository.ts
2026-07-02 09:21:05 +02:00

112 lines
3.2 KiB
TypeScript

/**
* Batched deposit sweeps awaiting super-admin approval.
*/
import { query } from '../db/connection';
export type SweepBatchStatus =
| 'pending_batch'
| 'pending_approval'
| 'approved'
| 'executing'
| 'completed'
| 'failed'
| 'cancelled';
export type DepositSweepBatchRow = {
id: string;
status: SweepBatchStatus;
sweep_count: number;
fund_request_id: string | null;
created_at: Date;
updated_at: Date;
};
export async function createBatch(): Promise<DepositSweepBatchRow> {
const rows = await query<DepositSweepBatchRow[]>(
`INSERT INTO deposit_sweep_batches (status, sweep_count) VALUES ('pending_batch', 0) RETURNING *`
);
return rows[0];
}
export async function getOrCreateOpenBatch(): Promise<DepositSweepBatchRow> {
const rows = await query<DepositSweepBatchRow[]>(
`SELECT * FROM deposit_sweep_batches
WHERE status = 'pending_batch'
ORDER BY created_at ASC
LIMIT 1`
);
if (rows[0]) return rows[0];
return createBatch();
}
export async function incrementBatchCount(batchId: string): Promise<void> {
await query(
`UPDATE deposit_sweep_batches
SET sweep_count = sweep_count + 1, updated_at = NOW()
WHERE id = $1`,
[batchId]
);
}
export async function linkFundRequest(batchId: string, fundRequestId: string): Promise<void> {
await query(
`UPDATE deposit_sweep_batches
SET status = 'pending_approval', fund_request_id = $2, updated_at = NOW()
WHERE id = $1`,
[batchId, fundRequestId]
);
}
export async function markBatchStatus(batchId: string, status: SweepBatchStatus): Promise<void> {
await query(
`UPDATE deposit_sweep_batches SET status = $2, updated_at = NOW() WHERE id = $1`,
[batchId, status]
);
}
export async function findById(id: string): Promise<DepositSweepBatchRow | null> {
const rows = await query<DepositSweepBatchRow[]>(`SELECT * FROM deposit_sweep_batches WHERE id = $1`, [
id,
]);
return rows[0] ?? null;
}
export async function listSweepIdsInBatch(batchId: string): Promise<string[]> {
const rows = await query<{ id: string }[]>(
`SELECT id FROM deposit_sweeps WHERE batch_id = $1 AND status = 'pending'`,
[batchId]
);
return rows.map((r) => r.id);
}
export async function countPendingUnbatchedSweeps(): Promise<number> {
const rows = await query<{ count: string }[]>(
`SELECT COUNT(*)::text AS count FROM deposit_sweeps WHERE batch_id IS NULL AND status = 'pending'`
);
return parseInt(rows[0]?.count ?? '0', 10);
}
export async function queryOpenBatchesReadyForApproval(): Promise<DepositSweepBatchRow[]> {
return query<DepositSweepBatchRow[]>(
`SELECT * FROM deposit_sweep_batches
WHERE status = 'pending_batch' AND sweep_count > 0 AND fund_request_id IS NULL
ORDER BY created_at ASC`
);
}
export async function closeBatchForNewSweeps(batchId: string): Promise<DepositSweepBatchRow> {
await query(
`UPDATE deposit_sweep_batches SET status = 'pending_approval', updated_at = NOW() WHERE id = $1`,
[batchId]
);
const rows = await query<DepositSweepBatchRow[]>(`SELECT * FROM deposit_sweep_batches WHERE id = $1`, [
batchId,
]);
return rows[0];
}
export async function createFreshBatch(): Promise<DepositSweepBatchRow> {
return createBatch();
}