From 2658cf89cb50f6ad43030cafb26c4e5b5e14d654 Mon Sep 17 00:00:00 2001 From: Admin Date: Tue, 29 Sep 2026 18:34:19 +0000 Subject: [PATCH] Sync static opportunity filter query --- src/db.ts | 62 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 20 deletions(-) diff --git a/src/db.ts b/src/db.ts index 427a59e..f71dab8 100644 --- a/src/db.ts +++ b/src/db.ts @@ -802,29 +802,51 @@ export async function sourceStatus() { } export async function listOpportunities(args: { status?: string; since?: string; limit?: number }) { - const values: unknown[] = []; - const where: string[] = []; - if (args.status) { - values.push(args.status); - where.push(`o.status=${values.length}`); - } else { - where.push("o.status <> 'closed'"); - } - if (args.since) { - values.push(args.since); - where.push(`o.created_at >= $${values.length}::timestamptz`); - } const limit = Math.min(Math.max(Number(args.limit ?? 25), 1), 100); - values.push(limit); + const base = `SELECT o.id,o.status,o.score,o.reasons,o.created_at,o.updated_at, + s.id AS signal_id,s.source_id,s.title,s.summary,s.url,s.published_at,s.detected_at,s.topics + FROM opportunities o + JOIN signals s ON s.id=o.signal_id`; + + if (args.status && args.since) { + const result = await pool.query( + `${base} + WHERE o.status=$1 AND o.created_at >= $2::timestamptz + ORDER BY o.created_at DESC + LIMIT $3`, + [args.status, args.since, limit] + ); + return result.rows; + } + + if (args.status) { + const result = await pool.query( + `${base} + WHERE o.status=$1 + ORDER BY o.created_at DESC + LIMIT $2`, + [args.status, limit] + ); + return result.rows; + } + + if (args.since) { + const result = await pool.query( + `${base} + WHERE o.status <> 'closed' AND o.created_at >= $1::timestamptz + ORDER BY o.created_at DESC + LIMIT $2`, + [args.since, limit] + ); + return result.rows; + } + const result = await pool.query( - `SELECT o.id,o.status,o.score,o.reasons,o.created_at,o.updated_at, - s.id AS signal_id,s.source_id,s.title,s.summary,s.url,s.published_at,s.detected_at,s.topics - FROM opportunities o - JOIN signals s ON s.id=o.signal_id - ${where.length ? "WHERE " + where.join(" AND ") : ""} + `${base} + WHERE o.status <> 'closed' ORDER BY o.created_at DESC - LIMIT $${values.length}`, - values + LIMIT $1`, + [limit] ); return result.rows; }