[object Object]

← back to 1960swallpaper

fix(api): /api/facets counts reflect filtered result set (skip own dimension)

7ae3889a69ad87b64a9a616be36a5328f0b2d1cb · 2026-06-01 07:13:49 -0700 · Steve

Files touched

Diff

commit 7ae3889a69ad87b64a9a616be36a5328f0b2d1cb
Author: Steve <steve@designerwallcoverings.com>
Date:   Mon Jun 1 07:13:49 2026 -0700

    fix(api): /api/facets counts reflect filtered result set (skip own dimension)
---
 server.js | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)

diff --git a/server.js b/server.js
index 8fe37a2..60d76e7 100644
--- a/server.js
+++ b/server.js
@@ -164,15 +164,22 @@ for (const [route, file] of Object.entries(PAGE_ROUTES)) {
   app.get('/' + route, (req, res) => res.sendFile(path.join(__dirname, 'public', file)));
 }
 
-app.get('/api/products', (req, res) => {
-  const { q, aesthetic, vendor, page = 1, limit = 24, sort = 'newest'} = req.query;
-  let list = PRODUCTS_NICHE;
+// Shared product filter — q (title/tags substring), aesthetic, vendor.
+// `skip` lets facet counts exclude their own dimension so selecting one value
+// in a facet doesn't zero out its sibling counts.
+function filterProducts(list, { q, aesthetic, vendor } = {}, skip) {
   if (q) {
     const needle = q.toLowerCase();
     list = list.filter(p => (p.title || '').toLowerCase().includes(needle) || (p.tags || []).some(t => t.toLowerCase().includes(needle)));
   }
-  if (aesthetic && aesthetic !== 'all') list = list.filter(p => p.aesthetic === aesthetic);
-  if (vendor && vendor !== 'all') list = list.filter(p => p.vendor === vendor);
+  if (skip !== 'aesthetic' && aesthetic && aesthetic !== 'all') list = list.filter(p => p.aesthetic === aesthetic);
+  if (skip !== 'vendor' && vendor && vendor !== 'all') list = list.filter(p => p.vendor === vendor);
+  return list;
+}
+
+app.get('/api/products', (req, res) => {
+  const { q, aesthetic, vendor, page = 1, limit = 24, sort = 'newest'} = req.query;
+  let list = filterProducts(PRODUCTS_NICHE, { q, aesthetic, vendor });
   list = sortProducts(list, sort);
   const total = list.length;
   const pageNum = Math.max(1, parseInt(page) || 1);
@@ -192,12 +199,19 @@ app.get('/api/sliders', (req, res) => {
 });
 
 app.get('/api/facets', (req, res) => {
+  const { q, aesthetic, vendor } = req.query;
   const aesthetics = {}; const vendors = {};
-  for (const p of PRODUCTS_NICHE) {
+  // Each dimension counts against the OTHER active filters (skip its own) so a
+  // selection in one facet doesn't zero out that facet's sibling values.
+  for (const p of filterProducts(PRODUCTS_NICHE, { q, aesthetic, vendor }, 'aesthetic')) {
     aesthetics[p.aesthetic] = (aesthetics[p.aesthetic] || 0) + 1;
+  }
+  for (const p of filterProducts(PRODUCTS_NICHE, { q, aesthetic, vendor }, 'vendor')) {
     vendors[p.vendor] = (vendors[p.vendor] || 0) + 1;
   }
-  res.json({ aesthetics, vendors, total: PRODUCTS_NICHE.length });
+  // `total` reflects the fully-filtered result set.
+  const total = filterProducts(PRODUCTS_NICHE, { q, aesthetic, vendor }).length;
+  res.json({ aesthetics, vendors, total });
 });
 
 app.get('/api/health', (req, res) => res.json({ status: 'ok', count: PRODUCTS_NICHE.length, dropped: DROPPED }));

← a2788ec fix: remove public bak file leak + fix 404-guard regex + git  ·  back to 1960swallpaper  ·  fix(security): add noopener,noreferrer to window.open sister cefd88a →