/// <inheritdoc />
        public void Update(Person person, bool updateEmailAddressVerification)
        {
            // As this is an update request, we can only update the record
            // if the email address exists currently.
            // Check for the Person instance first off.
            string emailAddress = person.ContactDetail.EmailAddress;

            ReadPersonResult readPersonResult =
                this.GetContactDetailByEmail(emailAddress);

            if (readPersonResult != null)
            {
                this.loggerProvider.Info(
                    $"\"{emailAddress}\" exists. Going ahead with update " +
                    $"of {person}.");

                this.UpdatePersonInDatabase(
                    person,
                    readPersonResult,
                    updateEmailAddressVerification);

                this.loggerProvider.Info(
                    $"{person} was updated successfully.");
            }
            else
            {
                throw new PersonRecordDoesNotExistException(emailAddress);
            }
        }
        /// <inheritdoc />
        public void Create(Person person)
        {
            string emailAddress = person.ContactDetail.EmailAddress;

            // As this is a create request, we can only create the record
            // if the email address does not exist currently.
            // Check for the Person instance first off.
            ReadPersonResult readPersonResult =
                this.GetContactDetailByEmail(emailAddress);

            if (readPersonResult == null)
            {
                this.loggerProvider.Info(
                    $"\"{emailAddress}\" does not exist. Going ahead " +
                    $"with insert of {person}.");

                this.InsertRecordsIntoDatabase(person);

                this.loggerProvider.Info(
                    $"{person} was inserted into the database " +
                    $"successfully.");
            }
            else
            {
                throw new PersonRecordExistsAlreadyException(emailAddress);
            }
        }
Exemple #3
0
        public void Update_EmailAddressExistsAlready_NoExceptionThrown()
        {
            // Arrange
            ReadPersonResult readPersonResult = new ReadPersonResult()
            {
                Id = 1234,
                ContactDetail_Id = 9876,
            };

            PersonManager personManager = this.CreatePersonManagerInstance(
                mockDataCaptureDatabaseAdapter =>
            {
                mockDataCaptureDatabaseAdapter
                .Setup(x => x.ReadPerson(It.IsAny <string>()))
                .Returns(readPersonResult);
            });

            Person person = new Person()
            {
                Consent = new Consent()
                {
                    GdprConsentDeclared = DateTime.Parse("2018-11-19 08:30:21"),
                    GdprConsentGiven    = true,
                },
                Cookie = new Cookie()
                {
                    Captured         = DateTime.Parse("2018-11-19 08:31:11"),
                    CookieIdentifier = "ABC0123456789",
                },
                Route = new Route()
                {
                    Captured        = DateTime.Parse("2018-11-19 08:29:03"),
                    RouteIdentifier = "ZYX9876543210",
                },
                ContactDetail = new ContactDetail()
                {
                    Captured     = DateTime.Parse("2018-11-18 08:30:21"),
                    EmailAddress = "*****@*****.**",
                    EmailVerificationCompletion = DateTime.Parse("2018-11-18 13:00:23"),
                },
                Enrolled  = DateTime.Parse("2018-11-18 08:30:20"),
                FirstName = "Joe",
                LastName  = "Bloggs",
            };

            PersonRecordDoesNotExistException thrownException = null;

            // Act
            try
            {
                personManager.Update(person, true);
            }
            catch (PersonRecordDoesNotExistException personRecordDoesNotExistException)
            {
                thrownException = personRecordDoesNotExistException;
            }

            // Assert
            Assert.IsNull(thrownException);
        }
        private ReadPersonResult GetContactDetailByEmail(
            string emailAddress)
        {
            ReadPersonResult toReturn = null;

            this.loggerProvider.Debug(
                $"Checking for existing {nameof(ContactDetail)} record " +
                $"using email address \"{emailAddress}\"...");

            toReturn = this.dataCaptureDatabaseAdapter.ReadPerson(
                emailAddress);

            return(toReturn);
        }
        /// <inheritdoc />
        public ReadPersonResult ReadPerson(
            string emailAddress)
        {
            ReadPersonResult toReturn = null;

            object sprocParameters =
                new
            {
                EmailAddress = emailAddress,
            };

            toReturn =
                this.ExecuteStoredProcedureSingularResult <ReadPersonResult>(
                    "Read_Person",
                    sprocParameters);

            return(toReturn);
        }
        private void UpdatePersonInDatabase(
            Person person,
            ReadPersonResult readContactDetailResult,
            bool updateEmailAddressVerification)
        {
            // First, insert the one-to-many records, as required.
            long personId = readContactDetailResult.Id;

            this.InsertIntoOneToManyTables(personId, person);

            // Then update the Person and ContactDetail tables.
            this.loggerProvider.Debug(
                $"Invoking " +
                $"{nameof(IDataCaptureDatabaseAdapter)}.{nameof(IDataCaptureDatabaseAdapter.UpdatePerson)} " +
                $"with id {personId}...");

            this.dataCaptureDatabaseAdapter.UpdatePerson(
                personId,
                person.FirstName,
                person.LastName);

            this.loggerProvider.Info(
                $"Updated {nameof(Person)} id {personId}.");

            if (updateEmailAddressVerification)
            {
                long contactDetailId = readContactDetailResult.ContactDetail_Id;

                this.loggerProvider.Debug(
                    $"Invoking " +
                    $"{nameof(IDataCaptureDatabaseAdapter)}.{nameof(IDataCaptureDatabaseAdapter.UpdateContactDetail)} " +
                    $"with id {contactDetailId}...");

                this.dataCaptureDatabaseAdapter.UpdateContactDetail(
                    contactDetailId,
                    person.ContactDetail.EmailVerificationCompletion);

                this.loggerProvider.Info(
                    $"Updated {nameof(ContactDetail)} id {contactDetailId}.");
            }
        }