Beispiel #1
0
        public async Task <HSSupplier> UpdateSupplier(string supplierID, PartialSupplier supplier, DecodedToken decodedToken)
        {
            var me = await _oc.Me.GetAsync(accessToken : decodedToken.AccessToken);

            Require.That(decodedToken.CommerceRole == CommerceRole.Seller || supplierID == me.Supplier.ID, new ErrorCode("Unauthorized", $"You are not authorized to update supplier {supplierID}", 401));
            var currentSupplier = await _oc.Suppliers.GetAsync <HSSupplier>(supplierID);

            var updatedSupplier = await _oc.Suppliers.PatchAsync <HSSupplier>(supplierID, supplier);

            // Update supplier products only on a name change
            if (currentSupplier.Name != supplier.Name || currentSupplier.xp.Currency.ToString() != supplier.xp.Currency.Value)
            {
                var productsToUpdate = await _oc.Products.ListAllAsync <HSProduct>(
                    supplierID : supplierID,
                    accessToken : decodedToken.AccessToken
                    );

                ApiClient supplierClient = await _apiClientHelper.GetSupplierApiClient(supplierID, decodedToken.AccessToken);

                if (supplierClient == null)
                {
                    throw new Exception($"Default supplier client not found. SupplierID: {supplierID}");
                }
                var configToUse = new OrderCloudClientConfig
                {
                    ApiUrl       = decodedToken.ApiUrl,
                    AuthUrl      = decodedToken.AuthUrl,
                    ClientId     = supplierClient.ID,
                    ClientSecret = supplierClient.ClientSecret,
                    GrantType    = GrantType.ClientCredentials,
                    Roles        = new[]
                    {
                        ApiRole.SupplierAdmin,
                        ApiRole.ProductAdmin
                    },
                };
                var ocClient = new OrderCloudClient(configToUse);
                await ocClient.AuthenticateAsync();

                var token = ocClient.TokenResponse.AccessToken;
                foreach (var product in productsToUpdate)
                {
                    product.xp.Facets["supplier"] = new List <string>()
                    {
                        supplier.Name
                    };
                    product.xp.Currency = supplier.xp.Currency;
                }
                await Throttler.RunAsync(productsToUpdate, 100, 5, product => ocClient.Products.SaveAsync(product.ID, product, accessToken: token));
            }

            return(updatedSupplier);
        }
Beispiel #2
0
 public async Task <HSSupplier> UpdateSupplier(string supplierID, [FromBody] PartialSupplier supplier)
 {
     return(await _command.UpdateSupplier(supplierID, supplier, VerifiedUserContext));
 }