Files
2026-10-16 19:12:13 +00:00

52 lines
2.0 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/common.php';
api_require_method('GET', 'POST');
$staff = api_require_staff();
api_require_admin_or_permission($staff, 'manage_priorities');
$pdo = get_db();
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$plans = $pdo->query(
'SELECT p.*,
(SELECT COUNT(*) FROM departments d WHERE d.sla_plan_id = p.id) AS dept_count,
(SELECT COUNT(*) FROM help_topics h WHERE h.sla_plan_id = p.id) AS topic_count
FROM sla_plans p ORDER BY p.name'
)->fetchAll();
api_json(['items' => array_map(fn(array $p) => [
'id' => (int) $p['id'],
'name' => $p['name'],
'responseHours' => (int) $p['response_hours'],
'resolutionHours' => (int) $p['resolution_hours'],
'isActive' => (bool) $p['is_active'],
'notes' => $p['notes'],
'departmentCount' => (int) $p['dept_count'],
'helpTopicCount' => (int) $p['topic_count'],
'updatedAt' => $p['updated_at'],
], $plans)]);
}
$body = api_request_body();
$action = $body['action'] ?? '';
$id = (int) ($body['id'] ?? 0);
try {
if ($action === 'save') {
$in = ['name' => $body['name'] ?? '', 'response_hours' => $body['responseHours'] ?? 0, 'resolution_hours' => $body['resolutionHours'] ?? 0, 'notes' => $body['notes'] ?? ''];
if (array_key_exists('isActive', $body)) $in['is_active'] = $body['isActive'];
$newId = sla_plan_save($pdo, $id, $in);
audit_log($staff['id'], ($id === 0 ? 'SLA-Plan erstellt: ' : 'SLA-Plan aktualisiert: ') . trim((string) ($body['name'] ?? '')) . ' (Mobile App)');
api_json(['id' => $newId]);
}
if ($action === 'delete') {
$deleted = sla_plan_delete($pdo, $id);
audit_log($staff['id'], "SLA-Plan gelöscht: {$deleted} (Mobile App)");
api_json(['ok' => true]);
}
} catch (AdminModelException $e) {
api_error($e->httpStatus(), $e->errorCode(), $e->getMessage());
}
api_error(422, 'INVALID_ACTION', 'Unbekannte Aktion.');