Exemplo n.º 1
0
        public ShopifyCustomer UpsertCustomer(Customer customer)
        {
            using (var transaction = _orderRepository.BeginTransaction())
            {
                var existingCustomer = _orderRepository.RetrieveCustomer(customer.id);

                if (existingCustomer == null)
                {
                    var newCustomer = new ShopifyCustomer();
                    newCustomer.ShopifyCustomerId   = customer.id;
                    newCustomer.ShopifyPrimaryEmail = customer.email;
                    newCustomer.DateCreated         = DateTime.UtcNow;
                    newCustomer.LastUpdated         = DateTime.UtcNow;
                    newCustomer.NeedsCustomerPut    = true;

                    _orderRepository.InsertCustomer(newCustomer);
                    _shopifyJsonService.Upsert(ShopifyJsonType.Customer, customer.id, customer.SerializeToJson());
                    transaction.Commit();
                    return(newCustomer);
                }
                else
                {
                    existingCustomer.ShopifyPrimaryEmail = customer.email;
                    existingCustomer.NeedsCustomerPut    = true;
                    existingCustomer.LastUpdated         = DateTime.UtcNow;

                    _orderRepository.SaveChanges();
                    _shopifyJsonService.Upsert(ShopifyJsonType.Customer, customer.id, customer.SerializeToJson());
                    transaction.Commit();
                    return(existingCustomer);
                }
            }
        }
Exemplo n.º 2
0
        public long UpsertProductAndInventory(Product product)
        {
            long productMonsterId;

            using (var transaction = _inventoryRepository.BeginTransaction())
            {
                var existing = _inventoryRepository.RetrieveProduct(product.id);
                if (existing == null)
                {
                    var data = new ShopifyProduct();
                    data.ShopifyProductId   = product.id;
                    data.ShopifyTitle       = product.title ?? "";
                    data.ShopifyProductType = product.product_type;
                    data.ShopifyVendor      = product.vendor;
                    data.DateCreated        = DateTime.UtcNow;
                    data.LastUpdated        = DateTime.UtcNow;

                    _executionLogService.Log(LogBuilder.DetectedNewProduct(data));
                    _inventoryRepository.InsertProduct(data);
                    _inventoryRepository.SaveChanges();

                    productMonsterId = data.MonsterId;
                }
                else
                {
                    existing.ShopifyTitle       = product.title ?? "";
                    existing.ShopifyProductType = product.product_type;
                    existing.ShopifyVendor      = product.vendor;
                    existing.LastUpdated        = DateTime.UtcNow;
                    _inventoryRepository.SaveChanges();

                    productMonsterId = existing.MonsterId;
                }

                _shopifyJsonService.Upsert(ShopifyJsonType.Product, product.id, product.SerializeToJson());
                transaction.Commit();
            }


            // Write the Product Variants
            UpsertProduct(productMonsterId, product);

            // Flags the missing Variants
            ProcessMissingVariants(productMonsterId, product);

            // Pull and write the Inventory
            PullAndUpsertInventory(productMonsterId);

            return(productMonsterId);
        }
Exemplo n.º 3
0
        private void PullTransactionsFromShopify(ShopifyOrder orderRecord)
        {
            var transactionsJson = _orderApi.RetrieveTransactions(orderRecord.ShopifyOrderId);
            var transactions     = transactionsJson.DeserializeFromJson <TransactionList>();
            var order            = _shopifyJsonService.RetrieveOrder(orderRecord.ShopifyOrderId);

            foreach (var transaction in transactions.transactions)
            {
                var transactionRecord = _orderRepository.RetrieveTransaction(transaction.id);
                if (transactionRecord != null)
                {
                    transactionRecord.LastUpdated = DateTime.UtcNow;
                    _orderRepository.SaveChanges();
                    continue;
                }

                using (var dbTransaction = _orderRepository.BeginTransaction())
                {
                    var record = new ShopifyTransaction();

                    if (transaction.kind == TransactionKind.Refund)
                    {
                        record.IsSyncableToPayment = true;

                        var refund = order.RefundByTransaction(transaction.id);
                        if (refund != null)
                        {
                            record.ShopifyRefundId = refund.id;
                            record.IsPureCancel    = refund.IsPureCancel;
                        }
                    }

                    if (transaction.kind == TransactionKind.Capture || transaction.kind == TransactionKind.Sale)
                    {
                        record.IsSyncableToPayment = true;
                    }

                    record.ShopifyOrderId        = transaction.order_id;
                    record.ShopifyTransactionId  = transaction.id;
                    record.ShopifyStatus         = transaction.status;
                    record.ShopifyKind           = transaction.kind;
                    record.ShopifyGateway        = transaction.gateway;
                    record.ShopifyAmount         = transaction.amount;
                    record.ShopifyOrderMonsterId = orderRecord.MonsterId;
                    record.DateCreated           = DateTime.UtcNow;
                    record.LastUpdated           = DateTime.UtcNow;

                    _logService.Log(LogBuilder.DetectedNewShopifyTransaction(record));
                    _orderRepository.InsertTransaction(record);

                    _shopifyJsonService.Upsert(
                        ShopifyJsonType.Transaction, transaction.id, transaction.SerializeToJson());
                    dbTransaction.Commit();
                }
            }

            orderRecord.NeedsTransactionGet = false;
            _orderRepository.SaveChanges();
        }
Exemplo n.º 4
0
        private void UpsertOrderAndCustomer(Order order)
        {
            var monsterCustomerRecord = _shopifyCustomerPull.UpsertCustomer(order.customer);

            using (var transaction = _orderRepository.BeginTransaction())
            {
                var existingOrder = _orderRepository.RetrieveOrder(order.id);
                if (existingOrder == null)
                {
                    var newOrder = new ShopifyOrder();

                    newOrder.ShopifyOrderId             = order.id;
                    newOrder.ShopifyOrderNumber         = order.name;
                    newOrder.ShopifyTotalPrice          = order.total_price;
                    newOrder.ShopifyFinancialStatus     = order.financial_status;
                    newOrder.ShopifyFulfillmentStatus   = order.fulfillment_status;
                    newOrder.ShopifyIsCancelled         = order.IsCancelled;
                    newOrder.ShopifyAreAllItemsRefunded = order.AreAllLineItemsRefunded;
                    newOrder.ShopifyTotalQuantity       = order.NetOrderedQuantity;

                    newOrder.NeedsOrderPut       = false;
                    newOrder.NeedsTransactionGet = true;
                    newOrder.ErrorCount          = 0;
                    newOrder.Ignore = false;

                    newOrder.CustomerMonsterId = monsterCustomerRecord.MonsterId;
                    newOrder.DateCreated       = DateTime.UtcNow;
                    newOrder.LastUpdated       = DateTime.UtcNow;

                    _executionLogService.Log(LogBuilder.DetectedNewShopifyOrder(newOrder));
                    _orderRepository.InsertOrder(newOrder);
                }
                else
                {
                    existingOrder.ShopifyTotalPrice          = order.total_price;
                    existingOrder.ShopifyFinancialStatus     = order.financial_status;
                    existingOrder.ShopifyFulfillmentStatus   = order.fulfillment_status;
                    existingOrder.ShopifyIsCancelled         = order.IsCancelled;
                    existingOrder.ShopifyAreAllItemsRefunded = order.AreAllLineItemsRefunded;
                    existingOrder.ShopifyTotalQuantity       = order.NetOrderedQuantity;

                    if (existingOrder.StatusChangeDetected(order))
                    {
                        existingOrder.NeedsOrderPut = true;
                    }

                    existingOrder.NeedsTransactionGet = true;
                    existingOrder.LastUpdated         = DateTime.UtcNow;

                    _executionLogService.Log(LogBuilder.DetectedUpdateShopifyOrder(existingOrder));
                    _orderRepository.SaveChanges();
                }

                _shopifyJsonService.Upsert(ShopifyJsonType.Order, order.id, order.SerializeToJson());
                transaction.Commit();
            }
        }
Exemplo n.º 5
0
        public void RunLocations()
        {
            var dataLocations = _locationRepository.RetreiveLocations();

            var shopifyLocations
                = _productApi
                  .RetrieveLocations()
                  .DeserializeFromJson <LocationList>();

            foreach (var shopifyLoc in shopifyLocations.locations)
            {
                var dataLocation = dataLocations.FindByShopifyId(shopifyLoc);

                using (var transaction = _locationRepository.BeginTransaction())
                {
                    if (dataLocation == null)
                    {
                        var newDataLocation = new ShopifyLocation
                        {
                            ShopifyLocationId   = shopifyLoc.id,
                            ShopifyLocationName = shopifyLoc.name,
                            ShopifyActive       = shopifyLoc.active,
                            DateCreated         = DateTime.UtcNow,
                            LastUpdated         = DateTime.UtcNow,
                        };

                        _locationRepository.InsertLocation(newDataLocation);
                    }
                    else
                    {
                        dataLocation.LastUpdated         = DateTime.UtcNow;
                        dataLocation.ShopifyLocationName = shopifyLoc.name;
                        dataLocation.ShopifyActive       = shopifyLoc.active;
                    }
                    _locationRepository.SaveChanges();
                    _shopifyJsonService.Upsert(ShopifyJsonType.Location, shopifyLoc.id, shopifyLoc.SerializeToJson());

                    transaction.Commit();
                }
            }
        }