private static TemplateProductFlat Map(HSProduct product, HSPriceSchedule priceSchedule, Asset asset = null)
 {
     return(new TemplateProductFlat
     {
         ID = product.ID,
         Active = product.Active,
         ApplyShipping = priceSchedule.ApplyShipping,
         ApplyTax = priceSchedule.ApplyTax,
         Description = product.Description,
         IsResale = product.xp.IsResale,
         MaxQuantity = priceSchedule.MaxQuantity,
         MinQuantity = priceSchedule.MinQuantity,
         Name = product.Name,
         Price = priceSchedule.PriceBreaks.FirstOrDefault()?.Price,
         ShipFromAddressID = product.ShipFromAddressID,
         ShipHeight = product.ShipHeight,
         ShipLength = product.ShipLength,
         ShipWidth = product.ShipWidth,
         ShipWeight = product.ShipWeight,
         QuantityMultiplier = product.QuantityMultiplier,
         RestrictedQuantity = priceSchedule.RestrictedQuantity,
         TaxCategory = product.xp.Tax.Category,
         TaxCode = product.xp.Tax.Code,
         TaxDescription = product.xp.Tax.Description,
         UseCumulativeQuantity = priceSchedule.UseCumulativeQuantity,
         UnitOfMeasure = product.xp.UnitOfMeasure.Unit,
         UnitOfMeasureQuantity = product.xp.UnitOfMeasure.Qty,
         ImageTitle = asset?.Title,
         Url = asset?.Url,
         Type = asset?.Type ?? AssetType.Image,
         Tags = asset?.Tags?.JoinString(","),
         FileName = asset?.FileName,
         ProductType = product.xp.ProductType,
         SizeTier = product.xp.SizeTier
     });
 }
Exemplo n.º 2
0
        public async Task <SuperHSProduct> UpdateMonitoredSuperProductNotificationStatus(Document <MonitoredProductFieldModifiedNotification> document, string supplierID, string productID, VerifiedUserContext user)
        {
            HSProduct product        = null;
            HSProduct patchedProduct = null;
            var       token          = await GetAdminToken();

            try
            {
                product = await _oc.Products.GetAsync <HSProduct>(productID);
            }
            catch (OrderCloudException ex)
            {
                //Product was deleted after it was updated. Delete orphaned notification
                if (ex.HttpStatus == System.Net.HttpStatusCode.NotFound)
                {
                    await _cms.Documents.Delete(_documentSchemaID, document.ID, token);

                    return(new SuperHSProduct());
                }
            }
            if (document.Doc.Status == NotificationStatus.ACCEPTED)
            {
                var supplierClient = await _apiClientHelper.GetSupplierApiClient(supplierID, user.AccessToken);

                if (supplierClient == null)
                {
                    throw new Exception($"Default supplier client not found. SupplierID: {supplierID}, ProductID: {productID}");
                }

                var configToUse = new OrderCloudClientConfig
                {
                    ApiUrl       = user.ApiUrl,
                    AuthUrl      = user.AuthUrl,
                    ClientId     = supplierClient.ID,
                    ClientSecret = supplierClient.ClientSecret,
                    GrantType    = GrantType.ClientCredentials,
                    Roles        = new[]
                    {
                        ApiRole.SupplierAdmin,
                        ApiRole.ProductAdmin
                    },
                };
                try
                {
                    await ClientHelper.RunAction(configToUse, async x =>
                    {
                        patchedProduct = await x.Products.PatchAsync <HSProduct>(productID, new PartialProduct()
                        {
                            Active = true
                        });
                    }
                                                 );
                }
                catch (OrderCloudException ex)
                {
                    if (ex?.Errors?[0]?.Data != null)
                    {
                        throw new Exception($"Unable to re-activate product: {ex?.Errors?[0]?.Message}: {ex?.Errors?[0]?.Data.ToJRaw()}");
                    }
                    throw new Exception($"Unable to re-activate product: {ex?.Errors?.ToJRaw()}");
                }

                //Delete document after acceptance
                await _cms.Documents.Delete(_documentSchemaID, document.ID, token);
            }
            else
            {
                await _cms.Documents.Save(_documentSchemaID, document.ID, document, token);
            }
            var superProduct = await _productCommand.Get(productID, token);

            superProduct.Product = patchedProduct;
            return(superProduct);
        }
 public async Task <HSProduct> PostProduct([FromBody] HSProduct obj)
 {
     return(await _command.SaveToQueue(obj, this.VerifiedUserContext, this.VerifiedUserContext.SupplierID));
 }