Ejemplo n.º 1
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "patch", Route = "Customers/{customerId}/Interactions/{interactionId}/ActionPlans/{actionPlanId}/Goals/{goalId}")] HttpRequestMessage req, ILogger log, string customerId, string interactionId, string actionPlanId, string goalId,
                                                           [Inject] IResourceHelper resourceHelper,
                                                           [Inject] IHttpRequestMessageHelper httpRequestMessageHelper,
                                                           [Inject] IValidate validate,
                                                           [Inject] IPatchGoalHttpTriggerService goalPatchService)
        {
            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 Goal 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));
            }

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

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

            Models.GoalPatch goalPatchRequest;

            try
            {
                goalPatchRequest = await httpRequestMessageHelper.GetGoalFromRequest <Models.GoalPatch>(req);
            }
            catch (JsonException ex)
            {
                return(HttpResponseMessageHelper.UnprocessableEntity(ex));
            }

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

            goalPatchRequest.LastModifiedBy = touchpointId;

            var errors = validate.ValidateResource(goalPatchRequest, 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.DoesInteractionExistAndBelongToCustomer(interactionGuid, customerGuid);

            if (!doesInteractionExist)
            {
                return(HttpResponseMessageHelper.NoContent(interactionGuid));
            }

            var doesActionPlanExist = resourceHelper.DoesActionPlanExistAndBelongToCustomer(actionPlanGuid, interactionGuid, customerGuid);

            if (!doesActionPlanExist)
            {
                return(HttpResponseMessageHelper.NoContent(actionPlanGuid));
            }

            var goalForCustomer = await goalPatchService.GetGoalForCustomerAsync(customerGuid, goalGuid);

            if (goalForCustomer == null)
            {
                return(HttpResponseMessageHelper.NoContent(goalGuid));
            }

            var patchedGoal = goalPatchService.PatchResource(goalForCustomer, goalPatchRequest);

            if (patchedGoal == null)
            {
                return(HttpResponseMessageHelper.NoContent(goalGuid));
            }

            var updatedGoal = await goalPatchService.UpdateCosmosAsync(patchedGoal, goalGuid);

            if (updatedGoal != null)
            {
                await goalPatchService.SendToServiceBusQueueAsync(updatedGoal, customerGuid, ApimURL);
            }

            return(updatedGoal == null?
                   HttpResponseMessageHelper.BadRequest(actionPlanGuid) :
                       HttpResponseMessageHelper.Ok(JsonHelper.SerializeObject(updatedGoal)));
        }