Skip to content

fix(#2086): generate the saved narrative from the updated metadata - #2128

Open
clinnygee wants to merge 1 commit into
mainfrom
issue/2086
Open

clinnygee wants to merge 1 commit into
mainfrom
issue/2086

Conversation

@clinnygee

Copy link
Copy Markdown
Collaborator

Fixes #2086

The problem

When a form is saved, QuestionnaireResponse.authored is updated, but the "Date Authored" value in the narrative heading is not. The narrative no longer represents the resource content. Example resource showing the mismatch.

The cause

saveQuestionnaireResponse in apps/smart-forms-app/src/api/saveQr.ts built the whole saved resource in a single object literal:

questionnaireResponseToSave = {
  ...questionnaireResponseToSave,
  text: { div: qrToHTML(questionnaire, questionnaireResponseToSave) },  // ← ran first
  subject: {...},
  author: {...},
  authored: dayjs().format()                                            // ← set after
};

qrToHTML was called on the incoming clone, before subject, author and authored were replaced. Every save therefore wrote a narrative carrying the previous save's values.

This affects all three fields in the narrative heading — Patient and Author as well as Date Authored — though Date Authored is the one that changes on every save and so is the one that shows up.

The change

Set the metadata first, then generate text.div from the updated resource:

questionnaireResponseToSave = {
  ...questionnaireResponseToSave,
  meta: {...}, subject: {...}, author: {...},
  authored: dayjs().format()
};

// Generate the narrative last, so it renders the subject, author and authored values set above
// rather than the stale ones the response came in with
questionnaireResponseToSave.text = {
  status: 'generated',
  div: qrToHTML(questionnaire, questionnaireResponseToSave)
};

No change to qrToHTML itself — it was always rendering faithfully, just from the wrong input.

Testing

Added a regression test to saveQr.test.ts that saves a response carrying stale subject, author and authored, then asserts qrToHTML received the fresh values. Confirmed it fails against the old ordering:

✕ generates the narrative from the updated subject, author and authored values
  Expected: "2025-08-20T14:32:27+10:00"
  Received: "2020-01-01T00:00:00+10:00"
Check Result
saveQr.test.ts + preview.test.ts 67 passed
npm run lint (repo root) 0 errors
tsc --noEmit for the app Clean

The renderer build was not needed — nothing under packages/smart-forms-renderer/src/ changed.

🤖 Generated with Claude Code

saveQuestionnaireResponse built the whole resource in one object literal,
which called qrToHTML on the incoming clone before subject, author and
authored were replaced. Every save therefore wrote a narrative heading
carrying the previous save's Patient, Author and Date Authored values,
so the narrative no longer represented the resource content.

Set the metadata first, then generate text.div from the updated resource.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

Narrative generator is not updating date authored date

1 participant