Upload via GUI (278 Dateien)

This commit is contained in:
2026-10-21 16:44:55 +00:00
parent 30df645ca9
commit 39c0a19cf7
6 changed files with 67 additions and 11 deletions
+21 -10
View File
@@ -19,8 +19,9 @@ $pdo = get_db();
const AVAILABILITY_STATUSES = ['available', 'busy', 'break', 'offline'];
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
staff_run_auto_offline($pdo);
$agents = $pdo->query(
'SELECT s.id, s.name, s.is_on_vacation, sa.status, sa.note, sa.updated_at
'SELECT s.id, s.name, s.is_on_vacation, s.last_active_at, sa.auto_offline, sa.status, sa.note, sa.updated_at
FROM staff s
LEFT JOIN staff_availability sa ON sa.staff_id = s.id
WHERE s.is_active = 1
@@ -37,6 +38,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
'note' => $a['is_on_vacation'] ? 'Urlaub' : ($a['note'] ?? ''),
'isOnVacation' => (bool) $a['is_on_vacation'],
'updatedAt' => $a['updated_at'],
'lastActiveAt' => $a['last_active_at'] ?? null,
'autoOffline' => !empty($a['auto_offline']),
];
$team[] = $entry;
if ((int) $a['id'] === (int) $staff['id']) {
@@ -57,12 +60,20 @@ if (!in_array($newStatus, AVAILABILITY_STATUSES, true)) {
api_error(422, 'INVALID_STATUS', 'Ungültiger Status.');
}
$drv = $pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
$sql = $drv === 'sqlite'
? 'INSERT INTO staff_availability (staff_id, status, note, updated_at) VALUES (?,?,?,?)
ON CONFLICT(staff_id) DO UPDATE SET status=excluded.status, note=excluded.note, updated_at=excluded.updated_at'
: 'INSERT INTO staff_availability (staff_id, status, note, updated_at) VALUES (?,?,?,?)
ON DUPLICATE KEY UPDATE status=VALUES(status), note=VALUES(note), updated_at=VALUES(updated_at)';
$pdo->prepare($sql)->execute([$staff['id'], $newStatus, $note ?: null, db_now()]);
api_json(['ok' => true, 'status' => $newStatus]);
// Ein Admin darf den Status eines anderen Mitarbeiters setzen (staffId), alle anderen nur den eigenen
$targetId = (int) $staff['id'];
if (isset($body['staffId']) && (int) $body['staffId'] !== $targetId) {
if (($staff['role'] ?? '') !== 'admin') {
api_error(403, 'FORBIDDEN', 'Nur Administratoren dürfen den Status anderer Mitarbeiter ändern.');
}
$targetId = (int) $body['staffId'];
$exists = $pdo->prepare('SELECT COUNT(*) FROM staff WHERE id = ? AND is_active = 1');
$exists->execute([$targetId]);
if ((int) $exists->fetchColumn() === 0) {
api_error(404, 'STAFF_NOT_FOUND', 'Mitarbeiter nicht gefunden.');
}
audit_log((int) $staff['id'], "Status von Mitarbeiter #$targetId auf $newStatus gesetzt");
}
staff_set_status($pdo, $targetId, $newStatus, $note);
staff_touch_activity($pdo, (int) $staff['id']);
api_json(['ok' => true, 'status' => $newStatus, 'staffId' => $targetId]);