public async Task <IActionResult> AddStudentLeaveAsync([FromBody] AddStudentLeaveAc addStudentLeave)
        {
            var instituteId = await GetUserCurrentSelectedInstituteIdAsync();

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

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

                                var leave = new StudentLeave()
                                {
                                    ApprovedById   = addStudentLeave.ApprovedById,
                                    CreatedOn      = DateTime.UtcNow,
                                    EndDate        = addStudentLeave.EndDate,
                                    FromDate       = addStudentLeave.FromDate,
                                    LeaveTypeId    = addStudentLeave.LeaveTypeId,
                                    StatusId       = addStudentLeave.StatusId,
                                    Reason         = addStudentLeave.Reason,
                                    StudentId      = addStudentLeave.StudentId,
                                    AcademicYearId = currentAcademicYear.Id
                                };
                                _iMSDbContext.StudentLeaves.Add(leave);
                                await _iMSDbContext.SaveChangesAsync();

                                #region Send Mail/Message

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

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

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

                                #endregion

                                await SendBellNotificationsForStudentsLeavesAsync(currentUser, addStudentLeave.LeaveTypeId, addStudentLeave.StudentId, instituteId);

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