public static Address ToDomain(this DbAddress address) { return(new Address { AddressLine1 = address.AddressLines, PostCode = address.PostCode }); }
public static AddressDomain DbAddressToAddressDomain(DbAddress address) { return(new AddressDomain() { Address = address.AddressLines, Postcode = address.PostCode, Uprn = address.Uprn }); }
public static AddressResponse ToResponse(this dbAddress address) { return(new AddressResponse() { AddressLine1 = address.AddressLines, DisplayAddressFlag = address.IsDisplayAddress, EndDate = address.EndDate, PostCode = address.PostCode }); }
public static AddNewResidentResponse ToResponse(this Person resident, dbAddress address, List <PersonOtherName> names, List <dbPhoneNumber> phoneNumbers, string caseNoteId, string caseNoteErrorMessage) { return(new AddNewResidentResponse { Id = resident.Id, AddressId = address?.AddressId, OtherNameIds = names?.Count > 0 ? names.Select(x => x.Id).ToList() : null, PhoneNumberIds = phoneNumbers?.Count > 0 ? phoneNumbers.Select(x => x.Id).ToList() : null, CaseNoteId = caseNoteId, CaseNoteErrorMessage = caseNoteErrorMessage }); }
public void CanMapDbAddressToAddressDomain() { dbAddress address = DatabaseGatewayHelper.CreateAddressDatabaseEntity(); AddressDomain expectedAddressDomain = new AddressDomain() { Address = address.AddressLines, Postcode = address.PostCode, Uprn = address.Uprn }; EntityFactory.DbAddressToAddressDomain(address).Should().BeEquivalentTo(expectedAddressDomain); }
public void UpdatePerson(UpdatePersonRequest request) { Person person = _databaseContext .Persons .Include(x => x.Addresses) .Include(x => x.PhoneNumbers) .Include(x => x.OtherNames) .FirstOrDefault(x => x.Id == request.Id); if (person == null) { throw new UpdatePersonException("Person not found"); } person.AgeContext = request.ContextFlag; person.DateOfBirth = request.DateOfBirth; person.DateOfDeath = request.DateOfDeath; person.EmailAddress = request.EmailAddress; person.Ethnicity = request.Ethnicity; person.FirstLanguage = request.FirstLanguage; person.FirstName = request.FirstName; person.FullName = $"{request.FirstName} {request.LastName}"; person.Gender = request.Gender; person.LastModifiedBy = request.CreatedBy; person.LastName = request.LastName; person.NhsNumber = request.NhsNumber; person.PreferredMethodOfContact = request.PreferredMethodOfContact; person.Religion = request.Religion; person.Restricted = request.Restricted; person.SexualOrientation = request.SexualOrientation; person.Title = request.Title; //replace phone numbers _databaseContext.PhoneNumbers.RemoveRange(person.PhoneNumbers); if (request.PhoneNumbers != null) { foreach (var number in request.PhoneNumbers) { person.PhoneNumbers.Add(number.ToEntity(person.Id, request.CreatedBy)); } } //replace other names _databaseContext.PersonOtherNames.RemoveRange(person.OtherNames); if (request.OtherNames != null) { foreach (var otherName in request.OtherNames) { person.OtherNames.Add(otherName.ToEntity(person.Id, request.CreatedBy)); } } //check for changed address if (request.Address != null) { Address displayAddress = person.Addresses.FirstOrDefault(x => x.IsDisplayAddress == "Y"); if (displayAddress == null) { person.Addresses.Add(GetNewDisplayAddress(request.Address.Address, request.Address.Postcode, request.Address.Uprn, request.CreatedBy)); } else { //has address changed? if (!(request.Address.Address == displayAddress.AddressLines && request.Address.Postcode == displayAddress.PostCode && displayAddress.Uprn == request.Address.Uprn)) { displayAddress.IsDisplayAddress = "N"; displayAddress.EndDate = DateTime.Now; displayAddress.LastModifiedBy = request.CreatedBy; person.Addresses.Add(GetNewDisplayAddress(request.Address.Address, request.Address.Postcode, request.Address.Uprn, request.CreatedBy)); } } } else //address not provided, remove current display address if it exists { Address displayAddress = person.Addresses.FirstOrDefault(x => x.IsDisplayAddress == "Y"); if (displayAddress != null) { displayAddress.IsDisplayAddress = "N"; displayAddress.EndDate = DateTime.Now; displayAddress.LastModifiedBy = request.CreatedBy; } } DateTime dt = DateTime.Now; UpdatePersonCaseNote note = new UpdatePersonCaseNote() { FirstName = person.FirstName, LastName = person.LastName, MosaicId = person.Id.ToString(), Timestamp = dt.ToString("dd/MM/yyyy H:mm:ss"), //in line with imported form data WorkerEmail = request.CreatedBy, Note = $"{dt.ToShortDateString()} Person details updated - by {request.CreatedBy}.", FormNameOverall = "API_Update_Person", FormName = "Person updated", CreatedBy = request.CreatedBy }; CaseNotesDocument caseNotesDocument = new CaseNotesDocument() { CaseFormData = JsonConvert.SerializeObject(note) }; //TODO: refactor so gateways don't call each other _ = _processDataGateway.InsertCaseNoteDocument(caseNotesDocument).Result; _databaseContext.SaveChanges(); }
//Handle case note creation exception like below for now public AddNewResidentResponse AddNewResident(AddNewResidentRequest request) { Address address = null; List <PersonOtherName> names = null; Person resident; List <dbPhoneNumber> phoneNumbers = null; try { resident = AddNewPerson(request); if (request.Address != null) { address = AddResidentAddress(request.Address, resident.Id, request.CreatedBy); resident.Addresses = new List <Address> { address }; } if (request.OtherNames?.Count > 0) { names = AddOtherNames(request.OtherNames, resident.Id, request.CreatedBy); resident.OtherNames = new List <PersonOtherName>(); resident.OtherNames.AddRange(names); } if (request.PhoneNumbers?.Count > 0) { phoneNumbers = AddPhoneNumbers(request.PhoneNumbers, resident.Id, request.CreatedBy); resident.PhoneNumbers = new List <dbPhoneNumber>(); resident.PhoneNumbers.AddRange(phoneNumbers); } _databaseContext.Persons.Add(resident); _databaseContext.SaveChanges(); } catch (DbUpdateException ex) { throw new ResidentCouldNotBeinsertedException( $"Error with inserting resident record has occurred - {ex.Message}"); } string caseNoteId = null; string caseNoteErrorMessage = null; //Add note try { DateTime dt = DateTime.Now; CreatePersonCaseNote note = new CreatePersonCaseNote() { FirstName = resident.FirstName, LastName = resident.LastName, MosaicId = resident.Id.ToString(), Timestamp = dt.ToString("dd/MM/yyyy H:mm:ss"), //in line with imported form data WorkerEmail = request.CreatedBy, Note = $"{dt.ToShortDateString()} Person added - by {request.CreatedBy}.", FormNameOverall = "API_Create_Person", FormName = "Person added", CreatedBy = request.CreatedBy }; CaseNotesDocument caseNotesDocument = new CaseNotesDocument() { CaseFormData = JsonConvert.SerializeObject(note) }; //TODO: refactor to appropriate pattern when using base API caseNoteId = _processDataGateway.InsertCaseNoteDocument(caseNotesDocument).Result; } catch (Exception ex) { caseNoteErrorMessage = $"Unable to create a case note for creating a person {resident.Id}: {ex.Message}"; } return(resident.ToResponse(address, names, phoneNumbers, caseNoteId, caseNoteErrorMessage)); }
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); }