示例#1
0
        public async Task <IActionResult> AddStaffLeaveAsync([FromBody] AddStaffLeaveAc addStaffLeave)
        {
            var instituteId = await GetUserCurrentSelectedInstituteIdAsync();

            ApplicationUser currentUser = await _userManager.FindByNameAsync(User.Identity.Name);

            return(Ok(await _leaveManagementRepository.AddStaffLeaveAsync(addStaffLeave, instituteId, currentUser)));
        }
示例#2
0
        /// <summary>
        /// Method to apply Staff leave - SS
        /// </summary>
        /// <param name="addStaffLeave">leave detail</param>
        /// <param name="instituteId">institute id</param>
        /// <returns>response</returns>
        public async Task <StaffLeaveResponse> AddStaffLeaveAsync(AddStaffLeaveAc addStaffLeave, int instituteId, ApplicationUser currentUser)
        {
            if (string.IsNullOrEmpty(addStaffLeave.Reason.Trim()))
            {
                return new StaffLeaveResponse()
                       {
                           HasError = true, Message = "Leave reason can't be empty", ErrorType = StaffLeaveResponseType.Reason
                       }
            }
            ;
            else
            {
                if (!await _iMSDbContext.StaffBasicPersonalInformation.AnyAsync(x => x.Id == addStaffLeave.StaffId && x.InstituteId == instituteId))
                {
                    return new StaffLeaveResponse()
                           {
                               HasError = true, Message = "Staff not found", ErrorType = StaffLeaveResponseType.StaffId
                           }
                }
                ;
                else
                {
                    if (!await _iMSDbContext.LeaveTypes.AnyAsync(x => x.Id == addStaffLeave.LeaveTypeId && x.InstituteId == instituteId))
                    {
                        return new StaffLeaveResponse()
                               {
                                   HasError = true, Message = "Leave type not found", ErrorType = StaffLeaveResponseType.LeaveTypeId
                               }
                    }
                    ;
                    else
                    {
                        if (!await _iMSDbContext.LeaveStatuses.AnyAsync(x => x.Id == addStaffLeave.StatusId && x.InstituteId == instituteId))
                        {
                            return new StaffLeaveResponse()
                                   {
                                       HasError = true, Message = "Status not found", ErrorType = StaffLeaveResponseType.StatusId
                                   }
                        }
                        ;
                        else
                        {
                            if (!await _iMSDbContext.UserInstituteMappings.AnyAsync(x => x.UserId == addStaffLeave.ApprovedById && x.InstituteId == instituteId))
                            {
                                return new StaffLeaveResponse()
                                       {
                                           HasError = true, Message = "Staff not found", ErrorType = StaffLeaveResponseType.ApprovedById
                                       }
                            }
                            ;
                            else
                            {
                                var currentAcademicYear = await _iMSDbContext.InstituteAcademicYears.FirstAsync(x => x.IsActive && x.InstituteId == instituteId);

                                var leave = new StaffLeave()
                                {
                                    ApprovedById   = addStaffLeave.ApprovedById,
                                    CreatedOn      = DateTime.UtcNow,
                                    EndDate        = addStaffLeave.EndDate,
                                    FromDate       = addStaffLeave.FromDate,
                                    LeaveTypeId    = addStaffLeave.LeaveTypeId,
                                    StatusId       = addStaffLeave.StatusId,
                                    Reason         = addStaffLeave.Reason,
                                    StaffId        = addStaffLeave.StaffId,
                                    AcademicYearId = currentAcademicYear.Id
                                };
                                _iMSDbContext.StaffLeaves.Add(leave);
                                await _iMSDbContext.SaveChangesAsync();

                                #region Send Mail/Message

                                leave = await _iMSDbContext.StaffLeaves.Include(s => s.Staff).Include(s => s.LeaveType)
                                        .Include(s => s.LeaveStatus).Include(s => s.ApprovedBy).FirstAsync(x => x.Id == leave.Id);

                                await _templateManagementRepository.TriggerMailOrMessageAsync(instituteId, TemplateTypeEnum.StaffLeaveAdd,
                                                                                              TemplateFormatEnum.Email, leave);

                                await _templateManagementRepository.TriggerMailOrMessageAsync(instituteId, TemplateTypeEnum.StaffLeaveAdd,
                                                                                              TemplateFormatEnum.Sms, leave);

                                #endregion

                                await SendBellNotificationsForStaffssLeavesAsync(currentUser, addStaffLeave.LeaveTypeId, addStaffLeave.StaffId, instituteId);

                                return(new StaffLeaveResponse()
                                {
                                    HasError = false, Message = "Leave applied succesfully"
                                });
                            }
                        }
                    }
                }
            }
        }