public IEnumerable <SuiteCRMUserViewModel> GetUsers(int pageNumber, int pageSize) { List <SuiteCRMUserViewModel> returnValue = new List <SuiteCRMUserViewModel>(); using (var connection = Helper.GetConnection(this.services, this.Logger)) using (SuiteCRMUserUnitOfWork userRepository = new SuiteCRMUserUnitOfWork(connection)) using (SuiteCRMEmailAddressUnitOfWork emailRepository = new SuiteCRMEmailAddressUnitOfWork(connection)) using (SuiteCRMEmailAddressBeanUnitOfWork emailBeanRepository = new SuiteCRMEmailAddressBeanUnitOfWork(connection)) { var suiteUsers = userRepository.GetUsers(pageNumber, pageSize); var ids = suiteUsers.Select(c => c.id).ToArray(); var suiteUserEmailBeans = emailBeanRepository.GetEmailAddressesBean(UsersBean, ids).ToArray(); var suiteUserEmails = emailRepository.GetEmailAddresses(suiteUserEmailBeans.Select(c => c.email_address_id).ToArray()).ToList(); string[] usernames = suiteUsers.Select(c => c.user_name).ToArray(); var orchardUsers = this.services.ContentManager.HqlQuery <UserPart>().Where(c => c.ContentPartRecord <UserPartRecord>(), c => c.In("UserName", usernames)).List(); foreach (var suiteUser in suiteUsers) { var suiteCRMEmailBean = suiteUserEmailBeans.FirstOrDefault(c => c.bean_id == suiteUser.id); if (suiteCRMEmailBean == null) { continue; } var suiteCRMUserEmail = suiteUserEmails.FirstOrDefault(c => c.id == suiteCRMEmailBean.email_address_id); if (suiteCRMUserEmail == null) { continue; } SuiteCRMUserViewModel item = new SuiteCRMUserViewModel(); item.SuiteCRMUsername = suiteUser.user_name; item.SuiteCRMUserId = suiteUser.id; item.SuiteCRMEmail = suiteCRMUserEmail.email_address; var user = orchardUsers.FirstOrDefault(c => c.UserName.ToLower() == suiteUser.user_name.ToLower() && c.Email != null && c.Email.ToLower() == suiteCRMUserEmail.email_address.ToLower()); if (user != null) { item.IsSync = true; item.OrchardUsername = user.UserName; item.OrchardEmail = user.Email; item.OrchardUserId = user.Id; } returnValue.Add(item); } } return(returnValue); }
public IEnumerable <SuiteCRMTaskDetailViewModel> CopyOrchardTicketsToSuite(CopyOrchardTasksToSuiteViewModel model) { List <SuiteCRMTaskDetailViewModel> returnValue = new List <SuiteCRMTaskDetailViewModel>(); using (var connection = Helper.GetConnection(this.services, this.Logger)) using (SuiteCRMTaskUnitOfWork taskRepository = new SuiteCRMTaskUnitOfWork(connection)) using (SuiteCRMEmailAddressBeanUnitOfWork suiteCRMEmailAddressBeanUnitOfWork = new SuiteCRMEmailAddressBeanUnitOfWork(taskRepository)) using (SuiteCRMEmailAddressUnitOfWork suiteCRMEmailAddressUnitOfWork = new SuiteCRMEmailAddressUnitOfWork(taskRepository)) using (SuiteCRMProjectTaskUnitOfWork projectTasksRepository = new SuiteCRMProjectTaskUnitOfWork(taskRepository)) using (var suiteCRMTransaction = taskRepository.BeginTransaction()) { TicketContext context = new TicketContext(); context.ProjectTaskUnitOfWork = projectTasksRepository; context.Priorities = this.priorityRepository.Table.ToList(); context.StatusList = this.statusRepository.Table.ToList(); try { var taskIds = model.Tasks.Where(c => !string.IsNullOrEmpty(c.SuiteCRMId)).Select(c => c.SuiteCRMId).ToArray(); var suiteTasks = taskRepository.GetTasks(taskIds); var suiteProjectTasks = projectTasksRepository.GetTasks(taskIds); var orchardTickets = this.services .ContentManager .GetMany <SuiteCRMTaskPart>( model.Tasks.Where(c => c.OrchardCollaborationTicketId.HasValue).Select(c => c.OrchardCollaborationTicketId.Value), VersionOptions.Published, new QueryHints().ExpandParts <TicketPart>()); foreach (var item in model.Tasks) { if (item.OrchardCollaborationTicketId == null) { continue; } var ticket = orchardTickets.FirstOrDefault(c => c.Id == item.OrchardCollaborationTicketId.Value); var suiteCRMTaskPart = ticket.As <SuiteCRMTaskPart>(); TicketPart ticketPart = ticket.As <TicketPart>(); ContentItemPermissionPart permissionPart = ticket.As <ContentItemPermissionPart>(); AttachToProjectPart attachToProjectPart = ticket.As <AttachToProjectPart>(); SuiteCRMProjectPart projectPart = null; if (!this.IsSyncingTicketValid(item, ticket, out projectPart)) { continue; } project_task suiteCRMProjectTask = null; task suiteCRMTask = null; if (!string.IsNullOrEmpty(suiteCRMTaskPart.ExternalId) && item.IsProjectTask) { suiteCRMProjectTask = suiteProjectTasks.FirstOrDefault(c => c.id == suiteCRMTaskPart.ExternalId); } if (!string.IsNullOrEmpty(suiteCRMTaskPart.ExternalId) && !item.IsProjectTask) { suiteCRMTask = suiteTasks.FirstOrDefault(c => c.id == suiteCRMTaskPart.ExternalId); } if (suiteCRMProjectTask == null && item.IsProjectTask) { suiteCRMProjectTask = new project_task(); suiteCRMProjectTask.project_id = item.SuiteCRMId; projectTasksRepository.Add(suiteCRMProjectTask); } if (suiteCRMTask == null && !item.IsProjectTask) { suiteCRMTask = new task(); taskRepository.Add(suiteCRMTask); } CommonPart commonPart = ticketPart.As <CommonPart>(); DateTime? lastOrchardTicketChangeDate = commonPart.ModifiedUtc ?? commonPart.CreatedUtc; if (suiteCRMProjectTask != null && ( !item.DoNotOverrideNewerValues || suiteCRMProjectTask.date_modified == null || (lastOrchardTicketChangeDate.HasValue && suiteCRMProjectTask.date_modified.Value <= lastOrchardTicketChangeDate.Value) )) { this.Copy(ticketPart, permissionPart, suiteCRMProjectTask, context); suiteCRMProjectTask.project_id = projectPart.ExternalId; projectTasksRepository.Save(); } if (suiteCRMTask != null && ( !item.DoNotOverrideNewerValues || suiteCRMTask.date_modified == null || (lastOrchardTicketChangeDate.HasValue && suiteCRMTask.date_modified.Value <= lastOrchardTicketChangeDate.Value) )) { this.Copy(ticketPart, permissionPart, suiteCRMTask, context); // set contact if (string.IsNullOrEmpty(suiteCRMTask.created_by) && commonPart.Owner != null) { var emailAddress = suiteCRMEmailAddressUnitOfWork.GetByEmail(commonPart.Owner.Email); if (emailAddress != null) { var contact = suiteCRMEmailAddressBeanUnitOfWork.GetBeanIdOfEmailAddress(Helper.ContactsModuleName, new[] { emailAddress.id }).FirstOrDefault(); if (contact != null) { suiteCRMTask.parent_id = contact.id; suiteCRMTask.parent_type = Helper.ContactsModuleName; } } } projectTasksRepository.Save(); } suiteCRMTaskPart.ExternalId = item.IsProjectTask ? suiteCRMProjectTask.id : suiteCRMTask.id; suiteCRMTaskPart.LastSyncTime = DateTime.UtcNow; suiteCRMTaskPart.TaskType = item.IsProjectTask ? SuiteCRMTaskPart.SuiteCRMProjectTaskTypeValue : SuiteCRMTaskPart.SuiteCRMTaskTypeValue; this.services.ContentManager.Publish(ticket.ContentItem); SuiteCRMTaskDetailViewModel returnValueItem = new SuiteCRMTaskDetailViewModel(); returnValueItem.OrchardCollaborationTicket = ticket.ContentItem; returnValueItem.IsSync = true; returnValueItem.IsProjectTask = item.IsProjectTask; returnValueItem.LastSyncTime = suiteCRMTaskPart.LastSyncTime; returnValueItem.SuiteCRMTaskId = suiteCRMTaskPart.ExternalId; returnValue.Add(returnValueItem); } suiteCRMTransaction.Commit(); } catch (Exception ex) { suiteCRMTransaction.Rollback(); throw ex; } } return(returnValue); }
public IEnumerable <SuiteCRMUserViewModel> CopySuiteCRMUsersToOrchard(CopySuiteCRMUsersToOrchardViewModel model) { List <SuiteCRMUserViewModel> returnValue = new List <SuiteCRMUserViewModel>(); using (var connection = Helper.GetConnection(this.services, this.Logger)) using (SuiteCRMUserUnitOfWork userRepository = new SuiteCRMUserUnitOfWork(connection)) using (SuiteCRMEmailAddressUnitOfWork emailRepository = new SuiteCRMEmailAddressUnitOfWork(connection)) using (SuiteCRMEmailAddressBeanUnitOfWork emailBeanRepository = new SuiteCRMEmailAddressBeanUnitOfWork(connection)) { var ids = model.Users.Where(c => !string.IsNullOrEmpty(c.SuiteCRMUserId)).Select(c => c.SuiteCRMUserId).ToArray(); var suiteUsers = userRepository.GetUsers(ids); var suiteUserEmailBeans = emailBeanRepository.GetEmailAddressesBean(UsersBean, ids).ToArray(); var suiteUserEmails = emailRepository.GetEmailAddresses(suiteUserEmailBeans.Select(c => c.email_address_id).ToArray()).ToList(); var orchardUsers = this.services .ContentManager .GetMany <IUser>(model.Users.Where(c => c.OrchardUserId.HasValue).Select(c => c.OrchardUserId.Value), VersionOptions.Published, new QueryHints()); var operatorRole = this.GetOperatorRole(); foreach (var item in model.Users.Where(c => !string.IsNullOrEmpty(c.SuiteCRMUserId))) { var suiteCRMUser = suiteUsers.FirstOrDefault(c => c.id == item.SuiteCRMUserId); if (suiteCRMUser == null) { continue; } var suiteCRMEmailBean = suiteUserEmailBeans.FirstOrDefault(c => c.bean_id == item.SuiteCRMUserId); if (suiteCRMEmailBean == null) { continue; } var suiteCRMUserEmail = suiteUserEmails.FirstOrDefault(c => c.id == suiteCRMEmailBean.email_address_id); if (suiteCRMUserEmail == null) { continue; } IUser user = orchardUsers.FirstOrDefault(c => c.UserName.ToLower() == suiteCRMUser.user_name.ToLower() || c.Email.ToLower() == suiteCRMUserEmail.email_address.ToLower()); if (user != null) { continue; } var newUser = this.membershipService.CreateUser(new CreateUserParams( suiteCRMUser.user_name, model.DefaultPassword, suiteCRMUserEmail.email_address, null, null, true)); SuiteCRMUserPart suiteCRMUserPart = newUser.As <SuiteCRMUserPart>(); suiteCRMUserPart.ExternalId = suiteCRMUser.id; suiteCRMUserPart.LastSyncTime = DateTime.UtcNow; // Full name var userPart = newUser.ContentItem.Parts.FirstOrDefault(d => d.PartDefinition.Name.ToLower(CultureInfo.InvariantCulture) == "user"); userPart.Store("FullName", string.Format("{0} {1}", suiteCRMUser.first_name, suiteCRMUser.last_name)); // role if (operatorRole != null) { UserRolesPartRecord newUserRole = new UserRolesPartRecord { UserId = newUser.Id, Role = new RoleRecord { Id = operatorRole.Id } }; this.userRolesPartRecordRepository.Create(newUserRole); } this.services.ContentManager.Publish(newUser.ContentItem); this.userRolesPartRecordRepository.Flush(); returnValue.Add(new SuiteCRMUserViewModel { IsSync = true, OrchardEmail = newUser.Email, OrchardUserId = newUser.Id, SuiteCRMUserId = suiteCRMUser.id, SuiteCRMEmail = suiteCRMUserEmail.email_address, SuiteCRMUsername = suiteCRMUser.user_name, OrchardUsername = newUser.UserName }); } } return(returnValue); }