Sync static opportunity filter query

This commit is contained in:
Admin 2026-09-29 18:34:19 +00:00
parent d3ef7c6ed2
commit 2658cf89cb

View File

@ -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 result = await pool.query(
`SELECT o.id,o.status,o.score,o.reasons,o.created_at,o.updated_at,
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
${where.length ? "WHERE " + where.join(" AND ") : ""}
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 $${values.length}`,
values
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(
`${base}
WHERE o.status <> 'closed'
ORDER BY o.created_at DESC
LIMIT $1`,
[limit]
);
return result.rows;
}