public ContactDetailsResponse Execute(ResidentContact contactRequest) { if (contactRequest.ResidentId == null && string.IsNullOrWhiteSpace(contactRequest.NccContactId)) { throw new NoIdentifierException(); } var contactDomain = new ContactDetailsDomain { ContactValue = contactRequest.Value, IsActive = contactRequest.Active, IsDefault = contactRequest.Default, TypeId = contactRequest.TypeId, SubtypeId = contactRequest.SubtypeId, Id = contactRequest?.Id }; var response = _residentGateway.InsertResidentContactDetails(contactRequest.ResidentId, contactRequest.NccContactId, contactDomain); if (response == null) { throw new ResidentNotFoundException(); } if (contactRequest.Id != null) { _residentGateway.InvalidateContactDetailsRecord(contactRequest.Id); } return(new ContactDetailsResponse { Id = response.Value }); }
public static ResidentContactViewModel ToResponse(this ResidentContact domain) { return(new ResidentContactViewModel { FirstName = domain.FirstName, LastName = domain.LastName, PhoneNumbers = domain.PhoneNumbers }); }
private async Task <HttpResponseMessage> CallPostEndpointWithRequest(ResidentContact contactRequest) { var url = new Uri("/api/v1/contact-details", UriKind.Relative); using var content = new StringContent(JsonConvert.SerializeObject(contactRequest), Encoding.UTF8, "application/json"); using var response = await Client.PostAsync(url, content).ConfigureAwait(true); return(response); }
public async Task Returns400IfResidentContactParamModelStateIsInvalid() { var contactRequest = new ResidentContact { Value = null }; var response = await CallPostEndpointWithRequest(contactRequest).ConfigureAwait(true); response.StatusCode.Should().Be(400); }
private void CheckContactHasBeSavedInDatabaseForResidentId(int residentId, ResidentContact contactRequest) { var savedContact = ResidentContactContext.ContactDetails .Where(c => c.ResidentId == residentId) .FirstOrDefault(c => c.ContactValue == contactRequest.Value); savedContact.Should().NotBeNull(); savedContact.ContactValue.Should().BeEquivalentTo(contactRequest.Value); savedContact.ContactSubTypeLookupId.Should().Be(contactRequest.SubtypeId); savedContact.ContactTypeLookupId.Should().Be(contactRequest.TypeId); savedContact.IsActive.Should().Be(contactRequest.Active); savedContact.IsDefault.Should().Be(contactRequest.Default); }
public IActionResult CreateContactRecord([FromBody] ResidentContact rcp) { try { var resident = _createContactDetails.Execute(rcp); return(CreatedAtAction("ViewRecord", resident)); } catch (NoIdentifierException) { return(BadRequest( "Request must include either the residents ID or the contact ID for the resident from NCC")); } catch (ResidentNotFoundException) { return(BadRequest("Resident ID and/or NCC Contact ID do not link to a resident record")); } }
public async Task Returns201IfNewContactRecordIsCreatedForResident() { var contactRequest = new ResidentContact { SubtypeId = _faker.Random.Int(1, 50), TypeId = _faker.Random.Int(1, 50), Value = "test@test", Active = _faker.Random.Bool(), Default = _faker.Random.Bool() }; var resident = E2ETestsHelper.AddPersonWithRelatedEntitiesToDb(ResidentContactContext, contactTypeLookupId: contactRequest.TypeId, contactSubTypeLookupId: contactRequest.SubtypeId); contactRequest.ResidentId = resident.Id; var response = await CallPostEndpointWithRequest(contactRequest).ConfigureAwait(true); response.StatusCode.Should().Be(201); CheckContactHasBeSavedInDatabaseForResidentId(resident.Id, contactRequest); }
public async Task CanCreateANewContactDetailGivenAnExternalContactIdToIdentifyTheResident() { var contactRequest = new ResidentContact { SubtypeId = _faker.Random.Int(1, 50), TypeId = _faker.Random.Int(1, 50), Value = "test@test", Active = _faker.Random.Bool(), Default = _faker.Random.Bool() }; var resident = E2ETestsHelper.AddPersonWithRelatedEntitiesToDb(ResidentContactContext, contactTypeLookupId: contactRequest.TypeId, contactSubTypeLookupId: contactRequest.SubtypeId); contactRequest.NccContactId = E2ETestsHelper.AddCrmContactIdForResidentId(ResidentContactContext, resident.Id).ExternalIdValue; var response = await CallPostEndpointWithRequest(contactRequest).ConfigureAwait(true); response.StatusCode.Should().Be(201); CheckContactHasBeSavedInDatabaseForResidentId(resident.Id, contactRequest); }
public Task <Resident> Update(ResidentRequest resident) { var residentExisting = GetResident(resident.ReferenceId); if (residentExisting == null) { throw new ArgumentNullException(nameof(resident)); } var residentEntity = ConvertToResidentEntity(resident); residentEntity.Id = residentExisting.Id; // Contact Info. Issue: Contact info is separate table but Email and Phone comes as values // Need to find if already exists? if so update else insert.. var existingResidentContacts = _residentContactDataProvider.GetResidentContactsByResidentId(residentEntity.Id); // _residentDataProvider.GetResidentContactsByResidentId(residentEntity.Id); List <ResidentContact> rcs = new List <ResidentContact>(); if (existingResidentContacts.Any()) { // get existing email address or phone existingResidentContacts.ForEach((rc) => { if (!string.IsNullOrEmpty(rc.ContactType) && rc.ContactType == CONTACT_TYPE.email.ToString()) { rc.Id = rc.Id; rc.Data = resident.EmailAddress; } if (!string.IsNullOrEmpty(rc.ContactType) && rc.ContactType == CONTACT_TYPE.phone.ToString()) { rc.Id = rc.Id; rc.Data = resident.PhoneNumber; } rcs.Add(rc); }); } else { // No existing contacts found if (!string.IsNullOrEmpty(residentEntity.EmailAddress)) { ResidentContact rc = new ResidentContact() { ContactType = CONTACT_TYPE.email.ToString(), Data = residentEntity.EmailAddress }; rcs.Add(rc); } if (!string.IsNullOrEmpty(residentEntity.PhoneNumber)) { ResidentContact rc = new ResidentContact() { ContactType = CONTACT_TYPE.phone.ToString(), Data = residentEntity.PhoneNumber }; rcs.Add(rc); } } residentEntity.ResidentContacts = rcs.ToArray(); // SocialWorker Info. Issue: SW info is separate table SocialWorker swToBeUpdIns = new SocialWorker(); if (resident.SocialWorker != null && resident.SocialWorker.ForeName != "") { swToBeUpdIns.ForeName = resident.SocialWorker.ForeName; swToBeUpdIns.SurName = resident.SocialWorker.SurName; swToBeUpdIns.EmailAddress = resident.SocialWorker.EmailAddress; swToBeUpdIns.PhoneNumber = resident.SocialWorker.PhoneNumber; } // Need to find if already exists? if so update else insert.. SocialWorker existingSocialWorker = _socialWorkerDataProvider.GetSocialWorkerByResidentId(residentEntity.Id); //_residentDataProvider.GetSocialWorker(residentEntity.Id); if (existingSocialWorker != null) { swToBeUpdIns.Id = existingSocialWorker.Id; } residentEntity.SocialWorker = swToBeUpdIns; var residentEntityUpdated = _residentDataProvider.Update(residentEntity); // todo: return new resident... var residentCreated = new Resident() { ReferenceId = residentEntity.ReferenceId }; return(Task.FromResult(residentCreated)); }