# Domain Module Template (Production-Safe)

Use this as a baseline for Sales, Inventory, and Accounting modules.

## Folder Layout

- domain/
- domain/<domain>.model.ts
- domain/<domain>.domain.ts
- application/
- application/<use-case>.mapper.ts
- application/<domain>.repository.ts
- application/<domain>-application-service.ts
- infrastructure/
- infrastructure/json-<domain>.repository.ts
- infrastructure/postgres-<domain>.repository.ts
- infrastructure/<domain>.repository.factory.ts

## Rules

1. Keep domain functions pure and deterministic.
2. Put orchestration and side effects in application service.
3. Keep repository interface in application layer.
4. Keep adapters in infrastructure layer.
5. Keep DTO mapping at boundaries only.
6. Preserve legacy behavior while introducing service wiring.

## Testing Baseline

- Unit tests for domain calculations and transitions.
- Contract tests for repository adapters.
- Optional Postgres contract tests gated by env flag.

## Wiring Pattern

1. Existing action validates request.
2. Action maps DTO -> command.
3. Action calls application service.
4. Action handles framework concerns:
   - revalidatePath
   - redirects
   - transport response shape

## Migration Checklist

- [ ] Extract pure rules from legacy layer.
- [ ] Introduce repository interface.
- [ ] Build JSON adapter first.
- [ ] Build Postgres adapter.
- [ ] Add contract test suite.
- [ ] Wire one use case in actions.
- [ ] Keep response shape unchanged.
- [ ] Add feature flag if risk is high.
