Ejemplo n.º 1
0
        /// <summary>
        /// Creates or updates the rows necessary to attach a Contact to an Assessment.
        /// Creates a new User based on email.
        /// Creates or overwrites the ASSESSMENT_CONTACTS connection.
        /// Creates a new Contact if needed.  If a Contact already exists for the email, no
        /// changes are made to the Contact row.
        /// </summary>
        public ContactDetail CreateAndAddContactToAssessment(ContactCreateParameters newContact)
        {
            int          assessmentId = Auth.AssessmentForUser();
            TokenManager tm           = new TokenManager();
            string       app_code     = tm.Payload(Constants.Token_Scope);

            NotificationManager nm = new NotificationManager();

            ASSESSMENT_CONTACTS existingContact = null;

            using (var db = new CSET_Context())
            {
                // See if the Contact already exists
                existingContact = db.ASSESSMENT_CONTACTS.Where(x => x.UserId == newContact.UserId && x.Assessment_Id == assessmentId).FirstOrDefault();
                if (existingContact == null)
                {
                    // Create Contact
                    var c = new ASSESSMENT_CONTACTS()
                    {
                        FirstName        = newContact.FirstName,
                        LastName         = newContact.LastName,
                        PrimaryEmail     = newContact.PrimaryEmail,
                        Assessment_Id    = assessmentId,
                        AssessmentRoleId = newContact.AssessmentRoleId
                    };

                    // Include the userid if such a user exists
                    USERS user = db.USERS.Where(u => !string.IsNullOrEmpty(u.PrimaryEmail) &&
                                                u.PrimaryEmail == newContact.PrimaryEmail)
                                 .FirstOrDefault();
                    if (user != null)
                    {
                        c.UserId = user.UserId;
                    }

                    db.ASSESSMENT_CONTACTS.AddOrUpdate(c, x => x.Assessment_Contact_Id);


                    // If there was no USER record for this new Contact, create one
                    if (user == null)
                    {
                        UserDetail userDetail = new UserDetail
                        {
                            Email                 = newContact.PrimaryEmail,
                            FirstName             = newContact.FirstName,
                            LastName              = newContact.LastName,
                            IsSuperUser           = false,
                            PasswordResetRequired = true
                        };
                        UserManager        um   = new UserManager();
                        UserCreateResponse resp = um.CheckUserExists(userDetail);
                        if (!resp.IsExisting)
                        {
                            resp = um.CreateUser(userDetail);

                            // Send this brand-new user an email with their temporary password (if they have an email)
                            if (!string.IsNullOrEmpty(userDetail.Email))
                            {
                                if (!UserAuthentication.IsLocalInstallation(app_code))
                                {
                                    nm.SendInviteePassword(userDetail.Email, userDetail.FirstName, userDetail.LastName, resp.TemporaryPassword);
                                }
                            }
                        }
                        c.UserId = resp.UserId;
                    }

                    db.SaveChanges();

                    AssessmentUtil.TouchAssessment(assessmentId);

                    existingContact = c;
                }
            }

            // Flip the 'invite' flag to true, if they are a contact on the current Assessment
            ContactsManager cm = new ContactsManager();

            cm.MarkContactInvited(newContact.UserId, assessmentId);

            // Tell the user that they have been invited to participate in an Assessment (if they have an email)
            if (!string.IsNullOrEmpty(newContact.PrimaryEmail))
            {
                if (!UserAuthentication.IsLocalInstallation(app_code))
                {
                    nm.InviteToAssessment(newContact);
                }
            }

            // Return the full list of Contacts for the Assessment
            return(new ContactDetail
            {
                FirstName = existingContact.FirstName,
                LastName = existingContact.LastName,
                PrimaryEmail = existingContact.PrimaryEmail,
                AssessmentId = existingContact.Assessment_Id,
                AssessmentRoleId = existingContact.AssessmentRoleId,
                Invited = existingContact.Invited,
                UserId = existingContact.UserId ?? null
            });
        }