public void CanMapPersonOtherNameFromDatabaseObjectToDomainObject()
        {
            PersonOtherName otherName = DatabaseGatewayHelper.CreatePersonOtherNameDatabaseEntity(_faker.Random.Long());

            var expectedResponse = new OtherName()
            {
                FirstName = otherName.FirstName,
                LastName  = otherName.LastName
            };

            otherName.ToDomain().Should().BeEquivalentTo(expectedResponse);
        }
        public void SetsTheMarkedForDeletionFlagToFalseForPersonAndRelatedEntitiesAndRemovesTheRecordFromDeletedPersonRecordTable()
        {
            //person to be restored, new master record and relationship
            var(deletedPerson, newMasterPersonRecord, personalRelationship, personalRelationshipType, _) = PersonalRelationshipsHelper.SavePersonWithPersonalRelationshipToDatabase(DatabaseContext);

            //address
            var address = DatabaseGatewayHelper.CreateAddressDatabaseEntity(personId: deletedPerson.Id);

            address.MarkedForDeletion = true;
            DatabaseContext.Addresses.Add(address);

            //telephone numbers
            var telephoneNumber = DatabaseGatewayHelper.CreatePhoneNumberEntity(personId: deletedPerson.Id);

            address.MarkedForDeletion = true;
            DatabaseContext.PhoneNumbers.Add(telephoneNumber);

            //other names
            var otherName = DatabaseGatewayHelper.CreatePersonOtherNameDatabaseEntity(personId: deletedPerson.Id);

            otherName.MarkedForDeletion = true;
            DatabaseContext.PersonOtherNames.Add(otherName);

            //allocations
            var allocation = TestHelpers.CreateAllocation(personId: (int)deletedPerson.Id);

            allocation.MarkedForDeletion = true;
            DatabaseContext.Allocations.Add(allocation);

            //warning notes
            var warningNote = TestHelpers.CreateWarningNote(personId: deletedPerson.Id);

            warningNote.MarkedForDeletion = true;
            DatabaseContext.WarningNotes.Add(warningNote);

            //inverse relationship (type not handled for simplicity)
            var inversePersonalRelationship = PersonalRelationshipsHelper.CreatePersonalRelationship(newMasterPersonRecord, deletedPerson, personalRelationshipType);

            inversePersonalRelationship.MarkedForDeletion = true;
            DatabaseContext.PersonalRelationships.Add(inversePersonalRelationship);

            //record marked as soft deleted
            DatabaseContext.DeletedPersonRecords.Add(new DeletedPersonRecord()
            {
                DeletedId = deletedPerson.Id,
                MasterId  = newMasterPersonRecord.Id
            });

            DatabaseContext.SaveChanges();

            //TODO: wrap into transaction when running manually (BEGIN; <SCRIPT> END;)
            //TODO: this is designed to be run against individual IDs. Replace the {deletedPerson.Id} with appropriate MosaicId (long)
            DatabaseContext.Database.ExecuteSqlInterpolated($@"
                   UPDATE dbo.dm_persons SET marked_for_deletion = false WHERE person_id = {deletedPerson.Id};
                   UPDATE dbo.dm_addresses SET marked_for_deletion = false WHERE person_id = {deletedPerson.Id};
                   UPDATE dbo.dm_telephone_numbers SET marked_for_deletion = false WHERE person_id = {deletedPerson.Id};
                   UPDATE dbo.sccv_person_other_name SET marked_for_deletion = false WHERE person_id = {deletedPerson.Id};
                   UPDATE dbo.sccv_allocations_combined SET marked_for_deletion = false WHERE mosaic_id = {deletedPerson.Id};
                   UPDATE dbo.sccv_warning_note SET marked_for_deletion = false WHERE person_id = {deletedPerson.Id};
                   UPDATE dbo.sccv_personal_relationship SET marked_for_deletion = false WHERE fk_person_id = {deletedPerson.Id};
                   UPDATE dbo.sccv_personal_relationship SET marked_for_deletion = false WHERE fk_other_person_id = {deletedPerson.Id};
                   DELETE from dbo.sccv_deleted_person_record where deleted_person_id = {deletedPerson.Id};
            ");

            DatabaseContext.Persons.First(x => x.Id == deletedPerson.Id).MarkedForDeletion.Should().BeFalse();
            DatabaseContext.Addresses.First(x => x.PersonId == deletedPerson.Id).MarkedForDeletion.Should().BeFalse();
            DatabaseContext.PhoneNumbers.First(x => x.PersonId == deletedPerson.Id).MarkedForDeletion.Should().BeFalse();
            DatabaseContext.PersonOtherNames.First(x => x.PersonId == deletedPerson.Id).MarkedForDeletion.Should().BeFalse();
            DatabaseContext.Allocations.First(x => x.PersonId == deletedPerson.Id).MarkedForDeletion.Should().BeFalse();
            DatabaseContext.WarningNotes.First(x => x.PersonId == deletedPerson.Id).MarkedForDeletion.Should().BeFalse();
            DatabaseContext.PersonalRelationships.First(x => x.PersonId == deletedPerson.Id).MarkedForDeletion.Should().BeFalse();
            DatabaseContext.PersonalRelationships.First(x => x.OtherPersonId == newMasterPersonRecord.Id).MarkedForDeletion.Should().BeFalse();

            DatabaseContext.DeletedPersonRecords.Any(x => x.DeletedId == deletedPerson.Id).Should().BeFalse();
        }
Beispiel #3
0
        public void SetsTheMarkedForDeletionFlagForPersonAndRelatedEntitiesAndUpdatesTheDeletedPersonRecordsTableWithCorrectRecords()
        {
            //person to be deleted, new master record and relationship
            var(personToBeDeleted, newMasterPersonRecord, personalRelationship, personalRelationshipType, _) = PersonalRelationshipsHelper.SavePersonWithPersonalRelationshipToDatabase(DatabaseContext);

            //address
            var address = DatabaseGatewayHelper.CreateAddressDatabaseEntity(personId: personToBeDeleted.Id);

            DatabaseContext.Addresses.Add(address);

            //telephone numbers
            var telephoneNumber = DatabaseGatewayHelper.CreatePhoneNumberEntity(personId: personToBeDeleted.Id);

            DatabaseContext.PhoneNumbers.Add(telephoneNumber);

            //other names
            var otherName = DatabaseGatewayHelper.CreatePersonOtherNameDatabaseEntity(personId: personToBeDeleted.Id);

            DatabaseContext.PersonOtherNames.Add(otherName);

            //allocations
            var allocation = TestHelpers.CreateAllocation(personId: (int)personToBeDeleted.Id);

            DatabaseContext.Allocations.Add(allocation);

            //warning notes
            var warningNote = TestHelpers.CreateWarningNote(personId: personToBeDeleted.Id);

            DatabaseContext.WarningNotes.Add(warningNote);

            //inverse relationship (type not handled for simplicity)
            var inversePersonalRelationship = PersonalRelationshipsHelper.CreatePersonalRelationship(newMasterPersonRecord, personToBeDeleted, personalRelationshipType);

            DatabaseContext.PersonalRelationships.Add(inversePersonalRelationship);

            //record marked for deletion
            DatabaseContext.PersonRecordsToBeDeleted.Add(new PersonRecordToBeDeleted()
            {
                IdToDelete = personToBeDeleted.Id,
                MasterId   = newMasterPersonRecord.Id
            });

            DatabaseContext.SaveChanges();

            DatabaseContext.Database.ExecuteSqlInterpolated(_queryUnderTest);

            //flags
            DatabaseContext.Persons.First(x => x.Id == personToBeDeleted.Id).MarkedForDeletion.Should().BeTrue();
            DatabaseContext.Addresses.First(x => x.PersonId == personToBeDeleted.Id).MarkedForDeletion.Should().BeTrue();
            DatabaseContext.PhoneNumbers.First(x => x.PersonId == personToBeDeleted.Id).MarkedForDeletion.Should().BeTrue();
            DatabaseContext.PersonOtherNames.First(x => x.PersonId == personToBeDeleted.Id).MarkedForDeletion.Should().BeTrue();
            DatabaseContext.Allocations.First(x => x.PersonId == personToBeDeleted.Id).MarkedForDeletion.Should().BeTrue();
            DatabaseContext.WarningNotes.First(x => x.PersonId == personToBeDeleted.Id).MarkedForDeletion.Should().BeTrue();
            DatabaseContext.PersonalRelationships.First(x => x.PersonId == personToBeDeleted.Id).MarkedForDeletion.Should().BeTrue();
            DatabaseContext.PersonalRelationships.First(x => x.OtherPersonId == newMasterPersonRecord.Id).MarkedForDeletion.Should().BeTrue();

            //record in deleted person record table
            var expectedDeletedPersonRecordEntry = new DeletedPersonRecord()
            {
                DeletedId = personToBeDeleted.Id,
                MasterId  = newMasterPersonRecord.Id,
                Timestamp = DateTime.Now.ToUniversalTime()
            };

            DatabaseContext.DeletedPersonRecords.First(x => x.DeletedId == personToBeDeleted.Id).MasterId.Should().Be(expectedDeletedPersonRecordEntry.MasterId);
            DatabaseContext.DeletedPersonRecords.First(x => x.DeletedId == personToBeDeleted.Id).DeletedId.Should().Be(expectedDeletedPersonRecordEntry.DeletedId);
            DatabaseContext.DeletedPersonRecords.First(x => x.DeletedId == personToBeDeleted.Id).Timestamp.Should().BeCloseTo(expectedDeletedPersonRecordEntry.Timestamp, precision: 5000);
        }
        public void CanMapPersonDetailsToGetPersonResponse()
        {
            Person person = DatabaseGatewayHelper.CreatePersonDatabaseEntity();

            person.Id = 123;

            Address address = DatabaseGatewayHelper.CreateAddressDatabaseEntity(person.Id);

            //set to display address
            address.IsDisplayAddress = "Y";

            PhoneNumber phoneNumber1 = DatabaseGatewayHelper.CreatePhoneNumberEntity(person.Id);
            PhoneNumber phoneNumber2 = DatabaseGatewayHelper.CreatePhoneNumberEntity(person.Id);

            PersonOtherName otherName1 = DatabaseGatewayHelper.CreatePersonOtherNameDatabaseEntity(person.Id);
            PersonOtherName otherName2 = DatabaseGatewayHelper.CreatePersonOtherNameDatabaseEntity(person.Id);

            person.Addresses = new List <Address>
            {
                address
            };

            person.PhoneNumbers = new List <PhoneNumber>
            {
                phoneNumber1,
                phoneNumber2
            };

            person.OtherNames = new List <PersonOtherName>
            {
                otherName1,
                otherName2
            };

            AddressDomain addressDomain = new AddressDomain()
            {
                Address  = address.AddressLines,
                Postcode = address.PostCode,
                Uprn     = address.Uprn
            };

            OtherName personOtherName1 = new OtherName()
            {
                FirstName = otherName1.FirstName,
                LastName  = otherName1.LastName
            };

            OtherName personOtherName2 = new OtherName()
            {
                FirstName = otherName2.FirstName,
                LastName  = otherName2.LastName
            };

            PhoneNumberDomain phoneNumberDomain1 = new PhoneNumberDomain()
            {
                Number = phoneNumber1.Number,
                Type   = phoneNumber1.Type
            };

            PhoneNumberDomain phoneNumberDomain2 = new PhoneNumberDomain()
            {
                Number = phoneNumber2.Number,
                Type   = phoneNumber2.Type
            };

            var expectedResponse = new GetPersonResponse()
            {
                EmailAddress      = person.EmailAddress,
                DateOfBirth       = person.DateOfBirth.Value,
                DateOfDeath       = person.DateOfDeath.Value,
                Address           = addressDomain,
                SexualOrientation = person.SexualOrientation,
                ContextFlag       = person.AgeContext,
                CreatedBy         = person.CreatedBy,
                Ethnicity         = person.Ethnicity,
                FirstLanguage     = person.FirstLanguage,
                FirstName         = person.FirstName,
                Gender            = person.Gender,
                LastName          = person.LastName,
                NhsNumber         = person.NhsNumber.Value,
                Id = person.Id,
                PreferredMethodOfContact = person.PreferredMethodOfContact,
                Religion   = person.Religion,
                Restricted = person.Restricted,
                Title      = person.Title,
                OtherNames = new List <OtherName>()
                {
                    personOtherName1, personOtherName2
                },
                PhoneNumbers = new List <PhoneNumberDomain>()
                {
                    phoneNumberDomain1, phoneNumberDomain2
                }
            };

            var result = ResponseFactory.ToResponse(person);

            result.Should().BeEquivalentTo(expectedResponse);
        }