payfrit-api/api/tasks/team/update.php
Mike e3933ce0c8 Add team task tracker API endpoints
New endpoints at api/tasks/team/ for centralized bot task tracking:
- POST create.php — register a new task
- POST update.php — change status, add notes (auto-sets PausedOn/CompletedOn)
- GET active.php — list active/paused tasks, optional BotName filter
- GET list.php — full listing with filters (BotName, Status, Channel, AssignedBy, Since) and pagination

Includes schema.sql for the TeamTasks table.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-23 19:00:12 +00:00

80 lines
2.2 KiB
PHP

<?php
require_once __DIR__ . '/../../helpers.php';
/**
* POST /api/tasks/team/update.php
* Update an existing team task (change status, add notes).
*
* Body: { ID, Status?, Notes?, Channel? }
* Returns: { OK: true }
*/
$data = readJsonBody();
$id = (int) ($data['ID'] ?? 0);
$status = trim($data['Status'] ?? '');
$notes = $data['Notes'] ?? null;
$channel = $data['Channel'] ?? null;
if ($id <= 0) {
http_response_code(400);
apiAbort(['OK' => false, 'ERROR' => 'ID is required']);
}
// Validate status if provided
$validStatuses = ['active', 'paused', 'done', 'cancelled'];
if ($status !== '' && !in_array($status, $validStatuses, true)) {
http_response_code(400);
apiAbort(['OK' => false, 'ERROR' => 'Status must be one of: ' . implode(', ', $validStatuses)]);
}
try {
// Verify task exists
$task = queryOne("SELECT ID, Status FROM TeamTasks WHERE ID = ?", [$id]);
if (!$task) {
http_response_code(404);
apiAbort(['OK' => false, 'ERROR' => 'Task not found']);
}
// Build dynamic update
$sets = [];
$params = [];
if ($status !== '') {
$sets[] = "Status = ?";
$params[] = $status;
// Auto-set timestamp columns based on status transitions
if ($status === 'paused') {
$sets[] = "PausedOn = NOW()";
} elseif ($status === 'done' || $status === 'cancelled') {
$sets[] = "CompletedOn = NOW()";
} elseif ($status === 'active' && $task['Status'] === 'paused') {
// Resuming from pause — clear PausedOn
$sets[] = "PausedOn = NULL";
}
}
if ($notes !== null) {
$sets[] = "Notes = ?";
$params[] = trim($notes);
}
if ($channel !== null) {
$sets[] = "Channel = ?";
$params[] = trim($channel);
}
if (empty($sets)) {
http_response_code(400);
apiAbort(['OK' => false, 'ERROR' => 'Nothing to update — provide Status, Notes, or Channel']);
}
$params[] = $id;
queryTimed("UPDATE TeamTasks SET " . implode(', ', $sets) . " WHERE ID = ?", $params);
jsonResponse(['OK' => true]);
} catch (Throwable $e) {
http_response_code(500);
apiAbort(['OK' => false, 'ERROR' => 'Failed to update task: ' . $e->getMessage()]);
}