public async Task <IActionResult> UpdateStudentLeaveAsync([FromBody] UpdateStudentLeaveAc updateStudentLeave)
        {
            var instituteId = await GetUserCurrentSelectedInstituteIdAsync();

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

            return(Ok(await _leaveManagementRepository.UpdateStudentLeaveAsync(updateStudentLeave, instituteId, currentUser)));
        }
Example #2
0
        /// <summary>
        /// Method to update applied student leave - SS
        /// </summary>
        /// <param name="updateStudentLeave">leave detail</param>
        /// <param name="instituteId">institute id</param>
        /// <returns>response</returns>
        public async Task <StudentLeaveResponse> UpdateStudentLeaveAsync(UpdateStudentLeaveAc updateStudentLeave, int instituteId, ApplicationUser currentUser)
        {
            if (string.IsNullOrEmpty(updateStudentLeave.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 == updateStudentLeave.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 == updateStudentLeave.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 == updateStudentLeave.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 == updateStudentLeave.ApprovedById && x.InstituteId == instituteId))
                            {
                                return new StudentLeaveResponse()
                                       {
                                           HasError = true, Message = "Staff not found", ErrorType = StudentLeaveResponseType.ApprovedById
                                       }
                            }
                            ;
                            else
                            {
                                var leave = await _iMSDbContext.StudentLeaves.FirstOrDefaultAsync(x => x.Id == updateStudentLeave.Id && x.Student.InstituteId == instituteId);

                                if (leave == null)
                                {
                                    return new StudentLeaveResponse()
                                           {
                                               HasError = true, Message = "Leave not found", ErrorType = StudentLeaveResponseType.Id
                                           }
                                }
                                ;
                                else
                                {
                                    leave.EndDate = updateStudentLeave.EndDate;

                                    leave.FromDate    = updateStudentLeave.FromDate;
                                    leave.LeaveTypeId = updateStudentLeave.LeaveTypeId;
                                    leave.Reason      = updateStudentLeave.Reason;
                                    _iMSDbContext.StudentLeaves.Update(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.StudentLeaveEdit,
                                                                                                  TemplateFormatEnum.Email, leave);

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

                                    #endregion

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

                                    return(new StudentLeaveResponse()
                                    {
                                        HasError = false, Message = "Applied leave updated succesfully"
                                    });
                                }
                            }
                        }
                    }
                }
            }
        }