From 9d9f1d766ce3dcee6084c1e775adece44b41b7cb Mon Sep 17 00:00:00 2001 From: warren Date: Wed, 15 Jan 2025 14:03:20 +0000 Subject: [PATCH] fix: incorrect updating of requestID for same participant --- .../RetrieveCohortDistribution.cs | 2 +- .../ICreateCohortDistributionData.cs | 2 +- .../Database/CreateCohortDistributionData.cs | 33 +++++++++------- .../Shared/Data/Database/DatabaseHelper.cs | 39 +++++++++++++------ .../AddCohortDistributionTests.cs | 6 +-- 5 files changed, 51 insertions(+), 31 deletions(-) diff --git a/application/CohortManager/src/Functions/CohortDistributionServices/RetrieveCohortDistribution/RetrieveCohortDistribution.cs b/application/CohortManager/src/Functions/CohortDistributionServices/RetrieveCohortDistribution/RetrieveCohortDistribution.cs index 7f1d31ca3..fd91d31eb 100644 --- a/application/CohortManager/src/Functions/CohortDistributionServices/RetrieveCohortDistribution/RetrieveCohortDistribution.cs +++ b/application/CohortManager/src/Functions/CohortDistributionServices/RetrieveCohortDistribution/RetrieveCohortDistribution.cs @@ -60,7 +60,7 @@ public async Task Run([HttpTrigger(AuthorizationLevel.Anonymou if (cohortDistributionParticipants.Count == 0) { cohortDistributionParticipants = _createCohortDistributionData - .GetUnextractedCohortDistributionParticipantsByScreeningServiceId(rowCount); + .GetUnextractedCohortDistributionParticipants(rowCount); } return cohortDistributionParticipants.Count == 0 diff --git a/application/CohortManager/src/Functions/Shared/Common/Interfaces/ICreateCohortDistributionData.cs b/application/CohortManager/src/Functions/Shared/Common/Interfaces/ICreateCohortDistributionData.cs index 9f1bf625b..2048099f9 100644 --- a/application/CohortManager/src/Functions/Shared/Common/Interfaces/ICreateCohortDistributionData.cs +++ b/application/CohortManager/src/Functions/Shared/Common/Interfaces/ICreateCohortDistributionData.cs @@ -6,7 +6,7 @@ namespace Common.Interfaces; public interface ICreateCohortDistributionData { bool InsertCohortDistributionData(CohortDistributionParticipant cohortDistributionParticipant); - List GetUnextractedCohortDistributionParticipantsByScreeningServiceId(int rowCount); + List GetUnextractedCohortDistributionParticipants(int rowCount); bool UpdateCohortParticipantAsInactive(string NhsNumber); CohortDistributionParticipant GetLastCohortDistributionParticipant(string NhsNumber); List GetCohortDistributionParticipantsByRequestId(string requestId); diff --git a/application/CohortManager/src/Functions/Shared/Data/Database/CreateCohortDistributionData.cs b/application/CohortManager/src/Functions/Shared/Data/Database/CreateCohortDistributionData.cs index ba5f3d882..b0ea7f48b 100644 --- a/application/CohortManager/src/Functions/Shared/Data/Database/CreateCohortDistributionData.cs +++ b/application/CohortManager/src/Functions/Shared/Data/Database/CreateCohortDistributionData.cs @@ -149,7 +149,7 @@ public bool InsertCohortDistributionData(CohortDistributionParticipant cohortDis return UpdateRecords(SQLToExecuteInOrder); } - public List GetUnextractedCohortDistributionParticipantsByScreeningServiceId(int rowCount) + public List GetUnextractedCohortDistributionParticipants(int rowCount) { var SQL = "SELECT TOP (@RowCount)" + " bcd.[PARTICIPANT_ID], " + @@ -189,7 +189,8 @@ public List GetUnextractedCohortDistributionPa " bcd.[IS_EXTRACTED], " + " bcd.[REQUEST_ID] " + " FROM [dbo].[BS_COHORT_DISTRIBUTION] bcd " + - " WHERE bcd.IS_EXTRACTED = @Extracted"; + " WHERE bcd.IS_EXTRACTED = @Extracted " + + " AND REQUEST_ID IS NULL"; var parameters = new Dictionary { @@ -200,15 +201,16 @@ public List GetUnextractedCohortDistributionPa var command = CreateCommand(parameters); command.CommandText = SQL; - var listOfAllParticipants = GetParticipant(command); + var participantsList = GetParticipant(command); var requestId = Guid.NewGuid().ToString(); - if (MarkCohortDistributionParticipantsAsExtracted(listOfAllParticipants, requestId)) + if (MarkCohortDistributionParticipantsAsExtracted(participantsList, requestId)) { LogRequestAudit(requestId, (int)HttpStatusCode.OK); - return CohortDistributionParticipantDto(listOfAllParticipants); + return CohortDistributionParticipantDto(participantsList); + } - var statusCode = listOfAllParticipants.Count == 0 ? (int)HttpStatusCode.NoContent : (int)HttpStatusCode.InternalServerError; + var statusCode = participantsList.Count == 0 ? (int)HttpStatusCode.NoContent : (int)HttpStatusCode.InternalServerError; LogRequestAudit(requestId, statusCode); return new List(); @@ -485,19 +487,22 @@ private bool MarkCohortDistributionParticipantsAsExtracted(List $"@param{i}")); var SQL = $@" - WITH cte_lastestParticipants AS ( - SELECT cd.PARTICIPANT_ID, - RANK() OVER(PARTITION BY PARTICIPANT_ID - ORDER BY RECORD_UPDATE_DATETIME DESC) as rank + WITH cte_latestParticipants AS ( + SELECT cd.PARTICIPANT_ID, cd.RECORD_UPDATE_DATETIME, + ROW_NUMBER() OVER(PARTITION BY PARTICIPANT_ID + ORDER BY RECORD_UPDATE_DATETIME DESC) as RowNum FROM BS_COHORT_DISTRIBUTION cd - WHERE PARTICIPANT_ID IN ({cohortParamList})) + WHERE PARTICIPANT_ID IN ({cohortParamList}) + AND IS_EXTRACTED = 0 + AND REQUEST_ID IS NULL) UPDATE cd SET IS_EXTRACTED = @Extracted, REQUEST_ID = @RequestId FROM BS_COHORT_DISTRIBUTION cd - INNER JOIN cte_lastestParticipants cte + INNER JOIN cte_latestParticipants cte ON cd.PARTICIPANT_ID = cte.PARTICIPANT_ID - WHERE cte.rank = 1"; + AND cd.RECORD_UPDATE_DATETIME = cte.RECORD_UPDATE_DATETIME + WHERE cte.RowNum = 1"; var parameters = new Dictionary { @@ -563,7 +568,7 @@ public CohortRequestAudit GetNextCohortRequestAudit(string requestId) var parameters = new Dictionary { {"@RequestId", requestIdGuid}, - {"@StatusCode", HttpStatusCode.NoContent.ToString()} + {"@StatusCode", "204"} }; using var command = CreateCommand(parameters); diff --git a/application/CohortManager/src/Functions/Shared/Data/Database/DatabaseHelper.cs b/application/CohortManager/src/Functions/Shared/Data/Database/DatabaseHelper.cs index ab0450e66..0e2f0d937 100644 --- a/application/CohortManager/src/Functions/Shared/Data/Database/DatabaseHelper.cs +++ b/application/CohortManager/src/Functions/Shared/Data/Database/DatabaseHelper.cs @@ -62,21 +62,36 @@ public static string GetStringValue(IDataReader reader, string columnName) return reader[columnName] == DBNull.Value ? null : reader[columnName].ToString(); } -public static T? GetValue(IDataReader reader, string columnName) -{ - object value = reader[columnName]; - if (value == DBNull.Value || value == null) return default; + public static T? GetValue(IDataReader reader, string columnName) + { + object value = reader[columnName]; + if (value == DBNull.Value || value == null) return default; - Type targetType = typeof(T); + Type targetType = typeof(T); - if (targetType.IsEnum) - { - short shortValue = Convert.ToInt16(value); - return (T)Enum.ToObject(targetType, shortValue); - } + switch (targetType) + { + case Type t when t == typeof(string): + if (value is DateTime) + return (T)(object)((DateTime)value).ToString(); + if (value is Guid) + return (T)(object)value.ToString(); + return (T)(object)value.ToString(); - return (T)Convert.ChangeType(value, targetType); -} + case Type t when t == typeof(Guid): + return (T)value; + + case Type t when t == typeof(DateTime): + return (T)value; + + case Type t when t.IsEnum: + short shortValue = Convert.ToInt16(value); + return (T)Enum.ToObject(targetType, shortValue); + + default: + return (T)Convert.ChangeType(value, targetType); + } + } public static object ConvertBoolStringToBoolByType(string environmentVariableName, string dataType) { diff --git a/tests/UnitTests/CohortDistributionTests/CohortDistributionDataTests/AddCohortDistributionTests.cs b/tests/UnitTests/CohortDistributionTests/CohortDistributionDataTests/AddCohortDistributionTests.cs index 3ba7f5d78..7d1896d60 100644 --- a/tests/UnitTests/CohortDistributionTests/CohortDistributionDataTests/AddCohortDistributionTests.cs +++ b/tests/UnitTests/CohortDistributionTests/CohortDistributionDataTests/AddCohortDistributionTests.cs @@ -120,7 +120,7 @@ public void ExtractCohortDistributionParticipants_ValidRequest_ReturnsListOfPart var rowCount = 1; // Act - var result = _createCohortDistributionData.GetUnextractedCohortDistributionParticipantsByScreeningServiceId(rowCount); + var result = _createCohortDistributionData.GetUnextractedCohortDistributionParticipants(rowCount); // Assert Assert.AreEqual("1", result.FirstOrDefault()?.ParticipantId); @@ -150,7 +150,7 @@ public void ExtractCohortDistributionParticipants_AfterExtraction_MarksBothParti var rowCount = 2; // Act - var result = _createCohortDistributionData.GetUnextractedCohortDistributionParticipantsByScreeningServiceId(rowCount); + var result = _createCohortDistributionData.GetUnextractedCohortDistributionParticipants(rowCount); // Assert _commandMock.Verify(x => x.ExecuteNonQuery(), Times.AtLeast(2)); @@ -168,7 +168,7 @@ public void GetParticipant_NoParticipants_ReturnsEmptyCollection() var rowCount = 0; // Act - var result = _createCohortDistributionData.GetUnextractedCohortDistributionParticipantsByScreeningServiceId(rowCount); + var result = _createCohortDistributionData.GetUnextractedCohortDistributionParticipants(rowCount); // Assert Assert.IsNotNull(result);