From 182e64c6d6ca5fd010b5d4aba3ef3294a7b2883f Mon Sep 17 00:00:00 2001 From: echobob Date: Thu, 3 Dec 2026 01:45:55 -0500 Subject: [PATCH] feat(checklist): turn checklists carry the property's amenity inspection sections [TURNWRK-805] buildWorkOrderChecklistFromParts read only beds and baths from the property, so a pool or hot-tub property got no amenity inspection on a turn. Turnover and GuestExperience work orders now get the per-turn sections planForProperty yields for the property's amenities, after the template and before stay-specific items. Repair and the other types get none; '' (no checklist) opts out; no amenities composes as before. Property also gains externalListingUrl, which dispatch already writes. Refs: TURNWRK-805 Co-Authored-By: Claude Opus 5.5 --- src/checklist/fromParts.ts | 31 +++++++++-- src/types/property.ts | 2 + tests/checklist/fromParts.test.ts | 93 +++++++++++++++++++++++++++++++ 3 files changed, 120 insertions(+), 6 deletions(-) diff --git a/src/checklist/fromParts.ts b/src/checklist/fromParts.ts index 7b0a5e2..379cd24 100644 --- a/src/checklist/fromParts.ts +++ b/src/checklist/fromParts.ts @@ -1,14 +1,19 @@ /** * Server-side checklist composition for a new work order: pick the template - * (explicit `checklistTemplateId`, else the org's per-type default), then - * compose it with any stay-specific items. Pure over already-loaded parts, so - * every work-order writer — dispatch's API routes and Accept, cortex's - * auto-accept — composes the same checklist from the same inputs. + * (explicit `checklistTemplateId`, else the org's per-type default), add the + * property's amenity inspection sections on a turn, then compose it with any + * stay-specific items. Pure over already-loaded parts, so every work-order + * writer — dispatch's API routes and Accept, cortex's auto-accept — composes + * the same checklist from the same inputs. */ import type { ChecklistInputType, ChecklistTemplateSection, WorkOrderChecklist } from '../types/checklist'; +import { planForProperty } from '../maintenance/plan'; import { composeWorkOrderChecklist, type ChecklistCustomItemInput } from './compose'; import { templateSections } from './seed'; +/** Work-order types that walk the property between stays, so inspect its amenities (TURNWRK-805). */ +const AMENITY_INSPECTION_WO_TYPES: ReadonlySet = new Set(['Turnover', 'GuestExperience']); + /** The slice of a `cmms_pmTemplates` doc composition reads. */ export interface ChecklistTemplatePart { id: string; @@ -26,7 +31,11 @@ export interface WorkOrderChecklistParts { }; org: { cmms?: { checklistDefaults?: Record } } | null | undefined; templates: ChecklistTemplatePart[]; - property: { supply?: { beds?: number; baths?: number } } | null | undefined; + /** + * The property doc, or the slice of it composition reads. `amenities` takes + * classes or free-text labels; unknown ones are ignored. + */ + property: { supply?: { beds?: number; baths?: number; amenities?: readonly string[] } } | null | undefined; now?: number; } @@ -37,9 +46,12 @@ export function buildWorkOrderChecklistFromParts(args: WorkOrderChecklistParts): const template = templateId ? args.templates.find((t) => t.id === templateId) : undefined; const sections = template ? templateSections(template) : []; + // `''` is the dispatcher's "no checklist", so it opts out of these too. + const turn = AMENITY_INSPECTION_WO_TYPES.has(args.workOrder.type) && args.workOrder.checklistTemplateId !== ''; + const amenitySections = turn ? amenityInspectionSections(args.property?.supply?.amenities) : []; return composeWorkOrderChecklist({ - templateSections: sections, + templateSections: [...sections, ...amenitySections], customItems: args.workOrder.checklistCustomItems, ...(templateId ? { templateId } : {}), ...(template?.name ? { templateName: template.name } : {}), @@ -50,3 +62,10 @@ export function buildWorkOrderChecklistFromParts(args: WorkOrderChecklistParts): ...(args.now !== undefined ? { seededAt: args.now } : {}), }); } + +/** The per-turn sections `planForProperty` yields for these amenities. Firestore data arrives cast, so junk is dropped. */ +function amenityInspectionSections(amenities: unknown): ChecklistTemplateSection[] { + if (!Array.isArray(amenities)) return []; + const labels = amenities.filter((a): a is string => typeof a === 'string'); + return [...planForProperty({ amenities: labels, turnsPerMonth: 0 }).perTurnInspection.sections]; +} diff --git a/src/types/property.ts b/src/types/property.ts index 86c18aa..2db098d 100644 --- a/src/types/property.ts +++ b/src/types/property.ts @@ -377,6 +377,8 @@ export interface Property { name?: string; ownerId?: string; geo?: GeoCache; + /** Public listing page (Airbnb, Vrbo, direct site). Dispatch writes it (TURNWRK-633); nothing reads it yet. */ + externalListingUrl?: string; /** * SOP-13 scope roster. Absent = `active` (see `getPropertyLifecycleStatus`). diff --git a/tests/checklist/fromParts.test.ts b/tests/checklist/fromParts.test.ts index b88fb7f..be92051 100644 --- a/tests/checklist/fromParts.test.ts +++ b/tests/checklist/fromParts.test.ts @@ -5,6 +5,8 @@ */ import { describe, expect, it } from 'vitest'; import { buildWorkOrderChecklistFromParts } from '../../src/checklist'; +import { planForProperty } from '../../src/maintenance'; +import type { Property } from '../../src/types/property'; const template = { id: 'tpl-guest', @@ -53,3 +55,94 @@ describe('buildWorkOrderChecklistFromParts', () => { expect(buildWorkOrderChecklistFromParts({ ...parts, workOrder: { type: 'Repair', checklistTemplateId: '' } })).toBeUndefined(); }); }); + +describe('buildWorkOrderChecklistFromParts: amenity inspection sections (TURNWRK-805)', () => { + const poolAndHotTub = { supply: { beds: 2, baths: 1, amenities: ['pool', 'hot-tub'] } }; + const base = { org: null, templates: [template], now: 1 }; + const ids = (c: ReturnType) => c?.sections.map((s) => s.id); + + it('adds a section per amenity on a Turnover, after the template and before stay-specific items', () => { + const checklist = buildWorkOrderChecklistFromParts({ + ...base, + workOrder: { type: 'Turnover', checklistTemplateId: 'tpl-guest', checklistCustomItems: [{ label: 'Extra crib' }] }, + property: poolAndHotTub, + }); + expect(ids(checklist)).toEqual(['amenities', 'amenity.pool', 'amenity.hot-tub', 'stay-specific']); + expect(checklist?.templateId).toBe('tpl-guest'); + }); + + it("copies planForProperty's per-turn sections item for item", () => { + const planned = planForProperty({ amenities: ['pool', 'hot-tub'], turnsPerMonth: 0 }).perTurnInspection.sections; + const checklist = buildWorkOrderChecklistFromParts({ + ...base, + workOrder: { type: 'GuestExperience' }, + property: poolAndHotTub, + }); + expect(checklist?.sections.map((s) => ({ id: s.id, title: s.title, labels: s.items.map((i) => i.label) }))).toEqual( + planned.map((s) => ({ id: s.id, title: s.title, labels: s.items.map((i) => i.label) })), + ); + expect(checklist?.sections[0].items[0].id).toBe(`amenity.pool.${planned[0].items[0].id}`); + }); + + it('gives a GuestExperience work order the sections with no template at all', () => { + const checklist = buildWorkOrderChecklistFromParts({ ...base, workOrder: { type: 'GuestExperience' }, property: poolAndHotTub }); + expect(ids(checklist)).toEqual(['amenity.pool', 'amenity.hot-tub']); + expect(checklist?.templateId).toBeUndefined(); + }); + + it('gives a Repair work order none', () => { + const repair = buildWorkOrderChecklistFromParts({ + ...base, + workOrder: { type: 'Repair', checklistTemplateId: 'tpl-guest' }, + property: poolAndHotTub, + }); + expect(ids(repair)).toEqual(['amenities']); + expect(buildWorkOrderChecklistFromParts({ ...base, workOrder: { type: 'Repair' }, property: poolAndHotTub })).toBeUndefined(); + }); + + it("honours '' (the dispatcher chose no checklist): no amenity sections either", () => { + const checklist = buildWorkOrderChecklistFromParts({ + ...base, + workOrder: { type: 'Turnover', checklistTemplateId: '' }, + property: poolAndHotTub, + }); + expect(checklist).toBeUndefined(); + }); + + it('composes a property with no amenities exactly as before', () => { + const workOrder = { type: 'Turnover', checklistTemplateId: 'tpl-guest', checklistCustomItems: [{ label: 'Extra crib' }] }; + const without = buildWorkOrderChecklistFromParts({ ...base, workOrder, property: { supply: { beds: 2, baths: 1 } } }); + for (const property of [ + { supply: { beds: 2, baths: 1, amenities: [] } }, + { supply: { beds: 2, baths: 1, amenities: ['helipad', 'Free parking'] } }, + ]) { + expect(buildWorkOrderChecklistFromParts({ ...base, workOrder, property })).toEqual(without); + } + expect(ids(without)).toEqual(['amenities', 'stay-specific']); + expect(buildWorkOrderChecklistFromParts({ ...base, workOrder: { type: 'Turnover' }, property: { supply: {} } })).toBeUndefined(); + }); + + it('ignores unknown amenities and junk entries, keeping the known ones', () => { + const checklist = buildWorkOrderChecklistFromParts({ + ...base, + workOrder: { type: 'Turnover' }, + // Firestore data arrives cast, not validated. + property: { supply: { amenities: ['helipad', 42, 'Private hot tub', null] as unknown as string[] } }, + }); + expect(ids(checklist)).toEqual(['amenity.hot-tub']); + }); + + it('takes a whole Property document as its property part', () => { + const property: Property = { + id: 'p1', + orgId: 'o1', + address: 'example address', + externalListingUrl: 'https://example.com/listing/1', + supply: { amenities: ['pool'] }, + createdAt: 0, + updatedAt: 0, + }; + const checklist = buildWorkOrderChecklistFromParts({ ...base, workOrder: { type: 'Turnover' }, property }); + expect(ids(checklist)).toEqual(['amenity.pool']); + }); +});