42 lines
2.1 KiB
JavaScript
42 lines
2.1 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.create = create;
|
|
exports.deleteByTransactionId = deleteByTransactionId;
|
|
exports.getLockedAmount = getLockedAmount;
|
|
const connection_1 = require("../db/connection");
|
|
/**
|
|
* Repository for withdrawal balance locks.
|
|
* Reserves funds while a withdrawal is pending to prevent double-spending.
|
|
*/
|
|
/**
|
|
* Creates a withdrawal lock for a user and wallet type.
|
|
* @param data - Lock details to persist.
|
|
* @param data.userId - ID of the user whose funds are locked.
|
|
* @param data.walletTypeId - Wallet type the lock applies to.
|
|
* @param data.transactionId - Associated transaction ID, or null for pre-transaction locks.
|
|
* @param data.amount - Amount locked in the smallest currency unit.
|
|
* @param data.expiresAt - Optional expiration time after which the lock may be released.
|
|
* @returns Resolves when the lock row has been inserted.
|
|
*/
|
|
async function create(data) {
|
|
await (0, connection_1.query)('INSERT INTO withdrawal_locks (user_id, wallet_type_id, transaction_id, amount, expires_at) VALUES ($1, $2, $3, $4, $5)', [data.userId, data.walletTypeId, data.transactionId, data.amount.toString(), data.expiresAt ?? null]);
|
|
}
|
|
/**
|
|
* Removes a withdrawal lock by its associated transaction ID.
|
|
* @param transactionId - ID of the transaction whose lock should be released.
|
|
* @returns Resolves when matching lock rows have been deleted.
|
|
*/
|
|
async function deleteByTransactionId(transactionId) {
|
|
await (0, connection_1.query)('DELETE FROM withdrawal_locks WHERE transaction_id = $1', [transactionId]);
|
|
}
|
|
/**
|
|
* Sums all active locked amounts for a user and wallet type.
|
|
* @param userId - ID of the user to query.
|
|
* @param walletTypeId - Wallet type to sum locks for.
|
|
* @returns Total locked amount in the smallest currency unit.
|
|
*/
|
|
async function getLockedAmount(userId, walletTypeId) {
|
|
const rows = await (0, connection_1.query)('SELECT COALESCE(SUM(amount), 0)::text as sum FROM withdrawal_locks WHERE user_id = $1 AND wallet_type_id = $2', [userId, walletTypeId]);
|
|
return BigInt(rows[0]?.sum ?? '0');
|
|
}
|
|
//# sourceMappingURL=withdrawalLockRepository.js.map
|