public void HttpResponseMessageHelperTests_ReturnsStatusCodeForbidden_WhenHttpResponseMessageForbiddenRequestIsCalled() { _httpResponseMessageHelper.Forbidden().Returns(x => new HttpResponseMessage(HttpStatusCode.Forbidden)); var response = _httpResponseMessageHelper.Forbidden(); Assert.IsInstanceOf <HttpResponseMessage>(response); Assert.AreEqual(HttpStatusCode.Forbidden, response.StatusCode); }
public async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "patch", Route = "Customers/{customerId}/Interactions/{interactionId}")] HttpRequest req, ILogger log, string customerId, string interactionId) { var touchpointId = _httpRequestMessageHelper.GetDssTouchpointId(req); if (string.IsNullOrEmpty(touchpointId)) { log.LogInformation("Unable to locate 'APIM-TouchpointId' in request header."); return(_httpResponseMessageHelper.BadRequest()); } var ApimURL = _httpRequestMessageHelper.GetDssApimUrl(req); if (string.IsNullOrEmpty(ApimURL)) { log.LogInformation("Unable to locate 'apimurl' in request header"); return(_httpResponseMessageHelper.BadRequest()); } log.LogInformation("Patch Interaction C# HTTP trigger function processed a request. " + touchpointId); if (!Guid.TryParse(customerId, out var customerGuid)) { return(_httpResponseMessageHelper.BadRequest(customerGuid)); } if (!Guid.TryParse(interactionId, out var interactionGuid)) { return(_httpResponseMessageHelper.BadRequest(interactionGuid)); } InteractionPatch interactionPatchRequest; try { interactionPatchRequest = await _httpRequestMessageHelper.GetResourceFromRequest <Models.InteractionPatch>(req); } catch (JsonException ex) { return(_httpResponseMessageHelper.UnprocessableEntity(ex)); } if (interactionPatchRequest == null) { return(_httpResponseMessageHelper.UnprocessableEntity(req)); } interactionPatchRequest.LastModifiedTouchpointId = touchpointId; var errors = _validate.ValidateResource(interactionPatchRequest); if (errors != null && errors.Any()) { return(_httpResponseMessageHelper.UnprocessableEntity(errors)); } 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 interaction = await _interactionPatchService.GetInteractionForCustomerAsync(customerGuid, interactionGuid); if (interaction == null) { return(_httpResponseMessageHelper.NoContent(interactionGuid)); } var updatedInteraction = await _interactionPatchService.UpdateAsync(interaction, interactionPatchRequest); if (updatedInteraction != null) { await _interactionPatchService.SendToServiceBusQueueAsync(updatedInteraction, customerGuid, ApimURL); } return(updatedInteraction == null? _httpResponseMessageHelper.BadRequest(interactionGuid) : _httpResponseMessageHelper.Ok(_jsonHelper.SerializeObjectAndRenameIdProperty(updatedInteraction, "id", "InteractionId"))); }
public async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "Customers/{customerId}/Interactions/{interactionId}/Transfers/")] HttpRequest req, ILogger log, string customerId, string interactionId) { var touchpointId = _httpRequestMessageHelper.GetDssTouchpointId(req); if (string.IsNullOrEmpty(touchpointId)) { log.LogInformation("Unable to locate 'TouchpointId' in request header."); return(_httpResponseMessageHelper.BadRequest()); } var ApimURL = _httpRequestMessageHelper.GetDssApimUrl(req); if (string.IsNullOrEmpty(ApimURL)) { log.LogInformation("Unable to locate 'apimurl' in request header"); return(_httpResponseMessageHelper.BadRequest()); } log.LogInformation("Post Transfer C# HTTP trigger function processed a request. By Touchpoint. " + touchpointId); if (!Guid.TryParse(customerId, out var customerGuid)) { return(_httpResponseMessageHelper.BadRequest(customerGuid)); } if (!Guid.TryParse(interactionId, out var interactionGuid)) { return(_httpResponseMessageHelper.BadRequest(interactionGuid)); } Models.Transfer transferRequest; try { transferRequest = await _httpRequestMessageHelper.GetResourceFromRequest <Models.Transfer>(req); } catch (JsonException ex) { return(_httpResponseMessageHelper.UnprocessableEntity(ex)); } if (transferRequest == null) { return(_httpResponseMessageHelper.UnprocessableEntity(req)); } transferRequest.SetIds(customerGuid, interactionGuid, touchpointId); var errors = _validate.ValidateResource(transferRequest, true); if (errors != null && errors.Any()) { return(_httpResponseMessageHelper.UnprocessableEntity(errors)); } 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 doesInteractionExist = _resourceHelper.DoesInteractionResourceExistAndBelongToCustomer(interactionGuid, customerGuid); if (!doesInteractionExist) { return(_httpResponseMessageHelper.NoContent(interactionGuid)); } var transfer = await _transferPostService.CreateAsync(transferRequest); if (transfer != null) { await _transferPostService.SendToServiceBusQueueAsync(transfer, ApimURL); } return(transfer == null? _httpResponseMessageHelper.BadRequest(customerGuid) : _httpResponseMessageHelper.Created(_jsonHelper.SerializeObjectAndRenameIdProperty(transfer, "id", "TransferId"))); }
public async Task <HttpResponseMessage> RunAsync([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "customers/{customerId}/ContactDetails/")] HttpRequest req, ILogger log, string customerId) { var touchpointId = _httpRequestMessageHelper.GetDssTouchpointId(req); if (string.IsNullOrEmpty(touchpointId)) { log.LogInformation("Unable to locate 'TouchpointId' in request header."); return(_responseHelper.BadRequest()); } var ApimURL = _httpRequestMessageHelper.GetDssApimUrl(req); if (string.IsNullOrEmpty(ApimURL)) { log.LogInformation("Unable to locate 'apimurl' in request header"); return(_responseHelper.BadRequest()); } log.LogInformation("C# HTTP trigger function Post Contact processed a request. " + touchpointId); if (!Guid.TryParse(customerId, out var customerGuid)) { return(_responseHelper.BadRequest(customerGuid)); } Models.ContactDetails contactdetailsRequest; try { contactdetailsRequest = await _httpRequestMessageHelper.GetResourceFromRequest <Contact.Models.ContactDetails>(req); } catch (JsonException ex) { return(_responseHelper.UnprocessableEntity(ex)); } if (contactdetailsRequest == null) { return(_responseHelper.UnprocessableEntity(req)); } contactdetailsRequest.SetIds(customerGuid, touchpointId); var errors = _validate.ValidateResource(contactdetailsRequest, null, true); if (errors != null && errors.Any()) { return(_responseHelper.UnprocessableEntity(errors)); } var doesCustomerExist = await _resourceHelper.DoesCustomerExist(customerGuid); if (!doesCustomerExist) { return(_responseHelper.NoContent(customerGuid)); } var isCustomerReadOnly = await _resourceHelper.IsCustomerReadOnly(customerGuid); if (isCustomerReadOnly) { return(_responseHelper.Forbidden(customerGuid)); } var doesContactDetailsExist = _contactdetailsPostService.DoesContactDetailsExistForCustomer(customerGuid); if (doesContactDetailsExist) { return(_responseHelper.Conflict()); } if (!string.IsNullOrEmpty(contactdetailsRequest.EmailAddress)) { var contacts = await _provider.GetContactsByEmail(contactdetailsRequest.EmailAddress); if (contacts != null) { foreach (var contact in contacts) { var isReadOnly = await _provider.DoesCustomerHaveATerminationDate(contact.CustomerId.GetValueOrDefault()); if (!isReadOnly) { //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(_responseHelper.Conflict()); } } } } var contactDetails = await _contactdetailsPostService.CreateAsync(contactdetailsRequest); if (contactDetails != null) { await _contactdetailsPostService.SendToServiceBusQueueAsync(contactDetails, ApimURL); } return(contactDetails == null ? _responseHelper.BadRequest(customerGuid) : _responseHelper.Created(JsonHelper.SerializeObject(contactDetails))); }
public async Task <HttpResponseMessage> RunAsync([HttpTrigger(AuthorizationLevel.Anonymous, "patch", Route = "customers/{customerId}/ContactDetails/{contactid}")] HttpRequest req, ILogger log, string customerId, string contactid) { var touchpointId = _httpRequestMessageHelper.GetDssTouchpointId(req); if (string.IsNullOrEmpty(touchpointId)) { log.LogInformation("Unable to locate 'TouchpointId' in request header."); return(_httpResponseMessageHelper.BadRequest()); } var ApimURL = _httpRequestMessageHelper.GetDssApimUrl(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.GetResourceFromRequest <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))); }