37243-vm/api/ai-call-logs.php
2026-01-02 22:45:22 +00:00

64 lines
1.9 KiB
PHP

<?php
require_once __DIR__ . '/config.php';
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
sendJsonResponse(['error' => 'Invalid request method'], 405);
exit;
}
if (!validateApiKey()) {
logWebhook('ai-call-logs', file_get_contents('php://input'), 401);
sendJsonResponse(['error' => 'Unauthorized'], 401);
exit;
}
$request_body = file_get_contents('php://input');
$data = json_decode($request_body, true);
if (json_last_error() !== JSON_ERROR_NONE) {
logWebhook('ai-call-logs', $request_body, 400);
sendJsonResponse(['error' => 'Invalid JSON'], 400);
exit;
}
$errors = [];
if (empty($data['call_id'])) {
$errors[] = 'call_id is required';
}
if (empty($data['call_start_time'])) {
$errors[] = 'call_start_time is required';
}
if (empty($data['call_intent'])) {
$errors[] = 'call_intent is required';
}
if (empty($data['call_outcome'])) {
$errors[] = 'call_outcome is required';
}
if (!empty($errors)) {
logWebhook('ai-call-logs', $request_body, 422);
sendJsonResponse(['errors' => $errors], 422);
exit;
}
try {
$stmt = db()->prepare("INSERT INTO ai_call_logs (call_id, conversation_id, call_start_time, call_end_time, call_duration_seconds, call_intent, call_outcome, ai_summary) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->execute([
$data['call_id'],
$data['conversation_id'] ?? null,
$data['call_start_time'],
$data['call_end_time'] ?? null,
$data['call_duration_seconds'] ?? null,
$data['call_intent'],
$data['call_outcome'],
$data['ai_summary'] ?? null
]);
$new_id = db()->lastInsertId();
logWebhook('ai-call-logs', $request_body, 201);
sendJsonResponse(['success' => true, 'id' => $new_id, 'message' => 'Call log created'], 201);
} catch (PDOException $e) {
error_log($e->getMessage());
logWebhook('ai-call-logs', $request_body, 500);
sendJsonResponse(['error' => 'Database error'], 500);
}