--- phase: 03-event-write-back-pwa-install plan: 02 type: tdd wave: 2 depends_on: ["03-01"] files_modified: - apps/api/src/broker/vevent.ts - apps/api/src/broker/write.ts - apps/api/tests/broker/vevent.test.ts - apps/api/tests/broker/write.test.ts autonomous: true requirements: [CAL-04, CAL-05, CAL-06, CAL-07] user_setup: [] must_haves: truths: - "buildVeventString produces a valid VCALENDAR/VEVENT for timed, all-day, and recurring events" - "All-day events serialize as DATE (no time component, no TZID) per D-13 — never coerced to DATETIME" - "createCalendarEvent / updateCalendarEvent / deleteCalendarEvent route all Fastmail writes through tsdav with correct If-Match/If-None-Match" artifacts: - path: "apps/api/src/broker/vevent.ts" provides: "buildVeventString(NewEventParams) → { uid, icsString }" exports: ["buildVeventString", "NewEventParams", "RRULE_PRESETS"] min_lines: 40 - path: "apps/api/src/broker/write.ts" provides: "tsdav PUT/DELETE wrappers (broker boundary, D-12)" exports: ["createCalendarEvent", "updateCalendarEvent", "deleteCalendarEvent"] key_links: - from: "apps/api/src/broker/vevent.ts" to: "ical.js ICAL.Component / ICAL.Time" via: "VEVENT construction" pattern: "ICAL\\.(Component|Time)" - from: "apps/api/src/broker/write.ts" to: "tsdav createCalendarObject/updateCalendarObject/deleteCalendarObject" via: "FastmailClient methods" pattern: "(create|update|delete)CalendarObject" --- Build the two pure broker primitives every write slice depends on: `vevent.ts` (construct a valid iCalendar VEVENT from form params) and `write.ts` (wrap tsdav's three CalDAV write methods to enforce the broker boundary, D-12). These are the most testable units in the phase — defined input → defined ICS/HTTP output — so they are built TDD against the RED stubs from Plan 01. Purpose: CAL-04/05/06/07 all reduce to "produce the right VEVENT and PUT/DELETE it through tsdav." Getting the D-13 DATE-vs-DATETIME split and the If-Match wiring right here means the worker (Plan 03) and endpoints (Plan 04) just orchestrate. Output: `vevent.ts`, `write.ts`, both GREEN against their Plan 01 test files. @$HOME/.claude/get-shit-done/workflows/execute-plan.md @$HOME/.claude/get-shit-done/templates/summary.md @.planning/phases/03-event-write-back-pwa-install/03-RESEARCH.md @.planning/phases/03-event-write-back-pwa-install/03-PATTERNS.md @apps/api/src/broker/client.ts @apps/api/src/broker/sync.ts Task 1: GREEN — buildVeventString VEVENT builder (vevent.ts) apps/api/src/broker/vevent.ts, apps/api/tests/broker/vevent.test.ts - apps/api/tests/broker/vevent.test.ts (RED stubs from Plan 01 — these define the contract) - .planning/phases/03-event-write-back-pwa-install/03-RESEARCH.md (§Pattern 1 — full buildVeventString reference incl. NewEventParams; §Pitfall 3 — DATE vs DATETIME) - apps/api/src/broker/sync.ts (lines ~89-101 — the existing D-13 isDate split this must mirror in reverse) - .planning/phases/03-event-write-back-pwa-install/03-PATTERNS.md (§vevent.ts — ICAL import, D-13 split, error isolation) RED → GREEN. Tests assert: - Timed event: output contains `BEGIN:VEVENT`, `DTSTART:` with a `Z` UTC suffix (no TZID param), matching UID and SUMMARY. - All-day event (allDay:true): DTSTART is a DATE value (`VALUE=DATE` or 8-digit YYYYMMDD with no `T`/time), NO TZID, NO time component (D-13). End is also DATE. - Recurring: passing `rruleString: 'FREQ=WEEKLY'` yields an `RRULE:FREQ=WEEKLY` line. - location/description optional properties appear only when provided. - omitting `uid` generates a `@familysync` UID via crypto.randomUUID(). Implement `buildVeventString(params: NewEventParams): { uid: string; icsString: string }` exactly per RESEARCH.md Pattern 1. Export the `NewEventParams` interface and a `RRULE_PRESETS` map (`daily:'FREQ=DAILY'`, `weekly:'FREQ=WEEKLY'`, `monthly:'FREQ=MONTHLY'`, `yearly:'FREQ=YEARLY'`). Use `import ICAL from 'ical.js'` and `import { randomUUID } from 'crypto'`. For all-day use `new ICAL.Time({ year, month, day, isDate: true })`; for timed use `ICAL.Time.fromJSDate(date, true)` (useUTC=true → Z suffix, no TZID). Always add VERSION 2.0 and PRODID `-//FamilySync//FamilySync//EN`. Use `.js`-suffixed relative imports if any. Never coerce DATE→DATETIME. cd /home/luc/Projects/familysync && pnpm --filter @familysync/api test -- broker/vevent - `pnpm --filter @familysync/api test -- broker/vevent` is GREEN (all assertions pass). - All-day test asserts no `T000000`/time component and no `TZID` in the DATE DTSTART. - `grep -q "RRULE_PRESETS" apps/api/src/broker/vevent.ts`. buildVeventString passes all vevent.test.ts cases including the D-13 DATE-vs-DATETIME split and RRULE serialization. Task 2: GREEN — tsdav write wrappers (write.ts) apps/api/src/broker/write.ts, apps/api/tests/broker/write.test.ts - apps/api/tests/broker/write.test.ts (RED stubs from Plan 01 — the contract) - .planning/phases/03-event-write-back-pwa-install/03-RESEARCH.md (§Pattern 2 — full write.ts reference; status-code interpretation; §Pitfall 4 — etag may be null) - apps/api/src/broker/client.ts (FastmailClient type; .js import convention; named-export style) - .planning/phases/03-event-write-back-pwa-install/03-PATTERNS.md (§write.ts — header/imports/exports pattern) RED → GREEN. With a mock FastmailClient, tests assert: - createCalendarEvent({client, calendar, uid, icsString}) calls `client.createCalendarObject` with `filename === \`${uid}.ics\`` and the iCalString, and returns the raw Response. - updateCalendarEvent(client, calendarObjectUrl, icsString, etag) calls `client.updateCalendarObject` with calendarObject `{ url, data, etag }` — etag drives the If-Match header. - deleteCalendarEvent(client, calendarObjectUrl, etag) calls `client.deleteCalendarObject` with `{ url, etag }`. - A null etag is passed through as `''` (no crash). Implement `createCalendarEvent`, `updateCalendarEvent`, `deleteCalendarEvent` per RESEARCH.md Pattern 2 as named exports returning `Promise`. Import `FastmailClient` from `./client.js` and `DAVCalendar` from `tsdav`. These functions are the ONLY place outside client.ts/sync.ts/poller.ts that touch tsdav write methods (D-12 broker boundary). Do not interpret status codes here — return the raw Response so the worker (Plan 03) classifies transient/hard/conflict. If `deleteCalendarObject` requires a `data` field, pass `''`. cd /home/luc/Projects/familysync && pnpm --filter @familysync/api test -- broker/write && pnpm --filter @familysync/api exec tsc --noEmit - `pnpm --filter @familysync/api test -- broker/write` is GREEN. - `grep -Eq "createCalendarObject|updateCalendarObject|deleteCalendarObject" apps/api/src/broker/write.ts` (all three present). - tsc --noEmit passes. write.ts wraps all three tsdav write methods with correct filenames/If-Match wiring; tests GREEN; types compile. ## Trust Boundaries | Boundary | Description | |----------|-------------| | broker → Fastmail CalDAV | Only write.ts issues PUT/DELETE to Fastmail (D-12) | ## STRIDE Threat Register | Threat ID | Category | Component | Disposition | Mitigation Plan | |-----------|----------|-----------|-------------|-----------------| | T-03-03 | Tampering | VEVENT field serialization (summary/location/description with special chars) | mitigate | ical.js ICAL.Component handles line-folding + escaping (commas, semicolons, newlines); never hand-roll ICS strings (RESEARCH §Don't Hand-Roll) | | T-03-04 | Spoofing | etag forgery to bypass conflict detection | mitigate | etag is sourced server-side (calendarEvents.etag) by the worker, never accepted from the browser; write.ts only forwards what the server supplies | | T-03-05 | Elevation of Privilege | write.ts called with another member's calendar | accept (here) | Calendar ownership is enforced at the route layer (Plan 04, V4); write.ts is a low-level primitive with no auth context | - `pnpm --filter @familysync/api test -- broker/vevent` GREEN. - `pnpm --filter @familysync/api test -- broker/write` GREEN. - `pnpm --filter @familysync/api exec tsc --noEmit` passes. - VEVENT builder correct for timed, all-day (DATE), and recurring events. - tsdav write wrappers enforce the broker boundary with correct If-Match/filename wiring. Create `.planning/phases/03-event-write-back-pwa-install/03-02-SUMMARY.md` when done.