Эх сурвалжийг харах

refactor: use chatbot_api.catalog_images stored function for product images

Replace raw SQL in /catalog/images endpoint with chatbot_api.catalog_images($1, $2).
Add erp-bridge/sql/chatbot_api_catalog_images.sql with function definition and GRANT.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Tony T 5 өдөр өмнө
parent
commit
9ba101ba40

+ 6 - 33
erp-bridge/main.py

@@ -307,42 +307,15 @@ async def catalog_images(
 ) -> dict[str, Any]:
     """
     Return product images for a given model number.
-    Fetches the image base URL from public.config and joins with shop picture tables.
+    Delegates to chatbot_api.catalog_images which reads File_Url from public.config
+    and joins shop.shop_product_picture with catalog.
     Returns: { base_url, images: [{ model, apppicture_path, full_url }] }
     """
-    assert DB_POOL is not None
-
-    # Get image server base URL from config
-    base_url_row = await DB_POOL.fetchrow(
-        "SELECT item_value FROM public.config WHERE item = 'File_Url' LIMIT 1"
+    result = await call_single(
+        "SELECT chatbot_api.catalog_images($1, $2)",
+        req.model, req.limit,
     )
-    base_url: str = (base_url_row["item_value"] if base_url_row else "https://www.homelegance.com/").rstrip("/")
-
-    # Fetch image paths for the requested model
-    rows = await DB_POOL.fetch(
-        """
-        SELECT c.model, spp.apppicture_path
-        FROM shop.shop_product_picture spp
-        JOIN shop.shop_product sp ON spp.product_id = sp.product_id
-        JOIN public.catalog c ON sp.caf_serial_no = c.serial_no
-        WHERE c.model ILIKE $1
-        LIMIT $2
-        """,
-        f"%{req.model}%",
-        req.limit,
-    )
-
-    images = [
-        {
-            "model": row["model"],
-            "apppicture_path": row["apppicture_path"],
-            "full_url": f"{base_url}{row['apppicture_path']}",
-        }
-        for row in rows
-        if row["apppicture_path"]
-    ]
-
-    return {"base_url": base_url, "images": images}
+    return result or {"base_url": "", "images": []}
 
 
 # ──────────────────────────────────────────────────────────────────────────────

+ 42 - 0
erp-bridge/sql/chatbot_api_catalog_images.sql

@@ -0,0 +1,42 @@
+-- chatbot_api.catalog_images
+-- Returns product image URLs for a given model pattern.
+-- base_url is read from public.config where item = 'File_Url'.
+-- full_url = base_url || apppicture_path
+
+CREATE OR REPLACE FUNCTION chatbot_api.catalog_images(
+    p_model TEXT,
+    p_limit INT DEFAULT 10
+)
+RETURNS JSON
+LANGUAGE sql
+STABLE
+AS $$
+    SELECT json_build_object(
+        'base_url', (
+            SELECT item_value FROM public.config WHERE item = 'File_Url' LIMIT 1
+        ),
+        'images', COALESCE(
+            (
+                SELECT json_agg(row)
+                FROM (
+                    SELECT json_build_object(
+                        'model',           c.model,
+                        'apppicture_path', spp.apppicture_path,
+                        'full_url',        (
+                            SELECT item_value FROM public.config WHERE item = 'File_Url' LIMIT 1
+                        ) || spp.apppicture_path
+                    ) AS row
+                    FROM shop.shop_product_picture spp
+                    JOIN shop.shop_product sp ON spp.product_id = sp.product_id
+                    JOIN public.catalog c ON sp.caf_serial_no = c.serial_no
+                    WHERE c.model ILIKE '%' || p_model || '%'
+                      AND spp.apppicture_path IS NOT NULL
+                    LIMIT p_limit
+                ) sub
+            ),
+            '[]'::json
+        )
+    );
+$$;
+
+GRANT EXECUTE ON FUNCTION chatbot_api.catalog_images(TEXT, INT) TO chatbot_readonly;