feat: Implement data source management and analysis flow, allowing users to add and analyze websites for project opportunities.
This commit is contained in:
@@ -52,24 +52,68 @@ export const addDataSource = mutation({
|
||||
}
|
||||
}
|
||||
|
||||
const sourceId = await ctx.db.insert("dataSources", {
|
||||
projectId: projectId!, // Assert exists
|
||||
type: args.type,
|
||||
url: args.url,
|
||||
name: args.name,
|
||||
analysisStatus: "pending",
|
||||
// analysisResults not set initially
|
||||
});
|
||||
const existing = await ctx.db
|
||||
.query("dataSources")
|
||||
.withIndex("by_project_url", (q) =>
|
||||
q.eq("projectId", projectId!).eq("url", args.url)
|
||||
)
|
||||
.first();
|
||||
|
||||
const sourceId = existing
|
||||
? existing._id
|
||||
: await ctx.db.insert("dataSources", {
|
||||
projectId: projectId!, // Assert exists
|
||||
type: args.type,
|
||||
url: args.url,
|
||||
name: args.name,
|
||||
analysisStatus: "pending",
|
||||
lastAnalyzedAt: undefined,
|
||||
lastError: undefined,
|
||||
// analysisResults not set initially
|
||||
});
|
||||
|
||||
// Auto-select this source in the project config
|
||||
const project = await ctx.db.get(projectId!);
|
||||
if (project) {
|
||||
const currentSelected = project.dorkingConfig.selectedSourceIds;
|
||||
await ctx.db.patch(projectId!, {
|
||||
dorkingConfig: { selectedSourceIds: [...currentSelected, sourceId] }
|
||||
});
|
||||
if (!currentSelected.includes(sourceId)) {
|
||||
await ctx.db.patch(projectId!, {
|
||||
dorkingConfig: { selectedSourceIds: [...currentSelected, sourceId] }
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return { sourceId, projectId: projectId! };
|
||||
},
|
||||
});
|
||||
|
||||
export const updateDataSourceStatus = mutation({
|
||||
args: {
|
||||
dataSourceId: v.id("dataSources"),
|
||||
analysisStatus: v.union(
|
||||
v.literal("pending"),
|
||||
v.literal("completed"),
|
||||
v.literal("failed")
|
||||
),
|
||||
lastError: v.optional(v.string()),
|
||||
lastAnalyzedAt: v.optional(v.number()),
|
||||
},
|
||||
handler: async (ctx, args) => {
|
||||
const userId = await getAuthUserId(ctx);
|
||||
if (!userId) throw new Error("Unauthorized");
|
||||
|
||||
const dataSource = await ctx.db.get(args.dataSourceId);
|
||||
if (!dataSource) throw new Error("Data source not found");
|
||||
|
||||
const project = await ctx.db.get(dataSource.projectId);
|
||||
if (!project || project.userId !== userId) {
|
||||
throw new Error("Project not found or unauthorized");
|
||||
}
|
||||
|
||||
await ctx.db.patch(args.dataSourceId, {
|
||||
analysisStatus: args.analysisStatus,
|
||||
lastError: args.lastError,
|
||||
lastAnalyzedAt: args.lastAnalyzedAt,
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user