private async Task <MemReconResult> AddNewUsers(IEnumerable <CourseMembershipVO> bbCmsVo, MemReconResult reconResult) { var bbCms = bbCmsVo.ToList(); var bbCmUserIds = bbCms.Select(bbcm => bbcm.userId).ToList(); var usersWithAccount = await ctxManager.Context.People .Where(p => bbCmUserIds.Contains(p.BbUserId)) .Select(p => new { p.BbUserId, p.PersonId, p.MpInstituteRole }) .ToListAsync(); var accountsNeedToCreate = bbCms.Where(cm => !usersWithAccount.Select(uwa => uwa.BbUserId).Contains(cm.userId)).ToList(); if (accountsNeedToCreate.Any()) { var userFilter = new UserFilter { filterTypeSpecified = true, filterType = (int)UserFilterType.UserByIdWithAvailability, available = true, availableSpecified = true, id = accountsNeedToCreate.Select(bbAccount => bbAccount.userId).ToArray() }; var autoRetryUsers = new Retrier <UserVO[]>(); var bbUsers = await autoRetryUsers.Try(() => _bbWs.BbCourseUsers(userFilter), 3); var courseMems = bbCms; if (bbUsers != null) { var users = bbUsers.Select(bbu => { var cm = courseMems.First(bbcm => bbcm.userId == bbu.id); return(new Person { MpInstituteRole = MpRoleTransform.BbWsRoleToEcat(cm.roleId), BbUserId = bbu.id, BbUserName = bbu.name, Email = $"{cm.id}-{bbu.extendedInfo.emailAddress}", IsActive = true, LastName = bbu.extendedInfo.familyName, FirstName = bbu.extendedInfo.givenName, MpGender = MpGender.Unk, MpAffiliation = MpAffiliation.Unk, MpComponent = MpComponent.Unk, RegistrationComplete = false, MpPaygrade = MpPaygrade.Unk, ModifiedById = Faculty.PersonId, ModifiedDate = DateTime.Now }); }).ToList(); foreach (var user in users) { ctxManager.Context.People.Add(user); } reconResult.NumOfAccountCreated = await ctxManager.Context.SaveChangesAsync(); foreach (var user in users) { usersWithAccount.Add(new { user.BbUserId, user.PersonId, user.MpInstituteRole }); switch (user.MpInstituteRole) { case MpInstituteRoleId.Faculty: user.Faculty = new ProfileFaculty { PersonId = user.PersonId, AcademyId = reconResult.AcademyId, HomeStation = StaticAcademy.AcadLookupById[reconResult.AcademyId].Base.ToString(), IsReportViewer = false, IsCourseAdmin = false }; break; case MpInstituteRoleId.Student: user.Student = new ProfileStudent { PersonId = user.PersonId }; break; default: user.Student = new ProfileStudent { PersonId = user.PersonId }; break; } } await ctxManager.Context.SaveChangesAsync(); } } reconResult.Students = usersWithAccount .Where(ecm => { var bbCM = bbCmsVo.First(cm => cm.userId == ecm.BbUserId); return(MpRoleTransform.BbWsRoleToEcat(bbCM.roleId) != MpInstituteRoleId.Faculty); }) .Select(ecm => new StudentInCourse { StudentPersonId = ecm.PersonId, CourseId = reconResult.CourseId, ReconResultId = reconResult.Id, BbCourseMemId = bbCms.First(bbcm => bbcm.userId == ecm.BbUserId).id }).ToList(); reconResult.Faculty = usersWithAccount .Where(ecm => { var bbCM = bbCmsVo.First(cm => cm.userId == ecm.BbUserId); return(MpRoleTransform.BbWsRoleToEcat(bbCM.roleId) == MpInstituteRoleId.Faculty); }) .Select(ecm => new FacultyInCourse { FacultyPersonId = ecm.PersonId, CourseId = reconResult.CourseId, ReconResultId = reconResult.Id, BbCourseMemId = bbCms.First(bbcm => bbcm.userId == ecm.BbUserId).id }).ToList(); var neededFacultyProfiles = usersWithAccount.Where(ecm => { var bbCM = bbCmsVo.First(cm => cm.userId == ecm.BbUserId); return(MpRoleTransform.BbWsRoleToEcat(bbCM.roleId) == MpInstituteRoleId.Faculty); }).Select(ecm => ecm.PersonId); var existingFacultyProfiles = ctxManager.Context.Faculty .Where(fac => neededFacultyProfiles.Contains(fac.PersonId)) .Select(fac => fac.PersonId); var newFacultyProfiles = neededFacultyProfiles .Where(id => !existingFacultyProfiles.Contains(id)) .Select(id => new ProfileFaculty { PersonId = id, AcademyId = reconResult.AcademyId, HomeStation = StaticAcademy.AcadLookupById[reconResult.AcademyId].Base.ToString(), IsReportViewer = false, IsCourseAdmin = false }); var neededStudentProfiles = usersWithAccount.Where(ecm => { var bbCM = bbCmsVo.First(cm => cm.userId == ecm.BbUserId); return(MpRoleTransform.BbWsRoleToEcat(bbCM.roleId) != MpInstituteRoleId.Faculty); }).Select(ecm => ecm.PersonId); var existingStudentProfiles = ctxManager.Context.Students .Where(stud => neededStudentProfiles.Contains(stud.PersonId)) .Select(stud => stud.PersonId); var newStudentProfiles = neededStudentProfiles .Where(id => !existingStudentProfiles.Contains(id)) .Select(id => new ProfileStudent { PersonId = id, }); if (newFacultyProfiles.Any()) { ctxManager.Context.Faculty.AddRange(newFacultyProfiles); } if (newStudentProfiles.Any()) { ctxManager.Context.Students.AddRange(newStudentProfiles); } if (reconResult.Students.Any()) { ctxManager.Context.StudentInCourses.AddRange(reconResult.Students); } if (reconResult.Faculty.Any()) { ctxManager.Context.FacultyInCourses.AddRange(reconResult.Faculty); } reconResult.NumAdded = reconResult.Faculty.Any() || reconResult.Students.Any() ? await ctxManager.Context.SaveChangesAsync() : 0; return(reconResult); }
public async Task <MemReconResult> ReconcileCourseMembers(int courseId) { //await GetProfile(); var ecatCourse = await ctxManager.Context.Courses .Where(crse => crse.Id == courseId) .Select(crse => new CourseReconcile { Course = crse, FacultyToReconcile = crse.Faculty.Select(fac => new UserReconcile { PersonId = fac.FacultyPersonId, BbUserId = fac.FacultyProfile.Person.BbUserId, CanDelete = !fac.FacSpComments.Any() && !fac.FacSpResponses.Any() && !fac.FacStratResponse.Any() }).ToList(), StudentsToReconcile = crse.Students.Select(sic => new UserReconcile { PersonId = sic.StudentPersonId, BbUserId = sic.Student.Person.BbUserId, CanDelete = !sic.WorkGroupEnrollments.Any() }).ToList() }).SingleOrDefaultAsync(); Contract.Assert(ecatCourse != null); var reconResult = new MemReconResult { Id = Guid.NewGuid(), CourseId = courseId, AcademyId = Faculty?.AcademyId }; var autoRetryCm = new Retrier <CourseMembershipVO[]>(); var courseMemFilter = new MembershipFilter { filterTypeSpecified = true, filterType = (int)CrseMembershipFilterType.LoadByCourseId, }; var bbCourseMems = await autoRetryCm.Try(() => _bbWs.BbCourseMembership(ecatCourse.Course.BbCourseId, courseMemFilter), 3); var existingCrseUserIds = ecatCourse.FacultyToReconcile .Select(fac => fac.BbUserId).ToList(); existingCrseUserIds.AddRange(ecatCourse.StudentsToReconcile.Select(sic => sic.BbUserId)); var newMembers = bbCourseMems .Where(cm => !existingCrseUserIds.Contains(cm.userId)) .Where(cm => cm.available == true) .ToList(); if (newMembers.Any()) { //var queryCr = await autoRetryCr.Try(client.getCourseRolesAsync(bbCourseMems.Select(bbcm => bbcm.roleId).ToArray()), 3); //var bbCourseRoles = [email protected](); reconResult = await AddNewUsers(newMembers, reconResult); reconResult.NumAdded = newMembers.Count(); } var usersBbIdsToRemove = existingCrseUserIds.Where(ecu => !bbCourseMems.Select(cm => cm.userId).Contains(ecu)).ToList(); if (usersBbIdsToRemove.Any()) { reconResult.RemovedIds = await RemoveOrFlagUsers(ecatCourse, usersBbIdsToRemove); reconResult.NumRemoved = reconResult.RemovedIds.Count(); } return(reconResult); }