public async Task Import(IJobCancellationToken token) { using var _ = _hub.PushScope(); var transaction = _hub.StartTransaction("import-catalog", "catalog.import"); _hub.ConfigureScope(s => s.Transaction = transaction); var logger = _loggerFactory.CreateLogger <NuGetCatalogImporter>(); logger.LogInformation("Starting importing catalog."); try { var httpClient = _httpClientFactory.CreateClient("nuget"); var catalogClient = new CatalogClient(httpClient, _loggerFactory.CreateLogger <CatalogClient>()); var settings = new CatalogProcessorSettings { DefaultMinCommitTimestamp = DateTimeOffset.MinValue, // Read everything ExcludeRedundantLeaves = false, }; var catalogProcessor = new CatalogProcessor( _cursorStore, catalogClient, _catalogLeafProcessor, settings, _loggerFactory.CreateLogger <CatalogProcessor>()); await catalogProcessor.ProcessAsync(token.ShutdownToken); logger.LogInformation("Finished importing catalog."); transaction.Finish(SpanStatus.Ok); } catch (Exception e) { transaction.Finish(e); SentrySdk.CaptureException(e); throw; } finally { await SentrySdk.FlushAsync(TimeSpan.FromSeconds(2)); } }
private async Task <T> GetClientAsync <T>(Func <NuGetClients, T> clientFactory, CancellationToken cancellationToken) { // TODO: This should periodically refresh the service index response. if (_clients == null) { await _mutex.WaitAsync(cancellationToken); try { if (_clients == null) { var serviceIndexClient = new ServiceIndexClient(_httpClient, _serviceIndexUrl); var serviceIndex = await serviceIndexClient.GetAsync(cancellationToken); var contentClient = new PackageContentClient(_httpClient, GetResourceUrl(serviceIndex, PackageBaseAddress)); var metadataClient = new PackageMetadataClient(_httpClient, GetResourceUrl(serviceIndex, RegistrationsBaseUrl)); var catalogClient = new CatalogClient(_httpClient, GetResourceUrl(serviceIndex, Catalog)); var searchClient = new SearchClient( _httpClient, GetResourceUrl(serviceIndex, SearchQueryService), GetResourceUrl(serviceIndex, SearchAutocompleteService)); _clients = new NuGetClients { ServiceIndexClient = serviceIndexClient, PackageContentClient = contentClient, PackageMetadataClient = metadataClient, SearchClient = searchClient, CatalogClient = catalogClient, }; } } finally { _mutex.Release(); } } return(clientFactory(_clients)); }
private void CalculateTaxes(OrderGroup order) { var catalogHelper = new CatalogClient(CatalogRepository, null, null, CacheRepository, null); foreach (var form in order.OrderForms) { decimal totalTaxes = 0; foreach (var shipment in form.Shipments) { decimal itemTax = 0; decimal shippingTax = 0; foreach (var shipItem in shipment.ShipmentItems) { var lineItem = shipItem.LineItem; if (lineItem == null) { continue; } decimal lineItemTaxTotal = 0m; // Try getting an address var address = GetAddressByName(form, shipment.ShippingAddressId); if (address != null) // no taxes if there is no address { // Try getting an item var item = catalogHelper.GetItem(lineItem.CatalogItemId); if (item != null) // no entry, no tax category, no tax { var taxCategory = item.TaxCategory; // calls the method that returns all the tax values var taxes = GetTaxes(taxCategory, Thread.CurrentThread.CurrentCulture.Name, address.CountryCode, address.StateProvince, address.PostalCode, address.RegionName, string.Empty, string.Empty, address.City); if (taxes != null && taxes.Any()) { foreach (var tax in taxes) { if (tax != null) { var taxAmount = Math.Round(lineItem.PlacedPrice * (tax.Percentage / 100), 2) * lineItem.Quantity; if (tax.Tax.TaxType == (int)TaxTypes.SalesTax) { itemTax += taxAmount; lineItemTaxTotal += taxAmount; totalTaxes += taxAmount; } else if (tax.Tax.TaxType == (int)TaxTypes.ShippingTax) { shippingTax += Math.Round(shipment.ShippingCost * (tax.Percentage / 100), 2); totalTaxes += shippingTax; } } } } } } lineItem.TaxTotal = lineItemTaxTotal; } //TODO Round taxes to money shipment.ItemTaxTotal = itemTax; shipment.ShippingTaxTotal = shippingTax; } form.TaxTotal = totalTaxes; } order.TaxTotal = order.OrderForms.Sum(x => x.TaxTotal); }
/// <summary> /// Initializes a new instance of the <see cref="CatalogController" /> class. /// </summary> /// <param name="catalogClient">The catalog client.</param> /// <param name="templateClient">The template client.</param> public CatalogController(CatalogClient catalogClient, DisplayTemplateClient templateClient) { _catalogClient = catalogClient; _templateClient = templateClient; }
public void Sync() { Task.Run(() => CatalogClient.Sync()).Wait(); SelectBudget(); Task.Run(() => BudgetClient.Sync()).Wait(); }
public async Task OperatesOnLatestPerPackageIdentityAndGroupsById() { var items = new[] { new CatalogCommitItem( uri: new Uri("https://example/0"), commitId: null, commitTimeStamp: new DateTime(2018, 1, 1), types: null, typeUris: new List <Uri> { Schema.DataTypes.PackageDetails }, packageIdentity: new PackageIdentity("NuGet.Versioning", NuGetVersion.Parse("1.0.0"))), new CatalogCommitItem( uri: new Uri("https://example/1"), commitId: null, commitTimeStamp: new DateTime(2018, 1, 2), types: null, typeUris: new List <Uri> { Schema.DataTypes.PackageDetails }, packageIdentity: new PackageIdentity("NuGet.Versioning", NuGetVersion.Parse("1.0.0"))), new CatalogCommitItem( uri: new Uri("https://example/2"), commitId: null, commitTimeStamp: new DateTime(2018, 1, 2), types: null, typeUris: new List <Uri> { Schema.DataTypes.PackageDetails }, packageIdentity: new PackageIdentity("NuGet.Versioning", NuGetVersion.Parse("2.0.0"))), new CatalogCommitItem( uri: new Uri("https://example/3"), commitId: null, commitTimeStamp: new DateTime(2018, 1, 2), types: null, typeUris: new List <Uri> { Schema.DataTypes.PackageDetails }, packageIdentity: new PackageIdentity("NuGet.Frameworks", NuGetVersion.Parse("1.0.0"))), }; await Target.OnProcessBatchAsync(items); CatalogClient.Verify(x => x.GetPackageDetailsLeafAsync("https://example/1"), Times.Once); CatalogClient.Verify(x => x.GetPackageDetailsLeafAsync("https://example/2"), Times.Once); CatalogClient.Verify(x => x.GetPackageDetailsLeafAsync("https://example/3"), Times.Once); CatalogClient.Verify(x => x.GetPackageDetailsLeafAsync(It.IsAny <string>()), Times.Exactly(3)); RegistrationUpdater.Verify( x => x.UpdateAsync( "NuGet.Versioning", It.Is <IReadOnlyList <CatalogCommitItem> >( y => y.Count == 2), It.Is <IReadOnlyDictionary <CatalogCommitItem, PackageDetailsCatalogLeaf> >( y => y.Count == 2)), Times.Once); RegistrationUpdater.Verify( x => x.UpdateAsync( "NuGet.Frameworks", It.Is <IReadOnlyList <CatalogCommitItem> >( y => y.Count == 1), It.Is <IReadOnlyDictionary <CatalogCommitItem, PackageDetailsCatalogLeaf> >( y => y.Count == 1)), Times.Once); }
/// <summary> /// Cart controller constructor /// </summary> /// <param name="catalogClient">Catalog client used to access catalog repository methods through caching and other helpers</param> /// <param name="countryClient">Allows to get countries from repository through caching</param> public CartController(CatalogClient catalogClient, CountryClient countryClient) { _catalogClient = catalogClient; _countryClient = countryClient; }
public StoreSearchFilterService(StoreClient client, ICustomerSessionService customerSession, CatalogClient catalogClient) { _storeClient = client; _customerSession = customerSession; _catalogClient = catalogClient; }
public CatalogClientTests(ProtocolFixture fixture) { _target = fixture.CatalogClient; }
/// <summary> /// Creates the item model. /// </summary> /// <param name="item">The item.</param> /// <param name="propertySet">The property set.</param> /// <returns>ItemModel.</returns> /// <exception cref="System.ArgumentNullException">item</exception> public static ItemModel CreateItemModel(Item item, PropertySet propertySet = null) { if (item == null) { throw new ArgumentNullException("item"); } var model = new ItemModel { Item = item }; model.InjectFrom(item); model.ItemAssets = new List<ItemAsset>(item.ItemAssets).ToArray(); var reviews = item.EditorialReviews; model.EditorialReviews = new List<EditorialReview>(reviews.Where(x => string.IsNullOrEmpty(x.Locale) || string.Equals(x.Locale, CultureInfo.CurrentUICulture.Name, StringComparison.InvariantCultureIgnoreCase) || (string.Equals(x.Locale, StoreHelper.GetDefaultLanguageCode(), StringComparison.InvariantCultureIgnoreCase) && !reviews.Any(val => val.Source == x.Source && string.Equals(val.Locale, CultureInfo.CurrentUICulture.Name, StringComparison.InvariantCultureIgnoreCase))))).ToArray(); model.AssociationGroups = new List<AssociationGroup>(item.AssociationGroups).ToArray(); if (propertySet != null && item.ItemPropertyValues != null) { var values = item.ItemPropertyValues; var properties = propertySet.PropertySetProperties.SelectMany(x => values.Where(v => v.Name == x.Property.Name && !x.Property.PropertyAttributes.Any(pa => pa.PropertyAttributeName.Equals("Hidden", StringComparison.OrdinalIgnoreCase)) //skip hidden && (!x.Property.IsLocaleDependant // if not localized ok || string.Equals(v.Locale, CultureInfo.CurrentUICulture.Name, StringComparison.InvariantCultureIgnoreCase) //if current locale match value locale ok || (string.Equals(v.Locale, StoreHelper.GetDefaultLanguageCode(), StringComparison.InvariantCultureIgnoreCase) //if default locale match value locale and values does not contain locale for current property ok && !values.Any(val => val.Name == x.Property.Name && string.Equals(val.Locale, CultureInfo.CurrentUICulture.Name, StringComparison.InvariantCultureIgnoreCase))))), (r, v) => CreatePropertyModel(r.Priority, r.Property, v, item)).ToArray(); model.Properties = new PropertiesModel(properties); } //Find item category if (item.CategoryItemRelations != null) { string categoryId = null; foreach (var rel in item.CategoryItemRelations) { if (rel.CatalogId == UserHelper.CustomerSession.CatalogId) { categoryId = rel.CategoryId; break; } var category = CatalogClient.GetCategoryById(rel.CategoryId); if (category == null) continue; var linkedCategory = category.LinkedCategories.FirstOrDefault( link => link.CatalogId == UserHelper.CustomerSession.CatalogId); if (linkedCategory == null) continue; categoryId = linkedCategory.CategoryId; break; } if (!string.IsNullOrEmpty(categoryId)) { var category = CatalogClient.GetCategoryById(categoryId); var cat = category as Category; if (cat != null) { model.CategoryName = cat.Name.Localize(); } } } return model; }
/// <summary> /// Creates the catalog model. /// </summary> /// <param name="itemId">The item identifier.</param> /// <param name="parentItemId">The parent item identifier.</param> /// <param name="associationType">Type of the association.</param> /// <param name="forcedActive">if set to <c>true</c> [forced active].</param> /// <param name="responseGroups">The response groups.</param> /// <param name="display">The display.</param> /// <param name="byItemCode">if set to <c>true</c> gets item by code.</param> /// <returns> /// CatalogItemWithPriceModel. /// </returns> public static CatalogItemWithPriceModel CreateCatalogModel(string itemId, string parentItemId = null, string associationType = null, bool forcedActive = false, ItemResponseGroups responseGroups = ItemResponseGroups.ItemLarge, ItemDisplayOptions display = ItemDisplayOptions.ItemLarge, bool byItemCode = false) { var dbItem = CatalogClient.GetItem(itemId, responseGroups, UserHelper.CustomerSession.CatalogId, bycode: byItemCode); if (dbItem != null) { if (dbItem.IsActive || forcedActive) { PriceModel priceModel = null; PropertySet propertySet = null; //ItemRelation[] variations = null; ItemAvailabilityModel itemAvaiability = null; if (display.HasFlag(ItemDisplayOptions.ItemPropertySets)) { propertySet = CatalogClient.GetPropertySet(dbItem.PropertySetId); //variations = CatalogClient.GetItemRelations(itemId); } var itemModel = CreateItemModel(dbItem, propertySet); if (display.HasFlag(ItemDisplayOptions.ItemAvailability)) { var fulfillmentCenter = UserHelper.StoreClient.GetCurrentStore().FulfillmentCenterId; var availability = CatalogClient.GetItemAvailability(dbItem.ItemId, fulfillmentCenter); itemAvaiability = new ItemAvailabilityModel(availability); } if (display.HasFlag(ItemDisplayOptions.ItemPrice)) { var lowestPrice = PriceListClient.GetLowestPrice(dbItem.ItemId, itemAvaiability != null ? itemAvaiability.MinQuantity : 1); var outlines = OutlineBuilder.BuildCategoryOutline(CatalogClient.CustomerSession.CatalogId, dbItem.ItemId); var tags = new Hashtable { { "Outline", outlines.ToString() } }; priceModel = MarketingHelper.GetItemPriceModel(dbItem, lowestPrice, tags); itemModel.CatalogOutlines = outlines; // get the category name if (outlines.Count > 0) { var outline = outlines[0]; if (outline.Categories.Count > 0) { var category = outline.Categories.OfType<Category>().Reverse().FirstOrDefault(); if (category != null) { itemModel.CategoryName = category.Name; } } } } itemModel.ParentItemId = parentItemId; return string.IsNullOrEmpty(associationType) ? new CatalogItemWithPriceModel(itemModel, priceModel, itemAvaiability) : new AssociatedCatalogItemWithPriceModel(itemModel, priceModel, itemAvaiability, associationType); } } return null; }
public ItemsController(IRepository <InventoryItem> itemsRepository, CatalogClient catalogClient) { _itemsRepository = itemsRepository; _catalogClient = catalogClient; }