public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "Customers/{customerId}")] HttpRequestMessage req, ILogger log, string customerId,
                                                           [Inject] IResourceHelper resourceHelper,
                                                           [Inject] IHttpRequestMessageHelper httpRequestMessageHelper,
                                                           [Inject] IGetCustomerByIdHttpTriggerService getCustomerByIdService)
        {
            var touchpointId = httpRequestMessageHelper.GetTouchpointId(req);

            if (string.IsNullOrEmpty(touchpointId))
            {
                log.LogInformation("Unable to locate 'APIM-TouchpointId' in request header");
                return(HttpResponseMessageHelper.BadRequest());
            }

            log.LogInformation("C# HTTP trigger function GetCustomerById processed a request. By Touchpoint " + touchpointId);

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

            var doesCustomerExist = await resourceHelper.DoesCustomerExist(customerGuid);

            if (!doesCustomerExist)
            {
                return(HttpResponseMessageHelper.NoContent(customerGuid));
            }

            var customer = await getCustomerByIdService.GetCustomerAsync(customerGuid);

            return(customer == null?
                   HttpResponseMessageHelper.NoContent(customerGuid) :
                       HttpResponseMessageHelper.Ok(JsonHelper.SerializeObject(customer)));
        }
Exemplo n.º 2
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "AdviserDetails/{adviserDetailId}")] HttpRequestMessage req, ILogger log, string adviserDetailId,
                                                           [Inject] IHttpRequestMessageHelper httpRequestMessageHelper,
                                                           [Inject] IGetAdviserDetailByIdHttpTriggerService getAdviserDetailByIdService)
        {
            var touchpointId = httpRequestMessageHelper.GetTouchpointId(req);

            if (string.IsNullOrEmpty(touchpointId))
            {
                log.LogInformation("Unable to locate 'APIM-TouchpointId' in request header.");
                return(HttpResponseMessageHelper.BadRequest());
            }

            log.LogInformation("Get Adviser Detail By Id C# HTTP trigger function  processed a request. " + touchpointId);

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

            var adviserDetail = await getAdviserDetailByIdService.GetAdviserDetailByIdAsync(adviserDetailGuid);

            return(adviserDetail == null
                ? HttpResponseMessageHelper.NoContent(adviserDetailGuid)
                : HttpResponseMessageHelper.Ok(JsonHelper.SerializeObject(adviserDetail)));
        }
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "Customers/{customerId}/DiversityDetails")] HttpRequestMessage req, ILogger log, string customerId,
                                                           [Inject] IResourceHelper resourceHelper,
                                                           [Inject] IHttpRequestMessageHelper httpRequestMessageHelper,
                                                           [Inject] IGetDiversityHttpTriggerService getDiversityService)
        {
            var touchpointId = httpRequestMessageHelper.GetTouchpointId(req);

            if (string.IsNullOrEmpty(touchpointId))
            {
                log.LogInformation("Unable to locate 'TouchpointId' in request header.");
                return(HttpResponseMessageHelper.BadRequest());
            }

            log.LogInformation("C# HTTP trigger function processed a request. " + touchpointId);

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

            var doesCustomerExist = await resourceHelper.DoesCustomerExist(customerGuid);

            if (!doesCustomerExist)
            {
                return(HttpResponseMessageHelper.NoContent(customerGuid));
            }

            var diversityId = await getDiversityService.GetDiversityDetailIdAsync(customerGuid);

            return(diversityId == null?
                   HttpResponseMessageHelper.NoContent(customerGuid) :
                       HttpResponseMessageHelper.Ok(diversityId.Value));
        }
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "Customers/{customerId}/Interactions/{interactionId}/actionplans/{actionplanId}/Outcomes")] HttpRequestMessage req, ILogger log, string customerId, string interactionId, string actionplanId,
                                                           [Inject] IResourceHelper resourceHelper,
                                                           [Inject] IHttpRequestMessageHelper httpRequestMessageHelper,
                                                           [Inject] IGetOutcomesHttpTriggerService outcomesGetService)
        {
            var touchpointId = httpRequestMessageHelper.GetTouchpointId(req);

            if (string.IsNullOrEmpty(touchpointId))
            {
                log.LogInformation("Unable to locate 'APIM-TouchpointId' in request header.");
                return(HttpResponseMessageHelper.BadRequest());
            }

            log.LogInformation("Get Outcomes 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));
            }

            //Check customer
            var doesCustomerExist = await resourceHelper.DoesCustomerExist(customerGuid);

            if (!doesCustomerExist)
            {
                return(HttpResponseMessageHelper.NoContent(customerGuid));
            }

            //Check interactions
            var doesInteractionExist = resourceHelper.DoesInteractionResourceExistAndBelongToCustomer(interactionGuid, customerGuid);

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

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

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

            var outcomes = await outcomesGetService.GetOutcomesAsync(customerGuid);

            return(outcomes == null?
                   HttpResponseMessageHelper.NoContent(customerGuid) :
                       HttpResponseMessageHelper.Ok(JsonHelper.SerializeObjects(outcomes)));
        }
Exemplo n.º 5
0
        public async Task PostAdviserDetailHttpTrigger_ReturnsStatusCodeBadRequest_WhenTouchpointIdIsNotProvided()
        {
            _httpRequestMessageHelper.GetTouchpointId(_request).Returns((string)null);

            // Act
            var result = await RunFunction();

            // Assert
            Assert.IsInstanceOf <HttpResponseMessage>(result);
            Assert.AreEqual(HttpStatusCode.BadRequest, result.StatusCode);
        }
Exemplo n.º 6
0
        public async Task GetActionPlanByIdHttpTrigger_ReturnsStatusCodeBadRequest_WhenTouchpointIdIsNotProvided()
        {
            _httpRequestMessageHelper.GetTouchpointId(_request).Returns((string)null);

            // Act
            var result = await RunFunction(InValidId, ValidInteractionId, ValidActionPlanId);

            // Assert
            Assert.IsInstanceOf <HttpResponseMessage>(result);
            Assert.AreEqual(HttpStatusCode.BadRequest, result.StatusCode);
        }
Exemplo n.º 7
0
        public async Task PatchContactHttpTrigger_ReturnsStatusCodeBadRequest_WhenTouchpointIdIsNotProvided()
        {
            // Arrange
            _httpRequestMessageHelper.GetTouchpointId(_request).Returns((string)null);

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

            // Assert
            Assert.IsInstanceOf <HttpResponseMessage>(result);
            Assert.AreEqual(HttpStatusCode.BadRequest, result.StatusCode);
        }
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "Customers/{customerId}/Interactions/{interactionId}/WebChats/{webChatId}")] HttpRequestMessage req, ILogger log, string customerId, string interactionId, string webChatId,
                                                           [Inject] IResourceHelper resourceHelper,
                                                           [Inject] IHttpRequestMessageHelper httpRequestMessageHelper,
                                                           [Inject] IGetWebChatByIdHttpTriggerService webChatGetService)
        {
            var touchpointId = httpRequestMessageHelper.GetTouchpointId(req);

            if (string.IsNullOrEmpty(touchpointId))
            {
                log.LogInformation("Unable to locate 'TouchpointId' in request header.");
                return(HttpResponseMessageHelper.BadRequest());
            }

            log.LogInformation("Get Web Chat By Id 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));
            }

            var doesCustomerExist = await resourceHelper.DoesCustomerExist(customerGuid);

            if (!doesCustomerExist)
            {
                return(HttpResponseMessageHelper.NoContent(customerGuid));
            }

            var doesInteractionExist = resourceHelper.DoesInteractionResourceExistAndBelongToCustomer(interactionGuid, customerGuid);

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

            var webChat = await webChatGetService.GetWebChatForCustomerAsync(customerGuid, interactionGuid, webChatGuid);

            return(webChat == null?
                   HttpResponseMessageHelper.NoContent(webChatGuid) :
                       HttpResponseMessageHelper.Ok(JsonHelper.SerializeObject(webChat)));
        }
Exemplo n.º 9
0
        public void Setup()
        {
            _request = new HttpRequestMessage()
            {
                Content    = new StringContent(string.Empty),
                RequestUri = new Uri($"http://localhost:7071/api/Customers/7E467BDB-213F-407A-B86A-1954053D3C24/DiversityDetails/")
            };

            _log                            = Substitute.For <ILogger>();
            _resourceHelper                 = Substitute.For <IResourceHelper>();
            _httpRequestMessageHelper       = Substitute.For <IHttpRequestMessageHelper>();
            _getDiversityHttpTriggerService = Substitute.For <IGetDiversityHttpTriggerService>();
            _httpRequestMessageHelper.GetTouchpointId(_request).Returns("0000000001");
        }
        public void Setup()
        {
            _adviserDetail = Substitute.For <Models.AdviserDetail>();

            _request = new HttpRequestMessage()
            {
                Content    = new StringContent(string.Empty),
                RequestUri =
                    new Uri($"http://localhost:7071/api/AdviserDetails/1e1a555c-9633-4e12-ab28-09ed60d51cb")
            };

            _log = Substitute.For <ILogger>();
            _httpRequestMessageHelper = Substitute.For <IHttpRequestMessageHelper>();
            _getAdviserDetailByIdHttpTriggerService = Substitute.For <IGetAdviserDetailByIdHttpTriggerService>();
            _httpRequestMessageHelper.GetTouchpointId(_request).Returns("00000000001");
        }
Exemplo n.º 11
0
 public void Setup()
 {
     _request = new HttpRequestMessage()
     {
         Content    = new StringContent(string.Empty),
         RequestUri =
             new Uri($"http://localhost:7071/api/Customers/7E467BDB-213F-407A-B86A-1954053D3C24/" +
                     $"Interactions/aa57e39e-4469-4c79-a9e9-9cb4ef410382/" +
                     $"Outcomes")
     };
     _log                           = Substitute.For <ILogger>();
     _resourceHelper                = Substitute.For <IResourceHelper>();
     _httpRequestMessageHelper      = Substitute.For <IHttpRequestMessageHelper>();
     _getOutcomesHttpTriggerService = Substitute.For <IGetOutcomesHttpTriggerService>();
     _httpRequestMessageHelper.GetTouchpointId(_request).Returns("0000000001");
 }
Exemplo n.º 12
0
        public void Setup()
        {
            _contact = Substitute.For <Models.ContactDetails>();

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

            _log                              = Substitute.For <ILogger>();
            _resourceHelper                   = Substitute.For <IResourceHelper>();
            _httpRequestMessageHelper         = Substitute.For <IHttpRequestMessageHelper>();
            _getContactByIdHttpTriggerService = Substitute.For <IGetContactDetailsByIdHttpTriggerService>();
            _httpRequestMessageHelper.GetTouchpointId(_request).Returns("0000000001");
        }
        public void Setup()
        {
            _action = Substitute.For <Models.Action>();

            _request = new HttpRequestMessage()
            {
                Content    = new StringContent(string.Empty),
                RequestUri =
                    new Uri($"http://localhost:7071/api/Customers/7E467BDB-213F-407A-B86A-1954053D3C24/" +
                            $"Interactions/233d861d-05ca-496e-aee1-6bd47ac13cb2/Actions")
            };
            _log                         = Substitute.For <ILogger>();
            _resourceHelper              = Substitute.For <IResourceHelper>();
            _httpRequestMessageHelper    = Substitute.For <IHttpRequestMessageHelper>();
            _getActionHttpTriggerService = Substitute.For <IGetActionHttpTriggerService>();
            _httpRequestMessageHelper.GetTouchpointId(_request).Returns("0000000001");
        }
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "AdviserDetails")] HttpRequestMessage req, ILogger log,
                                                           [Inject] IHttpRequestMessageHelper httpRequestMessageHelper,
                                                           [Inject] IValidate validate,
                                                           [Inject] IPostAdviserDetailHttpTriggerService adviserDetailsPostService)
        {
            var touchpointId = httpRequestMessageHelper.GetTouchpointId(req);

            if (string.IsNullOrEmpty(touchpointId))
            {
                log.LogInformation("Unable to locate 'APIM-TouchpointId' in request header.");
                return(HttpResponseMessageHelper.BadRequest());
            }

            log.LogInformation("Post Adviser Detail C# HTTP trigger function processed a request. " + touchpointId);

            Models.AdviserDetail adviserDetailRequest;

            try
            {
                adviserDetailRequest = await httpRequestMessageHelper.GetAdviserDetailFromRequest <Models.AdviserDetail>(req);
            }
            catch (JsonException ex)
            {
                return(HttpResponseMessageHelper.UnprocessableEntity(ex));
            }

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

            adviserDetailRequest.LastModifiedTouchpointId = touchpointId;

            var errors = validate.ValidateResource(adviserDetailRequest, true);

            if (errors != null && errors.Any())
            {
                return(HttpResponseMessageHelper.UnprocessableEntity(errors));
            }

            var adviserDetail = await adviserDetailsPostService.CreateAsync(adviserDetailRequest);

            return(adviserDetail == null
                ? HttpResponseMessageHelper.BadRequest()
                : HttpResponseMessageHelper.Created(JsonHelper.SerializeObject(adviserDetail)));
        }
Exemplo n.º 15
0
        public void Setup()
        {
            _address = Substitute.For <Models.AdviserDetail>();

            _request = new HttpRequestMessage()
            {
                Content    = new StringContent(string.Empty),
                RequestUri =
                    new Uri($"http://localhost:7071/api/AdviserDetails/")
            };

            _log = Substitute.For <ILogger>();
            _httpRequestMessageHelper = Substitute.For <IHttpRequestMessageHelper>();
            _validate = Substitute.For <IValidate>();
            _postAdviserDetailHttpTriggerService = Substitute.For <IPostAdviserDetailHttpTriggerService>();
            _httpRequestMessageHelper.GetTouchpointId(_request).Returns("00000000001");
        }
        public void Setup()
        {
            _request = new HttpRequestMessage()
            {
                Content    = new StringContent(string.Empty),
                RequestUri =
                    new Uri($"http://localhost:7071/api/Customers/7E467BDB-213F-407A-B86A-1954053D3C24/WebChats" +
                            $"Customers/7E467BDB-213F-407A-B86A-1954053D3C24/" +
                            $"Interactions/1e1a555c-9633-4e12-ab28-09ed60d51cb3/" +
                            $"WebChats")
            };

            _log            = Substitute.For <ILogger>();
            _resourceHelper = Substitute.For <IResourceHelper>();
            _getWebChatHttpTriggerService = Substitute.For <IGetWebChatHttpTriggerService>();
            _httpRequestMessageHelper     = Substitute.For <IHttpRequestMessageHelper>();
            _httpRequestMessageHelper.GetTouchpointId(_request).Returns("0000000001");
        }
Exemplo n.º 17
0
        public void Setup()
        {
            _goal = Substitute.For <Models.Goal>();

            _request = new HttpRequestMessage()
            {
                Content    = new StringContent(string.Empty),
                RequestUri =
                    new Uri($"http://localhost:7071/api/Customers/7E467BDB-213F-407A-B86A-1954053D3C24/" +
                            $"Interactions/aa57e39e-4469-4c79-a9e9-9cb4ef410382/" +
                            $"ActionPlans/d5369b9a-6959-4bd3-92fc-1583e72b7e51/" +
                            $"Goals/d5369b9a-6959-4bd3-92fc-1583e72b7e51")
            };
            _log                           = Substitute.For <ILogger>();
            _resourceHelper                = Substitute.For <IResourceHelper>();
            _httpRequestMessageHelper      = Substitute.For <IHttpRequestMessageHelper>();
            _getGoalByIdHttpTriggerService = Substitute.For <IGetGoalByIdHttpTriggerService>();
            _httpRequestMessageHelper.GetTouchpointId(_request).Returns("0000000001");
        }
        public void Setup()
        {
            _address      = Substitute.For <Models.AdviserDetail>();
            _addressPatch = Substitute.For <Models.AdviserDetailPatch>();

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

            _log      = Substitute.For <ILogger>();
            _validate = Substitute.For <IValidate>();
            _httpRequestMessageHelper             = Substitute.For <IHttpRequestMessageHelper>();
            _patchAdviserDetailHttpTriggerService = Substitute.For <IPatchAdviserDetailHttpTriggerService>();
            _httpRequestMessageHelper.GetTouchpointId(_request).Returns("00000000001");
        }
        public void Setup()
        {
            _address = Substitute.For <Models.Address>();

            _request = new HttpRequestMessage()
            {
                Content    = new StringContent(string.Empty),
                RequestUri =
                    new Uri($"http://localhost:7071/api/Customers/7E467BDB-213F-407A-B86A-1954053D3C24/Addressess/")
            };

            _log                           = Substitute.For <ILogger>();
            _resourceHelper                = Substitute.For <IResourceHelper>();
            _httpRequestMessageHelper      = Substitute.For <IHttpRequestMessageHelper>();
            _validate                      = Substitute.For <IValidate>();
            _postAddressHttpTriggerService = Substitute.For <IPostAddressHttpTriggerService>();
            _httpRequestMessageHelper.GetTouchpointId(_request).Returns("0000000001");
            _httpRequestMessageHelper.GetApimURL(_request).Returns("http://localhost:7071/");
        }
Exemplo n.º 20
0
        public void Setup()
        {
            _session      = Substitute.For <Session>();
            _sessionPatch = Substitute.For <SessionPatch>();

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

            _log                            = Substitute.For <ILogger>();
            _resourceHelper                 = Substitute.For <IResourceHelper>();
            _validate                       = Substitute.For <IValidate>();
            _httpRequestMessageHelper       = Substitute.For <IHttpRequestMessageHelper>();
            _patchSessionHttpTriggerService = Substitute.For <IPatchSessionHttpTriggerService>();
            _httpRequestMessageHelper.GetTouchpointId(_request).Returns("0000000001");
            _httpRequestMessageHelper.GetApimURL(_request).Returns("http://localhost:7071/");
        }
        public void Setup()
        {
            _action      = Substitute.For <Models.Action>();
            _actionPatch = Substitute.For <ActionPatch>();

            _request = new HttpRequestMessage()
            {
                Content    = new StringContent(string.Empty),
                RequestUri =
                    new Uri($"http://localhost:7071/api/Customers/7E467BDB-213F-407A-B86A-1954053D3C24/" +
                            $"Interactions/1e1a555c-9633-4e12-ab28-09ed60d51cb3" +
                            $"ActionPlans/d5369b9a-6959-4bd3-92fc-1583e72b7e51" +
                            $"Actions/233d861d-05ca-496e-aee1-6bd47ac13cb2")
            };

            _log                           = Substitute.For <ILogger>();
            _resourceHelper                = Substitute.For <IResourceHelper>();
            _validate                      = Substitute.For <IValidate>();
            _httpRequestMessageHelper      = Substitute.For <IHttpRequestMessageHelper>();
            _patchActionHttpTriggerService = Substitute.For <IPatchActionHttpTriggerService>();
            _httpRequestMessageHelper.GetTouchpointId(_request).Returns("0000000001");
            _httpRequestMessageHelper.GetApimURL(_request).Returns("http://localhost:7071/");
            _patchActionHttpTriggerService.PatchResource(Arg.Any <string>(), _actionPatch).Returns(_action.ToString());
        }
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "patch", Route = "Customers/{customerId}/Interactions/{interactionId}/actionplans/{actionplanId}/Outcomes/{OutcomeId}")] HttpRequestMessage req, ILogger log, string customerId, string interactionId, string actionplanId, string OutcomeId,
                                                           [Inject] IResourceHelper resourceHelper,
                                                           [Inject] IHttpRequestMessageHelper httpRequestMessageHelper,
                                                           [Inject] IValidate validate,
                                                           [Inject] IPatchOutcomesHttpTriggerService outcomesPatchService)
        {
            var touchpointId = httpRequestMessageHelper.GetTouchpointId(req);

            if (string.IsNullOrEmpty(touchpointId))
            {
                log.LogInformation("Unable to locate 'APIM-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 Action Plan 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(OutcomeId, out var outcomesGuid))
            {
                return(HttpResponseMessageHelper.BadRequest(outcomesGuid));
            }

            Models.OutcomesPatch outcomesPatchRequest;

            try
            {
                outcomesPatchRequest = await httpRequestMessageHelper.GetOutcomesFromRequest <Models.OutcomesPatch>(req);
            }
            catch (JsonException ex)
            {
                return(HttpResponseMessageHelper.UnprocessableEntity(ex));
            }

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

            outcomesPatchRequest.LastModifiedTouchpointId = touchpointId;

            var errors = validate.ValidateResource(outcomesPatchRequest);

            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 doesActionPlanExist = resourceHelper.DoesActionPlanResourceExistAndBelongToCustomer(actionplanGuid, interactionGuid, customerGuid);

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

            var outcomeForCustomer = await outcomesPatchService.GetOutcomesForCustomerAsync(customerGuid, interactionGuid, actionplanGuid, outcomesGuid);

            if (outcomeForCustomer == null)
            {
                return(HttpResponseMessageHelper.NoContent(outcomesGuid));
            }

            var patchedOutcome = outcomesPatchService.PatchResource(outcomeForCustomer, outcomesPatchRequest);

            if (patchedOutcome == null)
            {
                return(HttpResponseMessageHelper.NoContent(outcomesGuid));
            }

            var updatedOutcomes = await outcomesPatchService.UpdateCosmosAsync(patchedOutcome, outcomesGuid);

            if (updatedOutcomes != null)
            {
                await outcomesPatchService.SendToServiceBusQueueAsync(updatedOutcomes, customerGuid, ApimURL);
            }

            return(updatedOutcomes == null?
                   HttpResponseMessageHelper.BadRequest(outcomesGuid) :
                       HttpResponseMessageHelper.Ok(JsonHelper.SerializeObject(updatedOutcomes)));
        }
Exemplo n.º 23
0
        public static async Task <HttpResponseMessage> RunAsync([HttpTrigger(AuthorizationLevel.Anonymous, "patch", Route = "customers/{customerId}/ContactDetails/{contactid}")] HttpRequestMessage req, ILogger log,
                                                                string customerId, string contactid,
                                                                [Inject] IResourceHelper resourceHelper,
                                                                [Inject] IHttpRequestMessageHelper httpRequestMessageHelper,
                                                                [Inject] IValidate validate,
                                                                [Inject] IPatchContactDetailsHttpTriggerService contactdetailsPatchService,
                                                                [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 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.GetContactDetailsFromRequest <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)));
        }
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "Customers/{customerId}/DiversityDetails")] HttpRequestMessage req, ILogger log, string customerId,
                                                           [Inject] IResourceHelper resourceHelper,
                                                           [Inject] IHttpRequestMessageHelper httpRequestMessageHelper,
                                                           [Inject] IValidate validate,
                                                           [Inject] IPostDiversityHttpTriggerService postDiversityService)
        {
            var touchpointId = httpRequestMessageHelper.GetTouchpointId(req);

            if (string.IsNullOrEmpty(touchpointId))
            {
                log.LogInformation("Unable to locate 'TouchpointId' in request header.");
                return(HttpResponseMessageHelper.BadRequest());
            }

            log.LogInformation("C# HTTP trigger function processed a request. " + touchpointId);

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

            Models.Diversity diversityRequest;

            try
            {
                diversityRequest = await httpRequestMessageHelper.GetDiversityFromRequest <Models.Diversity>(req);
            }
            catch (JsonException ex)
            {
                return(HttpResponseMessageHelper.UnprocessableEntity(ex));
            }

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

            diversityRequest.SetIds(customerGuid, touchpointId);

            var errors = validate.ValidateResource(diversityRequest);

            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 doesDiversityDetailsExist = postDiversityService.DoesDiversityDetailsExistForCustomer(customerGuid);

            if (doesDiversityDetailsExist)
            {
                return(HttpResponseMessageHelper.Conflict());
            }

            var diversity = await postDiversityService.CreateAsync(diversityRequest);

            return(diversity == null
                ? HttpResponseMessageHelper.BadRequest(customerGuid)
                : HttpResponseMessageHelper.Created(JsonHelper.SerializeObject(diversity)));
        }
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get",
                                                                        Route = "CustomerSearch")] HttpRequestMessage req, ILogger log,
                                                           [Inject] IResourceHelper resourceHelper,
                                                           [Inject] IHttpRequestMessageHelper httpRequestMessageHelper,
                                                           [Inject] ISearchCustomerHttpTriggerService searchCustomerService)
        {
            var touchpointId = httpRequestMessageHelper.GetTouchpointId(req);

            if (string.IsNullOrEmpty(touchpointId))
            {
                log.LogInformation("Unable to locate 'APIM-TouchpointId' in request header");
                return(HttpResponseMessageHelper.BadRequest());
            }

            log.LogInformation("C# HTTP trigger function GetCustomerById processed a request. By Touchpoint " + touchpointId);

            SearchHelper.GetSearchServiceClient();
            var indexClient = SearchHelper.GetIndexClient();

            var query  = string.Empty;
            var filter = string.Empty;

            // Parse query parameter
            var givenName  = httpRequestMessageHelper.GetQueryNameValuePairs(req, "GivenName");
            var familyName = httpRequestMessageHelper.GetQueryNameValuePairs(req, "FamilyName");

            if (givenName != null && givenName.Length < 3)
            {
                log.LogWarning("Given Name must have a minimum of 3 characters");
                return(HttpResponseMessageHelper.NoContent());
            }

            if (!string.IsNullOrEmpty(givenName))
            {
                query += string.Format("GivenName:({0}* OR {0}) ", givenName.Trim());
            }

            if (familyName != null && familyName.Length < 3)
            {
                log.LogWarning("Family Name must have a minimum of 3 characters");
                return(HttpResponseMessageHelper.NoContent());
            }

            if (!string.IsNullOrEmpty(familyName))
            {
                query += string.Format("FamilyName:({0}* OR {0}) ", familyName.Trim());
            }

            var uniqueLearnerNumber = httpRequestMessageHelper.GetQueryNameValuePairs(req, "UniqueLearnerNumber");

            if (!string.IsNullOrEmpty(uniqueLearnerNumber))
            {
                query += string.Format("UniqueLearnerNumber:{0}", uniqueLearnerNumber.Trim());
            }

            var dob = httpRequestMessageHelper.GetQueryNameValuePairs(req, "DateofBirth");

            if (!string.IsNullOrEmpty(dob))
            {
                if (DateTime.TryParse(dob.Trim(), CultureInfo.CurrentCulture, DateTimeStyles.None, out var dateOfBirth))
                {
                    filter = string.Format("DateofBirth eq {0:yyyy-MM-dd}", dateOfBirth);
                }
                else
                {
                    return(HttpResponseMessageHelper.NoContent());
                }
            }

            if (string.IsNullOrWhiteSpace(query) && string.IsNullOrWhiteSpace(filter))
            {
                return(HttpResponseMessageHelper.NoContent());
            }

            log.LogInformation("Attempting to search customers");

            var customer = await searchCustomerService.SearchCustomerAsync(indexClient, query, filter);

            log.LogInformation("Search completed");

            return(customer == null?
                   HttpResponseMessageHelper.NoContent() :
                       HttpResponseMessageHelper.Ok(JsonHelper.SerializeObjects(customer)));
        }
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "patch", Route = "Customers/{customerId}/Interactions/{interactionId}/Transfers/{transferId}")] HttpRequestMessage req, ILogger log, string customerId, string interactionId, string transferId,
                                                           [Inject] IResourceHelper resourceHelper,
                                                           [Inject] IHttpRequestMessageHelper httpRequestMessageHelper,
                                                           [Inject] IValidate validate,
                                                           [Inject] IPatchTransferHttpTriggerService transferPatchService)
        {
            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 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));
            }

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

            Models.TransferPatch transferPatchRequest;

            try
            {
                transferPatchRequest = await httpRequestMessageHelper.GetTransferFromRequest <Models.TransferPatch>(req);
            }
            catch (JsonException ex)
            {
                return(HttpResponseMessageHelper.UnprocessableEntity(ex));
            }

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

            transferPatchRequest.LastModifiedTouchpointId = touchpointId;

            var errors = validate.ValidateResource(transferPatchRequest, 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 transfer = await transferPatchService.GetTransferForCustomerAsync(customerGuid, transferGuid);

            if (transfer == null)
            {
                return(HttpResponseMessageHelper.NoContent(transferGuid));
            }

            var updatedTransfer = await transferPatchService.UpdateAsync(transfer, transferPatchRequest);

            if (updatedTransfer != null)
            {
                await transferPatchService.SendToServiceBusQueueAsync(transfer, customerGuid, ApimURL);
            }

            return(updatedTransfer == null?
                   HttpResponseMessageHelper.BadRequest(transferGuid) :
                       HttpResponseMessageHelper.Ok(JsonHelper.SerializeObject(updatedTransfer)));
        }
Exemplo n.º 27
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "patch", Route = "Customers/{customerId}/Addresses/{addressId}")] HttpRequestMessage req, ILogger log, string customerId, string addressId,
                                                           [Inject] IResourceHelper resourceHelper,
                                                           [Inject] IHttpRequestMessageHelper httpRequestMessageHelper,
                                                           [Inject] IValidate validate,
                                                           [Inject] IPatchAddressHttpTriggerService addressPatchService)
        {
            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 Address C# HTTP trigger function  processed a request. By Touchpoint " + touchpointId);

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

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

            AddressPatch addressPatchRequest;

            try
            {
                addressPatchRequest = await httpRequestMessageHelper.GetAddressFromRequest <AddressPatch>(req);
            }
            catch (JsonException ex)
            {
                return(HttpResponseMessageHelper.UnprocessableEntity(ex));
            }

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

            addressPatchRequest.LastModifiedTouchpointId = touchpointId;

            var errors = validate.ValidateResource(addressPatchRequest, 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 addressForCustomer = await addressPatchService.GetAddressForCustomerAsync(customerGuid, addressGuid);

            if (addressForCustomer == null)
            {
                return(HttpResponseMessageHelper.NoContent(addressGuid));
            }

            var patchedAddress = addressPatchService.PatchResource(addressForCustomer, addressPatchRequest);

            if (patchedAddress == null)
            {
                return(HttpResponseMessageHelper.NoContent(addressGuid));
            }

            var updatedAddress = await addressPatchService.UpdateCosmosAsync(patchedAddress, addressGuid);

            if (updatedAddress != null)
            {
                await addressPatchService.SendToServiceBusQueueAsync(updatedAddress, customerGuid, ApimURL);
            }

            return(updatedAddress == null?
                   HttpResponseMessageHelper.BadRequest(addressGuid) :
                       HttpResponseMessageHelper.Ok(JsonHelper.SerializeObject(updatedAddress)));
        }
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "patch", Route = "Customers/{customerId}/DiversityDetails/{diversityId}")] HttpRequestMessage req, ILogger log, string customerId, string diversityId,
                                                           [Inject] IResourceHelper resourceHelper,
                                                           [Inject] IHttpRequestMessageHelper httpRequestMessageHelper,
                                                           [Inject] IValidate validate,
                                                           [Inject] IPatchDiversityHttpTriggerService patchDiversityService)
        {
            var touchpointId = httpRequestMessageHelper.GetTouchpointId(req);

            if (string.IsNullOrEmpty(touchpointId))
            {
                log.LogInformation("Unable to locate 'TouchpointId' in request header");
                return(HttpResponseMessageHelper.BadRequest());
            }

            log.LogInformation("C# HTTP trigger function Patch Customer processed a request. By Touchpoint " + touchpointId);

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

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

            Models.DiversityPatch diversityPatchRequest;

            try
            {
                diversityPatchRequest = await httpRequestMessageHelper.GetDiversityFromRequest <Models.DiversityPatch>(req);
            }
            catch (JsonException ex)
            {
                return(HttpResponseMessageHelper.UnprocessableEntity(ex));
            }

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

            diversityPatchRequest.LastModifiedBy = touchpointId;

            // validate the request
            var errors = validate.ValidateResource(diversityPatchRequest);

            if (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 diversity = await patchDiversityService.GetDiversityByIdAsync(customerGuid, diversityGuid);

            if (diversity == null)
            {
                return(HttpResponseMessageHelper.NoContent(customerGuid));
            }

            var updatedDiversity = await patchDiversityService.UpdateDiversityAsync(diversity, diversityPatchRequest);

            return(updatedDiversity == null?
                   HttpResponseMessageHelper.BadRequest(customerGuid) :
                       HttpResponseMessageHelper.Ok());
        }
        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)));
        }
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "customers/{customerId}/interactions/{interactionId}/sessions/")] HttpRequestMessage req, ILogger log, string customerId, string interactionId,
                                                           [Inject] IResourceHelper resourceHelper,
                                                           [Inject] IHttpRequestMessageHelper httpRequestMessageHelper,
                                                           [Inject] IValidate validate,
                                                           [Inject] IPostSessionHttpTriggerService sessionPostService)
        {
            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 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));
            }

            Session sessionRequest;

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

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

            sessionRequest.SetIds(customerGuid, interactionGuid, touchpointId);

            var errors = validate.ValidateResource(sessionRequest);

            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 session = await sessionPostService.CreateAsync(sessionRequest);

            if (session != null)
            {
                await sessionPostService.SendToServiceBusQueueAsync(session, ApimURL);
            }

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