/// <summary> /// Accepts the invitation. /// </summary> /// <param name="invitationId">The invitation identifier.</param> /// <param name="userId">The user identifier.</param> public void AcceptInvitation(int invitationId, int userId) { Invitation invitation = DataContext.Invitations.Where(inv => inv.InvitationId == invitationId).FirstOrDefault(); var invitationUserRoles = DataContext.InvitationUserRoles.Where(iur => iur.InvitationId == invitationId && iur.IsActive); Code invitationTypeCode = Utils.GetCodeByCodeId(invitation.InvitationTypeCodeId); User user = GetUser(userId); #region Create project/company user if (invitationTypeCode.Value == "PROJECTTEAM") { int projectId = invitation.RelatedId; InvitationUserRole projectInvitationUserRole = invitationUserRoles.FirstOrDefault(); if (projectInvitationUserRole != null) { //Create a new project user and save ProjectUser projUser = new ProjectUser(); projUser.ProjectId = projectId; projUser.UserId = invitation.ToUserId.Value; projUser.ProjectUserTypeCodeId = projectInvitationUserRole.UserTypeCodeId; projUser.Role = invitation.ProjectRole; projUser.IsActive = true; projUser.CreatedByUserId = projUser.LastUpdatedByUserId = userId; projUser.CreatedDate = projUser.LastUpdatedDate = Utils.Now; projUser.CanSeeBudgetSummary = invitation.CanSeeProjectBudgetSummary; DataContext.ProjectUsers.AddObject(projUser); #region Notifications Data.Notification nf = new Data.Notification(); nf.ModuleTypeCodeId = Utils.GetCodeIdByCodeValue("ModuleType", "PROJTEAM"); nf.OperationTypeCodeId = Utils.GetCodeIdByCodeValue("OperationType", "ADD"); nf.RelatedId = invitation.RelatedId; nf.ProjectId = invitation.RelatedId; nf.Message = string.Format("{0} accepted the Project invitation.", (user.FirstName + " " + user.LastName).Trim()); nf.CreatedByUserId = nf.LastUpdatedByUserId = userId; nf.CreatedDate = nf.LastUpdatedDate = Utils.Now; DataContext.Notifications.AddObject(nf); #endregion Notifications #region Project Notification Setting //Get the current latest notification Id for the project. int maxNotificationId = DataContext.Notifications.Where(pnf => pnf.ProjectId == projectId).Max(pnf => pnf.NotificationId); //Create a project notification setting for this user (if it doesn't exist already) UserNotificationSetting unf = DataContext.UserNotificationSettings.Where(s => s.RelatedTable == "Project" && s.RelatedId == projectId && s.UserID == userId).FirstOrDefault(); if (unf == null) { unf = new UserNotificationSetting(); unf.RelatedTable = "Project"; unf.RelatedId = projectId; unf.UserID = userId; unf.LastNotificationId = maxNotificationId; unf.CreatedBy = unf.LastUpdatedBy = userId; unf.CreatedDate = unf.LastUpdatedDate = Utils.Now; DataContext.UserNotificationSettings.AddObject(unf); } #endregion Project Notification Setting //Update Project Daily Usage Summary //ProjectUsageHandler.UpdateProjectUsage(DataContext.Projects.Where(p => p.ProjectId == projectId).FirstOrDefault(), userId, projUser.UserId, false, Utils.Today, DataContext); } } else if (invitationTypeCode.Value == "COMPANYADMIN" || invitationTypeCode.Value == "INVENTORYTEAM") { CompanyBL companyBL = new CompanyBL(DataContext); CompanyUser companyUser = companyBL.GetCompanyUserByUserIdAndCompanyId(userId, invitation.RelatedId); bool addNewCompanyUser = false; if (companyUser == null) { companyUser = new CompanyUser(); companyUser.CompanyId = invitation.RelatedId; companyUser.UserId = invitation.ToUserId.Value; companyUser.CreatedDate = companyUser.LastUpdatedDate = Utils.Now; addNewCompanyUser = true; } //companyUser.CompanyUserTypeCodeId = invitation.UserTypeCodeId; companyUser.IsActive = true; companyUser.CreatedByUserId = companyUser.LastUpdatedByUserId = userId; foreach (InvitationUserRole invitationUserRole in invitationUserRoles) { companyUser.CompanyUserRoles.Add( new CompanyUserRole { IsActive = true, CompanyUserTypeCodeId = invitationUserRole.UserTypeCodeId, CreatedByUserId = userId, CreatedDate = Utils.Now, LastUpdatedByUserId = userId, LastUpdatedDate = Utils.Now, LocationId = invitationUserRole.LocationId } ); } if (addNewCompanyUser) { DataContext.CompanyUsers.AddObject(companyUser); } } #endregion Create project/company user //Mark the invitation as Accepted invitation.InvitationStatusCodeId = Utils.GetCodeIdByCodeValue("InvitationStatus", "ACCEPTED"); invitation.LastUpdatedByUserId = userId; invitation.LastUpdatedDate = Utils.Now; DataContext.SaveChanges(); #region Check for pending invitations to the same project/company and discard them int pendingInvitationCodeId = Utils.GetCodeIdByCodeValue("InvitationStatus", "PENDING"); var simillarInvitations = DataContext.Invitations.Where(inv => inv.ToUserId == userId && inv.InvitationStatusCodeId == pendingInvitationCodeId && inv.RelatedId == invitation.RelatedId && inv.InvitationTypeCodeId == invitation.InvitationTypeCodeId); foreach (Invitation inv in simillarInvitations) { DataContext.DeleteInvitation(inv.InvitationId); } #endregion Check for pending invitations to the same project/company and discard them }