public async Task HandleAsync(GetProjectsNotSubmitReportQuery request)
            {
                var firstWorkingDayOfWeek = TimeHelper.GetFirstWorkingDateOfWeek(
                    TimeHelper.CalculateYear(request.SelectedYearWeek),
                    TimeHelper.CalculateWeek(request.SelectedYearWeek));
                var listProjectDto = await _dbContext.Projects
                                     .Where(p => p.PhrRequired)
                                     .Where(p => p.Statuses.SingleOrDefault(s => s.YearWeek == request.SelectedYearWeek) == null)
                                     .Where(p => p.PhrRequiredFrom < firstWorkingDayOfWeek)
                                     .Select(p => new ProjectDto()
                {
                    ProjectId        = p.Id,
                    ProjectName      = p.Name,
                    SelectedYearWeek = request.SelectedYearWeek,
                    PicEmail         = p.DeliveryResponsibleName + "@niteco.se",
                    DivisionName     = p.Division
                })
                                     .ToListAsync();

                var nextYearWeek = TimeHelper.GetNextYearWeek(request.SelectedYearWeek);
                var isoDate      = TimeHelper.GetIsoDayOfWeek(_rule.Value.PMsCanOnlyEditTheirReportsTill.Day);
                var date         = TimeHelper.GetDate(isoDate, nextYearWeek)
                                   .AddHours(_rule.Value.PMsCanOnlyEditTheirReportsTill.Hour);

                request.Response = new Dto()
                {
                    Deadline = date,
                    Projects = listProjectDto
                };
            }
Example #2
0
            public async Task HandleAsync(GetDoDReportLinksQuery request)
            {
                var yearWeeksToGetDod =
                    TimeHelper.GetYearWeeksOfXRecentWeeksStartFrom(TimeHelper.CalculateYear(request.YearWeek),
                                                                   TimeHelper.CalculateWeek(request.YearWeek), request.NumberOfWeek);

                yearWeeksToGetDod.Add(request.YearWeek);

                var reportLinks = await _dbContext.DoDReports.Where(d =>
                                                                    d.ProjectId == request.ProjectId && yearWeeksToGetDod.Contains(d.YearWeek))
                                  .Select(d => new Dto.ReportLink()
                {
                    YearWeek       = d.YearWeek,
                    LinkToReport   = d.LinkToReport,
                    ReportFileName = d.ReportFileName
                })
                                  .Distinct()
                                  .ToListAsync();

                var yearWeeks = reportLinks.Select(r => r.YearWeek);

                var listReportLinkToAdds = yearWeeksToGetDod.Select(yw =>
                {
                    if (!yearWeeks.Contains(yw))
                    {
                        return(new Dto.ReportLink()
                        {
                            YearWeek = yw
                        });
                    }

                    return(null);
                });

                reportLinks.AddRange(listReportLinkToAdds.Where(r => r != null));

                request.Response = new Dto()
                {
                    ReportLinks = reportLinks.OrderByDescending(r => r.YearWeek)
                };
            }
Example #3
0
            public async Task HandleAsync(MarkProjectsMissedDeadlineCommand request)
            {
                var currentYearWeek = TimeHelper.GetNextYearWeek(request.MissedDeadlineYearWeek);
                var date            = TimeHelper.GetDate(TimeHelper.GetIsoDayOfWeek(_rules.Value.PMsCanOnlyEditTheirReportsTill.Day),
                                                         currentYearWeek);

                date = date.AddHours(_rules.Value.PMsCanOnlyEditTheirReportsTill.Hour);

                if (DateTime.Now <= date)
                {
                    throw new InvalidOperationException(
                              "Projects can only be marked as missed deadline after the deadline!");
                }

                var projectIdsAlreadySubmitted = _dbContext.Statuses
                                                 .Where(s => s.YearWeek == request.MissedDeadlineYearWeek).Select(s => s.ProjectId);

                var firstDayOfMissedDeadlineYearWeek = TimeHelper.GetFirstWorkingDateOfWeek(
                    TimeHelper.CalculateYear(request.MissedDeadlineYearWeek),
                    TimeHelper.CalculateWeek(request.MissedDeadlineYearWeek));

                var projectIdsJustGotEnabledPhr = _dbContext.Projects
                                                  .Where(p => p.PhrRequired && p.PhrRequiredFrom >= firstDayOfMissedDeadlineYearWeek)
                                                  .Select(p => p.Id);

                var listWeeklyReportStatuses = (await _dbContext.Projects
                                                .Where(p => p.PhrRequired)
                                                .Select(p => p.Id)
                                                .Except(projectIdsAlreadySubmitted)
                                                .Except(projectIdsJustGotEnabledPhr)
                                                .ToListAsync())
                                               .Select(projectId => new WeeklyReportStatus(0, projectId, request.MissedDeadlineYearWeek, true));

                await _dbContext.WeeklyReportStatuses.AddRangeAsync(listWeeklyReportStatuses);

                await _dbContext.SaveChangesAsync();

                request.Response = 1;
            }
Example #4
0
        private async Task <ProjectsNotYetSubmittedWeeklyReport> CallApi()
        {
            var currentYearWeek = TimeHelper.GetYearWeek(DateTime.Now);
            var lastYearWeek    = TimeHelper.GetLastYearWeek(currentYearWeek);
            var phrEndpoint     = (await
                                   AppSettings.ExternalServices.PHR.GetFlurlRequest())
                                  .AppendPathSegment("/api/projects/phr/not-submit-report/year-week/")
                                  .AppendPathSegment(lastYearWeek);

            try
            {
                var res = await phrEndpoint.AllowHttpStatus(HttpStatusCode.Unauthorized)
                          .GetAsync();

                var content = await res.Content.ReadAsStringAsync();

                if (res.StatusCode == HttpStatusCode.Unauthorized)
                {
                    if (string.IsNullOrEmpty(content))
                    {
                        AppSettings.ResetToken();

                        res = await phrEndpoint.GetAsync();

                        content = await res.Content.ReadAsStringAsync();
                    }
                    else
                    {
                        var error = JsonConvert.DeserializeObject <ErrorResponse>(content);

                        throw new UnauthorizedAccessException(error.Error);
                    }
                }

                var response = JsonConvert.DeserializeObject <ProjectsNotYetSubmittedWeeklyReport>(content);

                foreach (var project in response.Projects)
                {
                    var htmlContent = MailService.CreateTemplate(MailService.ProjectNotSubmitWeeklyReportKey,
                                                                 MailService.ProjectNotSubmitWeeklyReportPath, new
                    {
                        project.ProjectName,
                        Deadline = response.Deadline.ToString("HH:mm dd MMM yyyy"),
                        Pic      = project.PicEmail.TrimEnd("niteco.se".ToCharArray()).TrimEnd('@'),
                        YearWeek = TimeHelper.CalculateWeek(project.SelectedYearWeek) + " - " +
                                   TimeHelper.CalculateYear(project.SelectedYearWeek),
                        PhrLink = AppSettings.ExternalServices.PHR.Endpoint,
                    });

                    project.HtmlContent = htmlContent;
                }

                _logger.LogInformation(nameof(NotifyProjectsNotYetSubmittedWeeklyReport) + " PHR response: {@response}",
                                       response);

                return(response);
            }
            catch (FlurlHttpException e)
            {
                _logger.LogError(
                    $"{nameof(ProjectsNotYetSubmittedWeeklyReport)} - {nameof(FlurlHttpException)}: " + "{error}",
                    await e.GetResponseStringAsync());
                throw;
            }
            catch (Exception e)
            {
                _logger.LogError(
                    $"{nameof(ProjectsNotYetSubmittedWeeklyReport)} - {nameof(Exception)}: " + "{error}", e.Message);
                throw;
            }
        }