games_app/backend/src/repositories/exchangeFundingRepository.ts

117 lines
3.6 KiB
TypeScript

import { query } from '../db/connection';
import { encryptSensitive } from '../utils/crypto';
export type ExchangeFundingRow = {
id: string;
wallet_type_id: number;
direction: string;
amount: string;
blockchain_tx_id: string | null;
exchange_name: string | null;
status: string;
approved_by: string | null;
from_address: string | null;
to_address: string | null;
notes_encrypted: string | null;
created_at: Date;
updated_at: Date;
};
export async function createFunding(input: {
walletTypeId: number;
direction: 'cold_to_hot' | 'hot_to_cold' | 'hot_to_cold_gas' | 'cold_to_hot_gas';
amount: bigint;
fromAddress?: string;
toAddress?: string;
notes?: string;
requestedBy?: string;
approvedBy?: string;
}): Promise<ExchangeFundingRow> {
const rows = await query<ExchangeFundingRow[]>(
`INSERT INTO exchange_funding (wallet_type_id, direction, amount, status, from_address, to_address, notes_encrypted, approved_by, requested_by)
VALUES ($1, $2, $3, 'pending', $4, $5, $6, $7, $8)
RETURNING *, amount::text`,
[
input.walletTypeId,
input.direction,
input.amount.toString(),
input.fromAddress ?? null,
input.toAddress ?? null,
input.notes ? encryptSensitive(input.notes) : null,
input.approvedBy ?? null,
input.requestedBy ?? null,
]
);
return rows[0];
}
export async function listFunding(opts: {
status?: string;
limit?: number;
offset?: number;
}): Promise<{ items: ExchangeFundingRow[]; total: number }> {
const limit = Math.min(opts.limit ?? 50, 100);
const offset = Math.max(opts.offset ?? 0, 0);
const where = opts.status ? 'WHERE status = $1' : '';
const countParams = opts.status ? [opts.status] : [];
const countRows = await query<{ count: string }[]>(
`SELECT COUNT(*)::text AS count FROM exchange_funding ${where}`,
countParams
);
const items = opts.status
? await query<ExchangeFundingRow[]>(
`SELECT *, amount::text FROM exchange_funding WHERE status = $1 ORDER BY created_at DESC LIMIT $2 OFFSET $3`,
[opts.status, limit, offset]
)
: await query<ExchangeFundingRow[]>(
`SELECT *, amount::text FROM exchange_funding ORDER BY created_at DESC LIMIT $1 OFFSET $2`,
[limit, offset]
);
return { items, total: Number(countRows[0]?.count ?? 0) };
}
export async function findFundingById(id: string): Promise<ExchangeFundingRow | null> {
const rows = await query<ExchangeFundingRow[]>(
'SELECT *, amount::text FROM exchange_funding WHERE id = $1',
[id]
);
return rows[0] ?? null;
}
export async function confirmFunding(
id: string,
blockchainTxId: string,
approvedBy: string
): Promise<ExchangeFundingRow | null> {
const rows = await query<ExchangeFundingRow[]>(
`UPDATE exchange_funding
SET status = 'completed', blockchain_tx_id = $2, approved_by = $3, updated_at = NOW()
WHERE id = $1 AND status = 'pending'
RETURNING *, amount::text`,
[id, blockchainTxId, approvedBy]
);
return rows[0] ?? null;
}
export async function completeFundingBroadcast(
id: string,
blockchainTxId: string,
approvedBy: string
): Promise<ExchangeFundingRow | null> {
const rows = await query<ExchangeFundingRow[]>(
`UPDATE exchange_funding
SET status = 'completed', blockchain_tx_id = $2, approved_by = $3, updated_at = NOW()
WHERE id = $1
RETURNING *, amount::text`,
[id, blockchainTxId, approvedBy]
);
return rows[0] ?? null;
}
export async function cancelFunding(id: string): Promise<void> {
await query(
`UPDATE exchange_funding SET status = 'cancelled', updated_at = NOW() WHERE id = $1 AND status = 'pending'`,
[id]
);
}