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

72 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/common.php';
api_require_method('GET');
$staff = api_require_staff();
$pdo = get_db();
$userId = ($_GET['userId'] ?? '') !== '' ? (int) $_GET['userId'] : null;
$email = trim($_GET['email'] ?? '');
if ($userId !== null) {
$stmt = $pdo->prepare(
'SELECT u.id, u.name, u.email, u.created_at, o.name AS organization_name
FROM users u LEFT JOIN organizations o ON o.id = u.organization_id
WHERE u.id = ?'
);
$stmt->execute([$userId]);
$customer = $stmt->fetch();
if (!$customer) {
api_error(404, 'CUSTOMER_NOT_FOUND', 'Der Kunde wurde nicht gefunden.');
}
$email = $customer['email'];
$isGuest = false;
} else {
if ($email === '') {
api_error(422, 'CUSTOMER_IDENTIFIER_REQUIRED', 'Bitte userId oder email angeben.');
}
$ticketStmt = $pdo->prepare(
'SELECT MAX(requester_name) AS name, MIN(created_at) AS created_at
FROM tickets WHERE requester_email = ? AND user_id IS NULL'
);
$ticketStmt->execute([$email]);
$guestRow = $ticketStmt->fetch();
if (!$guestRow || $guestRow['name'] === null) {
api_error(404, 'CUSTOMER_NOT_FOUND', 'Der Kunde wurde nicht gefunden.');
}
$customer = [
'id' => null,
'name' => $guestRow['name'],
'email' => $email,
'created_at' => $guestRow['created_at'],
'organization_name' => null,
];
$isGuest = true;
}
$ticketStmt = $pdo->prepare(
"SELECT t.*, p.name AS priority_name, p.color AS priority_color, d.name AS department_name,
s.name AS staff_name
FROM tickets t
JOIN priorities p ON p.id = t.priority_id
JOIN departments d ON d.id = t.department_id
LEFT JOIN staff s ON s.id = t.assigned_staff_id
WHERE t.requester_email = ? OR t.user_id = ?
ORDER BY t.created_at DESC LIMIT 100"
);
$ticketStmt->execute([$email, $userId]);
$tickets = array_map('api_ticket_summary', $ticketStmt->fetchAll());
api_json([
'customer' => [
'id' => $customer['id'] !== null ? (int) $customer['id'] : null,
'name' => $customer['name'],
'email' => $customer['email'],
'organizationName' => $customer['organization_name'],
'isGuest' => $isGuest,
'createdAt' => $customer['created_at'],
],
'tickets' => $tickets,
]);