public void When_UpsertCustomerContactData_andEmailAlreadyExists_throw_EmailAddressAlreadyExistsException() { var client = Substitute.For <IRestClient>(); client.LastResponse.Returns(new RestClient.APIResponse(new HttpResponseMessage(HttpStatusCode.Accepted)) { IsSuccess = false, Content = "Contact with Email Address [email protected] already exists" }); _dssService = new DssService(client, Options.Create(new DssSettings() { ApiKey = "9238dfjsjdsidfs83fds", CustomerApiUrl = "https://this.is.anApi.org.uk", AccountsTouchpointId = "9000000001", CustomerApiVersion = "V1", DigitalIdentitiesPatchByCustomerIdApiVersion = "https://this.is.anApi.org.uk", DigitalIdentitiesPatchByCustomerIdApiUrl = "https://this.is.anApi.org.uk", CustomerContactDetailsApiUrl = "https://this.is.anApi.org.uk" }), _logger); Assert.ThrowsAsync <EmailAddressAlreadyExistsException>(() => _dssService.UpsertCustomerContactData( new Customer { Contact = new Contact { ContactId = Guid.NewGuid().ToString(), EmailAddress = "*****@*****.**" }, })); }
public async Task When_UpsertCustomerContactData_ForExistingContactDetails_ReturnOk() { var client = Substitute.For <IRestClient>(); client.LastResponse.Returns(new RestClient.APIResponse(new HttpResponseMessage(HttpStatusCode.Accepted)) { IsSuccess = true, Content = string.Empty, }); _dssService = new DssService(client, Options.Create(new DssSettings() { ApiKey = "9238dfjsjdsidfs83fds", CustomerApiUrl = "https://this.is.anApi.org.uk", AccountsTouchpointId = "9000000001", CustomerApiVersion = "V1", DigitalIdentitiesPatchByCustomerIdApiVersion = "https://this.is.anApi.org.uk", DigitalIdentitiesPatchByCustomerIdApiUrl = "https://this.is.anApi.org.uk", CustomerContactDetailsApiUrl = "https://this.is.anApi.org.uk" }), _logger); await _dssService.UpsertCustomerContactData(new Customer { Contact = new Contact { ContactId = Guid.NewGuid().ToString(), }, }); await client.Received(1).PatchAsync <object>(Arg.Any <string>(), Arg.Any <HttpRequestMessage>()); }
public async Task <IActionResult> Body(EditDetailsCompositeViewModel viewModel, IFormCollection formCollection) { var customer = await GetCustomerDetails(); var additionalData = GetEditDetailsAdditionalData(formCollection); viewModel.Identity.MarketingPreferences = new MarketingPreferences { OptOutOfMarketing = !additionalData.MarketingOptIn, OptOutOfMarketResearch = !additionalData.MarketResearchOptIn }; var customerDetails = await _dssReader.GetCustomerData(customer.CustomerId.ToString()); if (IsValid(ModelState, viewModel)) { try { var dateOfBirthDay = viewModel.Identity.PersonalDetails.DateOfBirthDay; var dateOfBirthMonth = viewModel.Identity.PersonalDetails.DateOfBirthMonth; var dateOfBirthYear = viewModel.Identity.PersonalDetails.DateOfBirthYear; CultureInfo enGb = new CultureInfo("en-GB"); string dob = string.Empty; if (!string.IsNullOrEmpty(dateOfBirthDay) && !string.IsNullOrEmpty(dateOfBirthMonth) && !string.IsNullOrEmpty(dateOfBirthYear)) { dob = $"{dateOfBirthDay.PadLeft(2, '0')}/{dateOfBirthMonth.PadLeft(2, '0')}/{dateOfBirthYear.PadLeft(4, '0')}"; } if (DateTime.TryParseExact(dob, "dd/MM/yyyy", enGb, DateTimeStyles.AdjustToUniversal, out var dateOfBirth)) { viewModel.Identity.PersonalDetails.DateOfBirth = dateOfBirth; } else { viewModel.Identity.PersonalDetails.DateOfBirth = null; } var updatedDetails = GetUpdatedCustomerDetails(customerDetails, viewModel.Identity); await _dssWriter.UpdateCustomerData(updatedDetails); updatedDetails.Contact.LastModifiedDate = DateTime.UtcNow.AddMinutes(-1); await _dssWriter.UpsertCustomerContactData(updatedDetails); return(new RedirectResult("/your-account/your-details", false)); } catch (EmailAddressAlreadyExistsException) { ModelState.AddModelError("Identity.ContactDetails.ContactEmail", "Email address already in use"); } } ViewModel.Identity = viewModel.Identity; return(await base.Body()); }
public async Task <IActionResult> Body(EditDetailsCompositeViewModel viewModel, IFormCollection formCollection) { var customer = await GetCustomerDetails(); var additionalData = GetEditDetailsAdditionalData(formCollection); viewModel.Identity.MarketingPreferences = new MarketingPreferences { OptOutOfMarketing = !additionalData.MarketingOptIn, OptOutOfMarketResearch = !additionalData.MarketResearchOptIn }; if (!string.IsNullOrWhiteSpace(additionalData.FindAddress)) { ModelState.Clear(); await FindAddress(viewModel); } else if (!string.IsNullOrWhiteSpace(additionalData.SelectAddress)) { ModelState.Clear(); await GetSelectAddress(viewModel, additionalData); } else if (!string.IsNullOrWhiteSpace(additionalData.SaveDetails)) { var customerDetails = await _dssReader.GetCustomerData(customer.CustomerId.ToString()); if (IsValid(ModelState, viewModel)) { try { string dateOfBirthDay = viewModel.Identity.PersonalDetails.DateOfBirthDay; string dateOfBirthMonth = viewModel.Identity.PersonalDetails.DateOfBirthMonth; string dateOfBirthYear = viewModel.Identity.PersonalDetails.DateOfBirthYear; DateTime dateOfBirth = default(DateTime); CultureInfo enGb = new CultureInfo("en-GB"); string dob = string.Empty; if (!string.IsNullOrEmpty(dateOfBirthDay) && !string.IsNullOrEmpty(dateOfBirthMonth) && !string.IsNullOrEmpty(dateOfBirthYear)) { dob = string.Format("{0}/{1}/{2}", dateOfBirthDay.PadLeft(2, '0'), dateOfBirthMonth.PadLeft(2, '0'), dateOfBirthYear.PadLeft(4, '0')); } if (DateTime.TryParseExact(dob, "dd/MM/yyyy", enGb, DateTimeStyles.AdjustToUniversal, out dateOfBirth)) { viewModel.Identity.PersonalDetails.DateOfBirth = dateOfBirth; } else { viewModel.Identity.PersonalDetails.DateOfBirth = null; } var updatedDetails = GetUpdatedCustomerDetails(customerDetails, viewModel.Identity); await _dssWriter.UpdateCustomerData(updatedDetails); var addressToUpdate = string.IsNullOrEmpty(viewModel.Identity.PersonalDetails.AddressId) ? updatedDetails.Addresses.FirstOrDefault(x => string.IsNullOrEmpty(x.AddressId)) : updatedDetails.Addresses.FirstOrDefault(x => x.AddressId == viewModel.Identity.PersonalDetails.AddressId); if (addressToUpdate != null) { //await _dssWriter.UpsertCustomerAddressData(addressToUpdate, updatedDetails.CustomerId); } updatedDetails.Contact.LastModifiedDate = DateTime.UtcNow.AddMinutes(-1); await _dssWriter.UpsertCustomerContactData(updatedDetails); return(new RedirectResult("/your-account/your-details", false)); } catch (EmailAddressAlreadyExistsException ex) { ModelState.AddModelError("Identity.ContactDetails.ContactEmail", "Email address already in use"); } } } ViewModel.Items = viewModel.Items; ViewModel.Identity = viewModel.Identity; return(await base.Body()); }