public async Task <RedactedCivilFileDetailResponse> FileIdAsync(string fileId, bool isVcUser)
        {
            async Task <CivilFileDetailResponse> FileDetails() => await _filesClient.FilesCivilGetAsync(_requestAgencyIdentifierId, _requestPartId, _applicationCode, fileId);
            async Task <CivilFileContent> FileContent() => await _filesClient.FilesCivilFilecontentAsync(_requestAgencyIdentifierId, _requestPartId, _applicationCode, null, null, null, null, fileId);
            async Task <CivilAppearanceResponse> Appearances() => await PopulateDetailAppearancesAsync(FutureYN2.Y, HistoryYN2.Y, fileId);

            var fileDetailTask  = _cache.GetOrAddAsync($"CivilFileDetail-{fileId}-{_requestAgencyIdentifierId}", FileDetails);
            var fileContentTask = _cache.GetOrAddAsync($"CivilFileContent-{fileId}-{_requestAgencyIdentifierId}", FileContent);
            var appearancesTask = _cache.GetOrAddAsync($"CivilAppearancesFull-{fileId}-{_requestAgencyIdentifierId}", Appearances);

            var fileDetail = await fileDetailTask;

            ValidUserHelper.CheckIfValidUser(fileDetail.ResponseMessageTxt);

            var appearances = await appearancesTask;
            var fileContent = await fileContentTask;

            if (fileDetail?.PhysicalFileId == null)
            {
                return(null);
            }

            var detail = _mapper.Map <RedactedCivilFileDetailResponse>(fileDetail);

            foreach (var document in PopulateDetailCsrsDocuments(fileDetail.Appearance))
            {
                if (!isVcUser)
                {
                    detail.Document.Add(document);
                }
            }

            detail = await PopulateBaseDetail(detail);

            detail.Appearances = appearances;

            var fileContentCivilFile = fileContent?.CivilFile?.First(cf => cf.PhysicalFileID == fileId);

            detail.Party = await PopulateDetailParties(detail.Party);

            detail.Document = await PopulateDetailDocuments(detail.Document, fileContentCivilFile);

            detail.HearingRestriction = await PopulateDetailHearingRestrictions(fileDetail.HearingRestriction);

            if (isVcUser)
            {
                detail.HearingRestriction = new List <CivilHearingRestriction>();
            }

            return(detail);
        }
        public async Task <Models.CourtList.CourtList> CourtListAsync(string agencyId, string roomCode, DateTime proceeding, string divisionCode, string fileNumber)
        {
            var proceedingDateString = proceeding.ToString("yyyy-MM-dd");
            var agencyCode           = await _locationService.GetLocationCodeFromId(agencyId);

            async Task <CourtList> CourtList() => await _filesClient.FilesCourtlistAsync(_requestAgencyIdentifierId, _requestPartId, _applicationCode, agencyId, roomCode, proceedingDateString, divisionCode, fileNumber);
            async Task <CourtCalendarDetailByDay> CourtCalendarDetailByDay() =>
            await _filesClient.FilesCourtcalendardetailsbydayAsync(_requestAgencyIdentifierId, _requestPartId, _applicationCode, agencyCode,
                                                                   proceeding.ToString("yyyy-MM-dd HH:mm:ss.S"), roomCode);

            var courtCalendarDetailsTask = _cache.GetOrAdd($"CourtCalendarDetails-{agencyId}-{roomCode}-{proceedingDateString}-{fileNumber}-{_requestAgencyIdentifierId}",
                                                           CourtCalendarDetailByDay);
            var originalCourtListTask = _cache.GetOrAddAsync($"CourtList-{agencyId}-{roomCode}-{proceedingDateString}-{fileNumber}-{_requestAgencyIdentifierId}", CourtList);

            //Query by courtCalendarDetails, because it returns quite a bit faster than the courtList.
            var courtCalendarDetails = await courtCalendarDetailsTask;

            if (courtCalendarDetails.ResponseCd != "0")
            {
                _logger.LogInformation("Court calendar details returned responseCd != 0");
            }

            ValidUserHelper.CheckIfValidUser(courtCalendarDetails.ResponseMessageTxt);

            var civilCourtCalendarAppearances = courtCalendarDetails?.Appearance
                                                .WhereToList(app => app.CourtDivisionCd == CourtCalendarDetailAppearanceCourtDivisionCd.I);
            var criminalCourtCalendarAppearances = courtCalendarDetails?.Appearance
                                                   .WhereToList(app => app.CourtDivisionCd == CourtCalendarDetailAppearanceCourtDivisionCd.R);

            var civilFileIds    = civilCourtCalendarAppearances.SelectToList(ccl => ccl.PhysicalFileId);
            var criminalFileIds = criminalCourtCalendarAppearances.SelectToList(ccl => ccl.MdocJustinNo);

            if (civilFileIds.Count == 0 && criminalFileIds.Count == 0)
            {
                return(new Models.CourtList.CourtList());
            }

            //Start file details tasks.
            var civilFileDetailTasks    = CivilFileDetailTasks(civilFileIds);
            var criminalFileDetailTasks = CriminalFileDetailTasks(criminalFileIds);

            //Start appearances tasks.
            var civilAppearanceTasks    = CivilAppearancesTasks(proceeding, civilFileIds);
            var criminalAppearanceTasks = CriminalAppearancesTasks(proceeding, criminalFileIds);

            //Start file content tasks.
            var civilFileContentTasks = CivilFileContentTasks(civilFileIds);

            //Await our asynchronous requests.
            var civilFileDetails    = (await civilFileDetailTasks.WhenAll()).ToList();
            var criminalFileDetails = (await criminalFileDetailTasks.WhenAll()).ToList();
            var civilAppearances    = (await civilAppearanceTasks.WhenAll()).ToList();
            var criminalAppearances = (await criminalAppearanceTasks.WhenAll()).ToList();
            var civilFileContents   = (await civilFileContentTasks.WhenAll()).ToList();
            var originalCourtList   = await originalCourtListTask;

            //Note, there is test data that doesn't have a CourtList, but may have CourtCalendarDetails.
            //If this is the case with real data, or becomes an issue with testing, I'll have to
            //create a blank courtList object and overlay CourtCalendarDetails on top of it.
            //Some overlap happens already, but some of the fields aren't passed over (they already exist in CourtList).
            //PCSS for example isn't aware of CourtList, it only uses CourtCalendarDetails where as SCV uses the CourtList + CourtCalendarDetails.
            var courtList = _mapper.Map <Models.CourtList.CourtList>(originalCourtList);

            courtList.CivilCourtList = await PopulateCivilCourtListFromFileDetails(courtList.CivilCourtList, civilFileDetails);

            courtList.CriminalCourtList = await PopulateCriminalCourtListFromFileDetails(courtList.CriminalCourtList, criminalFileDetails);

            courtList.CivilCourtList = await PopulateCivilCourtListFromAppearances(courtList.CivilCourtList, civilAppearances);

            courtList.CriminalCourtList = await PopulateCriminalCourtListFromAppearances(courtList.CriminalCourtList, criminalAppearances);

            courtList.CivilCourtList    = PopulateCivilCourtListFromCourtCalendarDetails(courtList.CivilCourtList, civilCourtCalendarAppearances);
            courtList.CriminalCourtList = PopulateCriminalCourtListFromCourtCalendarDetails(courtList.CriminalCourtList, criminalCourtCalendarAppearances);

            courtList.CivilCourtList = PopulateCivilCourtListFromFileContent(courtList.CivilCourtList, civilFileContents);
            return(courtList);
        }