payfrit-api/api/tasks/team/list.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

89 lines
2.6 KiB
PHP

<?php
require_once __DIR__ . '/../../helpers.php';
/**
* GET /api/tasks/team/list.php
* List all tasks with optional filters.
*
* Query params:
* BotName — filter by bot name
* Status — filter by status (active/paused/done/cancelled)
* Channel — filter by channel
* AssignedBy — filter by who assigned
* Since — only tasks started on or after this date (YYYY-MM-DD)
* Limit — max rows (default 100, max 500)
* Offset — pagination offset (default 0)
*
* Returns: { OK: true, Tasks: [...], Total: <count> }
*/
$botName = trim($_GET['BotName'] ?? '');
$status = trim($_GET['Status'] ?? '');
$channel = trim($_GET['Channel'] ?? '');
$assignedBy = trim($_GET['AssignedBy'] ?? '');
$since = trim($_GET['Since'] ?? '');
$limit = min(max((int) ($_GET['Limit'] ?? 100), 1), 500);
$offset = max((int) ($_GET['Offset'] ?? 0), 0);
$validStatuses = ['active', 'paused', 'done', 'cancelled'];
try {
$where = [];
$params = [];
if ($botName !== '') {
$where[] = "BotName = ?";
$params[] = $botName;
}
if ($status !== '') {
if (!in_array($status, $validStatuses, true)) {
http_response_code(400);
apiAbort(['OK' => false, 'ERROR' => 'Invalid status filter']);
}
$where[] = "Status = ?";
$params[] = $status;
}
if ($channel !== '') {
$where[] = "Channel = ?";
$params[] = $channel;
}
if ($assignedBy !== '') {
$where[] = "AssignedBy = ?";
$params[] = $assignedBy;
}
if ($since !== '') {
// Validate date format
if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $since)) {
http_response_code(400);
apiAbort(['OK' => false, 'ERROR' => 'Since must be YYYY-MM-DD']);
}
$where[] = "StartedOn >= ?";
$params[] = $since . ' 00:00:00';
}
$whereClause = empty($where) ? '' : 'WHERE ' . implode(' AND ', $where);
// Get total count
$countRow = queryOne("SELECT COUNT(*) AS Total FROM TeamTasks $whereClause", $params);
$total = (int) ($countRow['Total'] ?? 0);
// Get paginated results
$params[] = $limit;
$params[] = $offset;
$tasks = queryTimed("
SELECT ID, BotName, Title, Status, Channel, StartedOn, PausedOn, CompletedOn, Notes, AssignedBy
FROM TeamTasks
$whereClause
ORDER BY StartedOn DESC
LIMIT ? OFFSET ?
", $params);
jsonResponse(['OK' => true, 'Tasks' => $tasks, 'Total' => $total]);
} catch (Throwable $e) {
http_response_code(500);
apiAbort(['OK' => false, 'ERROR' => 'Failed to fetch tasks: ' . $e->getMessage()]);
}