Skip to content

CHI-4039: Record post survey answers as they are given (2/5) - #4856

Merged
gpaoloni merged 3 commits into
gian_CHI-4039from
gian_CHI-4039-record-answers
Nov 19, 2026
Merged

gpaoloni merged 3 commits into
gian_CHI-4039from
gian_CHI-4039-record-answers

Conversation

@gpaoloni

@gpaoloni gpaoloni commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Stacked PRs for CHI-4039 (save partial post-survey answers). Review and merge in order:

  1. CHI-4039: Extract post survey helpers (1/5) #4855 Refactor: extract the post survey helpers
  2. CHI-4039: Record post survey answers as they are given (2/5) #4856 (this PR) Record post survey answers as they are given (lambda service + Studio route)
  3. CHI-4039: Chatbot post surveys record answers on every turn (3/5) #4857 Chatbot post surveys record answers on every turn
  4. CHI-4039: Voice post surveys record each answer, Lex idle TTL to 15 min (4/5) #4858 twilio-iac: voice flows create their control task and record each answer, Lex idle TTL
  5. CHI-4039: Remove end-of-survey post survey save endpoints (5/5) #4859 Remove the end-of-survey save endpoints

Adds the mechanism to save post survey answers as they are given, instead of only when the survey ends, so a helpseeker who answers only some questions (or hangs up / leaves) still has those answers saved.

recordPostSurveyAnswers (new postSurvey/recordPostSurveyAnswers.ts) runs after every answer:

  1. Merges the answered questions into the answers stored on the post survey control task (postSurveyAnswers attribute). Empty answers never overwrite a recorded one.
  2. Rebuilds the Insights attributes from all the answers (idempotent), and updates the task with patchTaskAttributes (ETag retries).
  3. Upserts the post survey in HRM with all the answers so far (PATCH postSurveys/{controlTaskSid}), so a failed patch is repaired by the next answer.
  4. If there are no answered questions, it records nothing: blank surveys are never saved, neither on the task nor in HRM.

When the survey finishes the control task is canceled; if it's abandoned, the task expires. Either way the answers are already persisted, so no end-of-survey save is needed.

Voice (Studio) support:

  • New Studio route postSurvey/recordAnswers ({ controlTaskSid, answers, surveyComplete? }), documented in STUDIO_ROUTES.md. surveyComplete: true cancels the control task.
  • New Studio route postSurvey/createControlTask ({ contactTaskSid, contactId, language? } → { controlTaskSid }), also in STUDIO_ROUTES.md. A post survey flow calls it as its first widget, before any question, and sends the returned controlTaskSid to every postSurvey/recordAnswers call.
  • The post studio flow TaskRouter listener (postStudioFlowTaskRouterListener.ts) does not create the control task: it only triggers the configured flow and stays unaware of what that flow does (post surveys are one of its uses). The flow owns its control task.
  • The listener now passes the contact language to every post studio flow: the task's language, else the helpline's helplineLanguage, else en-US (same rule as the chat post survey; getTaskLanguage moved to task/taskLanguage.ts and is shared). It arrives as {{trigger.call.taskLanguage}} (inProgressCall) or {{flow.data.taskLanguage}} (rest). For rest flows this replaces the previous empty string when the task had no language; USNM's only use (startPostSurveyChatbot) already fell back to the same languages.

Deploying this doesn't affect current flows: nothing calls the new routes until the flows in PR 4 are applied.

This PR is not used by anything yet: chatbots switch in PR 3, voice flows in PR 4.

⚠️ Requires a new HRM endpoint (to be implemented in HRM)

PATCH /internal/{hrmApiVersion}/accounts/{hrmAccountId}/postSurveys/{taskId}
Body: { "contactTaskId": "WT…", "data": { "<question>": "<answer>", … } }
  • taskId is the post survey control task SID; (accountId, taskId) identifies one post survey.
  • Upsert: creates the post survey if it doesn't exist, otherwise replaces data (we always send every answer recorded so far) and bumps updatedAt.
  • 200 with the saved post survey; 400 if contactTaskId is missing when creating; other failures non-2xx (the lambda retries 3 times).
  • The existing POST postSurveys must stay until PR 5 ships.

Checklist

  • Corresponding issue has been opened
  • New tests added
  • Feature flags added
  • Strings are localized
  • Tested for chat contacts
  • Tested for call contacts

Other Related Issues

HRM: needs the PATCH postSurveys/{taskId} upsert described above.

Verification steps

  • Unit tests: npm run test:unit in lambdas/.
  • Current voice post-call surveys behave as before (saved at the end via hrm/savePostSurvey) until PR 4's flows are applied.
  • End-to-end verification happens with PRs 3 and 4.

AFTER YOU MERGE

  1. Cut a release tag using the Github workflow. Wait for it to complete and notify in the #aselo-deploys Slack channel.
  2. Comment on the ticket with the release tag version AND any additional instructions required to configure an environment to test the changes.
  3. Only then move the ticket into the QA column in JIRA

You are responsible for ensuring the above steps are completed. If you move a ticket into QA without advising what version to test, the QA team will assume the latest tag has the changes. If it does not, the following confusion is on you! :-P

🤖 Generated with Claude Code

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Overlapping requests can overwrite newer HRM answers with stale snapshots, while completion-only requests can finalize tasks without syncing previously stored answers to HRM.

2 open issues
What changed in this PR

Adds the Lambda-side foundation for saving partial post-survey answers in Aselo. This second PR in the CHI-4039 series prepares the service and Studio route; later PRs enable chatbot and voice callers, and HRM needs the new upsert endpoint.

Changes:

  • Merge answers into control-task attributes, rebuild Insights data, and send accumulated answers to HRM.
  • Add the Studio recording route and opt-in control-task creation without changing existing flow behavior by default.
  • Add unit tests and route documentation.
File Description
lambdas/​account-scoped/​tests/​unit/​studioFlow/​postStudioFlowTaskRouterListener.test.ts Tests optional control-task creation and SID propagation.
lambdas/​account-scoped/​tests/​unit/​postSurvey/​recordPostSurveyAnswers.test.ts Tests recording, completion, and failure handling.
lambdas/​account-scoped/​tests/​unit/​postSurvey/​postSurveyAnswers.test.ts Tests answer merging and Insights rebuilding.
lambdas/​account-scoped/​STUDIO_ROUTES.md Documents the new recording route.
lambdas/​account-scoped/​src/​studioFlow/​postStudioFlowTaskRouterListener.ts Creates optional control tasks before starting flows.
lambdas/​account-scoped/​src/​router.ts Registers the recording endpoint.
lambdas/​account-scoped/​src/​postSurvey/​recordPostSurveyAnswers.ts Implements incremental persistence and completion handling.
lambdas/​account-scoped/​src/​postSurvey/​postSurveyControlTask.ts Adds control-task creation and cancellation helpers.
lambdas/​account-scoped/​src/​postSurvey/​postSurveyAnswers.ts Adds answer filtering, merging, and Insights helpers.
lambdas/​account-scoped/​src/​configuration/​aseloConfiguration.ts Adds the optional createSurveyControlTask setting.

Review effort: Balanced

Comment on lines +91 to +95
const hrmResult = await patchOnInternalHrmEndpoint(
accountSid,
hrmApiVersion,
`postSurveys/${controlTaskSid}`,
hrmPatch,
Comment on lines +54 to +56
if (!Object.keys(answeredQuestions(answers)).length) {
console.debug(`${logPrefix} No answered questions, nothing to record.`);
return newOk({ recorded: false });

@stephenhand stephenhand left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the copilot have a point about overlapping answers? Probably an unlikely scenario right, the studio flow effectively serialises the requests?

if (targetParticipants.length === 1) {
const [participant] = targetParticipants;
try {
const controlTaskSid = await createSurveyControlTaskIfConfigured({

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why this has to be created now, instead of as a HTTP call in the first step in the studio flow?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No not real reason other than just looking for consistency in the behavior and taking out complexity for the studio flow.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should consider the current chat based studio flows as legacy in the long term, it makes no sense that we keep the hardcoded ones long term if we can also implement them in studio flows. For that reason I'd consider keeping hardcoded studio flow behaviour out of the system to be more of a priority

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I think you already explained this, but could you speak to why we can't create the task just in time when we record the first answer? We wouldn't need to bake studio flow specific stuff into task routers and the post studio flow system OR have an extra step in the studio flow then

@gpaoloni
gpaoloni force-pushed the gian_CHI-4039-record-answers branch 2 times, most recently from 4b6bbcc to 5cfabdc Compare November 18, 2026 19:41
@gpaoloni
gpaoloni requested a review from stephenhand November 18, 2026 19:54
gpaoloni and others added 3 commits November 19, 2026 12:37
Add recordPostSurveyAnswers, which stores the answered questions on the
post survey control task (raw answers plus their Insights attributes)
and upserts the post survey in HRM with all the answers so far. Blank
surveys record nothing.

Expose it to Studio as postSurvey/recordAnswers, and create the control
task before triggering a post studio flow whose configuration sets
createSurveyControlTask, passing its SID to the flow. Flows without the
flag keep working as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Keep the post studio flow TaskRouter listener unaware of what the flow it
triggers does: instead of creating the control task there (behind the
createSurveyControlTask flag), expose postSurvey/createControlTask so a
post survey flow creates it as its first step and passes the returned
controlTaskSid to postSurvey/recordAnswers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The post studio flow listener now sends the task language (or the helpline
language, or en-US) to the flow it triggers: as taskLanguage on the
inProgressCall redirect, and as the taskLanguage execution parameter for
rest flows, which used to get an empty string when the task had none.
postSurvey/createControlTask accepts it as an optional language, stored on
the control task like the chatbot post survey does.

getTaskLanguage moves to task/taskLanguage.ts so both listeners share it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@gpaoloni
gpaoloni force-pushed the gian_CHI-4039-record-answers branch from 5cfabdc to 2cd0acf Compare November 19, 2026 15:37
@gpaoloni
gpaoloni merged commit 22703b2 into master Nov 19, 2026
25 checks passed
@gpaoloni
gpaoloni deleted the gian_CHI-4039-record-answers branch November 19, 2026 16:01
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.

3 participants