payfrit-api/api/tasks/completeChat.php
John Mizerek 1f81d98c52 Initial PHP API migration from CFML
Complete port of all 163 API endpoints from Lucee/CFML to PHP 8.3.
Shared helpers in api/helpers.php (DB, auth, request/response, security).
PDO prepared statements throughout. Same JSON response shapes as CFML.
2026-03-14 14:26:59 -07:00

46 lines
1.3 KiB
PHP

<?php
require_once __DIR__ . '/../helpers.php';
runAuth();
/**
* Complete/close a chat task (TaskTypeID = 2 only)
* POST: { TaskID: int }
*/
$data = readJsonBody();
$taskID = (int) ($data['TaskID'] ?? 0);
if ($taskID <= 0) {
apiAbort(['OK' => false, 'ERROR' => 'missing_params', 'MESSAGE' => 'TaskID is required.']);
}
try {
$qTask = queryOne("
SELECT ID, ClaimedByUserID, CompletedOn, OrderID, TaskTypeID
FROM Tasks WHERE ID = ?
", [$taskID]);
if (!$qTask) {
apiAbort(['OK' => false, 'ERROR' => 'not_found', 'MESSAGE' => 'Task not found.']);
}
if ((int) $qTask['TaskTypeID'] !== 2) {
apiAbort(['OK' => false, 'ERROR' => 'not_chat', 'MESSAGE' => 'This endpoint is only for chat tasks.']);
}
if (!empty(trim($qTask['CompletedOn'] ?? ''))) {
apiAbort(['OK' => false, 'ERROR' => 'already_completed', 'MESSAGE' => 'Chat has already been closed.']);
}
queryTimed("UPDATE Tasks SET CompletedOn = NOW() WHERE ID = ?", [$taskID]);
jsonResponse([
'OK' => true,
'ERROR' => '',
'MESSAGE' => 'Chat closed successfully.',
'TaskID' => $taskID,
]);
} catch (Exception $e) {
jsonResponse(['OK' => false, 'ERROR' => 'server_error', 'MESSAGE' => 'Error closing chat', 'DETAIL' => $e->getMessage()]);
}