|
| 1 | +using System.Globalization; |
| 2 | +using LantanaGroup.Link.MockDmrpApi.Contracts.Generated; |
| 3 | +using LantanaGroup.Link.MockDmrpApi.Domain.Entities; |
| 4 | + |
| 5 | +namespace LantanaGroup.Link.MockDmrpApi.Application.Mapping; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Projects stored entries into the generated contract type. |
| 9 | +/// </summary> |
| 10 | +/// <remarks> |
| 11 | +/// This is the seam that keeps the third-party contract out of the database. Everything |
| 12 | +/// below it deals in <see cref="ReportingPlanEntryEntity"/>; only this file and the two |
| 13 | +/// contract endpoints deal in generated types. When Contracts/dmrp-openapi.yaml is replaced |
| 14 | +/// -- which is expected, the current one being provisional -- the compile errors land here |
| 15 | +/// rather than in the service layer or a migration. |
| 16 | +/// <para> |
| 17 | +/// The support surface does not pass through here. It has its own models, because it is ours |
| 18 | +/// and has no reason to move when the third party's contract does. |
| 19 | +/// </para> |
| 20 | +/// </remarks> |
| 21 | +public static class EntryMapper |
| 22 | +{ |
| 23 | + /// <summary> |
| 24 | + /// Projects a facility's entries into a reporting plan. |
| 25 | + /// </summary> |
| 26 | + /// <param name="nhsnOrgId">The facility the plan belongs to.</param> |
| 27 | + /// <param name="reportingMonth"> |
| 28 | + /// The month the result was narrowed to, echoed from the request, or <c>null</c> when none |
| 29 | + /// was supplied. Always null on an annual plan, where a month has no meaning. |
| 30 | + /// </param> |
| 31 | + /// <param name="reportingYear"> |
| 32 | + /// The year the result was narrowed to, or <c>null</c> when none was supplied -- in which |
| 33 | + /// case the entries may span several years. |
| 34 | + /// </param> |
| 35 | + /// <param name="entries">The entries the facility is enrolled in.</param> |
| 36 | + /// <param name="retrievedOn">When the response was produced.</param> |
| 37 | + /// <remarks> |
| 38 | + /// Only the supplied entries appear in <c>measures</c>. A module the facility is not |
| 39 | + /// enrolled in is simply absent -- there is no negative representation -- so an empty |
| 40 | + /// collection produces an empty measures array rather than an error or a null. |
| 41 | + /// <para> |
| 42 | + /// The month and year are echoed rather than derived from the entries. They describe what |
| 43 | + /// the caller asked for, which is the only honest answer when no period was supplied and |
| 44 | + /// the result spans several. |
| 45 | + /// </para> |
| 46 | + /// </remarks> |
| 47 | + public static ReportingPlanResponse ToReportingPlan( |
| 48 | + string nhsnOrgId, |
| 49 | + int? reportingMonth, |
| 50 | + int? reportingYear, |
| 51 | + IReadOnlyList<ReportingPlanEntryEntity> entries, |
| 52 | + DateTimeOffset retrievedOn) |
| 53 | + { |
| 54 | + ArgumentNullException.ThrowIfNull(entries); |
| 55 | + |
| 56 | + return new ReportingPlanResponse |
| 57 | + { |
| 58 | + PsDMRptPlanID = PlanIdentifier(nhsnOrgId, reportingMonth, reportingYear), |
| 59 | + |
| 60 | + // Numeric at the root, a string inside plans. Both come from the same |
| 61 | + // facility identifier; only the type differs. See the note below. |
| 62 | + Orgid = int.TryParse(nhsnOrgId, NumberStyles.Integer, CultureInfo.InvariantCulture, out var orgId) |
| 63 | + ? orgId |
| 64 | + : null, |
| 65 | + Year = reportingYear, |
| 66 | + Month = reportingMonth, |
| 67 | + |
| 68 | + CreateDate = FormatTimestamp(entries.Count == 0 |
| 69 | + ? retrievedOn.UtcDateTime |
| 70 | + : entries.Min(e => e.CreateDate)), |
| 71 | + ModifyDate = FormatTimestamp(entries.Count == 0 |
| 72 | + ? retrievedOn.UtcDateTime |
| 73 | + : entries.Max(e => e.ModifyDate ?? e.CreateDate)), |
| 74 | + |
| 75 | + Plans = entries |
| 76 | + .Select(e => new ReportingPlanItem |
| 77 | + { |
| 78 | + Name = e.Measure, |
| 79 | + Nhsnorgid = nhsnOrgId, |
| 80 | + Month = e.ReportingMonth?.ToString(CultureInfo.InvariantCulture), |
| 81 | + Year = e.ReportingYear.ToString(CultureInfo.InvariantCulture), |
| 82 | + Reporting = e.IsReporting, |
| 83 | + RptSeq = 0 |
| 84 | + }) |
| 85 | + .ToList() |
| 86 | + }; |
| 87 | + } |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// The timestamp format the real API uses: a space separator, two fractional |
| 91 | + /// digits and no timezone. |
| 92 | + /// </summary> |
| 93 | + /// <remarks> |
| 94 | + /// Formatted by hand, and typed as a string in the contract, so it is emitted exactly as |
| 95 | + /// received rather than normalised to ISO 8601. Binding it as a date would produce |
| 96 | + /// <c>2023-09-09T11:12:12.59+00:00</c> — well-formed, and not what a consumer will have to |
| 97 | + /// parse in production. |
| 98 | + /// </remarks> |
| 99 | + private const string TimestampFormat = "yyyy-MM-dd HH:mm:ss.ff"; |
| 100 | + |
| 101 | + private static string FormatTimestamp(DateTime value) => |
| 102 | + value.ToString(TimestampFormat, CultureInfo.InvariantCulture); |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// A stable identifier for the plan a query describes. |
| 106 | + /// </summary> |
| 107 | + /// <remarks> |
| 108 | + /// The real API returns a stored record's key. Nothing here stores plans — they are |
| 109 | + /// assembled per request — so this derives one from what identifies the plan, which keeps |
| 110 | + /// it stable across repeated identical queries. A counter or a random value would change |
| 111 | + /// under a caller that reasonably expects it not to. |
| 112 | + /// </remarks> |
| 113 | + private static int PlanIdentifier(string nhsnOrgId, int? month, int? year) |
| 114 | + { |
| 115 | + var key = $"{nhsnOrgId}|{year?.ToString(CultureInfo.InvariantCulture) ?? "-"}" |
| 116 | + + $"|{month?.ToString(CultureInfo.InvariantCulture) ?? "-"}"; |
| 117 | + |
| 118 | + // FNV-1a rather than string.GetHashCode(), which is randomised per process in .NET |
| 119 | + // Core -- the same query would return a different identifier after every restart, |
| 120 | + // which is the one thing this is supposed not to do. |
| 121 | + unchecked |
| 122 | + { |
| 123 | + const uint offsetBasis = 2166136261; |
| 124 | + const uint prime = 16777619; |
| 125 | + |
| 126 | + var hash = offsetBasis; |
| 127 | + foreach (var c in key) |
| 128 | + { |
| 129 | + hash = (hash ^ c) * prime; |
| 130 | + } |
| 131 | + |
| 132 | + // Masked to 31 bits so it is always non-negative and reads like a record id. |
| 133 | + return (int)(hash & 0x7FFFFFFF); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments