Files
holacompi/app/api/cron/scheduled-calls/route.ts
T
Haitham Khalifa b538d84e17 Initial commit
2026-02-16 12:18:06 +01:00

37 lines
1.1 KiB
TypeScript

import { NextResponse } from 'next/server';
import { collection, getDocs, getFirestore, query, where, Timestamp } from 'firebase/firestore';
import { app } from '@/lib/firebase';
export async function GET(request: Request) {
// TODO: Secure this route using a secret header for Vercel Cron.
const db = getFirestore(app);
const now = Timestamp.now();
// TODO: Add index for scheduledAt + status.
const tasksQuery = query(
collection(db, 'callTasks'),
where('status', '==', 'pending'),
where('scheduledAt', '<=', now)
);
const snapshot = await getDocs(tasksQuery);
const origin = request.headers.get('origin') ?? 'http://localhost:3000';
for (const docSnap of snapshot.docs) {
const task = docSnap.data() as any;
// Trigger the call (TODO: include auth)
await fetch(`${origin}/api/vapi/create-call`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
phoneNumber: task.phoneNumber,
userId: task.userId,
scheduledTaskId: docSnap.id,
}),
});
}
return NextResponse.json({ processed: snapshot.size });
}