Treasury & Bank Transfer Management System — سامانه مدیریت خزانه‌داری و حواله‌های بانکی

1 # TreasuryFlow Phase 1 Foundation Implementation Plan
2
3 > **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
4
5 **Goal:** Build a deployable, Persian-first TreasuryFlow application foundation with executable financial invariants, CI and a staging-safe shell that performs no live banking action.
6
7 **Architecture:** A TypeScript modular monolith keeps financial domain rules framework-independent while Next.js provides the web/application boundary. PostgreSQL is the transactional authority; all external systems are typed adapters and live-bank submission is explicitly absent from Phase 1.
8
9 **Tech Stack:** Next.js 16, TypeScript strict, PostgreSQL, Vitest, Playwright, GitHub Actions, container-ready staging.
10
11 **Spec:** `docs/specs/PHASE_1_FOUNDATION_SPEC.md`
12
13 ## Global Constraints
14 - Money is integer IRR; never floating-point money.
15 - Maker cannot approve own payment.
16 - Material payment edits invalidate approval.
17 - Ambiguous bank outcome is never automatically retried.
18 - Authorization is server-side at each mutation boundary.
19 - No live-bank credential or submission in Phase 1.
20 - External services are adapter-driven and deployment remains portable.
21
22 ## Review Focus
23 - Duplicate logical payment: second submission must be rejected/idempotent, not create another transfer.
24 - Unknown bank outcome: no automatic transition to a new submission.
25 - Maker self-approval: rejected even if UI is bypassed.
26 - Material edit after approval: previous approval becomes unusable.
27 - Rial/Toman boundary: storage/domain remains integer IRR and UI labels presentation conversions.
28
29 ---
30
31 ### Task 1: Executable application scaffold
32
33 **Files:**
34 - Create: `package.json`, `tsconfig.json`, `next.config.ts`, `src/app/*`, test/lint config
35 - Create: `.github/workflows/ci.yml`
36
37 **Interfaces:**
38 - Produces: a strict TypeScript Next.js application that builds, lints and tests in CI.
39
40 - [ ] Scaffold Next.js 16 with App Router and strict TypeScript.
41 - [ ] Add test runner and a smoke test that initially fails before the app shell exists.
42 - [ ] Implement the minimal RTL Persian-first shell and make the smoke test pass.
43 - [ ] Add CI for install, lint, typecheck, test and build.
44 - [ ] Verify all checks locally/CI and commit.
45
46 ### Task 2: Domain primitives and Iran validators
47
48 **Files:**
49 - Create: `src/domain/money.ts`
50 - Create: `src/domain/iran/iban.ts`
51 - Create: `src/domain/iran/persian-normalization.ts`
52 - Test: corresponding `*.test.ts`
53
54 **Interfaces:**
55 - Produces: `RialAmount`, `validateIranianIban(value)`, `normalizePersianText(value)`.
56
57 - [ ] Write failing tests for integer IRR, valid/invalid IR IBAN checksum and Persian Yeh/Kaf/digit normalization.
58 - [ ] Run tests and verify failure.
59 - [ ] Implement minimal domain primitives/validators.
60 - [ ] Run tests and verify pass.
61 - [ ] Commit.
62
63 ### Task 3: Payment state machine
64
65 **Files:**
66 - Create: `src/domain/payments/payment-state.ts`
67 - Test: `src/domain/payments/payment-state.test.ts`
68
69 **Interfaces:**
70 - Produces: explicit `PaymentState`, `PaymentEvent`, and `transitionPayment(state,event)`.
71
72 - [ ] Write failing tests for valid happy path and forbidden direct/unsafe transitions.
73 - [ ] Add the review-focus test proving `UNKNOWN` cannot trigger automatic resubmission.
74 - [ ] Implement the explicit transition table.
75 - [ ] Run tests and verify pass.
76 - [ ] Commit.
77
78 ### Task 4: Approval/version invariants
79
80 **Files:**
81 - Create: `src/domain/approvals/approval-policy.ts`
82 - Test: `src/domain/approvals/approval-policy.test.ts`
83
84 **Interfaces:**
85 - Produces: `canApprove(actor,paymentVersion)`, approval version binding and material-change invalidation rules.
86
87 - [ ] Write failing maker-self-approval test.
88 - [ ] Write failing material-edit-invalidates-approval test.
89 - [ ] Implement minimal policy functions.
90 - [ ] Run tests and verify pass.
91 - [ ] Commit.
92
93 ### Task 5: Idempotency contract
94
95 **Files:**
96 - Create: `src/domain/payments/idempotency.ts`
97 - Test: `src/domain/payments/idempotency.test.ts`
98 - Create: persistence design note/migration placeholder for uniqueness enforcement.
99
100 **Interfaces:**
101 - Produces: deterministic logical-payment idempotency identity and duplicate decision contract.
102
103 - [ ] Write failing deterministic-key and duplicate-rejection tests.
104 - [ ] Implement canonical key material without secrets/PII leakage into logs.
105 - [ ] Add database uniqueness requirement to persistence contract.
106 - [ ] Verify tests.
107 - [ ] Commit.
108
109 ### Task 6: Coded RTL design foundation
110
111 **Files:**
112 - Create: `src/app/globals.css`, `src/components/*`, `src/app/(app)/dashboard/page.tsx`
113 - Test: component/accessibility smoke tests as supported by the scaffold.
114
115 **Interfaces:**
116 - Produces: reusable tokens/components and first Treasury dashboard shell.
117
118 - [ ] Define neutral enterprise tokens, typography, spacing, status semantics and focus states.
119 - [ ] Implement Persian RTL application frame with LTR islands for IBAN/reference identifiers.
120 - [ ] Implement dashboard cards for queue, daily amount, needs-review and bank health using non-production placeholder states clearly marked as demo.
121 - [ ] Verify keyboard/focus/contrast baseline and build.
122 - [ ] Commit.
123
124 ### Task 7: Threat model and release safety
125
126 **Files:**
127 - Create: `docs/security/THREAT_MODEL.md`
128 - Create: `docs/PROJECT_STATE.md`
129
130 **Interfaces:**
131 - Produces: ranked threats, controls, unresolved risks and Phase 2 handoff.
132
133 - [ ] Document assets/trust boundaries and threats for authz, duplicate/replay, SSRF/import, secret exposure, audit tampering and bank-outcome ambiguity.
134 - [ ] Map each critical threat to an implemented/tested control or explicit Phase 2 blocker.
135 - [ ] Record verification evidence and unresolved issues in project state.
136 - [ ] Verify no live-bank capability/credential exists.
137 - [ ] Commit.
138
139 ### Task 8: Staging blueprint and Phase 1 gate
140
141 **Files:**
142 - Create/modify deployment configuration selected by the scaffold.
143 - Update: `docs/PROJECT_STATE.md`
144
145 **Interfaces:**
146 - Produces: portable staging artifact and Phase 1 acceptance evidence.
147
148 - [ ] Build a container-ready artifact without production banking secrets.
149 - [ ] Configure Railway staging only after repository checks pass.
150 - [ ] Run deployed smoke test.
151 - [ ] Re-run CI/build/domain invariant tests.
152 - [ ] Record Phase 1 gate result and Phase 2 starting point.