public async Task <PsaReportDocument> GetCurrentShiftReportAsync(Guid departmentId)
        {
            // чтобы создать сводку по нужной смене, нужно понять:
            // какой отдел и филиал интересует пользователя?
            var org = await GetOrgStructureItemsAsync(departmentId);

            var dept   = org.Item1;
            var branch = org.Item2;

            // какое сейчас местное время в филиале?
            var currentTimeInDepartment = GetCurrentLocalTime(branch);
            // какой смене соответствует данное время?
            var absoluteShiftIndex = WorkingShiftAbsoluteIndex.GetAbsoluteIndex(currentTimeInDepartment);
            // выбрать из репозитория сводок все сводки с указанным absoluteShiftIndex и отделом
            var shiftSummaries = await _summaryService.GetSummariesInDepartmentForShift(departmentId, absoluteShiftIndex);

            // составить из них отчет
            var descriptor = GetShiftDescriptor(currentTimeInDepartment);
            var reportDoc  = new PsaReportDocument
            {
                BranchOfficeId   = (dept?.BranchOfficeId).Value,
                BranchOfficeName = branch?.Name,
                DepartmentId     = departmentId,
                DepartmentName   = dept?.Name,
                ShiftDate        = descriptor.ShiftDate,
                ShiftName        = descriptor.ShiftName,
                ShiftNumber      = descriptor.ShiftNumber,
                ShiftStartTime   = descriptor.ShiftStartTime,
                Summaries        = shiftSummaries
            };

            // fetch summaries for the shift in the department
            return(reportDoc);
        }
        private async Task <Report> GetOrInitReportAsync(SummaryDocument summary, WorkingShiftDescriptor descriptor)
        {
            {
                var existingReport = await GetExistingReport(summary.DepartmentId, descriptor.ShiftAbsoluteIndex);

                if (existingReport != null)
                {
                    // report found, return it
                    return(existingReport);
                }
            }

            var branch = await _orgStructReference.GetBranchOfficeAsync(summary.BranchOfficeId);

            var dept = await _orgStructReference.GetDepartmentAsync(summary.DepartmentId);

            var reportDoc = new PsaReportDocument
            {
                BranchOfficeId   = summary.BranchOfficeId,
                BranchOfficeName = branch?.Name,
                DepartmentId     = summary.DepartmentId,
                DepartmentName   = dept?.Name,
                ShiftDate        = descriptor.ShiftDate,
                ShiftName        = descriptor.ShiftName,
                ShiftNumber      = descriptor.ShiftNumber,
                ShiftStartTime   = descriptor.ShiftStartTime,
                Summaries        = new List <SummaryDocument>()
            };

            // create report, as it doesn't exist yet (the summary is the first for the shift)
            var newReport = new Report
            {
                DepartmentId   = reportDoc.DepartmentId,
                BranchOfficeId = reportDoc.BranchOfficeId,
                ReportDocument = reportDoc,
            };

            try
            {
                // save the report
                _reportRepository.Add(newReport);
                await _reportRepository.SaveChangesAsync();
            }
            catch (Exception ex)
            {
            }
            // if failed to save, try to get the report again
            {
                var existingReport = await GetExistingReport(summary.DepartmentId, descriptor.ShiftAbsoluteIndex);

                if (existingReport != null)
                {
                    // report found, return it
                    return(existingReport);
                }
            }
            throw new Exception("Cannot retrieve the existing report for update");
        }