Nopeuta Zammad-sync: artikkelit haetaan vasta kun tiketti avataan
Sync hakee nyt vain tikettien metatiedot (status, aihe, ryhmä) — ei enää satoja getArticles-kutsuja jokaiselle tiketille. Artikkelit haetaan on-demand ticket_detail-endpointissa kun käyttäjä avaa tiketin. Nopeuttaa synkkausta dramaattisesti. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
48
api.php
48
api.php
@@ -3068,6 +3068,40 @@ switch ($action) {
|
||||
echo json_encode(['error' => 'Tikettiä ei löydy']);
|
||||
break;
|
||||
}
|
||||
// Zammad-tiketti: hae artikkelit on-demand jos ei vielä haettu
|
||||
if (!empty($ticket['zammad_ticket_id']) && empty($ticket['messages'])) {
|
||||
try {
|
||||
$integ = dbGetIntegration($companyId, 'zammad');
|
||||
if ($integ && $integ['enabled']) {
|
||||
$z = new ZammadClient($integ['config']['url'], $integ['config']['token']);
|
||||
$articles = $z->getArticles((int)$ticket['zammad_ticket_id']);
|
||||
foreach ($articles as $art) {
|
||||
if (($art['internal'] ?? false)) continue;
|
||||
$artId = (int)$art['id'];
|
||||
$existingMsg = dbGetMessageByZammadArticleId($ticket['id'], $artId);
|
||||
if ($existingMsg) continue;
|
||||
$msgId = substr(uniqid(), -8) . bin2hex(random_bytes(2));
|
||||
$msgType = ($art['sender'] ?? '') === 'Customer' ? 'incoming' : 'outgoing';
|
||||
$body = $art['body'] ?? '';
|
||||
if (($art['content_type'] ?? '') === 'text/html') {
|
||||
$body = strip_tags($body, '<br><p><div><a><b><i><strong><em><ul><ol><li>');
|
||||
}
|
||||
_dbExecute(
|
||||
"INSERT INTO ticket_messages (id, ticket_id, type, from_email, from_name, body, timestamp, message_id, zammad_article_id)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
[$msgId, $ticket['id'], $msgType, $art['from'] ?? '', $art['from'] ?? '', $body,
|
||||
$art['created_at'] ? date('Y-m-d H:i:s', strtotime($art['created_at'])) : date('Y-m-d H:i:s'),
|
||||
$art['message_id'] ?? '', $artId]
|
||||
);
|
||||
}
|
||||
// Lataa tiketti uudelleen viestien kanssa
|
||||
$tickets = dbLoadTickets($companyId);
|
||||
foreach ($tickets as $t) {
|
||||
if ($t['id'] === $id) { $ticket = $t; break; }
|
||||
}
|
||||
}
|
||||
} catch (\Throwable $e) { /* Zammad ei saatavilla — näytä tiketti ilman viestejä */ }
|
||||
}
|
||||
echo json_encode($ticket);
|
||||
break;
|
||||
|
||||
@@ -5182,20 +5216,17 @@ switch ($action) {
|
||||
"UPDATE tickets SET status = ?, type = ?, priority = ?, subject = ?, zammad_group = ?, updated = ? WHERE id = ?",
|
||||
[$status, $type, $priority, $zt['title'] ?? '', $group, $zammadUpdated, $ticketId]
|
||||
);
|
||||
// Ohita artikkelien haku jos tiketti ei muuttunut (inkrementaalisessa syncissä)
|
||||
$existingUpdated = $existing['updated'] ?? '';
|
||||
if (!$fullSync && $existingUpdated === $zammadUpdated) {
|
||||
$updated++;
|
||||
continue;
|
||||
}
|
||||
$updated++;
|
||||
}
|
||||
|
||||
// Synkkaa artikkelit → viestit
|
||||
// Synkkaa artikkelit vain uusille tiketeille (ei jokaiselle — liian hidas)
|
||||
// Olemassa olevien tikettien artikkelit haetaan on-demand kun käyttäjä avaa tiketin
|
||||
if ($existing) continue;
|
||||
|
||||
try {
|
||||
$articles = $z->getArticles($zammadId);
|
||||
foreach ($articles as $art) {
|
||||
if (($art['internal'] ?? false)) continue; // Ohita sisäiset muistiinpanot
|
||||
if (($art['internal'] ?? false)) continue;
|
||||
$artId = (int)$art['id'];
|
||||
$existingMsg = dbGetMessageByZammadArticleId($ticketId, $artId);
|
||||
if ($existingMsg) continue;
|
||||
@@ -5203,7 +5234,6 @@ switch ($action) {
|
||||
$msgId = substr(uniqid(), -8) . bin2hex(random_bytes(2));
|
||||
$msgType = ($art['sender'] ?? '') === 'Customer' ? 'incoming' : 'outgoing';
|
||||
$body = $art['body'] ?? '';
|
||||
// Strippaa HTML tagit jos content_type on text/html
|
||||
if (($art['content_type'] ?? '') === 'text/html') {
|
||||
$body = strip_tags($body, '<br><p><div><a><b><i><strong><em><ul><ol><li>');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user