Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,84 @@ public CreateCohortDistributionData(IDataServiceClient<CohortDistribution> cohor
_bsSelectRequestAuditDataServiceClient = bsSelectRequestAuditDataServiceClient;
}


public async Task<List<CohortDistributionParticipantDto>> GetUnextractedCohortDistributionParticipants(int rowCount)
{
var participantsList = await _cohortDistributionDataServiceClient.GetByFilter(x => x.IsExtracted.Equals(0) && x.RequestId == Guid.Empty);
var participantsToBeExtracted = await GetRegularUnextractedParticipants(rowCount)
?? await GetSupersededParticipants(rowCount);

return await ProcessAndReturnParticipants(participantsToBeExtracted);
}

var participantsToBeExtracted = participantsList.OrderBy(x => x.RecordUpdateDateTime ?? x.RecordInsertDateTime).Take(rowCount).ToList();
//TODO do this filtering on the data services
var CohortDistributionParticipantList = participantsToBeExtracted.Select(x => new CohortDistributionParticipant(x)).ToList();
private async Task<List<CohortDistribution>?> GetRegularUnextractedParticipants(int rowCount)
{
var unextractedParticipants = await _cohortDistributionDataServiceClient.GetByFilter(
x => x.IsExtracted.Equals(0) && x.RequestId == Guid.Empty && x.SupersededNHSNumber == null);

return unextractedParticipants.Any()
? OrderAndTakeParticipants(unextractedParticipants, rowCount)
: null;
}

private async Task<List<CohortDistribution>> GetSupersededParticipants(int rowCount)
{
var supersededParticipants = await _cohortDistributionDataServiceClient.GetByFilter(
x => x.IsExtracted.Equals(0) && x.RequestId == Guid.Empty && x.SupersededNHSNumber != null);

// Get distinct non-null superseded NHS numbers
var supersededNhsNumbers = supersededParticipants
.Select(sp => sp.SupersededNHSNumber)
.Where(nhs => nhs.HasValue)
.Select(nhs => nhs.Value)
.Distinct()
.ToList();

// Find records matching the superseded NHS numbers
var matchingParticipants = await _cohortDistributionDataServiceClient.GetByFilter(
x => supersededNhsNumbers.Contains(x.NHSNumber) && x.IsExtracted.Equals(1));

// Filter superseded participants that have matching records
var filteredParticipants = supersededParticipants.ToList()
.Where(sp => matchingParticipants.Any(mp => mp.NHSNumber == sp.SupersededNHSNumber))
.ToList();

return OrderAndTakeParticipants(filteredParticipants, rowCount);
}

private static List<CohortDistribution> OrderAndTakeParticipants(IEnumerable<CohortDistribution> participants, int rowCount)
{
return participants
.OrderBy(x => (x.RecordUpdateDateTime ?? x.RecordInsertDateTime) ?? DateTime.MinValue)
.Take(rowCount)
.ToList();
}

private async Task<List<CohortDistributionParticipantDto>> ProcessAndReturnParticipants(List<CohortDistribution> participantsToBeExtracted)
{
var cohortDistributionParticipantList = participantsToBeExtracted
.Select(x => new CohortDistributionParticipant(x))
.ToList();

var requestId = Guid.NewGuid();
if (await MarkCohortDistributionParticipantsAsExtracted(participantsToBeExtracted, requestId))
{
await LogRequestAudit(requestId, (int)HttpStatusCode.OK);
var success = await MarkCohortDistributionParticipantsAsExtracted(participantsToBeExtracted, requestId);

return CohortDistributionParticipantDto(CohortDistributionParticipantList, requestId);
}
var statusCode = success ? (int)HttpStatusCode.OK
: (cohortDistributionParticipantList.Count == 0 ? (int)HttpStatusCode.NoContent : (int)HttpStatusCode.InternalServerError);

var statusCode = CohortDistributionParticipantList.Count == 0 ? (int)HttpStatusCode.NoContent : (int)HttpStatusCode.InternalServerError;
await LogRequestAudit(requestId, statusCode);

return new List<CohortDistributionParticipantDto>();
return success
? CohortDistributionParticipantDto(cohortDistributionParticipantList, requestId)
: new List<CohortDistributionParticipantDto>();
}

public async Task<List<CohortDistributionParticipantDto>> GetCohortDistributionParticipantsByRequestId(Guid requestId)
{
var requestIdString = requestId.ToString();

if (requestId == Guid.Empty)
{
CohortDistributionParticipantDto(new List<CohortDistributionParticipant>());
}

// TODO we should probably tidy this up and make it better.
var participantsList = await _cohortDistributionDataServiceClient.GetByFilter(x => x.RequestId == requestId);
return CohortDistributionParticipantDto(participantsList.Select(x => new CohortDistributionParticipant(x)).ToList());
Expand Down Expand Up @@ -141,16 +188,14 @@ private async Task<bool> MarkCohortDistributionParticipantsAsExtracted(List<Coho

var extractedParticipants = cohortParticipants.Select(x => x.CohortDistributionId);



foreach (var participantId in extractedParticipants)
{

var participant = await _cohortDistributionDataServiceClient.GetSingle(participantId.ToString());
participant.IsExtracted = 1;
participant.RequestId = requestId;

var updatedRecord = await _cohortDistributionDataServiceClient.Update(participant);

if (!updatedRecord)
{
return false;
Expand Down Expand Up @@ -205,8 +250,6 @@ private List<CohortRequestAudit> BuildCohortRequestAudits(IEnumerable<BsSelectRe
}).OrderBy(x => x.CreatedDateTime).ToList();
}



private static Expression<Func<BsSelectRequestAudit, bool>> BuildCohortRequestAuditQuery(string? requestId, string? statusCode, DateTime? dateFrom)
{
var conditions = new List<Expression<Func<BsSelectRequestAudit, bool>>>();
Expand All @@ -231,7 +274,6 @@ private static Expression<Func<BsSelectRequestAudit, bool>> BuildCohortRequestAu
conditions.Add(predicate);
}


Expression<Func<BsSelectRequestAudit, bool>> finalPredicate;
if (conditions.Count > 0)
{
Expand All @@ -253,7 +295,6 @@ private static Expression<Func<T, bool>> CombineWithAnd<T>(List<Expression<Func<
return firstExpression;
}


var body = expressions
.Skip(1)
.Aggregate(
Expand Down
Loading
Loading