[object Object]

← back to Wallco Ai

fix: regen-with-comment + ghosting endpoints fall back to DESIGNS in-memory cache when design isn't in spoon_all_designs PG table — file-backed designs (fliepaper-bugs, JSON-only) were failing with 'parent design not found'

cfe794d3521b0b6f14e9334fef444fcaa06de481 · 2026-05-20 10:52:09 -0700 · Steve Abrams

Files touched

Diff

commit cfe794d3521b0b6f14e9334fef444fcaa06de481
Author: Steve Abrams <steve@designerwallcoverings.com>
Date:   Wed May 20 10:52:09 2026 -0700

    fix: regen-with-comment + ghosting endpoints fall back to DESIGNS in-memory cache when design isn't in spoon_all_designs PG table — file-backed designs (fliepaper-bugs, JSON-only) were failing with 'parent design not found'
---
 server.js | 54 +++++++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 45 insertions(+), 9 deletions(-)

diff --git a/server.js b/server.js
index 29b5283..5feacee 100644
--- a/server.js
+++ b/server.js
@@ -6360,10 +6360,29 @@ app.post('/api/design/:id/regenerate-with-comment', express.json({ limit: '8kb'
   if (!Number.isFinite(id) || id < 1) return res.status(400).json({ error: 'bad id' });
   if (!comment) return res.status(400).json({ error: 'comment required' });
   try {
-    const parentRow = psqlQuery(`SELECT row_to_json(t) FROM (
-      SELECT category, prompt, width_in, height_in FROM spoon_all_designs WHERE id=${id}) t;`);
-    if (!parentRow) return res.status(404).json({ error: 'parent design not found' });
-    const parent = JSON.parse(parentRow);
+    // Source of truth precedence: spoon_all_designs (canonical DB) → DESIGNS
+    // in-memory cache (designs.json — file-backed designs not yet in PG).
+    // Designs generated by file-backed pipelines (fliepaper-bugs, etc) only
+    // live in the JSON file, not PG, so the PG-only query falsely returned
+    // 'not found' for any JSON-only design.
+    let parent = null;
+    try {
+      const pgRaw = psqlQuery(`SELECT row_to_json(t) FROM (
+        SELECT category, prompt, width_in, height_in FROM spoon_all_designs WHERE id=${id}) t;`);
+      if (pgRaw && pgRaw.length > 2) parent = JSON.parse(pgRaw);
+    } catch { /* PG miss is non-fatal — fall through to cache */ }
+    if (!parent) {
+      const cached = DESIGNS.find(x => x.id === id);
+      if (cached) {
+        parent = {
+          category: cached.category,
+          prompt:   cached.prompt,
+          width_in: cached.width_in,
+          height_in: cached.height_in,
+        };
+      }
+    }
+    if (!parent) return res.status(404).json({ error: 'parent design not found in PG or cache' });
     const baseCategory = parent.category || 'mixed';
     const w = parent.width_in || 36;
     const h = parent.height_in || 36;
@@ -6592,11 +6611,28 @@ app.post('/api/design/:id/ghosting', express.json({ limit: '16kb' }), (req, res)
   if (!['update-sku', 'delete'].includes(action)) return res.status(400).json({ error: "action must be 'update-sku' or 'delete'" });
 
   try {
-    const raw = psqlQuery(`SELECT row_to_json(t) FROM (SELECT id, prompt, category,
-      motifs, tags, generator, dominant_hex, local_path, parent_design_id, dig_number
-      FROM spoon_all_designs WHERE id=${id}) t;`);
-    if (!raw) return res.status(404).json({ error: 'design not found' });
-    const d = JSON.parse(raw);
+    // PG → JSON-cache fallback (same as regenerate-with-comment) so file-backed
+    // designs (fliepaper-bugs etc) can also be ghosted.
+    let d = null;
+    try {
+      const pgRaw = psqlQuery(`SELECT row_to_json(t) FROM (SELECT id, prompt, category,
+        motifs, tags, generator, dominant_hex, local_path, parent_design_id, dig_number
+        FROM spoon_all_designs WHERE id=${id}) t;`);
+      if (pgRaw && pgRaw.length > 2) d = JSON.parse(pgRaw);
+    } catch { /* PG miss is non-fatal */ }
+    if (!d) {
+      const cached = DESIGNS.find(x => x.id === id);
+      if (cached) {
+        d = {
+          id: cached.id, prompt: cached.prompt, category: cached.category,
+          motifs: cached.motifs || [], tags: cached.tags || [],
+          generator: cached.generator, dominant_hex: cached.dominant_hex,
+          local_path: cached.local_path, parent_design_id: cached.parent_design_id,
+          dig_number: cached.dig_number,
+        };
+      }
+    }
+    if (!d) return res.status(404).json({ error: 'design not found in PG or cache' });
 
     // Backup file forensically before we touch anything
     const backupDir = path.join(require('os').homedir(), '.wallco-deleted', new Date().toISOString().slice(0,10));

← 07d3dc2 add: 'Ghosting' button on /design/:id admin rating panel — p  ·  back to Wallco Ai  ·  hide Ghosting button when MURAL mode (level 0) is selected — 5f15c01 →