//End of PushProductToShopify /// <summary> /// Deals with any orphaned cases with a Mapped remaining. Attempts to clean up any shopify Items in the process. /// </summary> /// <param name="shopsterIds"></param> /// <param name="productMap"></param> /// <param name="shopifyMapping"></param> /// <param name="shopifyIds"></param> /// <param name="shopifyAuth"></param> /// <returns>Number of "items" affected</returns> private int FixOrphanedMappings(IList<int> shopsterIds, ConnectsterProductMap productMap, ShopifyMetafieldMap shopifyMapping, IList<int> shopifyIds, ShopifyStoreAuth shopifyAuth) { int returnCount = 0; //Foreach productMap for which there is no corresponding shopsterItem. // Delete the map, and if applicable delete the shopifyItem. foreach (ConnectsterProduct shopsterifyProduct in productMap.GetMappedItems() .Where(p => !shopsterIds.Contains(p.SourceId)).Select(p => p).ToList()) { //case 2 if (!shopifyIds.Contains(shopsterifyProduct.DestinationId)) { productMap.DeleteMapping(shopsterifyProduct); returnCount++; } else //case 6 { if (DeleteProductAndDeleteMap(shopifyAuth, productMap, shopsterifyProduct)) { shopifyIds.Remove(shopsterifyProduct.DestinationId); ConnectsterProduct product = shopsterifyProduct; ShopifyMetafieldMapping toRemove = shopifyMapping.GetMappedItems().Where( map => map.SourceId == product.DestinationId).Select(map => map). FirstOrDefault(); shopifyMapping.GetMappedItems().Remove(toRemove); returnCount++; } } } return returnCount; }
private bool DeleteProductAndDeleteMap(ShopifyStoreAuth shopifyAuth, ConnectsterProductMap productMap, ConnectsterProduct item) { //Attempt to delete the shopsterItem from shopify if (!_shopifyComm.DeleteProduct(shopifyAuth, item.DestinationId)) { //maybe a communication error, try next time around //todo: error, logs return false; } //Delete the Mapping return (productMap.DeleteMapping(item)); }