예제 #1
0
        public async Task PatchSessionHttpTrigger_ReturnsStatusCodeNoContent_WhenSessionDoesNotExist()
        {
            _httpRequestMessageHelper.GetSessionFromRequest <SessionPatch>(_request).Returns(Task.FromResult(_sessionPatch).Result);

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

            _patchSessionHttpTriggerService.GetSessionForCustomerAsync(Arg.Any <Guid>(), Arg.Any <Guid>()).Returns(Task.FromResult <string>(null).Result);

            // Act
            var result = await RunFunction(ValidCustomerId, ValidInteractionId, ValidSessionId);

            // Assert
            Assert.IsInstanceOf <HttpResponseMessage>(result);
            Assert.AreEqual(HttpStatusCode.NoContent, result.StatusCode);
        }
예제 #2
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "patch", Route = "customers/{customerId}/interactions/{interactionId}/sessions/{sessionId}")] HttpRequestMessage req, ILogger log, string customerId, string interactionId, string sessionId,
                                                           [Inject] IResourceHelper resourceHelper,
                                                           [Inject] IHttpRequestMessageHelper httpRequestMessageHelper,
                                                           [Inject] IValidate validate,
                                                           [Inject] IPatchSessionHttpTriggerService sessionPatchService)
        {
            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 Session 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));
            }

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

            SessionPatch sessionPatchRequest;

            try
            {
                sessionPatchRequest = await httpRequestMessageHelper.GetSessionFromRequest <SessionPatch>(req);
            }
            catch (JsonException ex)
            {
                return(HttpResponseMessageHelper.UnprocessableEntity(ex));
            }

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

            sessionPatchRequest.LastModifiedTouchpointId = touchpointId;

            var errors = validate.ValidateResource(sessionPatchRequest);

            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 sessionForCustomer = await sessionPatchService.GetSessionForCustomerAsync(customerGuid, sessionGuid);

            if (sessionForCustomer == null)
            {
                return(HttpResponseMessageHelper.NoContent(sessionGuid));
            }

            var patchedSession = sessionPatchService.PatchResource(sessionForCustomer, sessionPatchRequest);

            if (patchedSession == null)
            {
                return(HttpResponseMessageHelper.NoContent(sessionGuid));
            }

            var updatedSession = await sessionPatchService.UpdateCosmosAsync(patchedSession, sessionGuid);

            if (updatedSession != null)
            {
                await sessionPatchService.SendToServiceBusQueueAsync(updatedSession, customerGuid, ApimURL);
            }

            return(updatedSession == null?
                   HttpResponseMessageHelper.BadRequest(sessionGuid) :
                       HttpResponseMessageHelper.Ok(JsonHelper.SerializeObject(updatedSession)));
        }