Example #1
0
        /// <summary>
        /// Method to add or update homework - SS
        /// </summary>
        /// <param name="homeWork">home work</param>
        /// <param name="loggedInUser">logged in user</param>
        /// <returns>response</returns>
        public async Task <HomeworkManagementResponse> AddOrUpdateHomeworkAsync(AddHomeworkManagementAc homeWork, ApplicationUser loggedInUser)
        {
            var instituteId = await _instituteUserMappingHelperService.GetUserCurrentSelectedInstituteIdAsync(loggedInUser.Id, true);

            if (!await _iMSDbContext.StaffBasicPersonalInformation.AnyAsync(x => x.Id == homeWork.StaffId && x.InstituteId == instituteId))
            {
                return new HomeworkManagementResponse()
                       {
                           Message = "Staff not found", HasError = true, ErrorType = HomeworkManagementReponseType.StaffId
                       }
            }
            ;
            else if (!await _iMSDbContext.InstituteClasses.AnyAsync(x => x.Id == homeWork.ClassId && x.InstituteId == instituteId))
            {
                return new HomeworkManagementResponse()
                       {
                           Message = "Class not found", HasError = true, ErrorType = HomeworkManagementReponseType.ClassId
                       }
            }
            ;
            else if (!await _iMSDbContext.Sections.AnyAsync(x => x.Id == homeWork.SectionId && x.InstituteId == instituteId))
            {
                return new HomeworkManagementResponse()
                       {
                           Message = "Section not found", HasError = true, ErrorType = HomeworkManagementReponseType.SectionId
                       }
            }
            ;
            else
            {
                var subjectIds = homeWork.HomeworkSubjectMappings.Select(x => x.SubjectId).Distinct().ToList();

                var subjectCount = await _iMSDbContext.InstituteClassSubjectMappings.CountAsync(x => subjectIds.Contains(x.SubjectId) &&
                                                                                                x.ClassId == homeWork.ClassId && x.FacultyId == homeWork.StaffId || x.AlternateFacultyId == homeWork.StaffId);

                if (subjectIds.Count != subjectCount)
                {
                    return new HomeworkManagementResponse()
                           {
                               Message = "Subject not found", HasError = true, ErrorType = HomeworkManagementReponseType.SubjectId
                           }
                }
                ;
                else
                {
                    var homeworkTexts = homeWork.HomeworkSubjectMappings.Select(x => x.HomeworkData).ToList();

                    if (homeworkTexts.Any(x => string.IsNullOrEmpty(x.Trim())))
                    {
                        return new HomeworkManagementResponse()
                               {
                                   Message = "Home can't be empty", HasError = true, ErrorType = HomeworkManagementReponseType.HomeworkData
                               }
                    }
                    ;
                    else
                    {
                        var homework = await _iMSDbContext.Homeworks.FirstOrDefaultAsync(x => x.ClassId == homeWork.ClassId &&
                                                                                         x.StaffId == homeWork.StaffId && x.SectionId == homeWork.SectionId && x.HomeworkDate == homeWork.HomeworkDate);

                        if (homework == null)
                        {
                            homework = new Homework()
                            {
                                ClassId      = homeWork.ClassId,
                                CreatedOn    = DateTime.UtcNow,
                                HomeworkDate = homeWork.HomeworkDate,
                                SectionId    = homeWork.SectionId,
                                StaffId      = homeWork.StaffId,
                                UpdatedById  = loggedInUser.Id,
                                UpdatedOn    = DateTime.UtcNow
                            };
                            _iMSDbContext.Homeworks.Add(homework);
                            await _iMSDbContext.SaveChangesAsync();
                        }
                        else
                        {
                            homework.UpdatedById = loggedInUser.Id;
                            homework.UpdatedOn   = DateTime.UtcNow;
                            _iMSDbContext.Homeworks.Update(homework);
                            await _iMSDbContext.SaveChangesAsync();

                            var previousHomeWorkSubjects = await _iMSDbContext.HomeworkSubjectMappings.Where(x => x.HomeworkId == homework.Id).ToListAsync();

                            if (previousHomeWorkSubjects.Count != 0)
                            {
                                using (var db = await _iMSDbContext.Database.BeginTransactionAsync())
                                {
                                    await _iMSDbContext.BulkDeleteAsync(previousHomeWorkSubjects);

                                    db.Commit();
                                }
                            }
                        }
                        List <HomeworkSubjectMapping> homeworkSubjects = new List <HomeworkSubjectMapping>();
                        foreach (var homeworkSubject in homeWork.HomeworkSubjectMappings)
                        {
                            homeworkSubjects.Add(new HomeworkSubjectMapping()
                            {
                                CreatedOn    = DateTime.UtcNow,
                                HomeworkData = homeworkSubject.HomeworkData,
                                HomeworkId   = homework.Id,
                                IsSelected   = homeworkSubject.IsSelected,
                                SubjectId    = homeworkSubject.SubjectId
                            });
                        }
                        using (var db = await _iMSDbContext.Database.BeginTransactionAsync())
                        {
                            await _iMSDbContext.BulkInsertAsync(homeworkSubjects);

                            db.Commit();
                        }

                        // Set bell notification
                        await SendBellNotificationOnHomewordCreation(homeWork, loggedInUser, instituteId);

                        return(new HomeworkManagementResponse()
                        {
                            HasError = false, Message = "Homework updated successfully", Data = new { Id = homework.Id }
                        });
                    }
                }
            }
        }
Example #2
0
        /// <summary>
        /// Method to update Leave type - SS
        /// </summary>
        /// <param name="updateLeaveType">leave type</param>
        /// <param name="loggedInUser">logged in user</param>
        /// <returns>response</returns>
        public async Task <LeaveTypeManagementResponse> UpdateLeaveType(UpdateLeaveTypeManagementAc updateLeaveType, ApplicationUser loggedInUser)
        {
            if (string.IsNullOrEmpty(updateLeaveType.Code.Trim()))
            {
                return new LeaveTypeManagementResponse()
                       {
                           HasError = true, Message = "Code can't be empty", ErrorType = LeaveTypeManagementResponseType.Code
                       }
            }
            ;
            else if (string.IsNullOrEmpty(updateLeaveType.Name.Trim()))
            {
                return new LeaveTypeManagementResponse()
                       {
                           HasError = true, Message = "Code can't be empty", ErrorType = LeaveTypeManagementResponseType.Name
                       }
            }
            ;
            else
            {
                var instituteId = await _instituteUserMappingHelperService.GetUserCurrentSelectedInstituteIdAsync(loggedInUser.Id, true);

                var assignedToCount = await _iMSDbContext.UserInstituteMappings.CountAsync(x => updateLeaveType.LeaveAssignedTos.Contains(x.UserId) && x.InstituteId == instituteId);

                if (assignedToCount != updateLeaveType.LeaveAssignedTos.Count)
                {
                    return new LeaveTypeManagementResponse()
                           {
                               HasError = true, Message = "User not found", ErrorType = LeaveTypeManagementResponseType.LeaveAssignedTos
                           }
                }
                ;
                else
                {
                    var types = await _iMSDbContext.LeaveTypes.Where(x => x.InstituteId == instituteId && x.Id != updateLeaveType.Id).ToListAsync();

                    if (types.Any(x => x.Code.ToLowerInvariant() == updateLeaveType.Code.ToLowerInvariant()))
                    {
                        return new LeaveTypeManagementResponse()
                               {
                                   HasError = true, Message = "Leave type with same code already exist. Please use unique one", ErrorType = LeaveTypeManagementResponseType.Code
                               }
                    }
                    ;
                    else
                    {
                        var leaveType = await _iMSDbContext.LeaveTypes.FirstOrDefaultAsync(x => x.Id == updateLeaveType.Id && x.InstituteId == instituteId);

                        if (leaveType == null)
                        {
                            return new LeaveTypeManagementResponse()
                                   {
                                       HasError = true, Message = "Leave type not found", ErrorType = LeaveTypeManagementResponseType.Id
                                   }
                        }
                        ;
                        else
                        {
                            leaveType.Code = updateLeaveType.Code;

                            leaveType.Description           = updateLeaveType.Description;
                            leaveType.LeaveAssignedTypeEnum = EnumHelperService.GetValueFromDescription <LeaveAssignedTypeEnum>(updateLeaveType.LeaveAssignedTypeEnumDescription);
                            leaveType.Name = updateLeaveType.Name;
                            leaveType.NumberOfAllowedLeave = updateLeaveType.NumberOfAllowedLeave;
                            leaveType.UpdatedById          = loggedInUser.Id;
                            leaveType.UpdatedOn            = DateTime.UtcNow;
                            _iMSDbContext.LeaveTypes.Update(leaveType);
                            await _iMSDbContext.SaveChangesAsync();

                            var previous = await _iMSDbContext.LeaveAssignedTos.Where(x => x.LeaveTypeId == leaveType.Id).ToListAsync();

                            using (var db = await _iMSDbContext.Database.BeginTransactionAsync())
                            {
                                await _iMSDbContext.BulkDeleteAsync(previous);

                                db.Commit();
                            }
                            List <LeaveAssignedTo> assignedTos = new List <LeaveAssignedTo>();
                            foreach (var user in updateLeaveType.LeaveAssignedTos)
                            {
                                assignedTos.Add(new LeaveAssignedTo()
                                {
                                    CreatedOn   = DateTime.UtcNow,
                                    LeaveTypeId = leaveType.Id,
                                    UserId      = user
                                });
                            }
                            using (var db = await _iMSDbContext.Database.BeginTransactionAsync())
                            {
                                await _iMSDbContext.BulkInsertAsync(assignedTos);

                                db.Commit();
                            }
                            return(new LeaveTypeManagementResponse()
                            {
                                HasError = false, Message = "Leave type updated succesfully"
                            });
                        }
                    }
                }
            }
        }