[object Object]

← back to Ffepurchasing

/api/facets: drill-down counts over the active filtered set (q/aesthetic/vendor) via shared filterProducts() — was counting whole niche catalog

4c34b57c6f68bc6dbd41dab26cb177425f79954d · 2026-06-01 07:27:55 -0700 · Steve Abrams

Files touched

Diff

commit 4c34b57c6f68bc6dbd41dab26cb177425f79954d
Author: Steve Abrams <steve@designerwallcoverings.com>
Date:   Mon Jun 1 07:27:55 2026 -0700

    /api/facets: drill-down counts over the active filtered set (q/aesthetic/vendor) via shared filterProducts() — was counting whole niche catalog
---
 server.js | 33 ++++++++++++++++++++++++---------
 1 file changed, 24 insertions(+), 9 deletions(-)

diff --git a/server.js b/server.js
index e297d0b..cf4aad0 100644
--- a/server.js
+++ b/server.js
@@ -110,15 +110,25 @@ app.use((req, res, next) => {
 
 app.use(express.static(path.join(__dirname, 'public')));
 
-app.get('/api/products', (req, res) => {
-  const { q, aesthetic, vendor, page = 1, limit = 24, sort = 'newest'} = req.query;
+// Shared filter — applied identically by /api/products and /api/facets so facet
+// counts reflect the ACTUAL filtered result set (drill-down), not the whole niche
+// catalog. Pass `skip` to exclude one dimension (used to compute that dimension's
+// own counts over the set narrowed by the OTHER active filters).
+function filterProducts(opts, skip) {
+  const { q, aesthetic, vendor } = opts || {};
   let list = PRODUCTS_NICHE;
-  if (q) {
-    const needle = q.toLowerCase();
+  if (q && skip !== 'q') {
+    const needle = String(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 (aesthetic && aesthetic !== 'all' && skip !== 'aesthetic') list = list.filter(p => p.aesthetic === aesthetic);
+  if (vendor && vendor !== 'all' && skip !== 'vendor') 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({ q, aesthetic, vendor });
   list = sortProducts(list, sort);
   const total = list.length;
   const pageNum = Math.max(1, parseInt(page) || 1);
@@ -138,13 +148,18 @@ app.get('/api/sliders', (req, res) => {
 });
 
 app.get('/api/facets', (req, res) => {
+  const { q, aesthetic, vendor } = req.query;
+  // Drill-down counts: each dimension is counted over the set narrowed by the
+  // OTHER active filters (skip its own value), and `total` reflects the fully
+  // filtered set — not the unfiltered niche catalog.
   const aesthetics = {}; const vendors = {};
-  for (const p of PRODUCTS_NICHE) {
+  for (const p of filterProducts({ q, aesthetic, vendor }, 'aesthetic')) {
     aesthetics[p.aesthetic] = (aesthetics[p.aesthetic] || 0) + 1;
+  }
+  for (const p of filterProducts({ q, aesthetic, vendor }, 'vendor')) {
     vendors[p.vendor] = (vendors[p.vendor] || 0) + 1;
   }
-  // total must match the niche-filtered list actually served, not the raw unfiltered array.
-  res.json({ aesthetics, vendors, total: PRODUCTS_NICHE.length });
+  res.json({ aesthetics, vendors, total: filterProducts({ q, aesthetic, vendor }).length });
 });
 
 app.get('/api/health', (req, res) => res.json({ status: 'ok', count: PRODUCTS_NICHE.length, dropped: DROPPED }));

← 02ead9b remove served backup file public/index.html.bak-202605261521  ·  back to Ffepurchasing  ·  noopener,noreferrer on sister-site constellation window.open 1dadf89 →