Optimize Zammad sync: skip closed tickets in incremental sync

- Incremental sync now excludes closed/merged/removed tickets from query
- Full sync still fetches everything
- Reopened tickets (closed→new/open in Zammad) are handled correctly:
  existing ticket is updated instead of creating duplicate

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-13 14:56:38 +02:00
parent 15223760f5
commit d4e06fd586

11
api.php
View File

@@ -281,7 +281,7 @@ class ZammadClient {
}
/** Hae tikettejä (search API palauttaa kaikki joihin on oikeus) */
public function getTickets(array $groupIds = [], int $page = 1, int $perPage = 100, ?string $updatedSince = null): array {
public function getTickets(array $groupIds = [], int $page = 1, int $perPage = 100, ?string $updatedSince = null, bool $excludeClosed = false): array {
if (!empty($groupIds)) {
$parts = array_map(fn($id) => 'group_id:' . $id, $groupIds);
$query = '(' . implode(' OR ', $parts) . ')';
@@ -291,6 +291,10 @@ class ZammadClient {
if ($updatedSince) {
$query .= ' AND updated_at:>=' . $updatedSince;
}
// Ohita suljetut/merged/removed tiketit nopeuttaakseen synkkausta
if ($excludeClosed) {
$query .= ' AND NOT (state:closed OR state:merged OR state:removed)';
}
return $this->request('GET', 'tickets/search?query=' . urlencode($query) . '&per_page=' . $perPage . '&page=' . $page . '&expand=true');
}
@@ -5360,12 +5364,13 @@ switch ($action) {
}
}
// Hae tikettejä Zammadista
// Hae tikettejä Zammadista (inkrementaalinen: ohita suljetut nopeuttamiseksi)
$excludeClosed = !$fullSync;
$allTickets = [];
$page = 1;
$maxPages = $fullSync ? 10 : 3;
do {
$batch = $z->getTickets($groupIds, $page, 100, $lastSync);
$batch = $z->getTickets($groupIds, $page, 100, $lastSync, $excludeClosed);
if (empty($batch)) break;
$allTickets = array_merge($allTickets, $batch);
$page++;