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

@@ -126,3 +126,29 @@ export const listByProject = query({
.collect();
},
});
export const getLatestByDataSource = query({
args: {
dataSourceId: v.id("dataSources"),
},
handler: async (ctx, args) => {
const userId = await getAuthUserId(ctx);
if (!userId) return null;
const dataSource = await ctx.db.get(args.dataSourceId);
if (!dataSource) return null;
const project = await ctx.db.get(dataSource.projectId);
if (!project || project.userId !== userId) return null;
const jobs = await ctx.db
.query("analysisJobs")
.withIndex("by_dataSource_createdAt", (q) =>
q.eq("dataSourceId", args.dataSourceId)
)
.order("desc")
.take(1);
return jobs[0] ?? null;
},
});

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;
},
});

View File

@@ -138,6 +138,7 @@ const schema = defineSchema({
opportunities: defineTable({
projectId: v.id("projects"),
analysisId: v.optional(v.id("analyses")),
searchJobId: v.optional(v.id("searchJobs")),
url: v.string(),
platform: v.string(),
title: v.string(),
@@ -145,6 +146,8 @@ const schema = defineSchema({
relevanceScore: v.number(),
intent: v.string(),
status: v.string(),
sentAt: v.optional(v.number()),
archivedAt: v.optional(v.number()),
suggestedApproach: v.string(),
matchedKeywords: v.array(v.string()),
matchedProblems: v.array(v.string()),
@@ -156,7 +159,14 @@ const schema = defineSchema({
})
.index("by_project_status", ["projectId", "status"])
.index("by_project_createdAt", ["projectId", "createdAt"])
.index("by_project_url", ["projectId", "url"]),
.index("by_project_url", ["projectId", "url"])
.index("by_project_searchJob", ["projectId", "searchJobId"]),
userActivity: defineTable({
userId: v.id("users"),
lastActiveDate: v.string(),
streak: v.number(),
updatedAt: v.number(),
}).index("by_user", ["userId"]),
seenUrls: defineTable({
projectId: v.id("projects"),
url: v.string(),
@@ -193,7 +203,8 @@ const schema = defineSchema({
updatedAt: v.number(),
})
.index("by_project_status", ["projectId", "status"])
.index("by_project_createdAt", ["projectId", "createdAt"]),
.index("by_project_createdAt", ["projectId", "createdAt"])
.index("by_dataSource_createdAt", ["dataSourceId", "createdAt"]),
searchJobs: defineTable({
projectId: v.id("projects"),
status: v.union(

View File

@@ -1,4 +1,5 @@
import { query } from "./_generated/server";
import { mutation, query } from "./_generated/server";
import { v } from "convex/values";
import { getAuthUserId } from "@convex-dev/auth/server";
export const getCurrent = query({
@@ -24,3 +25,61 @@ export const getCurrentProfile = query({
return { user, accounts };
},
});
export const touch = mutation({
args: {},
handler: async (ctx) => {
const userId = await getAuthUserId(ctx);
if (!userId) return null;
const today = new Date().toISOString().slice(0, 10);
const yesterday = new Date(Date.now() - 24 * 60 * 60 * 1000)
.toISOString()
.slice(0, 10);
const existing = await ctx.db
.query("userActivity")
.withIndex("by_user", (q) => q.eq("userId", userId))
.first();
if (!existing) {
await ctx.db.insert("userActivity", {
userId,
lastActiveDate: today,
streak: 1,
updatedAt: Date.now(),
});
return { lastActiveDate: today, streak: 1 };
}
if (existing.lastActiveDate === today) {
return { lastActiveDate: existing.lastActiveDate, streak: existing.streak };
}
const streak =
existing.lastActiveDate === yesterday ? existing.streak + 1 : 1;
await ctx.db.patch(existing._id, {
lastActiveDate: today,
streak,
updatedAt: Date.now(),
});
return { lastActiveDate: today, streak };
},
});
export const getActivity = query({
args: {},
handler: async (ctx) => {
const userId = await getAuthUserId(ctx);
if (!userId) return null;
const activity = await ctx.db
.query("userActivity")
.withIndex("by_user", (q) => q.eq("userId", userId))
.first();
return activity ?? null;
},
});