payfrit-api/cron/expireStaleChats.php
John Mizerek 4d806d4e1e Port admin, cron, and receipt endpoints from CFML to PHP
- admin/quickTasks: list, create, save, delete
- admin/scheduledTasks: list, save, delete, toggle, run, runDue
- cron: expireStaleChats, expireTabs
- receipt: public order receipt page (no auth, UUID-secured)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-14 15:57:25 -07:00

53 lines
1.6 KiB
PHP

<?php
require_once __DIR__ . '/../api/helpers.php';
// No runAuth() — cron/public endpoint
/**
* Expire stale chats (older than 20 minutes with no recent activity).
* Called every minute by cron.
*/
try {
$staleChats = queryTimed("
SELECT t.ID, t.CreatedOn,
(SELECT MAX(cm.CreatedOn) FROM ChatMessages cm WHERE cm.TaskID = t.ID) AS LastMessageOn
FROM Tasks t
WHERE t.TaskTypeID = 2
AND t.CompletedOn IS NULL
AND t.CreatedOn < DATE_SUB(NOW(), INTERVAL 20 MINUTE)
");
$expiredCount = 0;
$expiredIds = [];
foreach ($staleChats as $chat) {
$shouldExpire = false;
if (empty($chat['LastMessageOn'])) {
$shouldExpire = true;
} else {
$lastMsg = new DateTime($chat['LastMessageOn'], new DateTimeZone('UTC'));
$now = new DateTime('now', new DateTimeZone('UTC'));
$diffMinutes = ($now->getTimestamp() - $lastMsg->getTimestamp()) / 60;
if ($diffMinutes > 20) {
$shouldExpire = true;
}
}
if ($shouldExpire) {
queryTimed("UPDATE Tasks SET CompletedOn = NOW() WHERE ID = ?", [$chat['ID']]);
$expiredCount++;
$expiredIds[] = (int) $chat['ID'];
}
}
jsonResponse([
'OK' => true,
'MESSAGE' => "Expired {$expiredCount} stale chat(s)",
'EXPIRED_TASK_IDS' => $expiredIds,
'CHECKED_COUNT' => count($staleChats),
]);
} catch (Exception $e) {
jsonResponse(['OK' => false, 'ERROR' => 'server_error', 'MESSAGE' => $e->getMessage()]);
}