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
185 changes: 185 additions & 0 deletions backend/openapi/UKPS.Api.json
Original file line number Diff line number Diff line change
Expand Up @@ -2596,6 +2596,169 @@
}
}
},
"/records/organisations/{organisationId}/company-codes": {
"get": {
"tags": [
"Record"
],
"summary": "Retrieves the company codes already used on an organisation's records that contain the\nsearch text, for suggesting while a user types a company code.",
"operationId": "GetOrganisationCompanyCodes",
"parameters": [
{
"name": "organisationId",
"in": "path",
"description": "The unique identifier of the organisation.",
"required": true,
"schema": {
"type": "integer",
"format": "int32"
}
},
{
"name": "Search",
"in": "query",
"description": "Gets or initialises the text the company codes must contain, ignoring case. A blank search\nreturns no suggestions.",
"schema": {
"maxLength": 200,
"type": "string"
}
},
{
"name": "Limit",
"in": "query",
"description": "Gets or initialises the maximum number of suggestions to return.",
"schema": {
"maximum": 20,
"minimum": 1,
"type": "integer",
"format": "int32"
}
}
],
"responses": {
"200": {
"description": "Returns the matching company codes.",
"content": {
"text/plain": {
"schema": {
"$ref": "#/components/schemas/CompanyCodeSuggestionsDto"
}
},
"application/json": {
"schema": {
"$ref": "#/components/schemas/CompanyCodeSuggestionsDto"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/CompanyCodeSuggestionsDto"
}
}
}
},
"400": {
"description": "The query parameters are invalid.",
"content": {
"text/plain": {
"schema": {
"$ref": "#/components/schemas/ValidationProblemDetails"
}
},
"application/json": {
"schema": {
"$ref": "#/components/schemas/ValidationProblemDetails"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/ValidationProblemDetails"
}
}
}
},
"403": {
"description": "The caller is not authorised to view the organisation's records.",
"content": {
"text/plain": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
},
"application/json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
}
}
},
"404": {
"description": "The specified organisation does not exist.",
"content": {
"text/plain": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
},
"application/json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
}
}
},
"401": {
"description": "Unauthorized",
"content": {
"text/plain": {
"schema": {
"$ref": "#/components/schemas/AuthenticationProblemDetails"
}
},
"application/json": {
"schema": {
"$ref": "#/components/schemas/AuthenticationProblemDetails"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/AuthenticationProblemDetails"
}
}
}
},
"409": {
"description": "Conflict",
"content": {
"text/plain": {
"schema": {
"$ref": "#/components/schemas/AuthenticationProblemDetails"
}
},
"application/json": {
"schema": {
"$ref": "#/components/schemas/AuthenticationProblemDetails"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/AuthenticationProblemDetails"
}
}
}
}
}
}
},
"/records/{id}/overview": {
"get": {
"tags": [
Expand Down Expand Up @@ -6026,6 +6189,22 @@
},
"description": "Contains why a record is being put on hold, resumed or archived."
},
"CompanyCodeSuggestionsDto": {
"required": [
"items"
],
"type": "object",
"properties": {
"items": {
"type": "array",
"items": {
"type": "string"
},
"description": "Gets the distinct matching company codes, those starting with the search first, then\nalphabetically."
}
},
"description": "The company codes already used on an organisation's records, offered as suggestions while a\nuser types a company code."
},
"ConditionKind": {
"enum": [
"AnswerIn"
Expand Down Expand Up @@ -7088,6 +7267,7 @@
"required": [
"formVersion",
"recordId",
"organisationId",
"section",
"task",
"page",
Expand All @@ -7108,6 +7288,11 @@
"description": "The record the page belongs to.",
"format": "int32"
},
"organisationId": {
"type": "integer",
"description": "The organisation that owns the record; used to look up suggestions such as the\norganisation's existing company codes.",
"format": "int32"
},
"section": {
"description": "The section the page belongs to.",
"$ref": "#/components/schemas/RecordPageSectionDto"
Expand Down
6 changes: 6 additions & 0 deletions backend/src/Application/RecordForms/Dtos/RecordPageDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ public sealed record RecordPageDto
/// <summary>The record the page belongs to.</summary>
public required int RecordId { get; init; }

/// <summary>
/// The organisation that owns the record; used to look up suggestions such as the
/// organisation's existing company codes.
/// </summary>
public required int OrganisationId { get; init; }

/// <summary>The section the page belongs to.</summary>
public required RecordPageSectionDto Section { get; init; }

Expand Down
2 changes: 1 addition & 1 deletion backend/src/Application/RecordForms/FormVersions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace UKPS.Api.Application.RecordForms;
/// </summary>
internal static class FormVersions
{
public const string Medicine = "2026.11.19.3";
public const string Medicine = "2026.11.20.1";

public static string? For(RecordType recordType) =>
recordType switch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Dictionary<string, IReadOnlyList<FormOption>> options
{
FormVersion = model.Version,
RecordId = loaded.Record.Id,
OrganisationId = loaded.Record.OrganisationId,
Section = new RecordPageSectionDto { Id = page.SectionId, Title = page.SectionTitle },
Task = new RecordPageTaskDto { Id = page.TaskId, Title = page.TaskTitle },
Page = new RecordPagePageDto
Expand Down
14 changes: 14 additions & 0 deletions backend/src/Application/Records/Dtos/CompanyCodeSuggestionsDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace UKPS.Api.Application.Records.Dtos;

/// <summary>
/// The company codes already used on an organisation's records, offered as suggestions while a
/// user types a company code.
/// </summary>
public sealed record CompanyCodeSuggestionsDto
{
/// <summary>
/// Gets the distinct matching company codes, those starting with the search first, then
/// alphabetically.
/// </summary>
public required IReadOnlyList<string> Items { get; init; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.ComponentModel.DataAnnotations;

namespace UKPS.Api.Application.Records.Dtos;

/// <summary>
/// Specifies the search parameters for company code suggestions.
/// </summary>
public sealed record GetCompanyCodeSuggestionsQueryDto
{
/// <summary>
/// Gets or initialises the text the company codes must contain, ignoring case. A blank search
/// returns no suggestions.
/// </summary>
[MaxLength(200, ErrorMessage = "Search must be 200 characters or fewer.")]
public string? Search { get; init; }

/// <summary>
/// Gets or initialises the maximum number of suggestions to return.
/// </summary>
[Range(1, 20, ErrorMessage = "Limit must be between 1 and 20.")]
public int Limit { get; init; } = 10;
}
18 changes: 18 additions & 0 deletions backend/src/Application/Records/IRecordService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ Task<Result<PaginatedResponseDto<RecordListItemDto>, GetRecordsError>> GetOrgani
CancellationToken cancellationToken
);

/// <summary>
/// Retrieves the distinct company codes on the latest revisions of an organisation's records
/// that contain the search text, ignoring case, for suggesting while a user types a company
/// code. Discarded records are excluded.
/// </summary>
/// <param name="organisationId">The unique identifier of the organisation.</param>
/// <param name="query">The search text and the maximum number of suggestions.</param>
/// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
/// <returns>
/// The matching company codes, or a <see cref="GetRecordsError"/> when the organisation does
/// not exist or the caller cannot read its records.
/// </returns>
Task<Result<CompanyCodeSuggestionsDto, GetRecordsError>> GetOrganisationCompanyCodes(
int organisationId,
GetCompanyCodeSuggestionsQueryDto query,
CancellationToken cancellationToken
);

/// <summary>
/// Discards a record that has never been submitted, setting the record and its draft
/// revision to discarded.
Expand Down
58 changes: 58 additions & 0 deletions backend/src/Application/Records/RecordService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,64 @@
);
}

public async Task<
Result<CompanyCodeSuggestionsDto, GetRecordsError>
> GetOrganisationCompanyCodes(
int organisationId,
GetCompanyCodeSuggestionsQueryDto query,
CancellationToken cancellationToken
)
{
GetRecordsError? organisationError = await ValidateOrganisationAsync(
organisationId,
Operation.Read,
cancellationToken
);

if (organisationError is not null)
{
return Result<CompanyCodeSuggestionsDto, GetRecordsError>.Err(organisationError);
}

string search = query.Search?.Trim() ?? string.Empty;

if (search.Length == 0)
{
return Result<CompanyCodeSuggestionsDto, GetRecordsError>.Ok(
new CompanyCodeSuggestionsDto { Items = [] }
);
}

string pattern = $"%{Helpers.EscapeLikePattern(search)}%";

// Discarded records are hidden by the records query filter. An organisation has one code
// per record, so the matches are few enough to rank in memory.
// TODO: when vaccine records are built, only suggest codes from records of the same type

Check warning on line 132 in backend/src/Application/Records/RecordService.cs

View workflow job for this annotation

GitHub Actions / Backend checks

Complete the task associated to this 'TODO' comment.

Check warning on line 132 in backend/src/Application/Records/RecordService.cs

View workflow job for this annotation

GitHub Actions / Backend checks

TODO when vaccine records are built, only suggest codes from records of the same type (https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0026.md)
// (medicine or vaccine) as the one being populated, still scoped to the organisation.
List<string> matches = await JoinProductDetails(GetBaseRecordProjection())
.Where(x =>
x.OrganisationId == organisationId
&& EF.Functions.ILike(x.CompanyCode, pattern, "\\")
)
.Select(x => x.CompanyCode)
.Distinct()
.ToListAsync(cancellationToken);

string[] items = matches
.Select(code => code.Trim())
.Where(code => code.Length > 0)
.DistinctBy(code => code, StringComparer.OrdinalIgnoreCase)
.OrderBy(code => code.StartsWith(search, StringComparison.OrdinalIgnoreCase) ? 0 : 1)
.ThenBy(code => code, StringComparer.OrdinalIgnoreCase)
.ThenBy(code => code, StringComparer.Ordinal)
.Take(query.Limit)
.ToArray();

return Result<CompanyCodeSuggestionsDto, GetRecordsError>.Ok(
new CompanyCodeSuggestionsDto { Items = items }
);
}

private async Task<GetRecordsError?> ValidateOrganisationAsync(
int organisationId,
Operation operation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ internal sealed class RecordProductDetail
"Company code",
FormControl.Text,
Hint = "Enter the primary internal code or working name your company uses for this product seeking market access.",
Display = "autocomplete:company-codes",
Order = 10
)]
[FormRequired("Enter company code")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ internal sealed class FormFieldAttribute(string id, string label, FormControl co

/// <summary>
/// A rendering hint for the frontend, for example <c>combobox</c>. It does not change
/// validation, and the frontend ignores hints it does not know.
/// validation, and the frontend ignores hints it does not know. A text field suggests values
/// as the user types with <c>autocomplete:&lt;source&gt;</c>, where the source names where the
/// suggestions come from, for example <c>autocomplete:company-codes</c>.
/// </summary>
public string? Display { get; set; }

Expand Down
Loading
Loading