payfrit-api/api/admin/scheduledTasks/toggle.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

60 lines
2 KiB
PHP

<?php
require_once __DIR__ . '/../../helpers.php';
require_once __DIR__ . '/_cronUtils.php';
runAuth();
/**
* Enable or disable a scheduled task.
* POST: { BusinessID, ScheduledTaskID, IsActive }
*/
$data = readJsonBody();
$businessID = (int) ($data['BusinessID'] ?? headerValue('X-Business-ID') ?? 0);
$taskID = (int) ($data['ScheduledTaskID'] ?? 0);
$isActive = isset($data['IsActive']) ? ($data['IsActive'] ? 1 : 0) : 0;
if ($businessID <= 0) {
apiAbort(['OK' => false, 'ERROR' => 'missing_params', 'MESSAGE' => 'BusinessID is required']);
}
if ($taskID <= 0) {
apiAbort(['OK' => false, 'ERROR' => 'missing_params', 'MESSAGE' => 'ScheduledTaskID is required']);
}
try {
$existing = queryOne("
SELECT ID, CronExpression, COALESCE(ScheduleType, 'cron') AS ScheduleType, IntervalMinutes
FROM ScheduledTaskDefinitions
WHERE ID = ? AND BusinessID = ?
", [$taskID, $businessID]);
if (!$existing) {
apiAbort(['OK' => false, 'ERROR' => 'not_found', 'MESSAGE' => 'Scheduled task not found']);
}
if ($isActive) {
// Recalculate next run time based on schedule type
$st = $existing['ScheduleType'];
$interval = (int) ($existing['IntervalMinutes'] ?? 0);
if (($st === 'interval' || $st === 'interval_after_completion') && $interval > 0) {
$nextRun = new DateTime("+{$interval} minutes", new DateTimeZone('UTC'));
} else {
$nextRun = calculateNextRun($existing['CronExpression']);
}
queryTimed("UPDATE ScheduledTaskDefinitions SET IsActive = 1, NextRunOn = ? WHERE ID = ?", [
$nextRun->format('Y-m-d H:i:s'),
$taskID,
]);
} else {
queryTimed("UPDATE ScheduledTaskDefinitions SET IsActive = 0 WHERE ID = ?", [$taskID]);
}
jsonResponse([
'OK' => true,
'MESSAGE' => $isActive ? 'Scheduled task enabled' : 'Scheduled task disabled',
]);
} catch (Exception $e) {
jsonResponse(['OK' => false, 'ERROR' => 'server_error', 'MESSAGE' => $e->getMessage()]);
}