public async Task DuplicateTimesheets_ProvideFrozenTargetDates_ReturnsBadRequest()
        {
            var duplicateEfforts = new DuplicateEffortsDTO
            {
                SourceDate  = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 5),
                TargetDates = new List <DateTime>
                {
                    new DateTime(DateTime.Now.Year, DateTime.Now.Month, 6),
                },
            };

            this.timesheetHelper
            .Setup(helper => helper.GetNotYetFrozenTimesheetDates(It.IsAny <List <DateTime> >(), DateTimeOffset.Now))
            .Returns(new List <DateTime>());
            this.timesheetHelper
            .Setup(helper => helper.IsClientCurrentDateValid(It.IsAny <DateTime>(), It.IsAny <DateTime>()))
            .Returns(true);

            var result = (ObjectResult)await this.timesheetController.DuplicateEffortsAsync(DateTime.UtcNow.Date, duplicateEfforts);

            Assert.AreEqual(StatusCodes.Status400BadRequest, result.StatusCode);
            var error = (ErrorResponse)result.Value;

            Assert.AreEqual("The timesheet can not be filled as the target dates are frozen.", error.Message);
        }
Пример #2
0
        public async Task DuplicateTimesheets_WithCorrectModel_ShouldReturnOKStatus()
        {
            // ARRANGE
            this.timesheetHelper
            .Setup(helper => helper.DuplicateEffortsAsync(It.IsAny <DateTime>(), It.IsAny <IEnumerable <DateTime> >(), It.IsAny <DateTime>(), It.IsAny <Guid>()))
            .Returns(Task.FromResult(TestData.OKResultResponse));

            var duplicateEfforts = new DuplicateEffortsDTO
            {
                SourceDate  = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 5),
                TargetDates = new List <DateTime>
                {
                    new DateTime(DateTime.Now.Year, DateTime.Now.Month, 6),
                },
            };

            // ACT
            var response = (ObjectResult)await this.timesheetController.DuplicateEffortsAsync(DateTime.UtcNow.Date, duplicateEfforts);

            // ASSERT
            Assert.AreEqual(StatusCodes.Status200OK, response.StatusCode);
        }
Пример #3
0
        public async Task <IActionResult> DuplicateEffortsAsync(DateTime clientLocalCurrentDate, [FromBody] DuplicateEffortsDTO duplicateEffortsDetails)
        {
            this.RecordEvent("Duplicate efforts- The HTTP POST call to duplicate efforts has been initiated.", RequestType.Initiated);

            // Validates whether client provided local date is valid.
            if (!this.timesheetHelper.IsClientCurrentDateValid(clientLocalCurrentDate, DateTime.UtcNow))
            {
                this.logger.LogInformation("The timesheet can not be filled as provided current date is invalid.");
                this.RecordEvent("Duplicate efforts- The HTTP POST call to duplicate efforts has been failed.", RequestType.Failed);
                return(this.BadRequest(new ErrorResponse {
                    Message = "The timesheet can not be filled as provided current date is invalid."
                }));
            }

            try
            {
                // Get target dates those aren't frozen.
#pragma warning disable CA1062 // Model validation is done by data annotations.
                var notYetFrozenTimesheetDates = this.timesheetHelper.GetNotYetFrozenTimesheetDates(duplicateEffortsDetails.TargetDates, clientLocalCurrentDate);
#pragma warning restore CA1062 // Model validation is done by data annotations.

                // If all target dates are frozen.
                if (notYetFrozenTimesheetDates.IsNullOrEmpty())
                {
                    this.logger.LogInformation("The timesheet can not be filled for frozen timesheet dates for user {0}.", this.UserAadId);
                    this.RecordEvent("Duplicate efforts- The HTTP POST call to duplicate efforts has been failed.", RequestType.Failed);
                    return(this.BadRequest(new ErrorResponse {
                        Message = "The timesheet can not be filled as the target dates are frozen."
                    }));
                }

                var timesheets = await this.timesheetHelper.GetTimesheetsAsync(duplicateEffortsDetails.SourceDate, duplicateEffortsDetails.SourceDate, Guid.Parse(this.UserAadId));

                var sourceDateTimesheet = timesheets.FirstOrDefault();

                if (sourceDateTimesheet == null || sourceDateTimesheet.ProjectDetails.IsNullOrEmpty())
                {
                    this.logger.LogInformation("The source date must have projects.");
                    this.RecordEvent("Duplicate efforts- The HTTP POST call to duplicate efforts has been failed.", RequestType.Failed);
                    return(this.BadRequest(new ErrorResponse {
                        Message = "The source date must have projects."
                    }));
                }

#pragma warning disable CA1062 // Validated arguments at model level
                var result = await this.timesheetHelper.DuplicateEffortsAsync(duplicateEffortsDetails.SourceDate, duplicateEffortsDetails.TargetDates, clientLocalCurrentDate, Guid.Parse(this.UserAadId));

#pragma warning restore CA1062 // Validated arguments at model level

                if (result != null)
                {
                    this.RecordEvent("Duplicate efforts- The HTTP POST call to duplicate efforts has been succeeded.", RequestType.Succeeded);
                    return(this.Ok(result));
                }

                this.RecordEvent("Duplicate efforts- The HTTP POST call to duplicate efforts has been failed.", RequestType.Failed);
                return(this.StatusCode((int)HttpStatusCode.InternalServerError, "Unable to duplicate efforts."));
            }
            catch (Exception ex)
            {
                this.RecordEvent("Duplicate efforts- The HTTP POST call to duplicate efforts has been failed.", RequestType.Failed);
                this.logger.LogError(ex, "Error occurred while duplicating efforts.");
                throw;
            }
        }
Пример #4
0
        public async Task <IActionResult> DuplicateEffortsAsync(DateTime clientLocalCurrentDate, [FromBody] DuplicateEffortsDTO duplicateEffortsDetails)
        {
            this.RecordEvent("Duplicate efforts- The HTTP POST call to duplicate efforts has been initiated.", RequestType.Initiated);

            try
            {
#pragma warning disable CA1062 // Validated arguments at model level
                var result = await this.timesheetHelper.DuplicateEffortsAsync(duplicateEffortsDetails.SourceDate, duplicateEffortsDetails.TargetDates, clientLocalCurrentDate, Guid.Parse(this.UserAadId));

#pragma warning restore CA1062 // Validated arguments at model level

                if (result.StatusCode == HttpStatusCode.OK)
                {
                    this.RecordEvent("Duplicate efforts- The HTTP POST call to duplicate efforts has been succeeded.", RequestType.Succeeded);
                    return(this.Ok(result.Response));
                }

                this.RecordEvent("Duplicate efforts- The HTTP POST call to duplicate efforts has been failed.", RequestType.Failed);
                return(this.StatusCode((int)result.StatusCode, result.ErrorMessage));
            }
            catch (Exception ex)
            {
                this.RecordEvent("Duplicate efforts- The HTTP POST call to duplicate efforts has been failed.", RequestType.Failed);
                this.logger.LogError(ex, "Error occurred while duplicating efforts.");
                throw;
            }
        }