Treasury & Bank Transfer Management System — سامانه مدیریت خزانهداری و حوالههای بانکی
feature/reconciliation-guard-20261013
ci/public-windows-bridge-probe-20261017
ci/windows-dispatch-bootstrap-20261016
ci/windows-dual-app-lab-20261016
dev/v7-production-failclosed-20261021
feature/reconciliation-guard-20261013
main
phase-1-foundation
phase-1-implementation
phase-2-treasury-core
phase-3-banking-reconciliation
phase-4-operations-enterprise-ux
phase-5-hardening-release
phase-6-production-platform
phase-7-production-infrastructure-bank-golive
phase-7-sql-readonly-ux-20261015
transfer-exe-to-drive-20261006
| 1 | /** |
| 2 | * Pure, offline TreasuryFlow bank-reconciliation transition guard. |
| 3 | * It DOES NOT send transfers, call a bank, authenticate evidence or write to a DB. |
| 4 | * Only a server-side trusted adapter may produce evidence with verifiedBy. |
| 5 | */ |
| 6 | import { createHash } from 'node:crypto'; |
| 7 | |
| 8 | export class ReconciliationError extends Error { |
| 9 | constructor(code, message) { |
| 10 | super(message); |
| 11 | this.name = 'ReconciliationError'; |
| 12 | this.code = code; |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | const assert = (condition, code, message) => { |
| 17 | if (!condition) throw new ReconciliationError(code, message); |
| 18 | }; |
| 19 | const validText = (value) => typeof value === 'string' && value.length > 0 && value.trim() === value; |
| 20 | const validMoney = (value) => typeof value === 'string' && /^(0|[1-9][0-9]*)$/.test(value); |
| 21 | const validCurrency = (value) => typeof value === 'string' && /^[A-Z]{3}$/.test(value); |
| 22 | const allowedStatuses = new Set(['READY', 'DISPATCHING', 'UNKNOWN', 'BANK_ACCEPTED', 'SETTLED', 'BANK_REJECTED']); |
| 23 | const allowedKinds = new Set(['ACCEPTED', 'SETTLED', 'REJECTED']); |
| 24 | |
| 25 | const stableDigest = (proof) => createHash('sha256').update(JSON.stringify([ |
| 26 | proof.evidenceId, proof.kind, proof.paymentId, proof.amountMinor, |
| 27 | proof.currency, proof.bankReference, proof.verifiedBy, proof.observedAt, |
| 28 | proof.definitive === true, |
| 29 | ])).digest('hex'); |
| 30 | |
| 31 | /** |
| 32 | * Uses a verified response from a trusted server-side banking adapter to move |
| 33 | * an already-dispatched payment toward a terminal state. |
| 34 | * The caller must commit the result with an atomic version check. |
| 35 | */ |
| 36 | export function applyVerifiedBankEvidence(payment, proof) { |
| 37 | assert(payment && typeof payment === 'object', 'INVALID_PAYMENT', 'Payment snapshot missing'); |
| 38 | assert(validText(payment.id), 'INVALID_PAYMENT', 'Payment ID missing'); |
| 39 | assert(allowedStatuses.has(payment.status), 'INVALID_PAYMENT', 'Payment status unknown'); |
| 40 | assert(validMoney(payment.amountMinor), 'INVALID_PAYMENT', 'Payment amount must be decimal minor units'); |
| 41 | assert(validCurrency(payment.currency), 'INVALID_PAYMENT', 'Payment currency must be ISO-like uppercase code'); |
| 42 | assert(Number.isSafeInteger(payment.version) && payment.version >= 0, 'INVALID_PAYMENT', 'Version missing'); |
| 43 | assert(Array.isArray(payment.appliedProofs), 'INVALID_PAYMENT', 'Applied proofs missing'); |
| 44 | assert(proof && typeof proof === 'object', 'INVALID_PROOF', 'Bank evidence missing'); |
| 45 | assert(validText(proof.evidenceId) && validText(proof.paymentId), 'INVALID_PROOF', 'Evidence ID/payment ID missing'); |
| 46 | assert(allowedKinds.has(proof.kind), 'INVALID_PROOF', 'Evidence kind unknown'); |
| 47 | assert(validMoney(proof.amountMinor), 'INVALID_PROOF', 'Evidence amount invalid'); |
| 48 | assert(validCurrency(proof.currency), 'INVALID_PROOF', 'Evidence currency invalid'); |
| 49 | assert(validText(proof.verifiedBy), 'UNVERIFIED_EVIDENCE', 'Only trusted adapters may verify bank evidence'); |
| 50 | assert(validText(proof.observedAt) && !Number.isNaN(Date.parse(proof.observedAt)), 'INVALID_PROOF', 'Evidence timestamp invalid'); |
| 51 | assert(payment.id === proof.paymentId, 'PAYMENT_MISMATCH', 'Evidence belongs to another payment'); |
| 52 | assert(payment.amountMinor === proof.amountMinor, 'AMOUNT_MISMATCH', 'Evidence amount differs from payment'); |
| 53 | assert(payment.currency === proof.currency, 'CURRENCY_MISMATCH', 'Evidence currency differs from payment'); |
| 54 | if (proof.kind === 'SETTLED' || proof.kind === 'ACCEPTED') { |
| 55 | assert(validText(proof.bankReference), 'MISSING_BANK_REFERENCE', 'Bank reference required'); |
| 56 | } |
| 57 | if (proof.kind === 'REJECTED') { |
| 58 | assert(proof.definitive === true, 'NON_DEFINITIVE_REJECTION', 'A transient bank failure is not a final rejection'); |
| 59 | } |
| 60 | |
| 61 | const digest = stableDigest(proof); |
| 62 | const existing = payment.appliedProofs.find((event) => event?.evidenceId === proof.evidenceId); |
| 63 | if (existing) { |
| 64 | assert(existing.digest === digest, 'EVIDENCE_ID_COLLISION', 'Evidence ID reused with changed content'); |
| 65 | return { payment, changed: false, reason: 'DUPLICATE_EVIDENCE' }; |
| 66 | } |
| 67 | assert(payment.status !== 'READY', 'NOT_DISPATCHED', 'Payment has no recorded dispatch attempt'); |
| 68 | assert(payment.status !== 'SETTLED' && payment.status !== 'BANK_REJECTED', 'TERMINAL_CONFLICT', 'Terminal payment requires manual conflict investigation'); |
| 69 | if (proof.kind === 'ACCEPTED' && payment.status === 'BANK_ACCEPTED' && payment.bankReference !== proof.bankReference) { |
| 70 | throw new ReconciliationError('BANK_REFERENCE_CONFLICT', 'Conflicting bank references require manual investigation'); |
| 71 | } |
| 72 | if (payment.bankReference && proof.bankReference && payment.bankReference !== proof.bankReference) { |
| 73 | throw new ReconciliationError('BANK_REFERENCE_CONFLICT', 'Conflicting bank references require manual investigation'); |
| 74 | } |
| 75 | |
| 76 | const nextStatus = { |
| 77 | ACCEPTED: 'BANK_ACCEPTED', |
| 78 | SETTLED: 'SETTLED', |
| 79 | REJECTED: 'BANK_REJECTED', |
| 80 | }[proof.kind]; |
| 81 | return { |
| 82 | changed: true, |
| 83 | reason: 'APPLIED', |
| 84 | payment: { |
| 85 | ...payment, |
| 86 | status: nextStatus, |
| 87 | version: payment.version + 1, |
| 88 | bankReference: proof.bankReference ?? payment.bankReference ?? null, |
| 89 | appliedProofs: [...payment.appliedProofs, { evidenceId: proof.evidenceId, digest }], |
| 90 | }, |
| 91 | }; |
| 92 | } |
| 93 | |
| 94 | /** No automatic retry after a dispatch attempt, including UNKNOWN and REJECTED. */ |
| 95 | export function mayAutomaticallyDispatch(status) { |
| 96 | assert(allowedStatuses.has(status), 'INVALID_STATUS', 'Unknown payment status'); |
| 97 | return status === 'READY'; |
| 98 | } |
| 99 | |
| 100 | /** A bank acceptance is NOT a settlement receipt. */ |
| 101 | export function hasFinalBankOutcome(status) { |
| 102 | assert(allowedStatuses.has(status), 'INVALID_STATUS', 'Unknown payment status'); |
| 103 | return status === 'SETTLED' || status === 'BANK_REJECTED'; |
| 104 | } |