fix(vendors): reach /market-share + /intelligence (next() fallthrough past /:id) + fix SQL (numeric casts for ROUND division, COUNT(*) not non-existent po.id)

This commit is contained in:
Rene Fichtmueller 2026-06-04 20:58:12 +00:00
parent b720afc92c
commit f067c0999d

View File

@ -1,4 +1,4 @@
import { Router, Request, Response } from "express";
import { Router, Request, Response, NextFunction } from "express";
import { pool } from "../db/client";
import { listVendors } from "../db/queries";
@ -114,7 +114,8 @@ vendorRouter.post("/", async (req: Request, res: Response) => {
});
// GET /api/vendors/:id — Get single vendor with full stats
vendorRouter.get("/:id", async (req: Request, res: Response) => {
vendorRouter.get("/:id", async (req: Request, res: Response, next: NextFunction) => {
if (req.params.id === "market-share" || req.params.id === "intelligence") return next();
try {
const { id } = req.params;
const result = await pool.query(
@ -181,8 +182,8 @@ vendorRouter.get("/market-share", async (req: Request, res: Response) => {
v.name AS vendor_name,
v.type,
COUNT(DISTINCT po.transceiver_id)::int AS sku_count,
ROUND((COUNT(DISTINCT po.transceiver_id)::numeric / NULLIF(t.total,0)) * 100, 1) AS market_share_pct,
COUNT(po.id)::int AS total_obs,
ROUND((COUNT(DISTINCT po.transceiver_id)::numeric / NULLIF(t.total,0)::numeric) * 100, 1) AS market_share_pct,
COUNT(*)::int AS total_obs,
MAX(po.time) AS last_seen
FROM price_observations po
JOIN vendors v ON v.id = po.source_vendor_id
@ -228,7 +229,7 @@ vendorRouter.get("/market-share", async (req: Request, res: Response) => {
(c.sku_count - COALESCE(p.sku_count, 0)) AS delta_skus,
CASE
WHEN COALESCE(p.sku_count, 0) = 0 THEN NULL
ELSE ROUND(((c.sku_count - p.sku_count)::numeric / p.sku_count) * 100, 1)
ELSE ROUND(((c.sku_count - p.sku_count)::numeric / p.sku_count::numeric) * 100, 1)
END AS delta_pct
FROM cur c
JOIN vendors v ON v.id = c.source_vendor_id
@ -273,7 +274,7 @@ vendorRouter.get("/intelligence", async (_req: Request, res: Response) => {
v.type,
v.website,
COUNT(DISTINCT po.transceiver_id)::int AS sku_count,
COUNT(po.id)::int AS price_obs,
COUNT(*)::int AS price_obs,
ROUND(AVG(po.price)::numeric, 2) AS avg_price,
ROUND(MIN(po.price)::numeric, 2) AS min_price,
ROUND(MAX(po.price)::numeric, 2) AS max_price,