Skip to content

Add company code autocomplete with organisation suggestions - #644

Merged
SmithR-IS merged 2 commits into
mainfrom
feature/company-code-autocomplete
Nov 20, 2026
Merged

SmithR-IS merged 2 commits into
mainfrom
feature/company-code-autocomplete

Conversation

@SmithR-IS

Copy link
Copy Markdown
Contributor

Company code autocomplete

Summary

Adds type-ahead suggestions to the Company code field. As a user types, it suggests the company codes already used on their organisation's records. Any value can still be entered.

Backend

  • New endpoint GET /records/organisations/{organisationId}/company-codes?search=&limit=. It returns the distinct company codes that contain the search text, ignoring case:
    • Codes are taken from the latest revision of each of the organisation's records, and discarded records are left out.
    • Codes that start with the search text come first, then the rest alphabetically.
    • Access uses the same organisation read check as GetOrganisationRecords (403 or 404).
    • The query is validated: search is at most 200 characters and limit is between 1 and 20, otherwise the endpoint returns a 400 ValidationProblemDetails.
  • RecordPageDto now includes organisationId, so dynamic form pages can look up suggestions for the organisation.
  • RecordProductDetail.CompanyCode is annotated with Display = "autocomplete:company-codes". The display hint names where the suggestions come from, so the frontend doesn't depend on question IDs. The format is documented on FormFieldAttribute.
  • The Medicine form version is bumped to 2026.11.20.1, with an updated fingerprint.

Frontend

  • New Autocomplete component that follows the WAI-ARIA 1.2 combobox pattern (list autocomplete):
    • Debounced requests, with any outdated request cancelled.
    • Keyboard support: arrow keys, Enter, Escape, Alt+Down and Tab.
    • Suggestion counts are announced in a live region.
    • The matching text in each suggestion is shown in bold.
    • If suggestions can't be fetched, it carries on as a plain text input.
  • Added to the component library at /portal/components/autocomplete.
  • Used for Company code on the create medicine record form, and on dynamic form pages through TextQuestion. A new FormPageContext gives questions the organisation ID. The hint is mapped to its source in lib/dynamic-form/suggestionSources.ts.
  • Regenerated the API client.

Screenshots / Evidence

image

Tests:

  • Backend: RecordServiceCompanyCodeTests covers matching, ranking, duplicates, wildcard escaping,
    discarded records, the latest revision, the limit, not allowed and not found. RecordControllerTests covers 200, the values passed to the service, 403, 404 and 400 validation. - Frontend: Autocomplete.test.tsx covers ARIA attributes, keyboard use, mouse selection, debounce
    and cancelling, leaving the field mid-request, failures and errors. FormPage.test.tsx and
    CreateMedicineRecordForm.test.tsx cover choosing a suggestion, free text, the required rule, and falling back to a plain input.

Risks and follow-up

  • Every keystroke loads all matching codes and ranks them in memory. The ILIKE '%…%' query hano index or SQL LIMIT. This is fine at current volumes, since each record has one code. If
    organisations grow large, the starts-with ranking and a cap could move into SQL.
  • The hint format changed. Display hints for text questions now use autocomplete:<source>. A bare autocomplete, or an unknown source, gives a plain input.
  • Source lookup isn't checked against built-in object keys. suggestionSources uses a plainobject lookup, so a hint naming a built-in key (for example autocomplete:constructor) would break
    the field. Only the backend sets the hint, so the risk is low. A small follow-up could add an Object.hasOwn check.
  • Form docs not yet updated. The form docs don't yet mention organisationId on RecordPageDto or the autocomplete:<source> hint (documentation/form-generation/high-level-design.md). Also, RecordPageQuestionDto.Display still only gives combobox as its example.

Checklist

  • Tests added or updated where appropriate
  • Linting and formatting pass
  • Accessibility considered for UI changes
  • Security/privacy impact considered
  • Documentation updated where needed

@twill742 twill742 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.

All looks good. Only note I have is that it may be worth adding a TODO to restrict the endpoint to only return results that are of the same record type (Vaccine or Medicine) as being currently populated (still scoped to the organisation obvs). We only really need to add this when we build out vaccines, but I don't want to forget

@SmithR-IS
SmithR-IS force-pushed the feature/company-code-autocomplete branch from 49cca2e to 65f9c4e Compare November 20, 2026 15:21
@SmithR-IS
SmithR-IS merged commit 7334eb8 into main Nov 20, 2026
4 of 8 checks passed
@SmithR-IS
SmithR-IS deleted the feature/company-code-autocomplete branch November 20, 2026 15:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants