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); }
public async Task <Product> FilterOptionOverride(string id, string supplierID, IDictionary <string, object> facets, VerifiedUserContext user) { ApiClient supplierClient = await _apiClientHelper.GetSupplierApiClient(supplierID, user.AccessToken); if (supplierClient == null) { throw new Exception($"Default supplier client not found. SupplierID: {supplierID}"); } var configToUse = new OrderCloudClientConfig { ApiUrl = user.TokenApiUrl, AuthUrl = user.TokenAuthUrl, 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; //Format the facet data to change for request body var facetDataFormatted = new ExpandoObject(); var facetDataFormattedCollection = (ICollection <KeyValuePair <string, object> >)facetDataFormatted; foreach (var kvp in facets) { facetDataFormattedCollection.Add(kvp); } dynamic facetDataFormattedDynamic = facetDataFormatted; //Update the product with a supplier token var updatedProduct = await ocClient.Products.PatchAsync( id, new PartialProduct() { xp = new { Facets = facetDataFormattedDynamic } }, accessToken : token ); return(updatedProduct); }