Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions src/checklist/fromParts.ts
Original file line number Diff line number Diff line change
@@ -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<string> = new Set(['Turnover', 'GuestExperience']);

/** The slice of a `cmms_pmTemplates` doc composition reads. */
export interface ChecklistTemplatePart {
id: string;
Expand All @@ -26,7 +31,11 @@ export interface WorkOrderChecklistParts {
};
org: { cmms?: { checklistDefaults?: Record<string, string> } } | 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;
}

Expand All @@ -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 } : {}),
Expand All @@ -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];
}
2 changes: 2 additions & 0 deletions src/types/property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
Expand Down
93 changes: 93 additions & 0 deletions tests/checklist/fromParts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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<typeof buildWorkOrderChecklistFromParts>) => 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']);
});
});
Loading