update doc file
This commit is contained in:
7 files changed
+509
No files matched your search
@@ -0,0 +1,86 @@
|
|||||||
|
# طراحی BankFlow
|
||||||
|
|
||||||
|
[English](DESIGN.md) | [فارسی](DESIGN.fa.md)
|
||||||
|
|
||||||
|
## معماری
|
||||||
|
|
||||||
|
BankFlow از معماری لایهای Laravel با تمرکز بر دامنه تسهیلات استفاده میکند:
|
||||||
|
|
||||||
|
```text
|
||||||
|
HTTP Request
|
||||||
|
-> Form Request / Filament Page
|
||||||
|
-> Action تکمنظوره
|
||||||
|
-> WorkflowEngine یا Eloquent Query
|
||||||
|
-> PostgreSQL
|
||||||
|
```
|
||||||
|
|
||||||
|
لایه HTTP مسئول ملاحظات Transport و JSON Resourceها است. Actionها Use Caseها و مرز Transactionها را نمایش میدهند. مسیر `app/Domain/Loan` شامل مقادیر دامنه و رفتار Workflow با کمترین وابستگی به Framework است. مدلهای Eloquent مسئول Relationshipها و ماندگاری هستند، اما پردازش را هماهنگ نمیکنند.
|
||||||
|
|
||||||
|
API عمومی زیر مسیر `/api/v1` نسخهبندی شده است. Filament رابط مدیریتی احرازهویتشده را برای مشاهده تسهیلات، انواع تسهیلات، تعریف Stageها و نسخههای Workflow فراهم میکند.
|
||||||
|
|
||||||
|
## مدل و اجرای Workflow
|
||||||
|
|
||||||
|
هر `LoanType` چند نسخه `WorkflowConfiguration` دارد. هر پیکربندی شامل ردیفهای مرتب `WorkflowConfigurationStep` است و هر Step به یک `StageDefinition` و یک شیء JSON حاوی قوانین اشاره میکند. فقط Workflow با وضعیت `PUBLISHED` به درخواست جدید اختصاص مییابد. شناسه همان پیکربندی در Loan نگهداری میشود؛ بنابراین تغییرات مدیریتی بعدی نمیتوانند تصمیم در حال اجرا یا تاریخی را تغییر دهند.
|
||||||
|
|
||||||
|
فایل `config/workflow-stages.php` Registry امن پیادهسازی است. این فایل کد پایدار هر Stage را به یک کلاس PHP پیادهساز `StageInterface` نگاشت میکند؛ مقادیر پایگاه داده هرگز کلاس دلخواه یا عبارت اجرایی را انتخاب نمیکنند. Handlerهای Stage بدون State هستند و یک `ExecutionResult` تغییرناپذیر شامل `PASS`، `FAIL` یا `MANUAL_REVIEW` و کد دلیل برمیگردانند.
|
||||||
|
|
||||||
|
پردازش بهصورت همزمان انجام میشود:
|
||||||
|
|
||||||
|
1. `ProcessLoanAction` یک Transaction پایگاه داده باز و ردیف Loan را قفل میکند.
|
||||||
|
2. Loan دارای وضعیت نهایی بدون اجرای دوباره بازگردانده میشود.
|
||||||
|
3. `WorkflowEngine` مراحل فعال را بر اساس `position` میخواند.
|
||||||
|
4. مراحلی که قبلاً در تاریخچه ثبت شدهاند نادیده گرفته میشوند.
|
||||||
|
5. Handler مشروط میتواند اعلام کند که قابل اجرا نیست؛ برای Stage ردشده تاریخچهای ایجاد نمیشود.
|
||||||
|
6. Handler با قوانین اعتبارسنجیشده Step اجرا میشود.
|
||||||
|
7. یک ردیف تاریخچه شامل Stage، نتیجه، دلیل، زمان اجرا و Snapshot قوانین ذخیره میشود.
|
||||||
|
8. نتیجه `FAIL` وضعیت `REJECTED` و نتیجه `MANUAL_REVIEW` وضعیت `MANUAL_REVIEW` را ایجاد میکند؛ در غیر این صورت Step مرتب بعدی انتخاب میشود.
|
||||||
|
9. عبور موفق از تمام مراحل قابل اجرا، وضعیت `APPROVED` را ایجاد میکند.
|
||||||
|
|
||||||
|
بنابراین Stage فعلی در یک Switch مربوط به Transitionها Hard-code نشده است؛ بلکه اولین Step فعال، قابل اجرا و اجرانشده در نسخه مرتب Workflow مربوط به Loan است.
|
||||||
|
|
||||||
|
## قوانین و چرخه عمر پیکربندی
|
||||||
|
|
||||||
|
آستانهها و Prefixهای کسبوکار در ستون JSON با نام `rules` برای هر Step قرار دارند. Workflowهای Seedشده، مقادیر پیشفرض چالش را تأمین میکنند. اعتبارسنجی اختصاصی هر Stage هنگام ایجاد یا ویرایش Draft توسط مدیر و بار دیگر پیش از انتشار اجرا میشود. Registry امن یک فایل پیکربندی PHP باقی میماند، زیرا Handlerهای اجرایی بخشی از کد برنامه هستند؛ در مقابل، مقادیر کسبوکار و ترتیب مراحل بهصورت دادههای نسخهبندیشده در پایگاه داده قرار دارند.
|
||||||
|
|
||||||
|
نسخههای Workflow چرخه `DRAFT -> PUBLISHED -> ARCHIVED` را طی میکنند. Draft قابل ویرایش است. انتشار، کل Workflow را اعتبارسنجی و نسخه منتشرشده قبلی همان نوع تسهیلات را بهصورت اتمیک Archive میکند. نسخههای Published و Archived تغییرناپذیر هستند.
|
||||||
|
|
||||||
|
## ماندگاری، وضعیت و تاریخچه ممیزی
|
||||||
|
|
||||||
|
Eloquent انواع تسهیلات، تعریف Stageها، نسخهها و مراحل Workflow، درخواستهای تسهیلات و تاریخچهها را ذخیره میکند. Backed Enumها سه مفهوم را از هم جدا میکنند:
|
||||||
|
|
||||||
|
- وضعیت Loan: `SUBMITTED`، `IN_PROGRESS`، `MANUAL_REVIEW`، `APPROVED` و `REJECTED`؛
|
||||||
|
- Stage قابل اجرا: `VALIDATION`، `FRAUD_CHECK`، `GUARANTOR_CHECK`، `CREDIT_CHECK` و `MANAGER_APPROVAL`؛
|
||||||
|
- نتیجه Stage: `PASS`، `FAIL` و `MANUAL_REVIEW`.
|
||||||
|
|
||||||
|
فیلد `loans.current_workflow_configuration_step_id` پیشرفت فعلی را نشان میدهد و `loan_histories` تاریخچه پایدار کسبوکار است. Snapshot قوانین در تاریخچه، ورودی دقیق هر تصمیم را حتی در صورت استفاده Workflowهای آینده از قوانین متفاوت حفظ میکند. Exceptionهای پیشبینینشده با Logging استاندارد Laravel ثبت میشوند و Transaction پردازش را Rollback میکنند.
|
||||||
|
|
||||||
|
درخواستهای دارای وضعیت بررسی دستی میتوانند بعداً در Filament توسط مدیر مجاز تأیید شوند. این عملیات Loan را قفل و مدیر تأییدکننده، زمان و یادداشت را ثبت میکند.
|
||||||
|
|
||||||
|
## جلوگیری از پردازش تکراری و همزمان
|
||||||
|
|
||||||
|
Idempotency در چند سطح اعمال میشود:
|
||||||
|
|
||||||
|
- وضعیت نهایی باعث بازگشت فوری و بدون عملیات میشود؛
|
||||||
|
- شناسه Stepهای اجراشده از تاریخچه استخراج میشود و دوباره اجرا نمیشوند؛
|
||||||
|
- Unique Constraint روی `(loan_id, workflow_configuration_step_id)` از ایجاد تاریخچه تکراری جلوگیری میکند؛
|
||||||
|
- `SELECT ... FOR UPDATE` پردازشهای همزمان یک Loan را سریالی میکند؛
|
||||||
|
- اجرای کامل Workflow و همه تاریخچهها در یک Transaction با حداکثر سه بار تلاش مجدد قرار دارند.
|
||||||
|
|
||||||
|
Constraintهای پایگاه داده نیز سازگاری نوع تسهیلات، Workflow و Step فعلی Loan را حفظ میکنند. PostgreSQL با یک Partial Unique Index اجازه میدهد برای هر نوع تسهیلات فقط یک Workflow منتشرشده وجود داشته باشد.
|
||||||
|
|
||||||
|
## افزودن `AML_CHECK`
|
||||||
|
|
||||||
|
افزودن یک Stage به تغییرات زیر نیاز دارد:
|
||||||
|
|
||||||
|
1. افزودن `AmlCheck` به `LoanStage`؛
|
||||||
|
2. پیادهسازی `StageInterface` و در صورت قابل ردشدن بودن، `ConditionalStageInterface`؛
|
||||||
|
3. ثبت `AML_CHECK` در `config/workflow-stages.php`؛
|
||||||
|
4. تعریف و اعتبارسنجی Schema قوانین آن در `ValidateWorkflowStepDataAction`؛
|
||||||
|
5. ایجاد یا Seed کردن `StageDefinition` و افزودن آن به یک نسخه Draft جدید؛
|
||||||
|
6. افزودن Unit Test برای Stage و Feature Test برای ترتیب، نتایج، تاریخچه و Idempotency.
|
||||||
|
|
||||||
|
Engine و کلاسهای Stage موجود تغییر نمیکنند. انتشار نسخه جدید فقط بر Loanهایی اثر میگذارد که پس از آن ساخته شوند.
|
||||||
|
|
||||||
|
## Trade-offهای اصلی
|
||||||
|
|
||||||
|
اجرای همزمان در محدوده یک Transaction، استدلال درباره صحت و رفتار API را ساده میکند؛ اما بررسیهای خارجی طولانی، Request و قفل پایگاه داده را برای مدت زیادی نگه میدارند. ترتیب Workflow مدیریتشده در پایگاه داده، مدیریت امن و بازتولید تصمیمهای تاریخی را فراهم میکند؛ در مقابل نسبت به پیکربندی ثابت به Schema و اعتبارسنجی انتشار بیشتری نیاز دارد. دسترسی مستقیم Eloquent برنامه را کوچک و منطبق با Laravel نگه میدارد، اما افزودن یک پیادهسازی ماندگاری دوم را پرهزینهتر میکند.
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
# BankFlow Design
|
||||||
|
|
||||||
|
[English](DESIGN.md) | [فارسی](DESIGN.fa.md)
|
||||||
|
|
||||||
|
## Architecture
|
||||||
|
|
||||||
|
BankFlow uses a layered Laravel architecture grouped around the loan domain:
|
||||||
|
|
||||||
|
```text
|
||||||
|
HTTP request
|
||||||
|
-> Form Request / Filament page
|
||||||
|
-> single-purpose Action
|
||||||
|
-> WorkflowEngine or Eloquent query
|
||||||
|
-> PostgreSQL
|
||||||
|
```
|
||||||
|
|
||||||
|
The HTTP layer owns transport concerns and JSON resources. Actions represent use cases and transaction boundaries. `app/Domain/Loan` contains framework-light domain values and workflow behavior. Eloquent models own relationships and persistence, but do not orchestrate processing.
|
||||||
|
|
||||||
|
The public API is versioned under `/api/v1`. Filament provides the authenticated administrative interface for loan inspection, loan types, stage definitions, and workflow versions.
|
||||||
|
|
||||||
|
## Workflow model and execution
|
||||||
|
|
||||||
|
A `LoanType` has multiple `WorkflowConfiguration` versions. Each configuration has ordered `WorkflowConfigurationStep` rows, and each step points to a `StageDefinition` plus a JSON rules object. Only a `PUBLISHED` workflow is assigned to a new loan. The loan retains that configuration ID, so later administrative changes cannot alter an in-flight or historical decision.
|
||||||
|
|
||||||
|
`config/workflow-stages.php` is the trusted implementation registry. It maps a stable stage code to a PHP class implementing `StageInterface`; database values never select arbitrary classes or executable expressions. Stage handlers are stateless and return an immutable `ExecutionResult` containing `PASS`, `FAIL`, or `MANUAL_REVIEW` and a reason code.
|
||||||
|
|
||||||
|
Processing is synchronous:
|
||||||
|
|
||||||
|
1. `ProcessLoanAction` opens a database transaction and locks the loan row.
|
||||||
|
2. A terminal loan is returned without further work.
|
||||||
|
3. `WorkflowEngine` reads enabled steps in `position` order.
|
||||||
|
4. Steps already present in loan history are skipped.
|
||||||
|
5. A conditional handler may declare that it does not apply; skipped stages create no history.
|
||||||
|
6. The handler executes with the step's validated rules.
|
||||||
|
7. A history row stores the stage, result, reason, execution time, and rules snapshot.
|
||||||
|
8. `FAIL` produces `REJECTED`; `MANUAL_REVIEW` produces `MANUAL_REVIEW`; otherwise the next ordered step is selected.
|
||||||
|
9. Passing all applicable steps produces `APPROVED`.
|
||||||
|
|
||||||
|
The current stage is therefore not hard-coded in a transition switch. It is the first enabled, applicable, unexecuted step in the loan's ordered workflow version.
|
||||||
|
|
||||||
|
## Rules and configuration lifecycle
|
||||||
|
|
||||||
|
Business thresholds and prefixes live in the `rules` JSON column on each configured step. Seeded workflows provide the challenge defaults. Stage-specific validation runs when an administrator creates or updates a draft and again before publication. The trusted registry remains a PHP configuration file because executable handlers are application code, while business values and order remain versioned database data.
|
||||||
|
|
||||||
|
Workflow versions move through `DRAFT -> PUBLISHED -> ARCHIVED`. Drafts are editable. Publishing validates the entire workflow and atomically archives the previous published version for that loan type. Published and archived versions are immutable.
|
||||||
|
|
||||||
|
## Persistence, state, and audit history
|
||||||
|
|
||||||
|
Eloquent persists loan types, stage definitions, workflow versions and steps, loans, and histories. Backed enums distinguish three concepts:
|
||||||
|
|
||||||
|
- loan status: `SUBMITTED`, `IN_PROGRESS`, `MANUAL_REVIEW`, `APPROVED`, `REJECTED`;
|
||||||
|
- executable stage: `VALIDATION`, `FRAUD_CHECK`, `GUARANTOR_CHECK`, `CREDIT_CHECK`, `MANAGER_APPROVAL`;
|
||||||
|
- stage result: `PASS`, `FAIL`, `MANUAL_REVIEW`.
|
||||||
|
|
||||||
|
`loans.current_workflow_configuration_step_id` exposes current progress, while `loan_histories` is the durable business audit trail. The history's rule snapshot preserves the exact inputs to a decision even if a future workflow uses different rules. Unexpected exceptions use normal Laravel logging and roll back the processing transaction.
|
||||||
|
|
||||||
|
Manual-review loans may later be approved by an authorized manager in Filament. That operation locks the loan and records the approver, timestamp, and note.
|
||||||
|
|
||||||
|
## Duplicate and concurrent processing
|
||||||
|
|
||||||
|
Idempotency is enforced at multiple levels:
|
||||||
|
|
||||||
|
- terminal statuses cause an immediate no-op response;
|
||||||
|
- executed workflow step IDs are derived from history and not run again;
|
||||||
|
- a unique constraint on `(loan_id, workflow_configuration_step_id)` prevents duplicate history rows;
|
||||||
|
- `SELECT ... FOR UPDATE` serializes concurrent processing of one loan;
|
||||||
|
- the complete workflow run and all history writes share one transaction, retried up to three times.
|
||||||
|
|
||||||
|
Database constraints also keep a loan's type, workflow, and current step consistent. PostgreSQL adds a partial unique index allowing only one published workflow per loan type.
|
||||||
|
|
||||||
|
## Adding `AML_CHECK`
|
||||||
|
|
||||||
|
Adding a stage requires:
|
||||||
|
|
||||||
|
1. add `AmlCheck` to `LoanStage`;
|
||||||
|
2. implement `StageInterface` (and `ConditionalStageInterface` if it can be skipped);
|
||||||
|
3. register `AML_CHECK` in `config/workflow-stages.php`;
|
||||||
|
4. define and validate its rule schema in `ValidateWorkflowStepDataAction`;
|
||||||
|
5. create/seed its `StageDefinition` and add it to a new draft workflow version;
|
||||||
|
6. add stage unit tests and feature tests for ordering, outcomes, history, and idempotency.
|
||||||
|
|
||||||
|
The engine and existing stage classes do not change. Publishing the new version affects only loans created afterward.
|
||||||
|
|
||||||
|
## Principal trade-offs
|
||||||
|
|
||||||
|
Synchronous, transaction-scoped execution makes correctness and API behavior easy to reason about, but long-running external checks would hold a request and database lock too long. Database-managed workflow order enables safe administration and historical reproducibility, at the cost of more schema and publication validation than static configuration. Direct Eloquent access keeps this application small and idiomatic, but would make a second persistence implementation more expensive.
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
# تصمیمهای مهندسی BankFlow
|
||||||
|
|
||||||
|
[English](ENGINEERING_DECISIONS.md) | [فارسی](ENGINEERING_DECISIONS.fa.md)
|
||||||
|
|
||||||
|
## ۱. معماری لایهای Laravel
|
||||||
|
|
||||||
|
**گزینههای بررسیشده:** Controllerهای دارای منطق داخلی، Laravel لایهای با Actionها، یا معماری کامل Clean/Hexagonal با Repository و Port.
|
||||||
|
|
||||||
|
**تصمیم:** استفاده از Controllerهای کمحجم، Actionهای تکمنظوره، ماژول دامنه تسهیلات و مدلهای Eloquent. این ساختار Use Caseها و مرز Transactionها را بدون افزودن Abstractionهای غیرضروری صریح نگه میدارد. برای توسعهدهندگان Laravel آشنا و بهسادگی قابل تست است. محدودیت آن، وابستگی بیشتر به Laravel و Eloquent در مقایسه با معماری Ports and Adapters است.
|
||||||
|
|
||||||
|
## ۲. Pipeline مرتب Stageها بهجای انشعاب Hard-codeشده
|
||||||
|
|
||||||
|
**گزینههای بررسیشده:** شرطهای تو در تو در یک Service، Rule Engine عمومی، یا Handlerهای مرتب و بدون State.
|
||||||
|
|
||||||
|
**تصمیم:** مدلسازی Workflow بهصورت Stepهای مرتب و نسخهبندیشده پایگاه داده که از طریق Registry امن PHP Resolve میشوند. هر Handler نتیجهای Type-safe برمیگرداند و Engine مسئول حرکت بین مراحل است. افزودن Stage موجب تغییر Enum، Handler، Registry، اعتبارسنجی قوانین، تعریف Stage یا نسخه Workflow و تستها میشود؛ Engine و Handlerهای موجود بدون تغییر باقی میمانند.
|
||||||
|
|
||||||
|
این طراحی بدون پذیرش ورودی اجرایی مدیر، توسعهپذیر و قابل ممیزی است. در مقایسه با Graph یا زبان قوانین عمومی انعطاف کمتری دارد: Engine فعلی یک Pipeline خطی و مرتب با Skip مشروط و نتایج نهایی را پشتیبانی میکند، نه Branch یا Cycle دلخواه.
|
||||||
|
|
||||||
|
## ۳. مالکیت قوانین توسط پایگاه داده و مالکیت Handlerها توسط کد
|
||||||
|
|
||||||
|
**گزینههای بررسیشده:** همه قوانین در فایلهای پیکربندی PHP، همه رفتار در پایگاه داده، یا رویکرد ترکیبی.
|
||||||
|
|
||||||
|
**تصمیم:** ترتیب Workflow و مقادیر کسبوکار بهشکل JSON اعتبارسنجیشده در Stepهای نسخهبندیشده پایگاه داده ذخیره میشوند و `config/workflow-stages.php` کدهای امن را به کلاسهای پیادهسازی نگاشت میکند. مدیران میتوانند آستانهها و ترتیب را بدون Deploy کد تغییر دهند، اما امکان تزریق PHP، SQL یا Expression ندارند.
|
||||||
|
|
||||||
|
مزیت این روش، انعطاف عملیاتی همراه با مرز امنیتی کنترلشده است. هزینه آن، نگهداری اعتبارسنجی اختصاصی هر Stage و چرخه انتشار مدیریتی است. با اینکه متن چالش خواندن قوانین از Configuration را مطرح میکند، پایگاه داده عمداً منبع نسخهبندیشده پیکربندی کسبوکار است و فایل پیکربندی PHP فقط Handlerهای امن را ثبت میکند.
|
||||||
|
|
||||||
|
## ۴. تخصیص Workflow تغییرناپذیر و تاریخچه پایدار
|
||||||
|
|
||||||
|
**گزینههای بررسیشده:** Resolve کردن همیشگی آخرین Workflow، کپی کامل اطلاعات Workflow روی هر Loan، یا ارجاع به یک پیکربندی نسخهبندیشده.
|
||||||
|
|
||||||
|
**تصمیم:** هنگام ایجاد Loan، مقدار `workflow_configuration_id` مربوط به نسخه منتشرشده فعلی به آن اختصاص مییابد. نسخه منتشرشده تغییرناپذیر است و هر رکورد تاریخچه، Step اجراشده و Snapshot قوانین را نگه میدارد. این کار تصمیمهای قدیمی را بدون تکرار کل پیکربندی برای هر Loan قابل بازتولید میکند. در مقابل، نسخههای دارای ارجاع باید حفظ شوند و حذف مخرب آنها مجاز نیست.
|
||||||
|
|
||||||
|
## ۵. پردازش همزمان و اتمیک
|
||||||
|
|
||||||
|
**گزینههای بررسیشده:** یک Request همزمان، یک Job صف برای هر Stage، یا هماهنگسازی Event-driven.
|
||||||
|
|
||||||
|
**تصمیم:** همه مراحل باقیمانده در یک Request و یک Transaction پایگاه داده پردازش میشوند. API بلافاصله وضعیت نهایی پردازش خودکار را برمیگرداند و خطا نمیتواند State ناقص باقی بگذارد. قفل ردیف، بررسی وضعیت نهایی، جستوجوی تاریخچه و Unique Constraint تاریخچه از اجرای تکراری یا همزمان جلوگیری میکنند.
|
||||||
|
|
||||||
|
این راهکار سادهترین انتخاب قابل اتکا برای بررسیهای سریع و Mockشده است. محدودیت اصلی آن مدت Transaction است: Integration واقعی اعتبار، تقلب یا AML ممکن است کند یا خارج از دسترس باشد. در آینده چنین Stageهایی باید به یک State Machine قابل ادامه روی Queue، همراه با Transactionهای کوتاه، سیاست Retry صریح، Timeout و Idempotency Key منتقل شوند.
|
||||||
|
|
||||||
|
## ۶. Eloquent و PostgreSQL
|
||||||
|
|
||||||
|
**گزینههای بررسیشده:** ذخیرهسازی درون حافظه، Abstraction مبتنی بر Repository، یا ماندگاری مستقیم Eloquent روی پایگاه داده رابطهای.
|
||||||
|
|
||||||
|
**تصمیم:** استفاده از Eloquent و PostgreSQL در محیط تولید Compose. Foreign Keyهای رابطهای، Composite Constraintها، قفل ردیف، ذخیره قوانین JSON و Partial Unique Index سازگاری و ماندگاری پس از Restart را فراهم میکنند. SQLite برای تستها و Image مستقل چالش باقی میماند.
|
||||||
|
|
||||||
|
مزیت، ماندگاری قوی با زیرساخت سفارشی اندک است. Trade-off این است که برخی تضمینهای تولید، بهویژه Partial Unique Index و رفتارهای Concurrency، مخصوص PostgreSQL هستند و تستهای SQLite آنها را بهطور کامل پوشش نمیدهند.
|
||||||
|
|
||||||
|
## سه Trade-off تعیینکننده
|
||||||
|
|
||||||
|
1. **سادگی در برابر Throughput:** پردازش همزمان شفاف و اتمیک است، اما برای وابستگی خارجی کند مناسب نیست.
|
||||||
|
2. **پیکربندیپذیری در برابر سادگی Schema:** جدولهای Workflow نسخهبندیشده تغییرات امن و ممیزی را ممکن میکنند، اما به اعتبارسنجی و مدیریت چرخه عمر نیاز دارند.
|
||||||
|
3. **Convention فریمورک در برابر قابلیت انتقال:** Actionها و Eloquent Boilerplate را کاهش میدهند، اما عمداً استقلال از پیادهسازی ماندگاری را کنار میگذارند.
|
||||||
|
|
||||||
|
## محدودیتها و بهبودهای بعدی
|
||||||
|
|
||||||
|
بزرگترین محدودیت معماری، Workflow خطی و همزمان است. با یک هفته زمان بیشتر، اولویتها شامل تست Concurrency روی PostgreSQL، تست تزریق خطا و Rollback، احراز هویت و Rate Limiting برای API، نقشهای Authorization غنیتر، تولید OpenAPI، مشاهدهپذیری Latency و خطای Stageها و طراحی Queue قابل ادامه برای بررسیهای خارجی واقعی خواهد بود.
|
||||||
|
|
||||||
|
با این حال، معماری فعلی از رشد آینده پشتیبانی میکند: قرارداد پایدار Stageها بررسیهای جدید را مستقل میسازد، نسخههای تغییرناپذیر از Loanهای موجود محافظت میکنند، قوانین JSON سیاست اختصاصی هر Stage را ممکن میسازند و Actionها نقاط اتصال شفافی برای Queue یا Adapterهای خارجی فراهم میکنند.
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
# BankFlow Engineering Decisions
|
||||||
|
|
||||||
|
[English](ENGINEERING_DECISIONS.md) | [فارسی](ENGINEERING_DECISIONS.fa.md)
|
||||||
|
|
||||||
|
## 1. Layered Laravel architecture
|
||||||
|
|
||||||
|
**Options considered:** controllers with embedded logic, layered Laravel with Actions, or full Clean/Hexagonal architecture with repositories and ports.
|
||||||
|
|
||||||
|
**Decision:** use thin controllers, single-purpose Actions, a loan domain module, and Eloquent models. This keeps use cases and transaction boundaries explicit without adding abstractions that the current service does not need. It is familiar to Laravel developers and straightforward to test. The limitation is tighter coupling to Laravel and Eloquent than a ports-and-adapters design.
|
||||||
|
|
||||||
|
## 2. Ordered stage pipeline instead of hard-coded branching
|
||||||
|
|
||||||
|
**Options considered:** nested conditionals in a service, a generic rule engine, or ordered stateless stage handlers.
|
||||||
|
|
||||||
|
**Decision:** model a workflow as versioned, ordered database steps resolved through a trusted PHP registry. Each handler returns a typed result and the engine owns progression. Adding a stage changes the enum, handler, registry, rule validation, stage definition/workflow version, and tests; the engine and existing handlers remain unchanged.
|
||||||
|
|
||||||
|
This design is extensible and auditable without accepting executable administrator input. It is less expressive than a general graph or rule language: the current engine supports a linear ordered pipeline with conditional skips and terminal results, not arbitrary branches or cycles.
|
||||||
|
|
||||||
|
## 3. Database-owned rules with code-owned handlers
|
||||||
|
|
||||||
|
**Options considered:** all rules in PHP configuration files, all behavior in the database, or a hybrid.
|
||||||
|
|
||||||
|
**Decision:** store workflow order and business values as validated JSON on versioned database steps, while `config/workflow-stages.php` maps trusted codes to implementation classes. Administrators can change thresholds and ordering without deploying code, but cannot inject PHP, SQL, or expressions.
|
||||||
|
|
||||||
|
The benefit is operational flexibility with a controlled security boundary. The cost is maintaining per-stage validation and an administrative publication lifecycle. Although the challenge describes reading rules from configuration, the database is deliberately the versioned business configuration source; the PHP config file only registers trusted handlers.
|
||||||
|
|
||||||
|
## 4. Immutable workflow assignment and durable history
|
||||||
|
|
||||||
|
**Options considered:** always resolve the latest workflow, copy all workflow data onto each loan, or reference a versioned configuration.
|
||||||
|
|
||||||
|
**Decision:** assign the current published `workflow_configuration_id` when a loan is created. Published versions are immutable and each history record stores the executed step and rule snapshot. This makes old decisions reproducible without duplicating the complete configuration per loan. It requires retaining referenced workflow versions and prevents destructive deletion.
|
||||||
|
|
||||||
|
## 5. Synchronous atomic processing
|
||||||
|
|
||||||
|
**Options considered:** one synchronous request, a queued job per stage, or event-driven orchestration.
|
||||||
|
|
||||||
|
**Decision:** process all remaining stages in one request and one database transaction. The API immediately returns a terminal automatic-processing status, and failures cannot leave partial state. A row lock, terminal-status guard, history lookup, and unique history constraint prevent repeated or concurrent execution.
|
||||||
|
|
||||||
|
This is the simplest reliable fit for fast, mocked checks. Its main limitation is transaction duration: real credit, fraud, or AML integrations could be slow or unavailable. Those stages should eventually move to a resumable queued state machine with short transactions, explicit retry policy, timeouts, and idempotency keys.
|
||||||
|
|
||||||
|
## 6. Eloquent and PostgreSQL
|
||||||
|
|
||||||
|
**Options considered:** in-memory storage, a repository abstraction, or direct Eloquent persistence backed by a relational database.
|
||||||
|
|
||||||
|
**Decision:** use Eloquent with PostgreSQL in the production Compose setup. Relational foreign keys, composite constraints, row locks, JSON rule storage, and a partial unique index provide the required consistency and restart durability. SQLite remains useful for tests and the self-contained challenge image.
|
||||||
|
|
||||||
|
The advantage is strong persistence with little custom infrastructure. The trade-off is that some production guarantees—especially the partial unique index and concurrency semantics—are PostgreSQL-specific and are not fully exercised by SQLite tests.
|
||||||
|
|
||||||
|
## Three defining trade-offs
|
||||||
|
|
||||||
|
1. **Simplicity over throughput:** synchronous processing is clear and atomic, but not suitable for slow external dependencies.
|
||||||
|
2. **Configurability over schema simplicity:** versioned workflow tables support safe changes and audits, but require validation and lifecycle management.
|
||||||
|
3. **Framework convention over portability:** Actions plus Eloquent reduce boilerplate, but deliberately avoid persistence independence.
|
||||||
|
|
||||||
|
## Limitations and next improvements
|
||||||
|
|
||||||
|
The largest architectural limit is the synchronous linear workflow. With another week, the priorities would be PostgreSQL-backed concurrency tests, failure-injection and rollback tests, API authentication and rate limiting, richer authorization roles, OpenAPI generation, observability around stage latency/failures, and a resumable queue design for genuine external checks.
|
||||||
|
|
||||||
|
Future growth is still supported: stable stage contracts isolate new checks; immutable versions protect existing loans; JSON rules permit stage-specific policies; and Actions provide clear seams for queues or external adapters when those needs become real.
|
||||||
+139
@@ -0,0 +1,139 @@
|
|||||||
|
# BankFlow
|
||||||
|
|
||||||
|
[English](README.md) | [فارسی](README.fa.md)
|
||||||
|
|
||||||
|
BankFlow یک سرویس Laravel برای ثبت و پردازش درخواستهای تسهیلات شخصی و تجاری است. به هر درخواست، یک Workflow نسخهبندیشده و تغییرناپذیر اختصاص داده میشود؛ پردازش، Stageهای پیکربندیشده را بهصورت همزمان اجرا میکند و برای هر Stage اجراشده نتیجهای قابل ممیزی ثبت میکند.
|
||||||
|
|
||||||
|
این مخزن شامل JSON API نسخهبندیشده، پنل مدیریت Filament برای تسهیلات و پیکربندی Workflow، محیط تولید مبتنی بر PostgreSQL و مجموعه تست Pest است.
|
||||||
|
|
||||||
|
## قابلیتهای اصلی
|
||||||
|
|
||||||
|
- ثبت و مشاهده درخواستهای تسهیلات از طریق `/api/v1/loans`؛
|
||||||
|
- اجرای مراحل اعتبارسنجی، تقلب، ضامن، اعتبار مالی و تأیید مشروط مدیر؛
|
||||||
|
- توقف پردازش در حالت رد یا بررسی دستی و بازگرداندن امن نتیجه نهایی در درخواستهای تکراری؛
|
||||||
|
- پیکربندی و انتشار Workflowهای نسخهبندیشده برای هر نوع تسهیلات از طریق پنل مدیریت؛
|
||||||
|
- نگهداری نتیجه مراحل و Snapshot دقیق قوانین استفادهشده برای هر تصمیم؛
|
||||||
|
- بررسی آمادگی سرویس از طریق `GET /health`.
|
||||||
|
|
||||||
|
قرارداد کامل HTTP در [docs/api-routes.md](docs/api-routes.md) قرار دارد.
|
||||||
|
|
||||||
|
## فناوریها
|
||||||
|
|
||||||
|
- PHP 8.4 و Laravel 13
|
||||||
|
- Filament 5 و Livewire 4
|
||||||
|
- PostgreSQL 17 در محیط Compose؛ پشتیبانی از SQLite برای تستها و Image مستقل
|
||||||
|
- Eloquent ORM
|
||||||
|
- Pest 4 و PHPUnit 12
|
||||||
|
- Tailwind CSS 4 و Vite 8
|
||||||
|
- Apache در Container تولید
|
||||||
|
|
||||||
|
## اجرا با Docker
|
||||||
|
|
||||||
|
Image مستقل و سازگار با قرارداد چالش از پایگاه داده SQLite داخلی استفاده میکند. Entrypoint هنگام راهاندازی Migrationها و Seederها را اجرا میکند.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker build -t bankflow .
|
||||||
|
docker run --rm -p 8080:8080 bankflow
|
||||||
|
```
|
||||||
|
|
||||||
|
پس از اجرا، سرویس روی پورت `8080` در دسترس است. برای بررسی سلامت آن اجرا کنید:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl http://127.0.0.1:8080/health
|
||||||
|
```
|
||||||
|
|
||||||
|
برای اجرا با PostgreSQL، فایل محیطی را کپی کنید، مقادیر `APP_KEY` و `DB_PASSWORD` را تنظیم کنید و سپس Compose را اجرا کنید:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cp .env.example .env
|
||||||
|
docker compose up --build
|
||||||
|
```
|
||||||
|
|
||||||
|
Compose دادههای PostgreSQL را در Volume با نام `postgres-data` نگه میدارد. هر دو روش Container، Migrationها و `BankFlowSeeder` تکرارپذیر را بهطور خودکار اجرا میکنند.
|
||||||
|
|
||||||
|
## توسعه محلی
|
||||||
|
|
||||||
|
### پیشنیازها
|
||||||
|
|
||||||
|
- PHP 8.4 بههمراه Extensionهای مورد نیاز Laravel و پایگاه داده انتخابشده
|
||||||
|
- Composer 2
|
||||||
|
- Node.js 24 و npm
|
||||||
|
- PostgreSQL، یا SQLite برای محیط محلی سبکتر
|
||||||
|
- Laravel Herd یا یک وبسرور محلی پیکربندیشده دیگر
|
||||||
|
|
||||||
|
برنامه را نصب و پیکربندی کنید:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cp .env.example .env
|
||||||
|
composer install
|
||||||
|
php artisan key:generate
|
||||||
|
npm install
|
||||||
|
npm run build
|
||||||
|
php artisan migrate --seed
|
||||||
|
```
|
||||||
|
|
||||||
|
پیش از اجرای Migration، مقادیر `DB_*` در `.env` را متناسب با پایگاه داده در دسترس میزبان تغییر دهید. برای استفاده از SQLite، مقدار `DB_CONNECTION=sqlite` را تنظیم کنید، سایر مقادیر `DB_*` را حذف کنید و در صورت نبود فایل `database/database.sqlite` آن را بسازید.
|
||||||
|
|
||||||
|
برای توسعه فعال Frontend، فرمان `npm run dev` را اجرا کنید. در محیط استاندارد پروژه، برنامه از قبل توسط Laravel Herd سرو میشود و نیازی به اجرای PHP Development Server دیگری نیست.
|
||||||
|
|
||||||
|
پنل مدیریت در مسیر `/admin` قرار دارد و به کاربر احرازهویتشده با ایمیل تأییدشده نیاز دارد.
|
||||||
|
|
||||||
|
## نمونه API
|
||||||
|
|
||||||
|
یک درخواست تسهیلات ایجاد و پردازش کنید:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl -X POST http://127.0.0.1:8080/api/v1/loans \
|
||||||
|
-H 'Content-Type: application/json' \
|
||||||
|
-d '{
|
||||||
|
"customerId": "C-1001",
|
||||||
|
"amount": 400000000,
|
||||||
|
"phone": "09121234567",
|
||||||
|
"loanType": "PERSONAL",
|
||||||
|
"monthlyIncome": 50000000,
|
||||||
|
"creditScore": 720,
|
||||||
|
"hasGuarantor": false
|
||||||
|
}'
|
||||||
|
```
|
||||||
|
|
||||||
|
از `loanId` بازگرداندهشده استفاده کنید:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl -X POST http://127.0.0.1:8080/api/v1/loans/{loanId}/process
|
||||||
|
curl http://127.0.0.1:8080/api/v1/loans/{loanId}
|
||||||
|
curl http://127.0.0.1:8080/api/v1/loans/{loanId}/history
|
||||||
|
```
|
||||||
|
|
||||||
|
## تست و قالببندی
|
||||||
|
|
||||||
|
محیط تست از پایگاه داده SQLite درون حافظه استفاده میکند و به PostgreSQL نیاز ندارد.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
php artisan test --compact
|
||||||
|
vendor/bin/pint --format agent
|
||||||
|
```
|
||||||
|
|
||||||
|
برای اجرای یک تست متمرکز از `php artisan test --compact --filter=ProcessLoanTest` استفاده کنید. راهبرد تست و پوشش فعلی در [TESTING.fa.md](TESTING.fa.md) توضیح داده شده است.
|
||||||
|
|
||||||
|
## ساختار پروژه
|
||||||
|
|
||||||
|
```text
|
||||||
|
app/Actions/ عملیات مستقل برنامه و Transactionها
|
||||||
|
app/Domain/Loan/ Enumها، قراردادها و Handlerهای Stage و Engine
|
||||||
|
app/Filament/ Resourceها، Pageها و Widgetهای مدیریتی
|
||||||
|
app/Http/ Controllerهای API، پردازش Request و Resourceها
|
||||||
|
app/Models/ مدلهای ماندگاری Eloquent
|
||||||
|
config/workflow-stages.php Registry امن نگاشت کد Stage به Handler
|
||||||
|
database/ Migrationها، Factoryها و داده اولیه Workflow
|
||||||
|
docs/ نیازمندیها، قرارداد API و Blueprint پیادهسازی
|
||||||
|
routes/ Routeهای وب و API نسخهبندیشده
|
||||||
|
tests/ تستهای Unit و Feature با Pest
|
||||||
|
```
|
||||||
|
|
||||||
|
## مستندات بیشتر
|
||||||
|
|
||||||
|
- [DESIGN.fa.md](DESIGN.fa.md) — معماری اجرا، مدل Workflow، وضعیت و توسعهپذیری
|
||||||
|
- [ENGINEERING_DECISIONS.fa.md](ENGINEERING_DECISIONS.fa.md) — گزینهها، Trade-offها، محدودیتها و کارهای آینده
|
||||||
|
- [TESTING.fa.md](TESTING.fa.md) — دامنه تست و فرمانهای اجرا
|
||||||
|
- [docs/bank-flow.md](docs/bank-flow.md) — نیازمندیهای اصلی چالش
|
||||||
|
- [docs/project-blueprint.md](docs/project-blueprint.md) — Blueprint تفصیلی پیادهسازی
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
# تست BankFlow
|
||||||
|
|
||||||
|
[English](TESTING.md) | [فارسی](TESTING.fa.md)
|
||||||
|
|
||||||
|
BankFlow از Pest 4 استفاده میکند. Feature Testها با Application Test Case مربوط به Laravel و `LazilyRefreshDatabase` اجرا میشوند. فایل `phpunit.xml` پایگاه داده SQLite درون حافظه، Cache و Session و Mail آرایهای و Queue Driver همزمان را انتخاب میکند.
|
||||||
|
|
||||||
|
## اجرای مجموعه تست
|
||||||
|
|
||||||
|
```bash
|
||||||
|
php artisan test --compact
|
||||||
|
```
|
||||||
|
|
||||||
|
برای اجرای تست متمرکز، برای نمونه از فرمانهای زیر استفاده کنید:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
php artisan test --compact tests/Feature/Loan/ProcessLoanTest.php
|
||||||
|
php artisan test --compact --filter="approves a valid personal loan"
|
||||||
|
```
|
||||||
|
|
||||||
|
## پوشش Unit Test
|
||||||
|
|
||||||
|
Unit Testها هر Stage کسبوکار را بهطور مستقل بررسی میکنند:
|
||||||
|
|
||||||
|
- اعتبارسنجی موفق و دلیل اولین خطا برای هر قانون؛
|
||||||
|
- Prefixهای تقلب و بررسی دستی؛
|
||||||
|
- نتایج بررسی ضامن؛
|
||||||
|
- مرزهای اعتبار مالی و قوانین Overrideشده؛
|
||||||
|
- قابل اجرا بودن تأیید مدیر، ضریب درآمد و نتایج Pass/Fail.
|
||||||
|
|
||||||
|
این تستها رفتار خالص Stage را بدون هماهنگسازی HTTP یا پایگاه داده بررسی میکنند.
|
||||||
|
|
||||||
|
## پوشش Feature و Integration
|
||||||
|
|
||||||
|
Feature Testها ایجاد Loan، اعتبارسنجی معنایی تأخیری، Request ناقص، نوع تسهیلات خارج از دسترس، مسیرهای پردازش شخصی و تجاری، بررسی دستی تقلب یا اعتبار، تأیید مشروط مدیر، Idempotency وضعیت نهایی، دریافت اطلاعات، تاریخچه مرتب، خطاهای مستند Not Found و Health Check را پوشش میدهند.
|
||||||
|
|
||||||
|
Integration Testهای مدیریتی دسترسی احرازهویتشده به پنل، نمایش فقطخواندنی Loan، ویرایش Workflow فقط در وضعیت Draft، کپی نسخه Workflow، انتشار اعتبارسنجیشده، Archive اتمیک نسخه قبلی و تأیید محافظتشده مدیر را بررسی میکنند. این تستها مرزهای HTTP/Filament، Action، Eloquent، Migration و Resource را در بر میگیرند.
|
||||||
|
|
||||||
|
## موارد پوششدادهنشده
|
||||||
|
|
||||||
|
مجموعه فعلی، Requestهای همزمان واقعی یا رفتارهای مخصوص PostgreSQL مانند رقابت قفل ردیف و Partial Published-workflow Index را آزمایش نمیکند. همچنین Docker Smoke Test، Browser Test فرانتاند، تست Performance/Load، تست امنیت، تزریق Exception پیشبینینشده برای بررسی Rollback و Integration واقعی تقلب یا اعتبار پوشش داده نشدهاند؛ چالش برای این موارد از قوانین قطعی و Mockشده استفاده میکند.
|
||||||
+40
@@ -0,0 +1,40 @@
|
|||||||
|
# BankFlow Testing
|
||||||
|
|
||||||
|
[English](TESTING.md) | [فارسی](TESTING.fa.md)
|
||||||
|
|
||||||
|
BankFlow uses Pest 4. Feature tests run with Laravel's application test case and `LazilyRefreshDatabase`; `phpunit.xml` selects an in-memory SQLite database, array-backed cache/session/mail, and the synchronous queue driver.
|
||||||
|
|
||||||
|
## Run the suite
|
||||||
|
|
||||||
|
```bash
|
||||||
|
php artisan test --compact
|
||||||
|
```
|
||||||
|
|
||||||
|
Run a focused group with, for example:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
php artisan test --compact tests/Feature/Loan/ProcessLoanTest.php
|
||||||
|
php artisan test --compact --filter="approves a valid personal loan"
|
||||||
|
```
|
||||||
|
|
||||||
|
## Unit coverage
|
||||||
|
|
||||||
|
Unit tests exercise each business stage independently:
|
||||||
|
|
||||||
|
- validation success and each first-failure reason;
|
||||||
|
- fraud and manual-review prefixes;
|
||||||
|
- guarantor outcomes;
|
||||||
|
- credit boundaries and overridden rules;
|
||||||
|
- manager-approval applicability, income multiplier, and pass/fail results.
|
||||||
|
|
||||||
|
These tests verify pure stage behavior without HTTP or database orchestration.
|
||||||
|
|
||||||
|
## Feature and integration coverage
|
||||||
|
|
||||||
|
Feature tests cover loan creation, deferred semantic validation, malformed requests, unavailable types, personal and business processing paths, fraud/credit manual review, conditional manager approval, terminal-state idempotency, retrieval, ordered history, documented not-found errors, and health checks.
|
||||||
|
|
||||||
|
Administrative integration tests cover authenticated panel access, read-only loan views, draft-only workflow editing, copying workflow versions, validated publication, atomic archival of the prior version, and guarded manager approval. These tests cross the HTTP/Filament, Action, Eloquent, migration, and resource boundaries.
|
||||||
|
|
||||||
|
## Known gaps
|
||||||
|
|
||||||
|
The suite does not currently exercise real concurrent requests or PostgreSQL-specific behavior such as row-lock contention and the partial published-workflow index. It also omits Docker smoke tests, frontend browser tests, performance/load tests, security testing, unexpected-exception rollback injection, and real external fraud/credit integrations (the challenge uses deterministic mocked rules).
|
||||||
Reference in new issue
Block a user