lots of changes

This commit is contained in:
2026-02-04 11:18:33 +00:00
parent d02d95e680
commit 4fdbfb0fb3
30 changed files with 1796 additions and 822 deletions

View File

@@ -20,6 +20,7 @@ export const listByProject = query({
projectId: v.id("projects"),
status: v.optional(v.string()),
intent: v.optional(v.string()),
searchJobId: v.optional(v.id("searchJobs")),
minScore: v.optional(v.number()),
limit: v.optional(v.number()),
},
@@ -31,17 +32,23 @@ export const listByProject = query({
if (!project || project.userId !== userId) return [];
const limit = args.limit ?? 50;
let queryBuilder = args.status
let queryBuilder = args.searchJobId
? ctx.db
.query("opportunities")
.withIndex("by_project_status", (q) =>
q.eq("projectId", args.projectId).eq("status", args.status!)
.withIndex("by_project_searchJob", (q) =>
q.eq("projectId", args.projectId).eq("searchJobId", args.searchJobId!)
)
: ctx.db
.query("opportunities")
.withIndex("by_project_createdAt", (q) =>
q.eq("projectId", args.projectId)
);
: args.status
? ctx.db
.query("opportunities")
.withIndex("by_project_status", (q) =>
q.eq("projectId", args.projectId).eq("status", args.status!)
)
: ctx.db
.query("opportunities")
.withIndex("by_project_createdAt", (q) =>
q.eq("projectId", args.projectId)
);
if (args.intent) {
queryBuilder = queryBuilder.filter((q) =>
@@ -64,6 +71,7 @@ export const upsertBatch = mutation({
args: {
projectId: v.id("projects"),
analysisId: v.optional(v.id("analyses")),
searchJobId: v.optional(v.id("searchJobs")),
opportunities: v.array(opportunityInput),
},
handler: async (ctx, args) => {
@@ -90,6 +98,7 @@ export const upsertBatch = mutation({
if (existing) {
await ctx.db.patch(existing._id, {
analysisId: args.analysisId,
searchJobId: args.searchJobId,
platform: opp.platform,
title: opp.title,
snippet: opp.snippet,
@@ -106,6 +115,7 @@ export const upsertBatch = mutation({
await ctx.db.insert("opportunities", {
projectId: args.projectId,
analysisId: args.analysisId,
searchJobId: args.searchJobId,
url: opp.url,
platform: opp.platform,
title: opp.title,
@@ -169,11 +179,81 @@ export const updateStatus = mutation({
throw new Error("Project not found or unauthorized");
}
await ctx.db.patch(args.id, {
const now = Date.now();
const patch: Record<string, unknown> = {
status: args.status,
notes: args.notes,
tags: args.tags,
updatedAt: Date.now(),
});
updatedAt: now,
};
if (args.status === "sent") patch.sentAt = now;
if (args.status === "archived") patch.archivedAt = now;
await ctx.db.patch(args.id, patch);
},
});
export const countByDay = query({
args: {
projectId: v.id("projects"),
days: v.optional(v.number()),
metric: v.union(
v.literal("created"),
v.literal("sent"),
v.literal("archived")
),
},
handler: async (ctx, args) => {
const userId = await getAuthUserId(ctx);
if (!userId) return [];
const project = await ctx.db.get(args.projectId);
if (!project || project.userId !== userId) return [];
const days = args.days ?? 14;
const now = Date.now();
const start = now - days * 24 * 60 * 60 * 1000;
const results = await ctx.db
.query("opportunities")
.withIndex("by_project_createdAt", (q) => q.eq("projectId", args.projectId))
.order("desc")
.collect();
const counts = new Map<string, number>();
const toDateKey = (timestamp: number) =>
new Date(timestamp).toISOString().slice(0, 10);
for (const opp of results) {
let timestamp: number | null = null;
if (args.metric === "created") {
timestamp = opp.createdAt;
} else if (args.metric === "sent") {
timestamp =
opp.sentAt ??
(opp.status === "sent" || opp.status === "converted"
? opp.updatedAt
: null);
} else if (args.metric === "archived") {
timestamp =
opp.archivedAt ??
(opp.status === "archived" || opp.status === "ignored"
? opp.updatedAt
: null);
}
if (!timestamp || timestamp < start) continue;
const key = toDateKey(timestamp);
counts.set(key, (counts.get(key) ?? 0) + 1);
}
const series = [];
for (let i = days - 1; i >= 0; i -= 1) {
const date = new Date(now - i * 24 * 60 * 60 * 1000)
.toISOString()
.slice(0, 10);
series.push({ date, count: counts.get(date) ?? 0 });
}
return series;
},
});