110 lines
5.1 KiB
JavaScript
110 lines
5.1 KiB
JavaScript
"use strict";
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
}
|
|
Object.defineProperty(o, k2, desc);
|
|
}) : (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
o[k2] = m[k];
|
|
}));
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
}) : function(o, v) {
|
|
o["default"] = v;
|
|
});
|
|
var __importStar = (this && this.__importStar) || (function () {
|
|
var ownKeys = function(o) {
|
|
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
var ar = [];
|
|
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
return ar;
|
|
};
|
|
return ownKeys(o);
|
|
};
|
|
return function (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
__setModuleDefault(result, mod);
|
|
return result;
|
|
};
|
|
})();
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.findByDepositTransactionId = findByDepositTransactionId;
|
|
exports.createSweep = createSweep;
|
|
exports.listSweeps = listSweeps;
|
|
exports.listPendingSweeps = listPendingSweeps;
|
|
exports.assignSweepToBatch = assignSweepToBatch;
|
|
exports.summarizeInProgressSweeps = summarizeInProgressSweeps;
|
|
exports.updateSweepStatus = updateSweepStatus;
|
|
const connection_1 = require("../db/connection");
|
|
async function findByDepositTransactionId(depositTransactionId) {
|
|
const rows = await (0, connection_1.query)('SELECT *, amount::text FROM deposit_sweeps WHERE deposit_transaction_id = $1', [depositTransactionId]);
|
|
return rows[0] ?? null;
|
|
}
|
|
async function createSweep(input) {
|
|
const rows = await (0, connection_1.query)(`INSERT INTO deposit_sweeps (user_id, wallet_type_id, from_address, to_cold_address, amount, deposit_transaction_id, batch_id, status)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, 'pending')
|
|
RETURNING *, amount::text`, [
|
|
input.userId,
|
|
input.walletTypeId,
|
|
input.fromAddress,
|
|
input.toColdAddress,
|
|
input.amount.toString(),
|
|
input.depositTransactionId ?? null,
|
|
input.batchId ?? null,
|
|
]);
|
|
return rows[0];
|
|
}
|
|
async function listSweeps(opts) {
|
|
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 (0, connection_1.query)(`SELECT COUNT(*)::text AS count FROM deposit_sweeps ${where}`, countParams);
|
|
const items = opts.status
|
|
? await (0, connection_1.query)(`SELECT *, amount::text FROM deposit_sweeps WHERE status = $1 ORDER BY created_at DESC LIMIT $2 OFFSET $3`, [opts.status, limit, offset])
|
|
: await (0, connection_1.query)(`SELECT *, amount::text FROM deposit_sweeps ORDER BY created_at DESC LIMIT $1 OFFSET $2`, [limit, offset]);
|
|
return { items, total: Number(countRows[0]?.count ?? 0) };
|
|
}
|
|
async function listPendingSweeps(limit = 20, opts) {
|
|
if (opts?.batchId) {
|
|
return (0, connection_1.query)(`SELECT *, amount::text FROM deposit_sweeps
|
|
WHERE batch_id = $1 AND status IN ('pending', 'gas_funding', 'broadcasting')
|
|
ORDER BY created_at ASC LIMIT $2`, [opts.batchId, limit]);
|
|
}
|
|
const batchFilter = opts?.unbatchedOnly ? 'AND batch_id IS NULL' : '';
|
|
return (0, connection_1.query)(`SELECT *, amount::text FROM deposit_sweeps
|
|
WHERE status IN ('pending', 'gas_funding', 'broadcasting') ${batchFilter}
|
|
ORDER BY created_at ASC LIMIT $1`, [limit]);
|
|
}
|
|
async function assignSweepToBatch(sweepId, batchId) {
|
|
await (0, connection_1.query)(`UPDATE deposit_sweeps SET batch_id = $2, updated_at = NOW() WHERE id = $1`, [sweepId, batchId]);
|
|
}
|
|
async function summarizeInProgressSweeps() {
|
|
const rows = await (0, connection_1.query)(`SELECT COUNT(*)::text AS count, COALESCE(SUM(amount), 0)::text AS total
|
|
FROM deposit_sweeps
|
|
WHERE status IN ('pending', 'gas_funding', 'broadcasting')`);
|
|
return {
|
|
count: Number(rows[0]?.count ?? 0),
|
|
totalAmountRaw: rows[0]?.total ?? '0',
|
|
};
|
|
}
|
|
async function updateSweepStatus(id, status, opts) {
|
|
const { encryptSensitive } = await Promise.resolve().then(() => __importStar(require('../utils/crypto')));
|
|
await (0, connection_1.query)(`UPDATE deposit_sweeps
|
|
SET status = $2,
|
|
blockchain_tx_id = COALESCE($3, blockchain_tx_id),
|
|
error_message_encrypted = COALESCE($4, error_message_encrypted),
|
|
updated_at = NOW()
|
|
WHERE id = $1`, [
|
|
id,
|
|
status,
|
|
opts?.blockchainTxId ?? null,
|
|
opts?.errorMessage ? encryptSensitive(opts.errorMessage) : null,
|
|
]);
|
|
}
|
|
//# sourceMappingURL=depositSweepRepository.js.map
|