Commit 7206323e authored by Christos Tranoris's avatar Christos Tranoris
Browse files

Service Order Ref input on the Hold / Firm reservation forms

parent 256e0508
Loading
Loading
Loading
Loading
+1 −1

File changed.

Preview size limit exceeded, changes collapsed.

+61
Changes for docs/20260830_1935_serviceorderref_reservation_forms.md: 61 added lines, 0 removed lines.
Original line number Diff line number Diff line
# Service Order Ref input on the Hold / Firm reservation forms

**Date:** 2026-08-30 19:35

## Problem

The Chronos contract (`api-docs.json` → `FeasibilityRequest`) accepts an optional
`serviceOrderRef` — *"Originating service order, stamped on the reservation and
usable as a cancellation selector"* — on both `POST /api/v1/reservations` (firm)
and the hold endpoint. Requirement **R7** explicitly lists `serviceOrderRef` as
part of the firm-reservation POST.

The whole stack already carried it — `domain/gateways.ts` `FeasibilityDraft.serviceOrderRef`,
`encodeFeasibilityDraft` in `adapters/api/mappers/entities.ts`, and the
`useRequestHold` / `useCreateFirm` hooks pass the draft straight through — and it
is displayed back on `ReservationDetailPage` and filtered on in Activity / Purge.
**The only missing piece was the input control**, so every reservation created
from the UI was stamped with no service order.

## Change

- **`features/reservations/HoldRequestForm.tsx`** and **`FirmReservationForm.tsx`**
  — new optional `initialServiceOrderRef` prop, local `serviceOrderRef` state, a
  *Service Order Ref* `TextField` (placeholder "Optional") after the coverage-mode
  selector, and a conditional-spread into the `mutateAsync` draft:
  `...(serviceOrderRef.trim() ? { serviceOrderRef: serviceOrderRef.trim() } : {})`.
  `handleAlternativeSelect` untouched — an alternative only shifts time, so the
  service order carries over just like pool/resource.

- **`features/availability/bookingHandoff.ts`** — added `readServiceOrderRefParam`
  / `writeServiceOrderRefParam` next to the capacity pair, matching the existing
  query-string handoff contract. No caller writes it yet; `write*` exists for
  symmetry so a future handoff source (e.g. assessment → book) stays a one-liner.

- **`features/reservations/ReservationsPage.tsx`** — reads `?serviceOrderRef=` and
  passes `initialServiceOrderRef` to both forms, like `poolId` / `from` / `to`.

- **`adapters/api/mappers/entities.ts`** — `encodeFeasibilityDraft` now runs
  `serviceOrderRef` through the existing `omitBlank()` helper (as `encodePurgeDraft`
  already does) so a whitespace-only value is never sent on the wire.

## Tests

- `FirmReservationForm.test.tsx` / `HoldRequestForm.test.tsx` — a typed ref reaches
  the draft; a blank/whitespace field produces a draft with no `serviceOrderRef`.
- `ReservationsPage.test.tsx` — `?serviceOrderRef=SO-9` seeds the field on both tabs.
- `bookingHandoff.test.ts` — round-trip incl. trimming and the blank case.
- `entities.test.ts` — `encodeFeasibilityDraft` trims and omits blank refs.

All 6 affected test files pass (109 tests). `npx eslint` clean on the changed
files. Pre-existing `tsc -b` failures (MUI v9 theme typings, `src/test/*`) are
unrelated and documented in `CLAUDE.md`.

## Verification

1. `npx vitest run src/features/reservations src/features/availability/bookingHandoff.test.ts src/adapters/api/mappers/entities.test.ts`
2. `npm run lint`
3. `npm run dev` → **Reservations** → both tabs show a *Service Order Ref* field;
   create a firm reservation with `SO-4711`, open it in **Activity** → the
   *Service Order Ref* row shows `SO-4711`. `/reservations?serviceOrderRef=SO-1&tab=firm`
   pre-fills the field.
+1
Changes for docs/index.md: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

| Date | File | Description |
|---|---|---|
| 2026-08-30 19:35 | [Service Order Ref input on the reservation forms](20260830_1935_serviceorderref_reservation_forms.md) | `FeasibilityRequest.serviceOrderRef` ("originating service order, stamped on the reservation and usable as a cancellation selector") was supported end-to-end — domain draft, `encodeFeasibilityDraft`, the hold/firm hooks, the detail view, the Activity and Purge filters — but had **no input control**, so every reservation booked from the UI carried no service order; added a *Service Order Ref* `TextField` to both `HoldRequestForm` and `FirmReservationForm` (trimmed, conditional-spread into the draft so blank stays absent), a `?serviceOrderRef=` URL prefill via new `read/writeServiceOrderRefParam` in `bookingHandoff.ts` wired through `ReservationsPage`, and hardened `encodeFeasibilityDraft` to run the value through the existing `omitBlank()` helper; +8 tests |
| 2026-08-09 16:40 | ["Book whole span" now respects the requested duration](20260809_1640_book_whole_span_respects_duration.md) | The span handed the reservation form the searched horizon verbatim, so *Slot duration* was write-only on that path and a two-hour request booked every window the preview returned; the fix is **not** `To = From + duration` — a clipped span books *open time*, so eight hours is however many calendar windows add up to eight, and a wall-clock range that wide against a 9-to-5 calendar would book fewer — `spanExtent()` walks the preview accumulating window lengths and ends the span the moment the duration is met, inside the last window when only part of it is needed (exact, since the server clips the span to the calendar either way); a preview shorter than the duration, or no duration at all, still books the whole horizon, and the header caption now says "the first *k* of *n*" so the button reports what it will actually reserve; **Book separate** is unchanged, because the server does not apply the duration per window in that mode; +5 tests (1042) |
| 2026-08-09 16:05 | [Availability results — "Book whole span" is a button, and the row action says "Book separate"](20260809_1605_availability_book_span_button.md) | The clipped-mode span action lived in the `action` slot of an info `Alert`, which renders a low-emphasis text button — the primary way to use a `CLIP_TO_CALENDAR` preview looked like a footnote, and an info box is not where an operator looks for something to click; it is now a `contained` button in the results header row beside the window count, with one caption naming both scopes, and the per-row action reads **Book separate** while the span button is on screen (still plain "Book" in `STRICT`, still "Select" in the embedded picker); the gating condition became a named `spanBookable` flag read by all three; presentation only — both handlers and their query strings, including the agreed-suggestion resource handoff, are untouched; +2 tests (1037) |
| 2026-08-09 15:45 | [Nav: Availability moves next to Pools, Assessment leaves the drawer](20260809_1545_nav_availability_after_pools.md) | **Availability** moved from below Reservations to directly after **Pools** — it is asked *about* a pool, `/pools/{id}` links straight into it per resource, and the drawer now reads in the order the work happens (pick a pool → ask when it is free → book it); **Assessment** dropped from the sidebar while keeping its route, page and capability, the same call made for Memberships: it answers a question about a service order, and opened cold from the drawer there is no order to assess; the order regression test reads the rendered link list rather than the `navItems` array, so it also fails if a capability hides Pools from under it; the one pre-existing `Shell.tsx` `badge` tsc error changes only its message text, because the printed union now lists Availability second |
+125
Changes for plans/reflective-floating-valley.md: 125 added lines, 0 removed lines.
Original line number Diff line number Diff line
# Plan: Expose `serviceOrderRef` in the Hold / Firm reservation forms

## Context

The Chronos contract (`api-docs.json` → `FeasibilityRequest`) accepts an optional
`serviceOrderRef` — *"Originating service order, stamped on the reservation and
usable as a cancellation selector"* — on both `POST /api/v1/reservations` (firm)
and `POST /api/v1/holds` (hold). Requirement **R7** (`chronos-ui-requirements-v0.1.md:267`)
explicitly says a firm reservation POST SHALL include `serviceOrderRef`.

The whole stack already carries it:

- `domain/gateways.ts:46` — `FeasibilityDraft.serviceOrderRef?: string`
- `adapters/api/mappers/entities.ts:401` `encodeFeasibilityDraft` already emits
  `serviceOrderRef` when present
- `app/reservations.ts` `useRequestHold` / `useCreateFirm` pass the whole draft through
- It is displayed back in `features/activity/ReservationDetailPage.tsx:74` and
  filtered on in `ActivityPage` / `PurgePage`

**The only gap is the input control** — `FirmReservationForm.tsx` and
`HoldRequestForm.tsx` never let the operator enter a value, so every reservation
created from the UI is stamped with no service order. This plan adds the field to
both forms plus URL prefill, matching the existing `initialPoolId` / `from` / `to`
handoff pattern.

## Changes

### 1. `src/features/reservations/HoldRequestForm.tsx` and `src/features/reservations/FirmReservationForm.tsx`

Both files follow the same structure; apply the same edit to each.

- Add prop: `initialServiceOrderRef?: string` (default `''`) to the `Props`
  interface and the destructured params.
- Add state: `const [serviceOrderRef, setServiceOrderRef] = useState(initialServiceOrderRef);`
- Add a `TextField` in the form column, after `CoverageModeSelect` /
  before the submit `Button`, mirroring `AssessmentPage.tsx:94-100`:

  ```tsx
  <TextField
    label="Service Order Ref"
    value={serviceOrderRef}
    onChange={e => setServiceOrderRef(e.target.value)}
    size="small"
    placeholder="Optional"
  />
  ```

- In `handleSubmit`, add to the `mutateAsync` draft object (same conditional-spread
  idiom already used for `capacity`):

  ```tsx
  ...(serviceOrderRef.trim() ? { serviceOrderRef: serviceOrderRef.trim() } : {}),
  ```

- Leave `handleAlternativeSelect` untouched — an alternative only shifts time, the
  service order carries over, exactly like pool/resource today.

### 2. `src/features/availability/bookingHandoff.ts`

Add a read/write pair next to `readCapacityParams` / `writeCapacityParams`:

```ts
export function writeServiceOrderRefParam(ref: string): Record<string, string> {
  return ref.trim() ? { serviceOrderRef: ref.trim() } : {};
}

export function readServiceOrderRefParam(params: URLSearchParams): string {
  return params.get('serviceOrderRef') ?? '';
}
```

(`write*` is added for symmetry / future handoff sources — e.g. an
assessment→book deep link. No current caller writes it; that is fine.)

### 3. `src/features/reservations/ReservationsPage.tsx`

- `const initialServiceOrderRef = readServiceOrderRefParam(params);`
  (or inline `params.get('serviceOrderRef') ?? ''` to match the other lines there —
  pick whichever reads more consistently with the surrounding block).
- Pass `initialServiceOrderRef={initialServiceOrderRef}` to both
  `<HoldRequestForm>` and `<FirmReservationForm>`.

### 4. (Optional hardening) `src/adapters/api/mappers/entities.ts:401`

`encodeFeasibilityDraft` currently does `d.serviceOrderRef != null && {...}`.
Change to reuse the existing `omitBlank()` helper (`entities.ts:311`, already used
by `encodePurgeDraft`) so a stray whitespace string is not sent on the wire:

```ts
...(omitBlank(d.serviceOrderRef) != null && { serviceOrderRef: omitBlank(d.serviceOrderRef)! }),
```

The forms already trim, so this is defence-in-depth; include it only if it stays tidy.

## Tests

- **`src/features/reservations/FirmReservationForm.test.tsx`** and
  **`HoldRequestForm.test.tsx`** — add a case: fill pool/resource/dates + type a
  Service Order Ref, submit, assert the `useCreateFirm` / `useRequestHold` mock was
  called with `serviceOrderRef: 'SO-4711'`. Add a second case asserting a
  blank/whitespace field yields a draft with **no** `serviceOrderRef` key.
- **`src/features/reservations/ReservationsPage.test.tsx`** — render at
  `/reservations?serviceOrderRef=SO-9` and assert the field shows `SO-9` (both tabs).
- **`src/features/availability/bookingHandoff.test.ts`** — round-trip
  `writeServiceOrderRefParam` / `readServiceOrderRefParam`, including the blank case.
- **`src/adapters/api/mappers/entities.test.ts`** — if change 4 is applied, add a
  whitespace-only `serviceOrderRef` case to the existing `encodeFeasibilityDraft`
  tests (search for `encodeFeasibilityDraft` in that file).

## Verification

1. `npm test -- src/features/reservations src/features/availability/bookingHandoff.test.ts src/adapters/api/mappers/entities.test.ts`
2. `npm run lint` (boundary rules — no new cross-layer imports introduced here)
3. `npx vite build` (note: `npm run build` has a pre-existing MUI theme-typing
   failure unrelated to this change — see `CLAUDE.md`)
4. Manual: `npm run dev` → **Reservations** → both tabs now show a
   *Service Order Ref* field; enter `SO-4711`, create a firm reservation, then open
   it in **Activity → reservation detail** and confirm the *Service Order Ref* row
   shows `SO-4711`. Also confirm `/reservations?serviceOrderRef=SO-1&tab=firm`
   pre-fills the field.

## Docs

Per `CLAUDE.md`: add `docs/YYYYMMDD_HHMM_serviceorderref_reservation_forms.md`
describing the change and append a row to `docs/index.md`.
+19
Changes for src/adapters/api/mappers/entities.test.ts: 19 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -522,6 +522,25 @@ describe('coverageMode encoding', () => {
    expect(body.membershipId).toBe('m1');
  });

  it('sends a trimmed serviceOrderRef when the draft carries one', () => {
    const body = encodeFeasibilityDraft({
      poolId: 'p1', membershipId: 'm1', bookedStart: START, bookedEnd: END,
      serviceOrderRef: '  SO-4711  ',
    });
    expect(body.serviceOrderRef).toBe('SO-4711');
  });

  it('omits serviceOrderRef for an absent or whitespace-only value', () => {
    const absent = encodeFeasibilityDraft({
      poolId: 'p1', membershipId: 'm1', bookedStart: START, bookedEnd: END,
    });
    const blank = encodeFeasibilityDraft({
      poolId: 'p1', membershipId: 'm1', bookedStart: START, bookedEnd: END, serviceOrderRef: '   ',
    });
    expect('serviceOrderRef' in absent).toBe(false);
    expect('serviceOrderRef' in blank).toBe(false);
  });

  it.each([
    ['pool search', (m?: 'STRICT' | 'CLIP_TO_CALENDAR') =>
      encodePoolAvailabilitySearch({
Loading