Skip to content

Add audit wizard with responses and action raising#4

Draft
jonbur wants to merge 15 commits into
mainfrom
cursor/audit-wizard-af56
Draft

Add audit wizard with responses and action raising#4
jonbur wants to merge 15 commits into
mainfrom
cursor/audit-wizard-af56

Conversation

@jonbur

@jonbur jonbur commented Jul 1, 2026

Copy link
Copy Markdown
Owner

Summary

Implements the audit wizard (CONTEXT.md build step 3) — section-by-section guided audit with responses and actions persisted to the database on every interaction.

Audit flow

  • /premises/[id]/audit — start or continue a draft audit (requires completed profile)
  • /premises/[id]/audit/[auditId]?section=... — full wizard with section navigation

Key behaviours

  • Resume support: draft audits are reused; AuditSection rows created from applicableSections
  • Stale draft repair: continuing a draft syncs section IDs and template from the current profile
  • GROUP items: compound topics render as labelled groups with individually answerable sub-questions (official September 2025 template structure)
  • Save on every answer: PUT /api/audit/[auditId]/responses upserts each response immediately (including sub-question IDs like s1-q1a)
  • Section progress: status auto-updates based on answerable sub-question count
  • Profile-aware questions: items with profile:* tags filtered via lib/audit-items.ts
  • Raise actions: modal form creates Action with sourceType: AUDIT and sets needsAction on the sub-question

API routes

Route Purpose
POST /api/premises/[id]/audit Start or resume draft audit
GET /api/audit/[auditId] Audit overview + section progress
GET /api/audit/[auditId]/sections/[sectionId] Section questions + saved responses
PUT /api/audit/[auditId]/responses Save a single response
POST /api/audit/[auditId]/actions Raise action from audit item

Components

AuditWizard, SectionNav, QuestionCard, QuestionGroupCard, ResponseInput, ActionRaiseForm

Tested

npm run db:seed
npm run build
# Template: 26 sections, 197 answerable sub-questions (dev profile: 23 sections)
# GROUP items render with nested sub-questions; each saves independently

Try it

http://localhost:3000/premises/dev-premises-hut/audit

Open in Web Open in Cursor 

cursoragent and others added 4 commits July 1, 2026 16:48
- Rewrite audit seed with official September 2025 section titles (25 sections, 100 items)
- Add lib/sections.ts to compute applicableSections from profile flags
- Add 5-step premises profile wizard at /premises/[id]/profile
- Add profile GET/PUT API with organisation-scoped access
- Seed dev organisation and premises for local testing

Co-authored-by: jonbur <jonbur@users.noreply.github.com>
- lib/audit.ts: start/resume audit, save responses, section status, actions
- lib/audit-items.ts: filter questions by premises profile tags
- API routes for audit lifecycle, responses, and actions
- AuditWizard with SectionNav, QuestionCard, ResponseInput, ActionRaiseForm
- Pages at /premises/[id]/audit and /premises/[id]/audit/[auditId]
- Responses saved to DB on each answer; actions linked to audit items
- Seed dev premises profile for audit testing

Co-authored-by: jonbur <jonbur@users.noreply.github.com>
- Widen app shell from max-w-3xl to max-w-6xl
- Use two-column grid from sm breakpoint (640px) instead of lg
- Sticky scrollable section nav on the left

Co-authored-by: jonbur <jonbur@users.noreply.github.com>
- Add needsAction flag on AuditResponse (separate from yes/no answer)
- Yes shows mandatory 'Provide details' notes field before saving
- Needs action checkbox available for Yes and No answers
- Section progress only counts complete answers (Yes requires notes)
- API validates response rules server-side

Co-authored-by: jonbur <jonbur@users.noreply.github.com>
@cursor cursor Bot force-pushed the cursor/audit-wizard-af56 branch from 067547f to 605893c Compare July 1, 2026 17:16
cursoragent and others added 11 commits July 1, 2026 18:01
Co-authored-by: jonbur <jonbur@users.noreply.github.com>
- Import full audit-template-seed.ts as source of truth (26 sections, 122 items)
- Add normalization layer flattening subsections (e.g. s14 staff/volunteers)
- Support YES_NO_NA_ACTION, OPEN_TEXT, and DATE_UPLOAD response types
- Update section/item profileFlag filtering to match template metadata
- Section IDs now s1–s26; reseed updates AuditTemplate JSON

Co-authored-by: jonbur <jonbur@users.noreply.github.com>
Draft audits created before the September 2025 template update stored
section IDs that no longer match the active template, so buildAuditOverview
returned no sections and the wizard page hit notFound().

Sync draft audit sections from the current profile on load and resume,
recomputing applicableSections and attaching the active template when needed.
Show a clear recovery message if no sections apply after sync.

Co-authored-by: jonbur <jonbur@users.noreply.github.com>
Replace the flattened audit template with the official September 2025
structure where compound topics use GROUP items with individually
answerable sub-questions.

- Extend template types with GROUP, subQuestions, and label fields
- Normalize section subsections and group items without flattening
- Count and save responses per sub-question for progress tracking
- Render grouped questions with QuestionGroupCard in the audit wizard

Co-authored-by: jonbur <jonbur@users.noreply.github.com>
QuestionCard synced local state from the entire item prop on every parent
re-render. In grouped sections, sibling saves created new item objects and
wiped unsaved Yes/details selections.

Only re-sync when the stored server response changes, debounce text saves,
and flush valid pending answers on unmount.

Co-authored-by: jonbur <jonbur@users.noreply.github.com>
Debounced saves set savingItemId which disabled the textarea, causing it
to grey out and lose focus mid-typing. Keep textareas editable during
background saves, skip note sync while focused, and slow autosave to 800ms.

Co-authored-by: jonbur <jonbur@users.noreply.github.com>
Unmount and debounced saves used activeSectionId, so leaving a section
could flush answers against the newly selected section (e.g. Electrical)
and fail with 'Question not found in this section'.

Pass the rendered section id with each save and resolve items across the
audit template on the server as a fallback.

Co-authored-by: jonbur <jonbur@users.noreply.github.com>
Add AuditResponse.recordedDate (PostgreSQL DATE) for certificate and
inspection dates. DATE_UPLOAD questions now render a native date picker
instead of a free-text field, with validation and autosave on change.

Open-text questions continue to use the textarea; completion logic
checks recordedDate for date items and notes for text items.

Co-authored-by: jonbur <jonbur@users.noreply.github.com>
Load linked Action records from the database per question instead of
inferring from the needsAction flag, which caused a flash of the Raise
action button after navigation. Items with an existing action now show
a stable Edit action link that opens the action form in edit mode.

Add PATCH /api/actions/[actionId] for updating actions.

Co-authored-by: jonbur <jonbur@users.noreply.github.com>
Remove the Needs action toggle from audit questions. Each item now has
an Actions panel with list, add, edit, and delete backed by Action
records in the database. Creating an action no longer mutates the audit
response needsAction flag.

Co-authored-by: jonbur <jonbur@users.noreply.github.com>
Co-authored-by: jonbur <jonbur@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants