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); }
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); }