|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Microsoft.EntityFrameworkCore; |
| 6 | +using VirtoCommerce.CatalogModule.Core.Model; |
| 7 | +using VirtoCommerce.CatalogModule.Core.Services; |
| 8 | +using VirtoCommerce.CatalogModule.Data.Repositories; |
| 9 | +using VirtoCommerce.CoreModule.Core.Seo; |
| 10 | +using VirtoCommerce.Platform.Core.Common; |
| 11 | + |
| 12 | +namespace VirtoCommerce.CatalogModule.Data.Services; |
| 13 | + |
| 14 | +public class CatalogSeoResolver : ISeoResolver |
| 15 | +{ |
| 16 | + private readonly Func<ICatalogRepository> _repositoryFactory; |
| 17 | + private readonly ICategoryService _categoryService; |
| 18 | + private readonly IItemService _itemService; |
| 19 | + |
| 20 | + private const string CategoryObjectType = "Category"; |
| 21 | + private const string CatalogProductObjectType = "CatalogProduct"; |
| 22 | + |
| 23 | + public CatalogSeoResolver(Func<ICatalogRepository> repositoryFactory, |
| 24 | + ICategoryService categoryService, |
| 25 | + IItemService itemService) |
| 26 | + { |
| 27 | + _repositoryFactory = repositoryFactory; |
| 28 | + _categoryService = categoryService; |
| 29 | + _itemService = itemService; |
| 30 | + } |
| 31 | + |
| 32 | + public async Task<IList<SeoInfo>> FindSeoAsync(SeoSearchCriteria criteria) |
| 33 | + { |
| 34 | + ArgumentNullException.ThrowIfNull(criteria); |
| 35 | + |
| 36 | + var permalink = criteria.Permalink ?? string.Empty; |
| 37 | + var segments = permalink.Split('/', StringSplitOptions.RemoveEmptyEntries).ToArray(); |
| 38 | + if (segments.Length == 0) |
| 39 | + { |
| 40 | + return []; |
| 41 | + } |
| 42 | + |
| 43 | + var currentEntitySeoInfos = await SearchSeoInfos(criteria.StoreId, criteria.LanguageCode, segments.Last()); |
| 44 | + |
| 45 | + if (currentEntitySeoInfos.Count == 0) |
| 46 | + { |
| 47 | + // Try to find deactivated seo entries and revert it back if we found it |
| 48 | + currentEntitySeoInfos = await SearchSeoInfos(criteria.StoreId, criteria.LanguageCode, segments.Last(), false); |
| 49 | + if (currentEntitySeoInfos.Count == 0) |
| 50 | + { |
| 51 | + return []; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + var groups = currentEntitySeoInfos.GroupBy(x => new { x.ObjectType, x.ObjectId }); |
| 56 | + |
| 57 | + // We found seo information by seo search criteria |
| 58 | + if (groups.Count() == 1) |
| 59 | + { |
| 60 | + return [currentEntitySeoInfos.First()]; |
| 61 | + } |
| 62 | + |
| 63 | + // It's not possibe to resolve because we don't have parent segment |
| 64 | + if (segments.Length == 1) |
| 65 | + { |
| 66 | + return []; |
| 67 | + } |
| 68 | + |
| 69 | + // We found multiple seo information by seo search criteria, need to find correct by checking parent. |
| 70 | + var parentSearchCriteria = criteria.Clone() as SeoSearchCriteria; |
| 71 | + parentSearchCriteria.Permalink = string.Join('/', segments.Take(segments.Length - 1)); |
| 72 | + var parentSeoInfos = await FindSeoAsync(parentSearchCriteria); |
| 73 | + |
| 74 | + if (parentSeoInfos.Count == 0) |
| 75 | + { |
| 76 | + return []; |
| 77 | + } |
| 78 | + |
| 79 | + var parentCategorieIds = parentSeoInfos.Select(x => x.ObjectId).Distinct().ToList(); |
| 80 | + |
| 81 | + foreach (var groupKey in groups.Select(g => g.Key)) |
| 82 | + { |
| 83 | + if (groupKey.ObjectType == CategoryObjectType) |
| 84 | + { |
| 85 | + var isMatch = await DoesParentMatchCategoryOutline(parentCategorieIds, groupKey.ObjectId); |
| 86 | + if (isMatch) |
| 87 | + { |
| 88 | + return currentEntitySeoInfos.Where(x => |
| 89 | + x.ObjectId == groupKey.ObjectId |
| 90 | + && groupKey.ObjectType == CategoryObjectType).ToList(); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + // Inside the method |
| 95 | + else if (groupKey.ObjectType == CatalogProductObjectType) |
| 96 | + { |
| 97 | + var isMatch = await DoesParentMatchProductOutline(parentCategorieIds, groupKey.ObjectId); |
| 98 | + |
| 99 | + if (isMatch) |
| 100 | + { |
| 101 | + return currentEntitySeoInfos.Where(x => |
| 102 | + x.ObjectId == groupKey.ObjectId |
| 103 | + && groupKey.ObjectType == CatalogProductObjectType).ToList(); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return []; |
| 109 | + } |
| 110 | + |
| 111 | + private async Task<bool> DoesParentMatchCategoryOutline(IList<string> parentCategorieIds, string objectId) |
| 112 | + { |
| 113 | + var category = await _categoryService.GetByIdAsync(objectId, CategoryResponseGroup.WithOutlines.ToString(), false); |
| 114 | + if (category == null) |
| 115 | + { |
| 116 | + throw new InvalidOperationException($"Category with ID '{objectId}' was not found."); |
| 117 | + } |
| 118 | + var outlines = category.Outlines.Select(x => x.Items.Skip(x.Items.Count - 2).First().Id).Distinct().ToList(); |
| 119 | + return outlines.Any(parentCategorieIds.Contains); |
| 120 | + } |
| 121 | + |
| 122 | + private async Task<bool> DoesParentMatchProductOutline(IList<string> parentCategorieIds, string objectId) |
| 123 | + { |
| 124 | + var product = await _itemService.GetByIdAsync(objectId, CategoryResponseGroup.WithOutlines.ToString(), false); |
| 125 | + if (product == null) |
| 126 | + { |
| 127 | + throw new InvalidOperationException($"Product with ID '{objectId}' was not found."); |
| 128 | + } |
| 129 | + var outlines = product.Outlines.Select(x => x.Items.Skip(x.Items.Count - 2).First().Id).Distinct().ToList(); |
| 130 | + return outlines.Any(parentCategorieIds.Contains); |
| 131 | + } |
| 132 | + |
| 133 | + private async Task<List<SeoInfo>> SearchSeoInfos(string storeId, string languageCode, string slug, bool isActive = true) |
| 134 | + { |
| 135 | + using var repository = _repositoryFactory(); |
| 136 | + |
| 137 | + return (await repository.SeoInfos.Where(s => s.IsActive == isActive |
| 138 | + && s.Keyword == slug |
| 139 | + && (s.StoreId == null || s.StoreId == storeId) |
| 140 | + && (s.Language == null || s.Language == languageCode)) |
| 141 | + .ToListAsync()) |
| 142 | + .Select(x => x.ToModel(AbstractTypeFactory<SeoInfo>.TryCreateInstance())) |
| 143 | + .OrderByDescending(s => GetPriorityScore(s, storeId, languageCode)) |
| 144 | + .ToList(); |
| 145 | + } |
| 146 | + |
| 147 | + private static int GetPriorityScore(SeoInfo seoInfo, string storeId, string language) |
| 148 | + { |
| 149 | + var score = 0; |
| 150 | + var hasStoreCriteria = !string.IsNullOrEmpty(storeId); |
| 151 | + var hasLangCriteria = !string.IsNullOrEmpty(language); |
| 152 | + |
| 153 | + if (hasStoreCriteria && seoInfo.StoreId == storeId) |
| 154 | + { |
| 155 | + score += 2; |
| 156 | + } |
| 157 | + |
| 158 | + if (hasLangCriteria && seoInfo.LanguageCode == language) |
| 159 | + { |
| 160 | + score += 1; |
| 161 | + } |
| 162 | + |
| 163 | + return score; |
| 164 | + } |
| 165 | +} |
| 166 | + |
0 commit comments