Skip to content

Commit

Permalink
fix: incorrect updating of requestID for same participant
Browse files Browse the repository at this point in the history
  • Loading branch information
Warren-Pitterson committed Jan 15, 2025
1 parent 72a7a71 commit 9d9f1d7
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymou
if (cohortDistributionParticipants.Count == 0)
{
cohortDistributionParticipants = _createCohortDistributionData
.GetUnextractedCohortDistributionParticipantsByScreeningServiceId(rowCount);
.GetUnextractedCohortDistributionParticipants(rowCount);
}

return cohortDistributionParticipants.Count == 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Common.Interfaces;
public interface ICreateCohortDistributionData
{
bool InsertCohortDistributionData(CohortDistributionParticipant cohortDistributionParticipant);
List<CohortDistributionParticipantDto> GetUnextractedCohortDistributionParticipantsByScreeningServiceId(int rowCount);
List<CohortDistributionParticipantDto> GetUnextractedCohortDistributionParticipants(int rowCount);
bool UpdateCohortParticipantAsInactive(string NhsNumber);
CohortDistributionParticipant GetLastCohortDistributionParticipant(string NhsNumber);
List<CohortDistributionParticipantDto> GetCohortDistributionParticipantsByRequestId(string requestId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public bool InsertCohortDistributionData(CohortDistributionParticipant cohortDis
return UpdateRecords(SQLToExecuteInOrder);
}

public List<CohortDistributionParticipantDto> GetUnextractedCohortDistributionParticipantsByScreeningServiceId(int rowCount)
public List<CohortDistributionParticipantDto> GetUnextractedCohortDistributionParticipants(int rowCount)
{
var SQL = "SELECT TOP (@RowCount)" +
" bcd.[PARTICIPANT_ID], " +
Expand Down Expand Up @@ -189,7 +189,8 @@ public List<CohortDistributionParticipantDto> 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<string, object>
{
Expand All @@ -200,15 +201,16 @@ public List<CohortDistributionParticipantDto> 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<CohortDistributionParticipantDto>();
Expand Down Expand Up @@ -485,19 +487,22 @@ private bool MarkCohortDistributionParticipantsAsExtracted(List<CohortDistributi
var cohortParamList = string.Join(", ", cohortParticipants.Select((_, i) => $"@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<string, object>
{
Expand Down Expand Up @@ -563,7 +568,7 @@ public CohortRequestAudit GetNextCohortRequestAudit(string requestId)
var parameters = new Dictionary<string, object>
{
{"@RequestId", requestIdGuid},
{"@StatusCode", HttpStatusCode.NoContent.ToString()}
{"@StatusCode", "204"}
};

using var command = CreateCommand(parameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(IDataReader reader, string columnName)
{
object value = reader[columnName];
if (value == DBNull.Value || value == null) return default;
public static T? GetValue<T>(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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand All @@ -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);
Expand Down

0 comments on commit 9d9f1d7

Please sign in to comment.