public async Task Check(SimCardOrder sentOrder)
        {
            var request = new HttpRequestMessage(HttpMethod.Get, $"{externalApiUrl}/api/orders/{sentOrder.MobileOrderId}");
            var client  = clientFactory.CreateClient();

            var response = await client.SendAsync(request);

            if (response.IsSuccessStatusCode)
            {
                using var responseStream = await response.Content.ReadAsStreamAsync();

                var simCardOrderFromExternalProvider = await JsonSerializer.DeserializeAsync <SimCardOrderFromExternalProvider>(responseStream, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });

                if (simCardOrderFromExternalProvider.Status.Trim() == "Completed")
                {
                    using var tx = simCardOrdersDataStore.BeginTransaction();
                    simCardOrdersDataStore.Complete(sentOrder.MobileOrderId);
                    PublishProvisioningOrderCompleted(sentOrder.MobileOrderId);
                    monitoring.SimCardOrderCompleted();
                    simCardOrdersDataStore.IncrementAttempts(sentOrder);
                }
                else
                {
                    using var tx = simCardOrdersDataStore.BeginTransaction();
                    simCardOrdersDataStore.IncrementAttempts(sentOrder);
                }
            }
            else
            {
                using var tx = simCardOrdersDataStore.BeginTransaction();
                simCardOrdersDataStore.IncrementAttempts(sentOrder);
            }
        }
Exemple #2
0
        public async Task <bool> Handle(ProvisionRequestedMessage receivedEvent)
        {
            var eventName = receivedEvent.GetType().Name;

            logger.LogInformation("Received event [{eventName}] with Name={Name}, ContactPhoneNumber={ContactPhoneNumber}", eventName, receivedEvent.Name, receivedEvent.ContactPhoneNumber);

            try
            {
                var existingOrder = simCardOrdersDataStore.GetExisting(receivedEvent.MobileId, receivedEvent.MobileOrderId);

                if (existingOrder != null)
                {
                    return(true);
                }

                using (var tx = simCardOrdersDataStore.BeginTransaction())
                {
                    simCardOrdersDataStore.Add(new SimCardOrder()
                    {
                        PhoneNumber   = receivedEvent.PhoneNumber,
                        Name          = receivedEvent.Name,
                        MobileId      = receivedEvent.MobileId,
                        MobileOrderId = receivedEvent.MobileOrderId,
                        Status        = "New"
                    });
                }

                using (var tx = simCardOrdersDataStore.BeginTransaction())
                {
                    var result = await externalSimCardsProvider.PostOrder(new ExternalSimCardOrder
                    {
                        PhoneNumber = receivedEvent.PhoneNumber,
                        Reference   = receivedEvent.MobileOrderId,
                        Name        = receivedEvent.Name
                    });

                    if (!result)
                    {
                        tx.Rollback();
                        monitoring.SimCardOrderFailed();
                        return(false);
                    }

                    simCardOrdersDataStore.Sent(receivedEvent.MobileOrderId);

                    Publish(receivedEvent.MobileOrderId);
                    monitoring.SimCardOrderSent();
                }

                return(true);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "Error while processing event {eventName} - MobileOrderId={mobileOrderId}", eventName, receivedEvent.MobileOrderId);
                return(false);
            }
        }