Example #1
0
        public static async System.Threading.Tasks.Task <HttpResponseMessage> RunAsync([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestMessage req, [DurableClient] IDurableClient starter, ILogger log)
        {
            var     inputToFunction   = JToken.ReadFrom(new JsonTextReader(new StreamReader(await req.Content.ReadAsStreamAsync())));
            dynamic eventGridSoleItem = (inputToFunction as JArray)?.SingleOrDefault();

            if (eventGridSoleItem == null)
            {
                return(req.CreateResponse(HttpStatusCode.BadRequest, @"Expecting only one item in the Event Grid message"));
            }

            if (eventGridSoleItem.eventType == @"Microsoft.EventGrid.SubscriptionValidationEvent")
            {
                log.LogTrace(@"Event Grid Validation event received.");
                return(req.CreateCompatibleResponse(HttpStatusCode.OK, $"{{ \"validationResponse\" : \"{((dynamic)inputToFunction)[0].data.validationCode}\" }}"));
            }

            CustomerBlobAttributes newCustomerFile = Helpers.ParseEventGridPayload(eventGridSoleItem, log);

            if (newCustomerFile == null)
            {
                return(req.CreateResponse(HttpStatusCode.NoContent));
            }

            string orderId = newCustomerFile.OrderId, transactionType = newCustomerFile.TransactionType, containerName = newCustomerFile.ContainerName;

            log.LogInformation($@"Processing new file. orderid: {orderId}, transactionType: {transactionType}");

            // get the prefix for the name so we can check for the matching payment in the same container with in the blob storage account
            var prefix = newCustomerFile.OrderId;
            await starter.SignalEntityAsync <IBatchEntity>(prefix, b => b.NewFile(newCustomerFile.FullUrl));

            return(req.CreateResponse(HttpStatusCode.Accepted));
        }
Example #2
0
        public async Task NewFile(string fileUri)
        {
            var newCustomerFile = CustomerBlobAttributes.Parse(fileUri);

            _logger.LogInformation($@"Got new file via event: {newCustomerFile.TransactionType}");

            if (newCustomerFile.TransactionType == "order")
            {
                this.IsPaymentReceived = true;
            }
            else if (newCustomerFile.TransactionType == "payment")
            {
                this.IsOrderReceived = true;
            }

            _logger.LogTrace($@"Actor '{_id}' got file '{newCustomerFile.TransactionType}'");

            if (this.IsOrderReceived && this.IsPaymentReceived)
            {
                _logger.LogInformation(@"Payment received...");

                // call next step in functions with the prefix so it knows what to go grab
                await Helpers.CreateShippingLabel(newCustomerFile.OrderId, _logger);
            }
            else
            {
                _logger.LogInformation($@"Waiting for Payment.....");
            }
        }
Example #3
0
        public static CustomerBlobAttributes ParseEventGridPayload(dynamic eventGridItem, ILogger log)
        {
            if (eventGridItem.eventType == @"Microsoft.Storage.BlobCreated" &&
                eventGridItem.data.api == @"PutBlob" &&
                eventGridItem.data.contentType == @"application/vnd.ms-excel")
            {
                try
                {
                    var retVal = CustomerBlobAttributes.Parse((string)eventGridItem.data.url);
                    return(retVal);
                }
                catch (Exception ex)
                {
                    log.LogError(@"Error parsing Event Grid payload", ex);
                }
            }

            return(null);
        }