Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m35s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m32s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 3m54s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 3m53s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 5m1s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 5m17s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 4m31s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m11s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 4m33s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
313 lines
13 KiB
PHP
313 lines
13 KiB
PHP
<?php
|
||
|
||
namespace Inhaus\Plugins\ItService;
|
||
|
||
use App\Models\User;
|
||
use Illuminate\Http\Client\Response;
|
||
use Illuminate\Support\Arr;
|
||
use Inhaus\Plugins\ItService\OpenNit\Client;
|
||
use Inhaus\Plugins\ItService\OpenNit\Settings;
|
||
|
||
/**
|
||
* IT-Self-Service für eine Person: alle Abfragen laufen mit ihrer E-Mail-Adresse,
|
||
* OpenNIT liefert damit nur ihre eigenen Geräte, Tickets und Ausleihen.
|
||
* Antworten werden defensiv aufbereitet – die Oberfläche bekommt feste Felder.
|
||
*/
|
||
class ItService
|
||
{
|
||
public const OPEN_STATUSES = ['open', 'new', 'pending', 'in_progress', 'waiting', 'reopened', 'offen', 'neu', 'in_bearbeitung', 'wartend'];
|
||
|
||
public function __construct(private readonly Client $client, private readonly Settings $settings) {}
|
||
|
||
public function configured(): bool
|
||
{
|
||
return $this->settings->configured();
|
||
}
|
||
|
||
/**
|
||
* @return list<array<string, mixed>>
|
||
*/
|
||
public function assets(User $user): array
|
||
{
|
||
return array_map(fn (array $asset) => [
|
||
'id' => (int) ($asset['id'] ?? 0),
|
||
'name' => (string) ($asset['name'] ?? $asset['title'] ?? 'Gerät'),
|
||
'type' => self::text($asset, ['type', 'category', 'device_type']),
|
||
'serial' => self::text($asset, ['serial', 'serial_number']),
|
||
'inventory' => self::text($asset, ['inventory_number', 'asset_tag', 'inventory']),
|
||
'manufacturer' => self::text($asset, ['manufacturer', 'vendor']),
|
||
'model' => self::text($asset, ['model']),
|
||
'location' => self::text($asset, ['location', 'location_name']),
|
||
'warranty_end' => self::text($asset, ['warranty_end', 'warranty_until']),
|
||
'maintenance' => self::text($asset, ['next_maintenance', 'maintenance_due', 'maintenance']),
|
||
], $this->list($this->client->get('/my-assets', ['email' => $user->email]), 'assets'));
|
||
}
|
||
|
||
/**
|
||
* @return list<array<string, mixed>>
|
||
*/
|
||
public function tickets(User $user): array
|
||
{
|
||
$tickets = array_map(fn (array $ticket) => self::ticketSummary($ticket), $this->list($this->client->get('/my-tickets', ['email' => $user->email]), 'tickets'));
|
||
|
||
usort($tickets, fn ($a, $b) => [$b['open'], $b['created_at']] <=> [$a['open'], $a['created_at']]);
|
||
|
||
return $tickets;
|
||
}
|
||
|
||
/**
|
||
* @return array<string, mixed>
|
||
*/
|
||
public function ticket(User $user, int $id): array
|
||
{
|
||
$data = $this->client->get("/tickets/{$id}", ['email' => $user->email]);
|
||
$ticket = (array) ($data['ticket'] ?? $data);
|
||
|
||
return [
|
||
...self::ticketSummary($ticket),
|
||
'description' => self::text($ticket, ['description', 'body', 'message', 'text']),
|
||
'category' => self::text($ticket, ['category', 'category_name']),
|
||
'location' => self::text($ticket, ['location', 'location_name']),
|
||
'asset' => self::text($ticket, ['asset', 'asset_name']),
|
||
'replies' => array_map(fn ($reply) => [
|
||
'message' => is_array($reply) ? (string) (self::text($reply, ['message', 'body', 'text', 'content']) ?? '') : (string) $reply,
|
||
'author' => is_array($reply) ? self::text($reply, ['author', 'from_name', 'name', 'user', 'author_name']) : null,
|
||
'from_staff' => is_array($reply) && (bool) ($reply['is_staff'] ?? $reply['internal_user'] ?? $reply['from_staff'] ?? (isset($reply['from_email']) && mb_strtolower((string) $reply['from_email']) !== mb_strtolower($user->email))),
|
||
'created_at' => is_array($reply) ? self::text($reply, ['created_at', 'date', 'created']) : null,
|
||
], array_values(array_filter((array) ($data['replies'] ?? []), fn ($reply) => ! (is_array($reply) && ($reply['internal'] ?? $reply['is_internal'] ?? false))))),
|
||
'attachments' => array_values(array_filter(array_map(fn ($file) => is_array($file) && isset($file['id']) ? [
|
||
'id' => (int) $file['id'],
|
||
'name' => (string) ($file['name'] ?? $file['filename'] ?? $file['original_name'] ?? 'Anhang'),
|
||
'size' => isset($file['size']) ? (int) $file['size'] : null,
|
||
] : null, (array) ($data['attachments'] ?? [])))),
|
||
'assignees' => array_values(array_filter(array_map(fn ($person) => is_array($person) ? self::text($person, ['name', 'display_name', 'username']) : (is_string($person) ? $person : null), (array) ($data['assignees'] ?? [])))),
|
||
];
|
||
}
|
||
|
||
/**
|
||
* @param array{subject: string, description?: ?string, category_id?: ?int, location_id?: ?int, asset_id?: ?int} $data
|
||
*/
|
||
public function createTicket(User $user, array $data): int
|
||
{
|
||
$response = $this->client->post('/tickets', array_filter([
|
||
'subject' => $data['subject'],
|
||
'from_email' => $user->email,
|
||
'from_name' => $user->name,
|
||
'description' => $data['description'] ?? null,
|
||
'category_id' => $data['category_id'] ?? null,
|
||
'location_id' => $data['location_id'] ?? null,
|
||
'asset_id' => $data['asset_id'] ?? null,
|
||
], fn ($value) => $value !== null && $value !== ''));
|
||
|
||
return (int) ($response['id'] ?? $response['ticket_id'] ?? 0);
|
||
}
|
||
|
||
public function reply(User $user, int $ticketId, string $message): void
|
||
{
|
||
$this->client->post("/tickets/{$ticketId}/reply", ['email' => $user->email, 'message' => $message]);
|
||
}
|
||
|
||
public function attachment(User $user, int $id): Response
|
||
{
|
||
return $this->client->download("/ticket-attachments/{$id}", ['email' => $user->email]);
|
||
}
|
||
|
||
/**
|
||
* @return list<array<string, mixed>>
|
||
*/
|
||
public function loans(User $user): array
|
||
{
|
||
return array_map(fn (array $loan) => [
|
||
'id' => (int) ($loan['id'] ?? 0),
|
||
'device' => (string) ($loan['device_title'] ?? $loan['device'] ?? 'Leihgerät'),
|
||
'status' => (string) ($loan['status'] ?? ''),
|
||
'status_label' => self::loanStatus((string) ($loan['status'] ?? '')),
|
||
'start' => self::text($loan, ['planned_start', 'start']),
|
||
'end' => self::text($loan, ['planned_end', 'end']),
|
||
'cancellable' => in_array($loan['status'] ?? '', ['pending', 'reserved'], true),
|
||
], $this->list($this->client->get('/my-loans', ['email' => $user->email]), 'loans'));
|
||
}
|
||
|
||
/**
|
||
* @return list<array<string, mixed>>
|
||
*/
|
||
public function loanDevices(?string $type, ?string $from, ?string $to): array
|
||
{
|
||
return array_map(fn (array $device) => [
|
||
'id' => (int) ($device['id'] ?? 0),
|
||
'title' => (string) ($device['title'] ?? $device['name'] ?? 'Leihgerät'),
|
||
'type' => self::text($device, ['type', 'category']),
|
||
'available' => (bool) ($device['available'] ?? true),
|
||
'description' => self::text($device, ['description', 'notes']),
|
||
], $this->list($this->client->get('/loan-devices', ['type' => $type, 'date_from' => $from, 'date_to' => $to]), 'devices'));
|
||
}
|
||
|
||
/**
|
||
* @return list<array{start: string, end: string}>
|
||
*/
|
||
public function slots(int $deviceId): array
|
||
{
|
||
return array_values(array_filter(array_map(fn ($slot) => is_array($slot) && isset($slot['start']) ? ['start' => (string) $slot['start'], 'end' => (string) ($slot['end'] ?? $slot['start'])] : null, $this->list($this->client->get("/loan-devices/{$deviceId}/slots"), 'slots'))));
|
||
}
|
||
|
||
/**
|
||
* @param array{device_id: int, planned_start: string, planned_end: string, purpose?: ?string} $data
|
||
* @return array{status: string, message: ?string}
|
||
*/
|
||
public function requestLoan(User $user, array $data): array
|
||
{
|
||
$body = [
|
||
'device_id' => $data['device_id'],
|
||
'planned_start' => $data['planned_start'],
|
||
'planned_end' => $data['planned_end'],
|
||
'borrower_name' => $user->name,
|
||
'borrower_email' => $user->email,
|
||
'purpose' => $data['purpose'] ?? null,
|
||
];
|
||
|
||
if (($auto = $this->settings->autoReserve()) !== null) {
|
||
$body['auto_reserve'] = $auto;
|
||
}
|
||
|
||
$response = $this->client->post('/loan-requests', array_filter($body, fn ($value) => $value !== null));
|
||
|
||
return ['status' => (string) ($response['status'] ?? 'pending'), 'message' => is_string($response['message'] ?? null) ? $response['message'] : null];
|
||
}
|
||
|
||
public function cancelLoan(User $user, int $id): void
|
||
{
|
||
$this->client->post("/loan-requests/{$id}/cancel", ['email' => $user->email]);
|
||
}
|
||
|
||
/**
|
||
* @return array{categories: list<array<string, mixed>>, locations: list<array<string, mixed>>}
|
||
*/
|
||
public function ticketOptions(): array
|
||
{
|
||
return [
|
||
'categories' => self::tree($this->list($this->client->cached('/ticket-categories'), 'categories')),
|
||
'locations' => self::tree($this->list($this->client->cached('/ticket-locations'), 'locations')),
|
||
];
|
||
}
|
||
|
||
/**
|
||
* @return list<string>
|
||
*/
|
||
public function deviceTypes(): array
|
||
{
|
||
return array_values(array_filter(array_map(fn ($type) => is_string($type) ? $type : (is_array($type) ? ($type['name'] ?? null) : null), $this->list($this->client->cached('/device-types'), 'types'))));
|
||
}
|
||
|
||
/**
|
||
* @param array<string, mixed> $ticket
|
||
* @return array<string, mixed>
|
||
*/
|
||
private static function ticketSummary(array $ticket): array
|
||
{
|
||
$status = (string) ($ticket['status'] ?? '');
|
||
|
||
return [
|
||
'id' => (int) ($ticket['id'] ?? 0),
|
||
'subject' => (string) ($ticket['subject'] ?? $ticket['title'] ?? 'Ticket'),
|
||
'status' => $status,
|
||
'status_label' => self::ticketStatus($status),
|
||
'open' => self::isOpen($status),
|
||
'created_at' => self::text($ticket, ['created_at', 'created', 'date']) ?? '',
|
||
'updated_at' => self::text($ticket, ['updated_at', 'last_activity']),
|
||
'number' => self::text($ticket, ['number', 'ticket_number', 'reference']),
|
||
];
|
||
}
|
||
|
||
public static function isOpen(string $status): bool
|
||
{
|
||
return ! in_array(mb_strtolower($status), ['closed', 'resolved', 'done', 'geschlossen', 'erledigt', 'geloest', 'gelöst', 'cancelled'], true);
|
||
}
|
||
|
||
public static function ticketStatus(string $status): string
|
||
{
|
||
return match (mb_strtolower($status)) {
|
||
'open', 'new', 'offen', 'neu' => 'Offen',
|
||
'in_progress', 'in_bearbeitung', 'assigned' => 'In Bearbeitung',
|
||
'pending', 'waiting', 'wartend', 'on_hold' => 'Wartet auf Rückmeldung',
|
||
'reopened' => 'Wieder geöffnet',
|
||
'resolved', 'geloest', 'gelöst', 'done', 'erledigt' => 'Gelöst',
|
||
'closed', 'geschlossen' => 'Geschlossen',
|
||
'cancelled' => 'Storniert',
|
||
default => $status !== '' ? ucfirst(str_replace('_', ' ', $status)) : 'Offen',
|
||
};
|
||
}
|
||
|
||
public static function loanStatus(string $status): string
|
||
{
|
||
return match ($status) {
|
||
'pending' => 'Angefragt',
|
||
'reserved' => 'Reserviert',
|
||
'active' => 'Ausgeliehen',
|
||
'returned' => 'Zurückgegeben',
|
||
'rejected' => 'Abgelehnt',
|
||
'cancelled' => 'Storniert',
|
||
default => $status !== '' ? ucfirst($status) : 'Angefragt',
|
||
};
|
||
}
|
||
|
||
/**
|
||
* @param array<string, mixed> $data
|
||
* @return list<array<string, mixed>>
|
||
*/
|
||
private function list(array $data, string $key): array
|
||
{
|
||
// Laut Doku {"key": [...]}; manche Versionen verpacken in {"data": …}
|
||
$items = $data[$key] ?? $data['data'][$key] ?? (isset($data['data']) && is_array($data['data']) && array_is_list($data['data']) ? $data['data'] : null) ?? (array_is_list($data) ? $data : []);
|
||
|
||
return array_values(array_filter((array) $items, fn ($item) => is_array($item) || is_string($item)));
|
||
}
|
||
|
||
/**
|
||
* Hierarchie (parent_id) als eingerückte Liste für Auswahlfelder.
|
||
*
|
||
* @param list<array<string, mixed>> $items
|
||
* @return list<array{value: int, label: string}>
|
||
*/
|
||
private static function tree(array $items): array
|
||
{
|
||
$children = [];
|
||
|
||
foreach ($items as $item) {
|
||
if (is_array($item) && isset($item['id'])) {
|
||
$children[$item['parent_id'] ?? 0][] = $item;
|
||
}
|
||
}
|
||
|
||
$result = [];
|
||
$walk = function ($parent, int $depth) use (&$walk, &$children, &$result) {
|
||
foreach ($children[$parent] ?? [] as $item) {
|
||
$result[] = ['value' => (int) $item['id'], 'label' => str_repeat('– ', $depth).($item['name'] ?? '')];
|
||
$walk($item['id'], $depth + 1);
|
||
}
|
||
};
|
||
$walk(0, 0);
|
||
|
||
return $result;
|
||
}
|
||
|
||
/**
|
||
* @param array<string, mixed> $data
|
||
* @param list<string> $keys
|
||
*/
|
||
private static function text(array $data, array $keys): ?string
|
||
{
|
||
foreach ($keys as $key) {
|
||
$value = Arr::get($data, $key);
|
||
|
||
if (is_array($value)) {
|
||
$value = $value['name'] ?? $value['title'] ?? null;
|
||
}
|
||
|
||
if (is_scalar($value) && trim((string) $value) !== '') {
|
||
return trim((string) $value);
|
||
}
|
||
}
|
||
|
||
return null;
|
||
}
|
||
}
|