public async Task <EmployeeLeavesCreateResult> Create(EmployeeLeavesCreateParameters parameters) { var validationResult = await ValidateCreate(parameters); if (validationResult != EmployeeLeavesValidationStatus.Success) { return(new EmployeeLeavesCreateResult { ValidationStatus = validationResult, CategoryId = parameters.CategoryId, EmployeeId = parameters.EmployeeId, StartDate = parameters.StartDate.Date, EndDate = parameters.EndDate.Date, AttachmentFile = parameters.AttachmentFile, LeaveStatus = parameters.LeaveStatus, }); } //var draft = GetDraft(parameters.EmployeeId); //if (draft != null) //{ // await Update(draft.i) //} else { var leaveType = await _dbContext.EmployeeLeaves.AsQueryable() .Include(l => l.LeaveCategory) .FirstOrDefaultAsync(d => d.CategoryId == parameters.CategoryId); var result = await _dbContext.EmployeeLeaves.AddAsync(new EmployeeLeave() { StartDate = parameters.StartDate, EndDate = parameters.EndDate, Duration = parameters.EndDate.Subtract(parameters.StartDate).Days, AttachmentFile = parameters.AttachmentFile, Status = parameters.LeaveStatus, CategoryId = parameters.CategoryId, EmployeeId = parameters.EmployeeId, }); await _dbContext.SaveChangesAsync(); var leave = result.Entity; return(new EmployeeLeavesCreateResult() { CategoryId = parameters.CategoryId, EmployeeId = parameters.EmployeeId, StartDate = parameters.StartDate.Date, EndDate = parameters.EndDate.Date, AttachmentFile = parameters.AttachmentFile, LeaveStatus = parameters.LeaveStatus, }); } }
public async Task <IActionResult> Post(EmployeeLeavesCreateParameters parameters) { var leave = await _employeeLeaveService.Create(parameters); if (leave == null) { return(NotFound()); } return(Ok(leave)); }
public async Task <EmployeeLeavesValidationStatus> ValidateCreate(EmployeeLeavesCreateParameters parameters) { if (await _dbContext.EmployeeLeaves.AnyAsync(l => l.EmployeeId == parameters.EmployeeId && l.Status == LeaveStatus.Draft)) { return(EmployeeLeavesValidationStatus.DraftAlreadyExists); } var hasDuplicate = await _dbContext.EmployeeLeaves.AnyAsync(l => l.CategoryId.Equals(parameters.CategoryId) && l.EmployeeId.Equals(parameters.EmployeeId) && l.StartDate == parameters.StartDate.Date && l.EndDate == parameters.EndDate.Date && (l.Status != LeaveStatus.Pending || l.Status != LeaveStatus.Approved)); var employeeDuration = _dbContext.EmployeeLeaves.Include(l => l.LeaveCategory) .Where(e => e.EmployeeId.Equals(parameters.EmployeeId) && e.LeaveCategory.Name.Equals(LeaveType.annual) && e.Status == LeaveStatus.Approved) .Sum(d => d.Duration); var duration = employeeDuration + (parameters.EndDate.Subtract(parameters.StartDate).Days); var leaveBalance = _dbContext.LeavesCategories .Where(l => l.Name.Equals(LeaveType.annual)).Select(l => l.Balance).FirstOrDefault(); if (hasDuplicate) { return(EmployeeLeavesValidationStatus.RequestAlreadyExists); } else if (employeeDuration.Equals(leaveBalance) || duration > leaveBalance) { return(EmployeeLeavesValidationStatus.LeaveBalanceExceeded); } else { return(EmployeeLeavesValidationStatus.Success); } }