lots of changes
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { convexAuthNextjsToken, isAuthenticatedNextjs } from "@convex-dev/auth/nextjs/server";
|
||||
import { fetchMutation } from "convex/nextjs";
|
||||
import { fetchMutation, fetchQuery } from "convex/nextjs";
|
||||
import { api } from "@/convex/_generated/api";
|
||||
import { z } from 'zod'
|
||||
import { analyzeFromText } from '@/lib/scraper'
|
||||
@@ -146,9 +146,54 @@ export async function POST(request: NextRequest) {
|
||||
})
|
||||
}
|
||||
|
||||
let persisted = false
|
||||
if (jobId) {
|
||||
try {
|
||||
const job = await fetchQuery(
|
||||
api.analysisJobs.getById,
|
||||
{ jobId: jobId as any },
|
||||
{ token }
|
||||
)
|
||||
|
||||
if (job?.dataSourceId && job.projectId) {
|
||||
const existing = await fetchQuery(
|
||||
api.analyses.getLatestByDataSource,
|
||||
{ dataSourceId: job.dataSourceId as any },
|
||||
{ token }
|
||||
)
|
||||
if (!existing || existing.createdAt < job.createdAt) {
|
||||
await fetchMutation(
|
||||
api.analyses.createAnalysis,
|
||||
{
|
||||
projectId: job.projectId as any,
|
||||
dataSourceId: job.dataSourceId as any,
|
||||
analysis,
|
||||
},
|
||||
{ token }
|
||||
)
|
||||
}
|
||||
|
||||
await fetchMutation(
|
||||
api.dataSources.updateDataSourceStatus,
|
||||
{
|
||||
dataSourceId: job.dataSourceId as any,
|
||||
analysisStatus: "completed",
|
||||
lastError: undefined,
|
||||
lastAnalyzedAt: Date.now(),
|
||||
},
|
||||
{ token }
|
||||
)
|
||||
persisted = true
|
||||
}
|
||||
} catch (persistError) {
|
||||
console.error("Failed to persist manual analysis:", persistError)
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: analysis,
|
||||
persisted,
|
||||
stats: {
|
||||
features: analysis.features.length,
|
||||
keywords: analysis.keywords.length,
|
||||
@@ -177,6 +222,27 @@ export async function POST(request: NextRequest) {
|
||||
},
|
||||
{ token }
|
||||
);
|
||||
try {
|
||||
const job = await fetchQuery(
|
||||
api.analysisJobs.getById,
|
||||
{ jobId: jobId as any },
|
||||
{ token }
|
||||
)
|
||||
if (job?.dataSourceId) {
|
||||
await fetchMutation(
|
||||
api.dataSources.updateDataSourceStatus,
|
||||
{
|
||||
dataSourceId: job.dataSourceId as any,
|
||||
analysisStatus: "failed",
|
||||
lastError: error.message || "Manual analysis failed",
|
||||
lastAnalyzedAt: Date.now(),
|
||||
},
|
||||
{ token }
|
||||
)
|
||||
}
|
||||
} catch {
|
||||
// Best-effort data source update only.
|
||||
}
|
||||
} catch {
|
||||
// Best-effort job update only.
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user