예제 #1
0
        public void Setup()
        {
            _webChat      = Substitute.For <Models.WebChat>();
            _webChatPatch = Substitute.For <WebChatPatch>();

            _request = new HttpRequestMessage()
            {
                Content    = new StringContent(string.Empty),
                RequestUri =
                    new Uri($"http://localhost:7071/api/Customers/7E467BDB-213F-407A-B86A-1954053D3C24/" +
                            $"Customers/7E467BDB-213F-407A-B86A-1954053D3C24/" +
                            $"Interactions/1e1a555c-9633-4e12-ab28-09ed60d51cb3/" +
                            $"WebChats/1e1a555c-9633-4e12-ab28-09ed60d51cb3")
            };

            _log                            = Substitute.For <ILogger>();
            _resourceHelper                 = Substitute.For <IResourceHelper>();
            _validate                       = Substitute.For <IValidate>();
            _httpRequestMessageHelper       = Substitute.For <IHttpRequestMessageHelper>();
            _patchWebChatHttpTriggerService = Substitute.For <IPatchWebChatHttpTriggerService>();
            _httpRequestMessageHelper.GetTouchpointId(_request).Returns("0000000001");
            _httpRequestMessageHelper.GetApimURL(_request).Returns("http://localhost:7071/");
        }
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "patch", Route = "Customers/{customerId}/Interactions/{interactionId}/WebChats/{webChatId}")] HttpRequestMessage req, ILogger log, string customerId, string interactionId, string webChatId,
                                                           [Inject] IResourceHelper resourceHelper,
                                                           [Inject] IHttpRequestMessageHelper httpRequestMessageHelper,
                                                           [Inject] IValidate validate,
                                                           [Inject] IPatchWebChatHttpTriggerService webChatPatchService)
        {
            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("Patch Web Chat 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));
            }

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

            WebChatPatch webChatPatchRequest;

            try
            {
                webChatPatchRequest = await httpRequestMessageHelper.GetWebChatFromRequest <Models.WebChatPatch>(req);
            }
            catch (JsonException ex)
            {
                return(HttpResponseMessageHelper.UnprocessableEntity(ex));
            }

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

            webChatPatchRequest.LastModifiedTouchpointId = touchpointId;
            webChatPatchRequest.SetDefaultValues();

            var errors = validate.ValidateResource(webChatPatchRequest, false);

            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 webChat = await webChatPatchService.GetWebChatForCustomerAsync(customerGuid, interactionGuid, webChatGuid);

            if (webChat == null)
            {
                return(HttpResponseMessageHelper.NoContent(webChatGuid));
            }

            var updatedWebChat = await webChatPatchService.UpdateAsync(webChat, webChatPatchRequest);

            if (updatedWebChat != null)
            {
                await webChatPatchService.SendToServiceBusQueueAsync(updatedWebChat, customerGuid, ApimURL);
            }

            return(updatedWebChat == null?
                   HttpResponseMessageHelper.BadRequest(webChatGuid) :
                       HttpResponseMessageHelper.Ok(JsonHelper.SerializeObject(updatedWebChat)));
        }