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:
parent
b720afc92c
commit
f067c0999d
@ -1,4 +1,4 @@
|
|||||||
import { Router, Request, Response } from "express";
|
import { Router, Request, Response, NextFunction } from "express";
|
||||||
import { pool } from "../db/client";
|
import { pool } from "../db/client";
|
||||||
import { listVendors } from "../db/queries";
|
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
|
// 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 {
|
try {
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
const result = await pool.query(
|
const result = await pool.query(
|
||||||
@ -181,8 +182,8 @@ vendorRouter.get("/market-share", async (req: Request, res: Response) => {
|
|||||||
v.name AS vendor_name,
|
v.name AS vendor_name,
|
||||||
v.type,
|
v.type,
|
||||||
COUNT(DISTINCT po.transceiver_id)::int AS sku_count,
|
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,
|
ROUND((COUNT(DISTINCT po.transceiver_id)::numeric / NULLIF(t.total,0)::numeric) * 100, 1) AS market_share_pct,
|
||||||
COUNT(po.id)::int AS total_obs,
|
COUNT(*)::int AS total_obs,
|
||||||
MAX(po.time) AS last_seen
|
MAX(po.time) AS last_seen
|
||||||
FROM price_observations po
|
FROM price_observations po
|
||||||
JOIN vendors v ON v.id = po.source_vendor_id
|
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,
|
(c.sku_count - COALESCE(p.sku_count, 0)) AS delta_skus,
|
||||||
CASE
|
CASE
|
||||||
WHEN COALESCE(p.sku_count, 0) = 0 THEN NULL
|
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
|
END AS delta_pct
|
||||||
FROM cur c
|
FROM cur c
|
||||||
JOIN vendors v ON v.id = c.source_vendor_id
|
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.type,
|
||||||
v.website,
|
v.website,
|
||||||
COUNT(DISTINCT po.transceiver_id)::int AS sku_count,
|
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(AVG(po.price)::numeric, 2) AS avg_price,
|
||||||
ROUND(MIN(po.price)::numeric, 2) AS min_price,
|
ROUND(MIN(po.price)::numeric, 2) AS min_price,
|
||||||
ROUND(MAX(po.price)::numeric, 2) AS max_price,
|
ROUND(MAX(po.price)::numeric, 2) AS max_price,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user