public PatchContactHttpTrigger(IResourceHelper resourceHelper, IHttpRequestHelper httpRequestMessageHelper, IValidate validate, IPatchContactDetailsHttpTriggerService contactdetailsPatchService, IDocumentDBProvider provider, IHttpResponseMessageHelper httpResponseMessageHelper) { _resourceHelper = resourceHelper; _httpRequestMessageHelper = httpRequestMessageHelper; _validate = validate; _contactdetailsPatchService = contactdetailsPatchService; _provider = provider; _httpResponseMessageHelper = httpResponseMessageHelper; }
public void Setup() { _contactDetails = Substitute.For <Models.ContactDetails>(); _contactDetailsPatch = Substitute.For <Models.ContactDetailsPatch>(); _request = new HttpRequestMessage() { Content = new StringContent(string.Empty), RequestUri = new Uri($"http://localhost:7071/api/Customers/7E467BDB-213F-407A-B86A-1954053D3C24/ContactDetails/1e1a555c-9633-4e12-ab28-09ed60d51cb3") }; _log = Substitute.For <ILogger>(); _resourceHelper = Substitute.For <IResourceHelper>(); _validate = Substitute.For <IValidate>(); _httpRequestMessageHelper = Substitute.For <IHttpRequestMessageHelper>(); _patchContactHttpTriggerService = Substitute.For <IPatchContactDetailsHttpTriggerService>(); _provider = Substitute.For <IDocumentDBProvider>(); _httpRequestMessageHelper.GetTouchpointId(_request).Returns("0000000001"); _httpRequestMessageHelper.GetApimURL(_request).Returns("http://localhost:7071/"); }
public static async Task <HttpResponseMessage> RunAsync([HttpTrigger(AuthorizationLevel.Anonymous, "patch", Route = "customers/{customerId}/ContactDetails/{contactid}")] HttpRequestMessage req, ILogger log, string customerId, string contactid, [Inject] IResourceHelper resourceHelper, [Inject] IHttpRequestMessageHelper httpRequestMessageHelper, [Inject] IValidate validate, [Inject] IPatchContactDetailsHttpTriggerService contactdetailsPatchService, [Inject] IDocumentDBProvider provider) { var touchpointId = httpRequestMessageHelper.GetTouchpointId(req); if (string.IsNullOrEmpty(touchpointId)) { log.LogInformation("Unable to locate 'TouchpointId' in request header."); return(HttpResponseMessageHelper.BadRequest()); } var ApimURL = httpRequestMessageHelper.GetApimURL(req); if (string.IsNullOrEmpty(ApimURL)) { log.LogInformation("Unable to locate 'apimurl' in request header"); return(HttpResponseMessageHelper.BadRequest()); } log.LogInformation("C# HTTP trigger function Patch Contact processed a request. " + touchpointId); if (!Guid.TryParse(customerId, out var customerGuid)) { return(HttpResponseMessageHelper.BadRequest(customerGuid)); } if (!Guid.TryParse(contactid, out var contactGuid)) { return(HttpResponseMessageHelper.BadRequest(contactGuid)); } ContactDetailsPatch contactdetailsPatchRequest; try { contactdetailsPatchRequest = await httpRequestMessageHelper.GetContactDetailsFromRequest <ContactDetailsPatch>(req); } catch (JsonException ex) { return(HttpResponseMessageHelper.UnprocessableEntity(ex)); } if (contactdetailsPatchRequest == null) { return(HttpResponseMessageHelper.UnprocessableEntity(req)); } contactdetailsPatchRequest.LastModifiedTouchpointId = touchpointId; var doesCustomerExist = await resourceHelper.DoesCustomerExist(customerGuid); if (!doesCustomerExist) { return(HttpResponseMessageHelper.NoContent(customerGuid)); } var isCustomerReadOnly = await resourceHelper.IsCustomerReadOnly(customerGuid); if (isCustomerReadOnly) { return(HttpResponseMessageHelper.Forbidden(customerGuid)); } var contactdetails = await contactdetailsPatchService.GetContactDetailsForCustomerAsync(customerGuid, contactGuid); if (contactdetails == null) { return(HttpResponseMessageHelper.NoContent(contactGuid)); } var errors = validate.ValidateResource(contactdetailsPatchRequest, contactdetails, false); if (!string.IsNullOrEmpty(contactdetailsPatchRequest.EmailAddress)) { var contacts = await provider.GetContactsByEmail(contactdetailsPatchRequest.EmailAddress); if (contacts != null) { foreach (var contact in contacts) { var isReadOnly = await provider.DoesCustomerHaveATerminationDate(contact.CustomerId.GetValueOrDefault()); if (!isReadOnly && contact.CustomerId != contactdetails.CustomerId) { //if a customer that has the same email address is not readonly (has date of termination) //then email address on the request cannot be used. return(HttpResponseMessageHelper.Conflict()); } } } } // Set Digital account properties so that contentenhancer can queue change on digital identity topic. var diaccount = await provider.GetIdentityForCustomerAsync(contactdetails.CustomerId.Value); if (diaccount != null) { if (contactdetailsPatchRequest.EmailAddress == string.Empty) { if (errors == null) { errors = new List <System.ComponentModel.DataAnnotations.ValidationResult>(); } errors.Add(new System.ComponentModel.DataAnnotations.ValidationResult("Email Address cannot be removed because it is associated with a Digital Account", new List <string>() { "EmailAddress" })); return(HttpResponseMessageHelper.UnprocessableEntity(errors)); } if (!string.IsNullOrEmpty(contactdetails.EmailAddress) && !string.IsNullOrEmpty(contactdetailsPatchRequest.EmailAddress) && contactdetails.EmailAddress?.ToLower() != contactdetailsPatchRequest.EmailAddress?.ToLower() && diaccount.IdentityStoreId.HasValue) { contactdetails.SetDigitalAccountEmailChanged(contactdetailsPatchRequest.EmailAddress?.ToLower(), diaccount.IdentityStoreId.Value); } } if (errors != null && errors.Any()) { return(HttpResponseMessageHelper.UnprocessableEntity(errors)); } var updatedContactDetails = await contactdetailsPatchService.UpdateAsync(contactdetails, contactdetailsPatchRequest); if (updatedContactDetails != null) { await contactdetailsPatchService.SendToServiceBusQueueAsync(updatedContactDetails, customerGuid, ApimURL); } return(updatedContactDetails == null? HttpResponseMessageHelper.BadRequest(contactGuid) : HttpResponseMessageHelper.Ok(JsonHelper.SerializeObject(updatedContactDetails))); }