"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createAdjustment = createAdjustment; exports.listAdjustments = listAdjustments; exports.findById = findById; exports.attachTransactionId = attachTransactionId; const connection_1 = require("../db/connection"); const crypto_1 = require("../utils/crypto"); async function createAdjustment(input) { const rows = await (0, connection_1.query)(`INSERT INTO ledger_adjustments (user_id, wallet_type_id, adjustment_type, amount, reason_encrypted, approved_by, transaction_id) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING id, user_id, wallet_type_id, adjustment_type, amount::text, reason_encrypted, approved_by, transaction_id, created_at`, [ input.userId, input.walletTypeId, input.adjustmentType, input.amount.toString(), (0, crypto_1.encryptSensitive)(input.reason), input.approvedBy, input.transactionId ?? null, ]); return rows[0]; } async function listAdjustments(opts) { const limit = Math.min(opts.limit ?? 50, 100); const offset = Math.max(opts.offset ?? 0, 0); const countRows = opts.userId ? await (0, connection_1.query)('SELECT COUNT(*)::text AS count FROM ledger_adjustments WHERE user_id = $1', [opts.userId]) : await (0, connection_1.query)('SELECT COUNT(*)::text AS count FROM ledger_adjustments'); const items = opts.userId ? await (0, connection_1.query)(`SELECT id, user_id, wallet_type_id, adjustment_type, amount::text, reason_encrypted, approved_by, transaction_id, created_at FROM ledger_adjustments WHERE user_id = $1 ORDER BY created_at DESC LIMIT $2 OFFSET $3`, [opts.userId, limit, offset]) : await (0, connection_1.query)(`SELECT id, user_id, wallet_type_id, adjustment_type, amount::text, reason_encrypted, approved_by, transaction_id, created_at FROM ledger_adjustments ORDER BY created_at DESC LIMIT $1 OFFSET $2`, [limit, offset]); return { items, total: Number(countRows[0]?.count ?? 0) }; } async function findById(id) { const rows = await (0, connection_1.query)(`SELECT id, user_id, wallet_type_id, adjustment_type, amount::text, reason_encrypted, approved_by, transaction_id, created_at FROM ledger_adjustments WHERE id = $1`, [id]); return rows[0] ?? null; } async function attachTransactionId(id, transactionId) { await (0, connection_1.query)('UPDATE ledger_adjustments SET transaction_id = $1 WHERE id = $2', [transactionId, id]); } //# sourceMappingURL=ledgerAdjustmentRepository.js.map