Esempio n. 1
0
        public async Task PostContactHttpTrigger_ReturnsStatusCodeConflict_WhenContactDetailsForCustomerExists()
        {
            _httpRequestMessageHelper.GetContactDetailsFromRequest <Models.ContactDetails>(_request).Returns(Task.FromResult(_contactDetails).Result);

            _resourceHelper.DoesCustomerExist(Arg.Any <Guid>()).Returns(true);
            _postContactHttpTriggerService.DoesContactDetailsExistForCustomer(Arg.Any <Guid>()).ReturnsForAnyArgs(true);

            var result = await RunFunction(ValidCustomerId);

            // Assert
            Assert.IsInstanceOf <HttpResponseMessage>(result);
            Assert.AreEqual(HttpStatusCode.Conflict, result.StatusCode);
        }
        public static async Task <HttpResponseMessage> RunAsync([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "customers/{customerId}/ContactDetails/")] HttpRequestMessage req, ILogger log,
                                                                string customerId,
                                                                [Inject] IResourceHelper resourceHelper,
                                                                [Inject] IHttpRequestMessageHelper httpRequestMessageHelper,
                                                                [Inject] IValidate validate,
                                                                [Inject] IPostContactDetailsHttpTriggerService contactdetailsPostService,
                                                                [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 Post Contact processed a request. " + touchpointId);

            if (!Guid.TryParse(customerId, out var customerGuid))
            {
                return(HttpResponseMessageHelper.BadRequest(customerGuid));
            }

            Models.ContactDetails contactdetailsRequest;
            try
            {
                contactdetailsRequest = await httpRequestMessageHelper.GetContactDetailsFromRequest <Contact.Models.ContactDetails>(req);
            }
            catch (JsonException ex)
            {
                return(HttpResponseMessageHelper.UnprocessableEntity(ex));
            }

            if (contactdetailsRequest == null)
            {
                return(HttpResponseMessageHelper.UnprocessableEntity(req));
            }

            contactdetailsRequest.SetIds(customerGuid, touchpointId);

            var errors = validate.ValidateResource(contactdetailsRequest, null, 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 doesContactDetailsExist = contactdetailsPostService.DoesContactDetailsExistForCustomer(customerGuid);

            if (doesContactDetailsExist)
            {
                return(HttpResponseMessageHelper.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(HttpResponseMessageHelper.Conflict());
                        }
                    }
                }
            }

            var contactDetails = await contactdetailsPostService.CreateAsync(contactdetailsRequest);

            if (contactDetails != null)
            {
                await contactdetailsPostService.SendToServiceBusQueueAsync(contactDetails, ApimURL);
            }

            return(contactDetails == null
                ? HttpResponseMessageHelper.BadRequest(customerGuid)
                : HttpResponseMessageHelper.Created(JsonHelper.SerializeObject(contactDetails)));
        }