public async Task <IActionResult> UpdateLeaveStatus(LeaveRequest leaveRequest)
        {
            var updateLeaveDto = new UpdateLeaveDto();

            await LeaveRepo.UpdateLeaveStatus(leaveRequest.Id, leaveRequest.Status);

            String logFilePath = Environment.ContentRootPath + Configuration.GetValue <String>("LogFile");

            if (leaveRequest.Status.Equals("Approved"))
            {
                DateTimeHandler dateTimeHandler = new DateTimeHandler();
                float           usedCasualLeaves = 0, usedEarnedLeaves = 0;

                if (leaveRequest.Type.Equals("Casual"))
                {
                    usedCasualLeaves = dateTimeHandler.CalculateDays(leaveRequest.FromDate, leaveRequest.ToDate);
                }
                else if (leaveRequest.Type.Equals("Earned"))
                {
                    usedEarnedLeaves = dateTimeHandler.CalculateDays(leaveRequest.FromDate, leaveRequest.ToDate);
                }

                updateLeaveDto = new UpdateLeaveDto {
                    UsedCasualLeaves = usedCasualLeaves,
                    UsedEarnedLeaves = usedEarnedLeaves
                };
                await LeaveRepo.UpdateUsedLeaves(leaveRequest.EId, updateLeaveDto);
            }
            else
            {
                updateLeaveDto = new UpdateLeaveDto {
                    UsedCasualLeaves = 0,
                    UsedEarnedLeaves = 0
                };
            }

            Logger.log("  LRSU\t:" +
                       " ID: " + leaveRequest.Id +
                       ", E-ID: " + leaveRequest.EId +
                       ", M-ID: " + leaveRequest.ManagerId +
                       ", Type: " + leaveRequest.Type +
                       ", From: " + leaveRequest.FromDate +
                       ", To: " + leaveRequest.ToDate +
                       ", Status: " + leaveRequest.Status, logFilePath);

            return(Ok(
                       new {
                leaveRequest.EId,
                updateLeaveDto
            }
                       ));
        }
예제 #2
0
        public void CalculateDays_WhenCalled_ReturnCountOfWeekDaysInRange(string startDate, string endDate, float days)
        {
            var dateTimeHandler = new DateTimeHandler();

            var result = dateTimeHandler.CalculateDays(startDate, endDate);

            Assert.That(result, Is.EqualTo(days));
        }
        public async Task <IActionResult> ApplyLeave(LeaveRequest leaveRequest)
        {
            DateTimeHandler dateTimeHandler = new DateTimeHandler();
            var             totalLeave      = await LeaveRepo.GetTotalLeaves();

            var user = await UserRepo.GetUser(leaveRequest.EId);

            try
            {
                float noOfLeaveDays = dateTimeHandler.CalculateDays(leaveRequest.FromDate, leaveRequest.ToDate);
                if (noOfLeaveDays <= 0)
                {
                    return(BadRequest("Please check From and To dates"));
                }

                if (leaveRequest.Type.Equals("Earned") && (totalLeave.TotalEarnedLeaves - user.UsedEarnedLeaves) >= noOfLeaveDays ||
                    leaveRequest.Type.Equals("Casual") && (totalLeave.TotalCasualLeaves - user.UsedCasualLeaves) >= noOfLeaveDays)
                {
                    var leave = await LeaveRepo.ApplyLeave(leaveRequest);

                    String logFilePath = Environment.ContentRootPath + Configuration.GetValue <String>("LogFile");
                    Logger.log("  NLR\t:" +
                               " E-ID: " + leaveRequest.EId +
                               ", Type: " + leaveRequest.Type +
                               ", From: " + leaveRequest.FromDate +
                               ", To: " + leaveRequest.ToDate, logFilePath);

                    return(Created(
                               leave.Id.ToString(),
                               leave
                               ));
                }
                else
                {
                    return(BadRequest(
                               "Your available leaves of the selected type are exhausted"
                               ));
                }
            }
            catch (FormatException)
            {
                return(StatusCode(500, "Could not send leave request. Please try again later!"));
            }
        }