74 lines
2.1 KiB
TypeScript
74 lines
2.1 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { getCallTasks, createCallTask, getUser } from '@/lib/firestore';
|
|
import { Timestamp } from 'firebase/firestore';
|
|
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
// TODO: Get userId from auth token
|
|
const userId = 'test-user-id'; // Replace with actual auth
|
|
const tasks = await getCallTasks(userId);
|
|
return NextResponse.json({ tasks });
|
|
} catch (error) {
|
|
console.error('Error fetching call tasks:', error);
|
|
return NextResponse.json({ error: 'Failed to fetch tasks' }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const body = await request.json();
|
|
const userId = 'test-user-id'; // TODO: Get from auth token
|
|
|
|
const {
|
|
businessName,
|
|
phoneNumber,
|
|
preferredLanguage,
|
|
callGoals,
|
|
scheduledAt,
|
|
timezone,
|
|
maxMinutes,
|
|
allowRecalls,
|
|
maxRecalls,
|
|
} = body;
|
|
|
|
if (!businessName || !phoneNumber || !callGoals || !scheduledAt || !maxMinutes) {
|
|
return NextResponse.json(
|
|
{ error: 'Missing required fields' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
// Validate user has enough credits
|
|
const user = await getUser(userId);
|
|
if (!user || user.creditBalance < maxMinutes) {
|
|
return NextResponse.json(
|
|
{ error: 'Insufficient credits' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const taskId = await createCallTask({
|
|
userId,
|
|
businessName,
|
|
phoneNumber,
|
|
preferredLanguage: preferredLanguage || 'English',
|
|
callGoals,
|
|
scheduledAt: Timestamp.fromDate(new Date(scheduledAt)),
|
|
timezone: timezone || undefined,
|
|
maxMinutes,
|
|
maxCredits: maxMinutes, // 1:1 for v1
|
|
allowRecalls: allowRecalls || false,
|
|
maxRecalls: maxRecalls || 0,
|
|
recallsUsed: 0,
|
|
status: 'pending',
|
|
createdAt: Timestamp.now(),
|
|
updatedAt: Timestamp.now(),
|
|
});
|
|
|
|
return NextResponse.json({ taskId });
|
|
} catch (error) {
|
|
console.error('Error creating call task:', error);
|
|
return NextResponse.json({ error: 'Failed to create task' }, { status: 500 });
|
|
}
|
|
}
|