Initial commit

This commit is contained in:
Haitham Khalifa
2026-02-16 12:18:06 +01:00
commit b538d84e17
63 changed files with 15059 additions and 0 deletions
+75
View File
@@ -0,0 +1,75 @@
import { NextRequest, NextResponse } from 'next/server';
import {
deleteCallTask,
getCallTask,
updateCallTask,
} from '@/lib/firestore';
import { Timestamp } from 'firebase/firestore';
export async function GET(
request: NextRequest,
{ params }: { params: { id: string } }
) {
try {
// TODO: Get userId from auth token
const userId = 'test-user-id'; // Replace with actual auth
const task = await getCallTask(params.id);
if (!task || task.userId !== userId) {
return NextResponse.json({ error: 'Task not found' }, { status: 404 });
}
return NextResponse.json({ task });
} catch (error) {
console.error('Error fetching call task:', error);
return NextResponse.json({ error: 'Failed to fetch task' }, { status: 500 });
}
}
export async function PATCH(
request: NextRequest,
{ params }: { params: { id: string } }
) {
try {
const body = await request.json();
// TODO: Get userId from auth token
const userId = 'test-user-id'; // Replace with actual auth
const existing = await getCallTask(params.id);
if (!existing || existing.userId !== userId) {
return NextResponse.json({ error: 'Task not found' }, { status: 404 });
}
const updates: Record<string, unknown> = { ...body };
if (updates.scheduledAt) {
updates.scheduledAt = Timestamp.fromDate(new Date(updates.scheduledAt as string));
}
await updateCallTask(params.id, updates);
const task = await getCallTask(params.id);
return NextResponse.json({ task });
} catch (error) {
console.error('Error updating call task:', error);
return NextResponse.json({ error: 'Failed to update task' }, { status: 500 });
}
}
export async function DELETE(
request: NextRequest,
{ params }: { params: { id: string } }
) {
try {
// TODO: Get userId from auth token
const userId = 'test-user-id'; // Replace with actual auth
const existing = await getCallTask(params.id);
if (!existing || existing.userId !== userId) {
return NextResponse.json({ error: 'Task not found' }, { status: 404 });
}
await deleteCallTask(params.id);
return NextResponse.json({ success: true });
} catch (error) {
console.error('Error deleting call task:', error);
return NextResponse.json({ error: 'Failed to delete task' }, { status: 500 });
}
}
+73
View File
@@ -0,0 +1,73 @@
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 });
}
}