Sync static opportunity filter query
This commit is contained in:
parent
d3ef7c6ed2
commit
2658cf89cb
62
src/db.ts
62
src/db.ts
@ -802,29 +802,51 @@ export async function sourceStatus() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function listOpportunities(args: { status?: string; since?: string; limit?: number }) {
|
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);
|
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(
|
const result = await pool.query(
|
||||||
`SELECT o.id,o.status,o.score,o.reasons,o.created_at,o.updated_at,
|
`${base}
|
||||||
s.id AS signal_id,s.source_id,s.title,s.summary,s.url,s.published_at,s.detected_at,s.topics
|
WHERE o.status <> 'closed'
|
||||||
FROM opportunities o
|
|
||||||
JOIN signals s ON s.id=o.signal_id
|
|
||||||
${where.length ? "WHERE " + where.join(" AND ") : ""}
|
|
||||||
ORDER BY o.created_at DESC
|
ORDER BY o.created_at DESC
|
||||||
LIMIT $${values.length}`,
|
LIMIT $1`,
|
||||||
values
|
[limit]
|
||||||
);
|
);
|
||||||
return result.rows;
|
return result.rows;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user