public async Task <CriminalAppearanceDetail> AppearanceDetailAsync(string fileId, string appearanceId, string partId) { async Task <CriminalFileDetailResponse> FileDetails() => await _filesClient.FilesCriminalGetAsync(_requestAgencyIdentifierId, _requestPartId, _applicationCode, fileId); async Task <CriminalFileContent> FileContent() => await _filesClient.FilesCriminalFilecontentAsync(_requestAgencyIdentifierId, _requestPartId, _applicationCode, null, null, null, null, fileId); async Task <CriminalFileAppearances> Appearances() => await PopulateDetailsAppearancesAsync(fileId, FutureYN.Y, HistoryYN.Y); async Task <CriminalFileAppearanceCountResponse> AppearanceCounts() => await _filesClient.FilesCriminalAppearanceCountsAsync(_requestAgencyIdentifierId, _requestPartId, _applicationCode, appearanceId); async Task <CriminalFileAppearanceApprMethodResponse> AppearanceMethods() => await _filesClient.FilesCriminalAppearanceAppearancemethodsAsync(_requestAgencyIdentifierId, _requestPartId, _applicationCode, appearanceId); var appearancesTask = _cache.GetOrAddAsync($"CriminalAppearancesFull-{fileId}-{_requestAgencyIdentifierId}", Appearances); var fileDetailTask = _cache.GetOrAddAsync($"CriminalFileDetail-{fileId}-{_requestAgencyIdentifierId}", FileDetails); var fileContentTask = _cache.GetOrAddAsync($"CriminalFileContent-{fileId}-{_requestAgencyIdentifierId}", FileContent); var appearanceCountTask = _cache.GetOrAddAsync($"CriminalAppearanceCounts-{fileId}-{appearanceId}-{_requestAgencyIdentifierId}", AppearanceCounts); var appearanceMethodsTask = _cache.GetOrAddAsync($"CriminalAppearanceMethods-{fileId}-{appearanceId}-{_requestAgencyIdentifierId}", AppearanceMethods); //Probably cached, user has the path: FileDetails -> Appearances -> AppearanceDetails. var appearances = await appearancesTask; var detail = _mapper.Map <RedactedCriminalFileDetailResponse>(await fileDetailTask); var agencyId = await _locationService.GetLocationAgencyIdentifier(detail.HomeLocationAgenId); detail.Witness = await PopulateDetailWitnesses(detail); var targetAppearance = appearances?.ApprDetail?.FirstOrDefault(app => app.AppearanceId == appearanceId && app.PartId == partId); if (targetAppearance == null || agencyId == null) { return(null); } async Task <CourtList> CourtList() => await _filesClient.FilesCourtlistAsync(_requestAgencyIdentifierId, _requestPartId, _applicationCode, agencyId, targetAppearance.CourtRoomCd, targetAppearance.AppearanceDt, "CR", detail.FileNumberTxt); var courtList = await _cache.GetOrAddAsync($"CriminalCourtList-{agencyId}-{targetAppearance.CourtRoomCd}-{targetAppearance.AppearanceDt}-{detail.FileNumberTxt}-{_requestAgencyIdentifierId}", CourtList); var fileContent = await fileContentTask; var appearanceCount = await appearanceCountTask; var appearanceMethods = await appearanceMethodsTask; var accusedFile = fileContent?.AccusedFile.FirstOrDefault(af => af.MdocJustinNo == fileId && af.PartId == partId); var criminalParticipant = detail?.Participant.FirstOrDefault(x => x.PartId == partId); var appearanceFromAccused = accusedFile?.Appearance.FirstOrDefault(a => a?.AppearanceId == appearanceId); var targetCourtList = courtList.CriminalCourtList.FirstOrDefault(cl => cl.CriminalAppearanceID == appearanceId); var attendanceMethods = targetCourtList?.AttendanceMethod; //CourtList AttendanceMethod is present state, AppearanceMethod is past state they look to be the same values, but haven't found any test data on DEV where they differ. if (criminalParticipant == null || accusedFile == null || appearanceFromAccused == null) { return(null); } var appearanceDetail = new CriminalAppearanceDetail { JustinNo = fileId, AgencyId = await _locationService.GetLocationAgencyIdentifier(detail.HomeLocationAgenId), AppearanceId = appearanceId, PartId = targetAppearance.PartId, ProfSeqNo = targetAppearance.ProfSeqNo, CourtRoomCd = targetAppearance.CourtRoomCd, FileNumberTxt = detail.FileNumberTxt, AppearanceMethods = await PopulateAppearanceMethods(appearanceMethods.AppearanceMethod), AppearanceDt = targetAppearance.AppearanceDt, //AppearanceNote = appearanceFromAccused.AppearanceNote?.ReturnNullIfEmpty(), //JudgesRecommendation = appearanceFromAccused.JudgesRecommendation, EstimatedTimeHour = appearanceFromAccused.EstimatedTimeHour?.ReturnNullIfEmpty(), EstimatedTimeMin = appearanceFromAccused.EstimatedTimeMin?.ReturnNullIfEmpty(), Accused = await PopulateAppearanceCriminalAccused(criminalParticipant.FullName, appearanceFromAccused, attendanceMethods, partId, appearanceMethods.AppearanceMethod), Prosecutor = await PopulateAppearanceDetailProsecutor(appearanceFromAccused, attendanceMethods, appearanceMethods.AppearanceMethod), Adjudicator = await PopulateAppearanceDetailAdjudicator(appearanceFromAccused, attendanceMethods, appearanceMethods.AppearanceMethod), JustinCounsel = await PopulateAppearanceDetailJustinCounsel(criminalParticipant, appearanceFromAccused, attendanceMethods, appearanceMethods.AppearanceMethod), Charges = await PopulateCharges(appearanceCount.ApprCount), InitiatingDocuments = GetInitiatingDocuments(accusedFile.Document), CourtLevelCd = detail.CourtLevelCd }; return(appearanceDetail); }