payfrit-api/api/chat/getActiveChat.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

61 lines
1.6 KiB
PHP

<?php
require_once __DIR__ . '/../helpers.php';
runAuth();
/**
* Check for an active (uncompleted) chat task at a service point
* POST: { BusinessID, ServicePointID }
*/
$data = readJsonBody();
$businessID = (int) ($data['BusinessID'] ?? 0);
$servicePointID = (int) ($data['ServicePointID'] ?? 0);
if ($businessID <= 0) {
apiAbort(['OK' => false, 'ERROR' => 'missing_params', 'MESSAGE' => 'BusinessID is required']);
}
if ($servicePointID <= 0) {
jsonResponse([
'OK' => true,
'HAS_ACTIVE_CHAT' => false,
'TASK_ID' => 0,
'TASK_TITLE' => '',
]);
}
try {
$qChat = queryOne("
SELECT t.ID, t.Title,
(SELECT MAX(cm.CreatedOn) FROM ChatMessages cm WHERE cm.TaskID = t.ID) as LastMessageTime
FROM Tasks t
WHERE t.BusinessID = ?
AND t.TaskTypeID = 2
AND t.CompletedOn IS NULL
AND t.SourceType = 'servicepoint'
AND t.SourceID = ?
ORDER BY
CASE WHEN t.ClaimedByUserID > 0 THEN 0 ELSE 1 END,
t.CreatedOn DESC
LIMIT 1
", [$businessID, $servicePointID]);
if ($qChat) {
jsonResponse([
'OK' => true,
'HAS_ACTIVE_CHAT' => true,
'TASK_ID' => (int) $qChat['ID'],
'TASK_TITLE' => $qChat['Title'],
]);
} else {
jsonResponse([
'OK' => true,
'HAS_ACTIVE_CHAT' => false,
'TASK_ID' => 0,
'TASK_TITLE' => '',
]);
}
} catch (Exception $e) {
jsonResponse(['OK' => false, 'ERROR' => 'server_error', 'MESSAGE' => $e->getMessage()]);
}