public async Task <ActionResult <TestsDataDto> > GetTestsDataForDateAsync(DateTime date) { TestsDataDto testsData = await _testingPersonnelsService .GetTestsDataDtoForDateAsync(date) .ConfigureAwait(false); return(testsData); }
public async Task <TestsDataWithIsEarliestDateDto> GetTestsDataDtoAsync(DateTime startDate, bool isForOneDate) { const int companyOrganizationId = 82000; const int pharmacyOrganizationId = 82001; const int nursingHomesOrganizationId = 82003; const int hospitalOrganizationId = 82004; const int smeOrganizationId = 99990; const int campOrganizationId = 82006; startDate = startDate.Date; var endDate = startDate.AddDays(DaysPeriod); if (isForOneDate) { endDate = startDate.AddDays(OneDayPeriod); } await using var context = ContextFactory.CreateDataContext(null); var orgs = await context.Organizations .Where(x => x.Status != OrganizationStatus.NotActive && x.OrganizationTypeId != campOrganizationId && ((x.FirstTestTimestamp.HasValue && x.FirstTestTimestamp.Value.Date < endDate.Date) || (x.SecondTestTimestamp.HasValue && x.SecondTestTimestamp.Value.Date < endDate.Date) || (x.ThirdTestTimestamp.HasValue && x.ThirdTestTimestamp.Value.Date < endDate.Date) || (x.FourthTestTimestamp.HasValue && x.FourthTestTimestamp.Value.Date < endDate.Date) || (x.FifthTestTimestamp.HasValue && x.FifthTestTimestamp.Value.Date < endDate.Date))) .Select(x => new { TotalSamples = x.NumberOfSamples, RegisteredEmployees = x.RegisteredEmployees, FirstDate = x.FirstTestTimestamp, SecondDate = x.SecondTestTimestamp, ThirdDate = x.ThirdTestTimestamp, FourthDate = x.FourthTestTimestamp, FifthDate = x.FifthTestTimestamp, ExclusionStartDate = x.ExclusionStartDate, ExclusionEndDate = x.ExclusionEndDate, IsCompanyOrPharmacyOrNursingOrganization = x.OrganizationTypeId == companyOrganizationId || x.OrganizationTypeId == pharmacyOrganizationId || x.OrganizationTypeId == nursingHomesOrganizationId || x.OrganizationTypeId == hospitalOrganizationId || x.OrganizationTypeId == smeOrganizationId, IsNursery = x.OrganizationTypeId == nursingHomesOrganizationId }) .ToListAsync() .ConfigureAwait(false); var invitations = await context.TestingPersonnelInvitations .Where(x => x.InvitationForDate >= startDate.Date && x.InvitationForDate < endDate.Date) .Include(x => x.TestingPersonnelConfirmations) .ThenInclude(x => x.TestingPersonnel) .Select(x => new { InvitationId = x.Id, InvitationDate = x.InvitationForDate, Shift1 = new { RequiredPersonnelCountShift = x.RequiredPersonnelCountShift1, ShiftNumber = ShiftNumber.First, ConfirmedNotCanceledEmployeesCount = x.TestingPersonnelConfirmations.Where(conf => conf.ShiftNumber == ShiftNumber.First && !conf.CanceledOn.HasValue).Count(), ConfirmedEmployees = x.TestingPersonnelConfirmations.Where(conf => conf.ShiftNumber == ShiftNumber.First).OrderByDescending(conf => conf.CanceledOn) .Select(c => new TestingPersonnelTestDataDto { FirstName = _aesCryptography.Decrypt(c.TestingPersonnel.FirstName), LastName = _aesCryptography.Decrypt(c.TestingPersonnel.LastName), Email = _aesCryptography.Decrypt(c.TestingPersonnel.Email), IsCanceled = c.CanceledOn.HasValue }) }, Shift2 = new { RequiredPersonnelCountShift = x.RequiredPersonnelCountShift2, ShiftNumber = ShiftNumber.Second, ConfirmedNotCanceledEmployeesCount = x.TestingPersonnelConfirmations.Where(conf => conf.ShiftNumber == ShiftNumber.Second && !conf.CanceledOn.HasValue).Count(), ConfirmedEmployees = x.TestingPersonnelConfirmations.Where(conf => conf.ShiftNumber == ShiftNumber.Second).OrderByDescending(conf => conf.CanceledOn) .Select(c => new TestingPersonnelTestDataDto { FirstName = _aesCryptography.Decrypt(c.TestingPersonnel.FirstName), LastName = _aesCryptography.Decrypt(c.TestingPersonnel.LastName), Email = _aesCryptography.Decrypt(c.TestingPersonnel.Email), IsCanceled = c.CanceledOn.HasValue }) } }) .ToListAsync() .ConfigureAwait(false); var fixedPersonnel = await context.TestingPersonnels .Where(x => x.Type == TestingPersonnelType.Fixed && x.TestingPersonnelWorkingAreas.Any(wa => wa.Area == WorkingArea.Pooling)) .Include(x => x.FixedTestingPersonnelCancelations) .ToListAsync() .ConfigureAwait(false); var cantonsWithWeekdaysSamples = await context.Cantons .Where(c => c.CantonWeekdaysSamples != null) .Include(c => c.CantonWeekdaysSamples) .OrderBy(c => c.Name) .ToListAsync() .ConfigureAwait(false); var confirmationsWithoutInvitation = await context.TestingPersonnelConfirmationsWithoutInvitation .Where(c => c.Date >= startDate.Date && c.Date < endDate.Date) .Include(c => c.TestingPersonnel) .ToListAsync() .ConfigureAwait(false); var testDatesValues = new List <TestsDataDto>(); var currentDate = startDate; while (currentDate < endDate) { var invitation = invitations.FirstOrDefault(x => x.InvitationDate.Date == currentDate.Date); if (currentDate.DayOfWeek != DayOfWeek.Saturday && currentDate.DayOfWeek != DayOfWeek.Sunday) { var testsDataValue = new TestsDataDto { InvitationId = invitation?.InvitationId, Date = currentDate, InvitationAlreadySent = invitation != null, Shifts = new List <TestDataPerShiftDto>() { new TestDataPerShiftDto() { ShiftNumber = invitation == null ? ShiftNumber.First : invitation.Shift1.ShiftNumber, RequiredPersonnelCountShift = invitation == null ? 0 : invitation.Shift1.RequiredPersonnelCountShift, ConfirmedNotCanceledEmployeesCount = invitation == null ? 0 : invitation.Shift1.ConfirmedNotCanceledEmployeesCount, ConfirmedEmployees = invitation == null ? new List <TestingPersonnelTestDataDto>() : invitation.Shift1.ConfirmedEmployees.DistinctBy(x => x.Email).ToList(), FixedEmployees = GetFixedTestingPersonnelForWeekday(fixedPersonnel, currentDate, Shift.First), ConfirmedWithoutInvitation = GetConfirmedTestingPersonnelWithoutInvitationForDateAndShift(confirmationsWithoutInvitation, currentDate, ShiftNumber.First) }, new TestDataPerShiftDto() { ShiftNumber = invitation == null ? ShiftNumber.Second : invitation.Shift2.ShiftNumber, RequiredPersonnelCountShift = invitation == null ? 0 : invitation.Shift2.RequiredPersonnelCountShift, ConfirmedNotCanceledEmployeesCount = invitation == null ? 0 : invitation.Shift2.ConfirmedNotCanceledEmployeesCount, ConfirmedEmployees = invitation == null ? new List <TestingPersonnelTestDataDto>() : invitation.Shift2.ConfirmedEmployees.DistinctBy(x => x.Email).ToList(), FixedEmployees = GetFixedTestingPersonnelForWeekday(fixedPersonnel, currentDate, Shift.Second), ConfirmedWithoutInvitation = GetConfirmedTestingPersonnelWithoutInvitationForDateAndShift(confirmationsWithoutInvitation, currentDate, ShiftNumber.Second) } }, CantonsSamplesData = GetTestDataPerCantonAndWeekday(cantonsWithWeekdaysSamples, currentDate) }; testDatesValues.Add(testsDataValue); foreach (var organization in orgs) { if ((CheckIfInTestDate(organization.FirstDate, currentDate) || CheckIfInTestDate(organization.SecondDate, currentDate) || CheckIfInTestDate(organization.ThirdDate, currentDate) || CheckIfInTestDate(organization.FourthDate, currentDate) || CheckIfInTestDate(organization.FifthDate, currentDate)) && !CheckIfDateIsExcluded(organization.ExclusionStartDate, organization.ExclusionEndDate, currentDate)) { var allTestingDates = new List <DateTime?>(); allTestingDates.Add(organization.FirstDate); allTestingDates.Add(organization.SecondDate); allTestingDates.Add(organization.ThirdDate); allTestingDates.Add(organization.FourthDate); allTestingDates.Add(organization.FifthDate); var countOfTestingDates = allTestingDates.Where(x => x.HasValue).Count(); var avgSample = 0; if (organization.IsCompanyOrPharmacyOrNursingOrganization) { avgSample = organization.RegisteredEmployees / countOfTestingDates ?? 0; if (organization.IsNursery) { avgSample = 2 * avgSample; } } else { avgSample = organization.TotalSamples / countOfTestingDates; } testsDataValue.Samples += avgSample; } } testsDataValue.CantonsSamples = testsDataValue.CantonsSamplesData.Sum(x => x.Samples); testsDataValue.TotalSamples = testsDataValue.Samples + testsDataValue.CantonsSamples; } currentDate = currentDate.AddDays(1); } var testsDataWithEarliestDate = new TestsDataWithIsEarliestDateDto() { IsEarliestDate = startDate == _testsHistoryEarliestDate, TestsData = testDatesValues }; return(testsDataWithEarliestDate); }