feat: complete admission workspace

This commit is contained in:
2026-07-02 17:30:24 -07:00
parent c394d85236
commit 9480030391
13 changed files with 1335 additions and 350 deletions

View File

@@ -191,6 +191,66 @@ export async function getOrdersWithItems(params: {
## Advanced SQL Patterns
## Scenario: Admission and Bed Mutations
### 1. Scope / Trigger
- Trigger: changing elder admission, transfer, discharge, or bed occupancy behavior.
- These mutations span `admissions`, `beds`, and `elders`, so partial writes are not acceptable.
### 2. Signatures
- `POST /api/admissions`: admit an elder or transfer an active elder to a new available bed.
- `PATCH /api/admissions/[id]`: discharge an active admission and release its bed.
- Mutations use `getDatabase().transaction(async (transaction) => ...)`.
### 3. Contracts
- `POST /api/admissions` request: `{ elderId: string; bedId: string; notes?: string }`.
- `PATCH /api/admissions/[id]` request: `{ notes?: string }`.
- Success responses include `{ success: true, reason: string, admission }`.
- Failure responses include `{ success: false, reason: string }`.
### 4. Validation & Error Matrix
- Missing active organization -> `400`.
- Missing elder or bed -> `404`.
- Target bed not `available` -> `409`.
- Discharge target not found -> `404`.
- Discharge target not `active` -> `409`.
- Insert/update returning no row -> `500`.
### 5. Good/Base/Bad Cases
- Good: return a typed mutation result from inside the transaction, then convert it to `jsonFailure` or `jsonSuccess` outside the transaction.
- Base: read-only admission lists may use the temporary `readData()` compatibility model while the domain helper layer is being extracted.
- Bad: throw ordinary `Error` for business conflicts such as occupied beds, because it loses the structured status/reason contract.
### 6. Tests Required
- `pnpm lint`
- `pnpm type-check`
- `pnpm build`
- Integration assertions: occupied-bed admission returns `409`; transfer closes the old active admission and frees the old bed; discharge closes the active admission, frees the bed, and marks the elder discharged.
### 7. Wrong vs Correct
#### Wrong
```ts
if (bed.status !== "available") {
throw new Error("床位不可分配");
}
```
#### Correct
```ts
if (bed.status !== "available") {
return { success: false, reason: "床位不可分配", status: 409 };
}
```
### JSON Column Operations
When using PostgreSQL JSON/JSONB columns, proper casting is required for JSON functions.