Initial commit
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
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 });
|
||||
}
|
||||
Reference in New Issue
Block a user