diff --git a/database/filetrs.js b/database/filetrs.js index 824707f..14b0660 100644 --- a/database/filetrs.js +++ b/database/filetrs.js @@ -8,6 +8,7 @@ const filterCache = { goods: {}, tools: {}, tags: {}, + alcohol: {}, }; const filterSlugToIdMap = {} @@ -19,6 +20,7 @@ const keyMapping = { goods: 'goods', tools: 'tools', tags: 'tags', + alcohol: 'alcohol', }; async function initializeFilterCache() { @@ -28,6 +30,7 @@ async function initializeFilterCache() { const goods = await Database.collection('goods').find().toArray(); const tools = await Database.collection('tools').find().toArray(); const tags = await Database.collection('tags').find().toArray(); + const alcohol = await Database.collection('alcohol').find().toArray(); alcoholVolumes.forEach(av => filterCache.alcoholVolumes[av.slug] = new Set(av.cocktailSlugs)); tastes.forEach(taste => filterCache.tastes[taste.slug] = new Set(taste.cocktailSlugs)); @@ -35,6 +38,7 @@ async function initializeFilterCache() { goods.forEach(good => filterCache.goods[good.slug] = new Set(good.cocktailSlugs)); tools.forEach(tool => filterCache.tools[tool.slug] = new Set(tool.cocktailSlugs)); tags.forEach(tag => filterCache.tags[tag.slug] = new Set(tag.cocktailSlugs)); + alcohol.forEach(al => filterCache.alcohol[al.slug] = new Set(al.cocktailSlugs)); alcoholVolumes.forEach(av => filterSlugToIdMap[av.slug] = av.id); tastes.forEach(taste => filterSlugToIdMap[taste.slug] = taste.id); @@ -42,6 +46,7 @@ async function initializeFilterCache() { goods.forEach(good => filterSlugToIdMap[good.slug] = good.id); tools.forEach(tool => filterSlugToIdMap[tool.slug] = tool.id); tags.forEach(tag => filterSlugToIdMap[tag.slug] = tag.id); + alcohol.forEach(al => filterSlugToIdMap[al.slug] = al.id); console.log("Filter cache initialized"); } @@ -161,7 +166,6 @@ async function buildFutureCounter(inputFilters, filterKey) { }); if (Object.keys(futureFilter).length === 0) { - // return empty query and total cocktail count const totalCount = await Database.collection('cocktails').countDocuments(); return { @@ -191,7 +195,7 @@ async function buildFutureCounter(inputFilters, filterKey) { } async function getCocktailFilterState(filters, skip, limit, sortType) { - const [totalCount, cocktails, alcoholVolumeFuture, tasteFuture, glasswareFuture, toolsFuture, goodsFuture, tagsFuture] = await Promise.all([ + const [totalCount, cocktails, alcoholVolumeFuture, tasteFuture, glasswareFuture, toolsFuture, goodsFuture, tagsFuture, alcoholFuture] = await Promise.all([ getCocktailCountByFilter(filters), getCocktailSubsetByFilter(filters, skip, limit, sortType), buildFutureCounter(filters, 'alcohol-volume'), @@ -199,7 +203,8 @@ async function getCocktailFilterState(filters, skip, limit, sortType) { buildFutureCounter(filters, 'glassware'), buildFutureCounter(filters, 'tools'), buildFutureCounter(filters, 'goods'), - buildFutureCounter(filters, 'tags') + buildFutureCounter(filters, 'tags'), + buildFutureCounter(filters, 'alcohol'), ]); const selectedFilterCount = Object.keys(filters).reduce((acc, key) => acc + filters[key].length, 0); @@ -215,6 +220,7 @@ async function getCocktailFilterState(filters, skip, limit, sortType) { 3: tasteFuture, 4: alcoholVolumeFuture, 5: glasswareFuture, + 6: alcoholFuture, }, isAddToIndex: isAddToIndex, } diff --git a/database/filters.js b/database/filters.js index 4e75c4f..45ed6bf 100644 --- a/database/filters.js +++ b/database/filters.js @@ -120,12 +120,40 @@ async function getToolsData() { return tools; } +async function getAlcohole() { + const alcohol = await Database + .collection('alcohol') + .aggregate([ + { + $project: { + _id: 0, + id: 1, + name: 1, + slug: 1, + count: { $size: "$cocktailSlugs" } + } + }, + { $sort: { count: -1 } } + ]) + .toArray(); + + return alcohol; +} + async function getFiltersData() { return [ + { + id: 6, + queryName: 'alcohol', + name: 'Алкоголь', + items: await getAlcohole(), + selectionType: 'MULTIPLE', + sortOrder: 2, + }, { id: 4, queryName: 'alcohol-volume', - name: 'Алкоголь', + name: 'Міцність', items: await getAlcoholoVolume(), selectionType: 'SINGLE', sortOrder: 1, @@ -136,7 +164,7 @@ async function getFiltersData() { name: 'Смак', items: await getTastes(), selectionType: 'MULTIPLE', - sortOrder: 2, + sortOrder: 3, }, { id: 5, @@ -144,7 +172,7 @@ async function getFiltersData() { name: 'Стакан', items: await getGlasswares(), selectionType: 'SINGLE', - sortOrder: 3, + sortOrder: 4, }, { id: 1, @@ -152,7 +180,7 @@ async function getFiltersData() { name: 'Інгрідієнти', items: await getGoods(), selectionType: 'MULTIPLE', - sortOrder: 4, + sortOrder: 5, }, { id: 0, @@ -160,7 +188,7 @@ async function getFiltersData() { name: 'Інше', items: await getTagsData(), selectionType: 'MULTIPLE', - sortOrder: 5, + sortOrder: 6, }, { id: 2, @@ -168,7 +196,7 @@ async function getFiltersData() { name: 'Приладдя', items: await getToolsData(), selectionType: 'MULTIPLE', - sortOrder: 6, + sortOrder: 7, } ] } diff --git a/features/filters/description.js b/features/filters/description.js index 1b73e08..92dce1a 100644 --- a/features/filters/description.js +++ b/features/filters/description.js @@ -5,7 +5,7 @@ class DescriptionBuilder { let description = ''; // Start building the description - description += await this.addAlcoholDescriptionIfExist(filters['alcohol-volume']); + description += await this.addAlcoholVolumeDescriptionIfExist(filters['alcohol-volume']); if (filters['taste']?.length > 0) { description += ', '; @@ -16,6 +16,7 @@ class DescriptionBuilder { description += await this.addTasteDescriptionIfExist(filters['taste']); description += 'коктейлі'; // COCKTAIL_NAME + description += await this.addAlchoholDescriptionIfExist(filters['alcohol']); description += await this.addTagsDescriptionIfExist(filters['tags']); description += await this.addGoodsDescriptionIfExist(filters['goods']); description += await this.addGlasswareDescriptionIfExist(filters['glassware']); @@ -24,6 +25,7 @@ class DescriptionBuilder { description = description.trim().replace(/(^\s*,)|(,\s*$)/g, '').replace(/^./, char => char.toUpperCase()).trim(); description = description.trim().length > 0 && description.trim() !== 'коктейлі' ? description.trim() : null; + description = description ? description.charAt(0).toUpperCase() + description.slice(1).toLowerCase() : null; return description; } @@ -58,6 +60,17 @@ class DescriptionBuilder { return ''; } + async addAlchoholDescriptionIfExist(alcoholSlugs) { + console.log('alcoholSlugs', alcoholSlugs); + if (alcoholSlugs?.length > 0) { + const alcohols = await Database.collection('alcohol').find({ slug: { $in: alcoholSlugs } }).toArray(); + if (alcohols.length > 0) { + return ` з ${alcohols.map(t => this.capitalize(t.name)).join(', ')}`; + } + } + return ''; + } + async addTasteDescriptionIfExist(tasteSlugs) { if (tasteSlugs?.length > 0) { const tastes = await Database.collection('tastes').find({ slug: { $in: tasteSlugs } }).toArray(); @@ -68,7 +81,7 @@ class DescriptionBuilder { return ''; } - async addAlcoholDescriptionIfExist(alcoholSlugs) { + async addAlcoholVolumeDescriptionIfExist(alcoholSlugs) { if (alcoholSlugs?.length > 0) { const alcoholSlug = alcoholSlugs[0]; const alcoholVolume = await Database.collection('alcoholVolumes').findOne({ slug: alcoholSlug }); diff --git a/features/filters/rest.js b/features/filters/rest.js index 57256ad..5dfa2db 100644 --- a/features/filters/rest.js +++ b/features/filters/rest.js @@ -41,7 +41,7 @@ const filterDataCache = {}; router.get('/api/filters', async (req, res) => { if (filterDataCache.data) { - return res.status(200).send(filterDataCache.data); + // return res.status(200).send(filterDataCache.data); } const response = await getFiltersData(); filterDataCache.data = response; diff --git a/features/sitemap/rest.js b/features/sitemap/rest.js index e42cb70..794cd4e 100644 --- a/features/sitemap/rest.js +++ b/features/sitemap/rest.js @@ -45,6 +45,13 @@ async function getAllAlcoholVolumeSlug() { .toArray(); } +async function getAllAlcoholSlugs() { + return Database.collection('alcohol') + .find({}, { slug: 1 }) + .toArray(); +} + + router.get('/api/sitemap', async (req, res) => { const cocktails = (await getAllCocktailsSlug()).map((cocktail) => `cocktails/${cocktail.slug}`); const goods = (await getAllGoodsSlug()).map((good) => `goods/${good.slug}`); @@ -56,18 +63,20 @@ router.get('/api/sitemap', async (req, res) => { const tastsFilter = (await getAllTastesSlug()).map((tag) => `taste=${tag.slug}`); const alcoholVolumeFilter = (await getAllAlcoholVolumeSlug()).map((tag) => `alcohol-volume=${tag.slug}`); const glasswareFilter = (await getAllGlasswareSlug()).map((glass) => `glassware=${glass.slug}`); - + const alcoholFilter = (await getAllAlcoholSlugs()).map((alcohol) => `alcohol=${alcohol.slug}`); + const urls = cocktails .concat(goods) .concat(tools) .concat(glassware) - .concat(tagsFilter) - .concat(goodsFilter) - .concat(toolsFilter) - .concat(tastsFilter) - .concat(alcoholVolumeFilter) - .concat(glasswareFilter); + .concat(tagsFilter) + .concat(goodsFilter) + .concat(toolsFilter) + .concat(tastsFilter) + .concat(alcoholVolumeFilter) + .concat(glasswareFilter) + .concat(alcoholFilter); return res.status(200).send(urls); });