tenantpilot/app/api/policy-sync/status/route.ts

52 lines
1.3 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server';
import { getUserAuth } from '@/lib/auth/utils';
import { syncQueue } from '@/lib/queue/syncQueue';
export async function GET(request: NextRequest) {
try {
const { session } = await getUserAuth();
if (!session?.user) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const { searchParams } = new URL(request.url);
const jobId = searchParams.get('jobId');
if (!jobId) {
return NextResponse.json({ error: 'Job ID required' }, { status: 400 });
}
// Get job from BullMQ
const job = await syncQueue.getJob(jobId);
if (!job) {
return NextResponse.json({ error: 'Job not found' }, { status: 404 });
}
// Get job state
const state = await job.getState();
const progress = job.progress;
const returnValue = job.returnvalue;
const failedReason = job.failedReason;
return NextResponse.json({
jobId: job.id,
state,
progress,
data: job.data,
result: returnValue,
failedReason,
timestamp: job.timestamp,
processedOn: job.processedOn,
finishedOn: job.finishedOn,
});
} catch (error) {
console.error('Error fetching job status:', error);
return NextResponse.json(
{ error: 'Failed to fetch job status' },
{ status: 500 }
);
}
}