125 lines
5.1 KiB
JavaScript
125 lines
5.1 KiB
JavaScript
"use strict";
|
|
/**
|
|
* Fund operation approval requests — unified queue for sensitive platform fund ops.
|
|
*/
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.createRequest = createRequest;
|
|
exports.findById = findById;
|
|
exports.listRequests = listRequests;
|
|
exports.markApproved = markApproved;
|
|
exports.markProcessing = markProcessing;
|
|
exports.markRejected = markRejected;
|
|
exports.markExecuted = markExecuted;
|
|
exports.markConfirmed = markConfirmed;
|
|
exports.markFailed = markFailed;
|
|
exports.markCancelled = markCancelled;
|
|
exports.touchApprovedBy = touchApprovedBy;
|
|
const connection_1 = require("../db/connection");
|
|
function normalizeRow(row) {
|
|
if (row.status === 'executed') {
|
|
return { ...row, status: 'confirmed' };
|
|
}
|
|
return row;
|
|
}
|
|
async function createRequest(input) {
|
|
const m = input.meta ?? {};
|
|
const rows = await (0, connection_1.query)(`INSERT INTO fund_operation_requests (
|
|
operation_type, payload, requested_by,
|
|
policy_id, policy_snapshot, required_quorum,
|
|
amount_raw, asset_code, network, from_address, to_address
|
|
) VALUES (
|
|
$1, $2::jsonb, $3, $4, $5::jsonb, $6::jsonb, $7, $8, $9, $10, $11
|
|
)
|
|
RETURNING *`, [
|
|
input.operationType,
|
|
JSON.stringify(input.payload),
|
|
input.requestedBy,
|
|
m.policyId ?? null,
|
|
m.policySnapshot != null ? JSON.stringify(m.policySnapshot) : null,
|
|
m.requiredQuorum != null ? JSON.stringify(m.requiredQuorum) : null,
|
|
m.amountRaw ?? null,
|
|
m.assetCode ?? null,
|
|
m.network ?? null,
|
|
m.fromAddress ?? null,
|
|
m.toAddress ?? null,
|
|
]);
|
|
return normalizeRow(rows[0]);
|
|
}
|
|
async function findById(id) {
|
|
const rows = await (0, connection_1.query)(`SELECT * FROM fund_operation_requests WHERE id = $1`, [id]);
|
|
return rows[0] ? normalizeRow(rows[0]) : null;
|
|
}
|
|
async function listRequests(filters) {
|
|
const where = [];
|
|
const values = [];
|
|
let i = 1;
|
|
if (filters.status) {
|
|
if (filters.status === 'confirmed') {
|
|
where.push(`status IN ('confirmed', 'executed')`);
|
|
}
|
|
else {
|
|
where.push(`status = $${i++}`);
|
|
values.push(filters.status);
|
|
}
|
|
}
|
|
if (filters.operationType) {
|
|
where.push(`operation_type = $${i++}`);
|
|
values.push(filters.operationType);
|
|
}
|
|
const whereSql = where.length ? `WHERE ${where.join(' AND ')}` : '';
|
|
const limit = Math.min(filters.limit ?? 50, 200);
|
|
const offset = filters.offset ?? 0;
|
|
const countRows = await (0, connection_1.query)(`SELECT COUNT(*)::text AS count FROM fund_operation_requests ${whereSql}`, values);
|
|
const items = await (0, connection_1.query)(`SELECT * FROM fund_operation_requests ${whereSql}
|
|
ORDER BY created_at DESC
|
|
LIMIT $${i++} OFFSET $${i}`, [...values, limit, offset]);
|
|
return { items: items.map(normalizeRow), total: parseInt(countRows[0]?.count ?? '0', 10) };
|
|
}
|
|
async function markApproved(id, approvedBy) {
|
|
const rows = await (0, connection_1.query)(`UPDATE fund_operation_requests
|
|
SET status = 'approved', approved_by = $2, approved_at = NOW()
|
|
WHERE id = $1 AND status = 'pending_approval'
|
|
RETURNING *`, [id, approvedBy]);
|
|
return rows[0] ? normalizeRow(rows[0]) : null;
|
|
}
|
|
async function markProcessing(id) {
|
|
const rows = await (0, connection_1.query)(`UPDATE fund_operation_requests
|
|
SET status = 'processing'
|
|
WHERE id = $1 AND status IN ('approved', 'pending_approval')
|
|
RETURNING *`, [id]);
|
|
return rows[0] ? normalizeRow(rows[0]) : null;
|
|
}
|
|
async function markRejected(id, approvedBy, reason) {
|
|
const rows = await (0, connection_1.query)(`UPDATE fund_operation_requests
|
|
SET status = 'rejected', approved_by = $2, rejection_reason = $3, approved_at = NOW()
|
|
WHERE id = $1 AND status = 'pending_approval'
|
|
RETURNING *`, [id, approvedBy, reason]);
|
|
return rows[0] ? normalizeRow(rows[0]) : null;
|
|
}
|
|
/** @deprecated prefer markConfirmed — kept for callers; writes status confirmed */
|
|
async function markExecuted(id, executionResult) {
|
|
await markConfirmed(id, executionResult);
|
|
}
|
|
async function markConfirmed(id, executionResult) {
|
|
await (0, connection_1.query)(`UPDATE fund_operation_requests
|
|
SET status = 'confirmed', executed_at = NOW(), execution_result = $2::jsonb
|
|
WHERE id = $1`, [id, JSON.stringify(executionResult)]);
|
|
}
|
|
async function markFailed(id, errorMessage) {
|
|
await (0, connection_1.query)(`UPDATE fund_operation_requests
|
|
SET status = 'failed', executed_at = NOW(), execution_result = $2::jsonb
|
|
WHERE id = $1`, [id, JSON.stringify({ error: errorMessage })]);
|
|
}
|
|
async function markCancelled(id) {
|
|
const rows = await (0, connection_1.query)(`UPDATE fund_operation_requests
|
|
SET status = 'cancelled'
|
|
WHERE id = $1 AND status = 'pending_approval'
|
|
RETURNING *`, [id]);
|
|
return rows[0] ? normalizeRow(rows[0]) : null;
|
|
}
|
|
async function touchApprovedBy(id, approvedBy) {
|
|
await (0, connection_1.query)(`UPDATE fund_operation_requests
|
|
SET approved_by = $2, approved_at = COALESCE(approved_at, NOW())
|
|
WHERE id = $1`, [id, approvedBy]);
|
|
}
|
|
//# sourceMappingURL=fundOperationRequestRepository.js.map
|