95 lines
5.1 KiB
JavaScript
95 lines
5.1 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.create = create;
|
|
exports.updateStatus = updateStatus;
|
|
exports.findByBlockchainTxId = findByBlockchainTxId;
|
|
exports.findById = findById;
|
|
exports.listByUserId = listByUserId;
|
|
exports.countByUserId = countByUserId;
|
|
const connection_1 = require("../db/connection");
|
|
/**
|
|
* Repository for blockchain transaction records.
|
|
* Persists deposits, withdrawals, and transfers with status and confirmation tracking.
|
|
*/
|
|
/**
|
|
* Inserts a new transaction record.
|
|
* @param data - Transaction fields to persist.
|
|
* @param data.userId - ID of the user who owns the transaction.
|
|
* @param data.walletTypeId - Wallet type for this transaction.
|
|
* @param data.type - Transaction kind: deposit, withdrawal, or transfer.
|
|
* @param data.amount - Amount in the smallest currency unit.
|
|
* @param data.fee - Optional fee amount (defaults to 0).
|
|
* @param data.status - Current transaction status.
|
|
* @param data.blockchainTxId - Optional on-chain transaction hash or ID.
|
|
* @param data.fromAddress - Optional sender address.
|
|
* @param data.toAddress - Optional recipient address.
|
|
* @param data.confirmations - Optional current confirmation count.
|
|
* @param data.requiredConfirmations - Optional confirmations required before completion.
|
|
* @param data.metadata - Optional additional context as key-value pairs.
|
|
* @returns The newly created transaction record.
|
|
*/
|
|
async function create(data) {
|
|
const rows = await (0, connection_1.query)(`INSERT INTO transactions (user_id, wallet_type_id, type, amount, fee, status, blockchain_tx_id, from_address, to_address, confirmations, required_confirmations, metadata)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
|
|
RETURNING id, user_id, wallet_type_id, type, amount::text, fee::text, status, blockchain_tx_id, from_address, to_address, confirmations, required_confirmations, metadata, created_at, updated_at`, [
|
|
data.userId,
|
|
data.walletTypeId,
|
|
data.type,
|
|
data.amount.toString(),
|
|
(data.fee ?? 0).toString(),
|
|
data.status,
|
|
data.blockchainTxId ?? null,
|
|
data.fromAddress ?? null,
|
|
data.toAddress ?? null,
|
|
data.confirmations ?? null,
|
|
data.requiredConfirmations ?? null,
|
|
data.metadata ? JSON.stringify(data.metadata) : null,
|
|
]);
|
|
return rows[0];
|
|
}
|
|
/**
|
|
* Updates a transaction's status and optionally its confirmations or blockchain TX ID.
|
|
* @param id - ID of the transaction to update.
|
|
* @param status - New status value.
|
|
* @param confirmations - Optional confirmation count (existing value kept if omitted).
|
|
* @param blockchainTxId - Optional blockchain TX ID (existing value kept if omitted).
|
|
* @returns Resolves when the row has been updated.
|
|
*/
|
|
async function updateStatus(id, status, confirmations, blockchainTxId) {
|
|
await (0, connection_1.query)('UPDATE transactions SET status = $1, confirmations = COALESCE($2, confirmations), blockchain_tx_id = COALESCE($3, blockchain_tx_id), updated_at = NOW() WHERE id = $4', [status, confirmations ?? null, blockchainTxId ?? null, id]);
|
|
}
|
|
/**
|
|
* Finds a transaction by its on-chain transaction ID.
|
|
* @param blockchainTxId - Blockchain transaction hash or ID.
|
|
* @returns The matching transaction, or null if not found.
|
|
*/
|
|
async function findByBlockchainTxId(blockchainTxId) {
|
|
const rows = await (0, connection_1.query)('SELECT id, user_id, wallet_type_id, type, amount::text, fee::text, status, blockchain_tx_id, from_address, to_address, confirmations, required_confirmations, metadata, created_at, updated_at FROM transactions WHERE blockchain_tx_id = $1', [blockchainTxId]);
|
|
return rows[0] || null;
|
|
}
|
|
/**
|
|
* Finds a transaction by its primary key.
|
|
* @param id - Transaction UUID.
|
|
* @returns The matching transaction, or null if not found.
|
|
*/
|
|
async function findById(id) {
|
|
const rows = await (0, connection_1.query)('SELECT id, user_id, wallet_type_id, type, amount::text, fee::text, status, blockchain_tx_id, from_address, to_address, confirmations, required_confirmations, metadata, created_at, updated_at FROM transactions WHERE id = $1', [id]);
|
|
return rows[0] || null;
|
|
}
|
|
/**
|
|
* Lists a user's transactions ordered by creation time (newest first).
|
|
* @param userId - ID of the user whose transactions to fetch.
|
|
* @param limit - Maximum number of rows to return (default 50).
|
|
* @param offset - Number of rows to skip for pagination (default 0).
|
|
* @returns Array of transaction records.
|
|
*/
|
|
async function listByUserId(userId, limit = 50, offset = 0) {
|
|
return (0, connection_1.query)(`SELECT id, user_id, wallet_type_id, type, amount::text, fee::text, status, blockchain_tx_id, from_address, to_address, confirmations, required_confirmations, metadata, created_at, updated_at
|
|
FROM transactions WHERE user_id = $1 ORDER BY created_at DESC LIMIT $2 OFFSET $3`, [userId, limit, offset]);
|
|
}
|
|
/** Total transaction count for a user (pagination). */
|
|
async function countByUserId(userId) {
|
|
const rows = await (0, connection_1.query)('SELECT COUNT(*)::text AS count FROM transactions WHERE user_id = $1', [userId]);
|
|
return parseInt(rows[0]?.count ?? '0', 10);
|
|
}
|
|
//# sourceMappingURL=transactionRepository.js.map
|