示例#1
0
        public async Task <IActionResult> UploadProductTemplate(
            [HttpTrigger(AuthorizationLevel.Anonymous, "POST", Route = "templateupload/{supplierId}")]
            HttpRequest req, string supplierId, ILogger log)
        {
            var user = await _token.Authorize(req, new[] { ApiRole.ProductAdmin, ApiRole.PriceScheduleAdmin });

            Require.That(user.SupplierID == supplierId, new ErrorCode("Authorization.InvalidToken", 401, "Authorization.InvalidToken: Access token is invalid or expired."));

            var form = await req.ReadFormAsync();

            var result = await _product.ParseProductTemplateFlat(form.Files.GetFile("file"), user);

            return(await Task.FromResult(new OkObjectResult(result)));
        }
        public async Task <JObject> CreateAsync(WorkItem wi)
        {
            var obj = wi.Current.ToObject <TemplateProductFlat>(OrchestrationSerializer.Serializer);

            _user ??= await _token.Authorize(wi.Token, new[] { ApiRole.ProductAdmin, ApiRole.PriceScheduleAdmin });

            try
            {
                obj.ID ??= wi.RecordId;
                var priceSchedule = await _oc.PriceSchedules.SaveAsync <HSPriceSchedule>(obj.ID, new HSPriceSchedule()
                {
                    ID            = obj.ID,
                    ApplyShipping = obj.ApplyShipping,
                    ApplyTax      = obj.ApplyTax,
                    MaxQuantity   = obj.MaxQuantity,
                    MinQuantity   = obj.MinQuantity,
                    Name          = obj.Name,
                    PriceBreaks   = new List <PriceBreak>()
                    {
                        new PriceBreak()
                        {
                            Price = obj.Price.To <decimal>(), Quantity = obj.QuantityMultiplier
                        }
                    },
                    RestrictedQuantity    = obj.RestrictedQuantity,
                    UseCumulativeQuantity = obj.UseCumulativeQuantity
                }, _user.AccessToken);

                var product = await _oc.Products.SaveAsync <HSProduct>(obj.ID, new HSProduct()
                {
                    Active                 = true,
                    AutoForward            = false,
                    DefaultSupplierID      = wi.ResourceId,
                    DefaultPriceScheduleID = priceSchedule.ID,
                    ShipFromAddressID      = obj.ShipFromAddressID,
                    ID                 = obj.ID,
                    Name               = obj.Name,
                    Description        = obj.Description,
                    QuantityMultiplier = obj.QuantityMultiplier,
                    ShipWeight         = obj.ShipWeight,
                    ShipLength         = obj.ShipLength,
                    ShipHeight         = obj.ShipHeight,
                    ShipWidth          = obj.ShipWidth,
                    xp                 = new ProductXp()
                    {
                        IsResale     = obj.IsResale,
                        Accessorials = new List <ProductAccessorial>(),
                        Tax          = new TaxProperties()
                        {
                            Category    = obj.TaxCategory,
                            Code        = obj.TaxCode,
                            Description = obj.TaxDescription
                        },
                        UnitOfMeasure = new UnitOfMeasure()
                        {
                            Qty  = obj.UnitOfMeasureQuantity,
                            Unit = obj.UnitOfMeasure
                        },
                        ProductType = obj.ProductType,
                        Currency    = obj.Currency,
                        SizeTier    = obj.SizeTier
                    }
                }, _user.AccessToken);

                Asset image = new Asset()
                {
                    ID       = obj.ID,
                    Active   = true,
                    FileName = obj.FileName,
                    Type     = AssetType.Image,
                    Url      = obj.Url,
                    Title    = obj.ImageTitle,
                    Tags     = obj.Tags?.Split(',', StringSplitOptions.RemoveEmptyEntries).ToList()
                };
                if (obj.Url != null)
                {
                    image = await _cms.Assets.Save(obj.ID, image, _user.AccessToken);

                    await _cms.Assets.SaveAssetAssignment(new AssetAssignment()
                    {
                        AssetID = image.ID,
                        //ParentResourceID = product.ID,
                        ResourceType = ResourceType.Products,
                        ResourceID   = product.ID
                    }, _user.AccessToken);
                }

                return(JObject.FromObject(Map(product, priceSchedule, image), OrchestrationSerializer.Serializer));
            }
            catch (OrderCloudException exId) when(IdExists(exId))
            {
                // handle 409 errors by refreshing cache
                await _log.Save(new OrchestrationLog(wi)
                {
                    ErrorType        = OrchestrationErrorType.CreateExistsError,
                    Message          = exId.Message,
                    Level            = LogLevel.Error,
                    OrderCloudErrors = exId.Errors
                });

                return(await GetAsync(wi));
            }
            catch (OrderCloudException ex)
            {
                await _log.Save(new OrchestrationLog(wi)
                {
                    ErrorType        = OrchestrationErrorType.CreateGeneralError,
                    Message          = ex.Message,
                    Level            = LogLevel.Error,
                    OrderCloudErrors = ex.Errors
                });

                throw new Exception(OrchestrationErrorType.CreateGeneralError.ToString(), ex);
            }
            catch (Exception e)
            {
                await _log.Save(new OrchestrationLog(wi)
                {
                    ErrorType = OrchestrationErrorType.CreateGeneralError,
                    Message   = e.Message,
                    Level     = LogLevel.Error
                });

                throw new Exception(OrchestrationErrorType.CreateGeneralError.ToString(), e);
            }
        }
 public async Task <VerifiedUserContext> DefineUserContext([ActivityTrigger] WorkItem wi) => await _token.Authorize(wi.Token, new[] { ApiRole.ProductAdmin, ApiRole.PriceScheduleAdmin });