Update UpdateLastSyncFromDocuments hook to handle AI sync status and timestamps; modify state file for cache and microtime values
This commit is contained in:
@@ -6,8 +6,8 @@ use Espo\ORM\Repository\Option\SaveOptions;
|
|||||||
use Espo\Core\Hook\Hook\BeforeSave;
|
use Espo\Core\Hook\Hook\BeforeSave;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hook: Aktualisiert lastSync basierend auf dem neuesten lastSyncTimestamp
|
* Hook: Aktualisiert lastSync + syncStatus (Advoware) und aiLastSync + aiSyncStatus (AI)
|
||||||
* der verknüpften Dokumente
|
* basierend auf dem neuesten Timestamp und schlechtesten Status der verknüpften Dokumente.
|
||||||
*/
|
*/
|
||||||
class UpdateLastSyncFromDocuments implements BeforeSave
|
class UpdateLastSyncFromDocuments implements BeforeSave
|
||||||
{
|
{
|
||||||
@@ -17,12 +17,10 @@ class UpdateLastSyncFromDocuments implements BeforeSave
|
|||||||
|
|
||||||
public function beforeSave(Entity $entity, SaveOptions $options): void
|
public function beforeSave(Entity $entity, SaveOptions $options): void
|
||||||
{
|
{
|
||||||
// Überspringe, wenn skipHooks gesetzt ist (verhindert Loops)
|
|
||||||
if ($options->get('skipHooks')) {
|
if ($options->get('skipHooks')) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Nur wenn Entity bereits existiert (nicht bei Create)
|
|
||||||
if ($entity->isNew()) {
|
if ($entity->isNew()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -31,31 +29,13 @@ class UpdateLastSyncFromDocuments implements BeforeSave
|
|||||||
$pdo = $this->entityManager->getPDO();
|
$pdo = $this->entityManager->getPDO();
|
||||||
$aktenId = $entity->getId();
|
$aktenId = $entity->getId();
|
||||||
|
|
||||||
// Hole das neueste lastSyncTimestamp aller verknüpften Dokumente
|
|
||||||
$stmt = $pdo->prepare(
|
$stmt = $pdo->prepare(
|
||||||
"SELECT MAX(last_sync_timestamp) AS maxLastSync
|
"SELECT
|
||||||
|
MAX(last_sync_timestamp) AS maxAdvLastSync,
|
||||||
|
MAX(ai_last_sync) AS maxAiLastSync,
|
||||||
|
sync_status,
|
||||||
|
ai_sync_status
|
||||||
FROM c_dokumente
|
FROM c_dokumente
|
||||||
WHERE c_akten_id = :aktenId AND deleted = 0 AND last_sync_timestamp IS NOT NULL"
|
|
||||||
);
|
|
||||||
$stmt->execute([':aktenId' => $aktenId]);
|
|
||||||
$result = $stmt->fetch(\PDO::FETCH_ASSOC);
|
|
||||||
|
|
||||||
if ($result && $result['maxLastSync']) {
|
|
||||||
$entity->set('lastSync', $result['maxLastSync']);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Berechne syncStatus basierend auf verknüpften Dokumenten
|
|
||||||
$this->updateSyncStatus($entity, $pdo, $aktenId);
|
|
||||||
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
$GLOBALS['log']->error('CAkten UpdateLastSyncFromDocuments Hook Error: ' . $e->getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private function updateSyncStatus(Entity $entity, \PDO $pdo, string $aktenId): void
|
|
||||||
{
|
|
||||||
$stmt = $pdo->prepare(
|
|
||||||
"SELECT sync_status FROM c_dokumente
|
|
||||||
WHERE c_akten_id = :aktenId AND deleted = 0"
|
WHERE c_akten_id = :aktenId AND deleted = 0"
|
||||||
);
|
);
|
||||||
$stmt->execute([':aktenId' => $aktenId]);
|
$stmt->execute([':aktenId' => $aktenId]);
|
||||||
@@ -63,30 +43,82 @@ class UpdateLastSyncFromDocuments implements BeforeSave
|
|||||||
|
|
||||||
if (empty($rows)) {
|
if (empty($rows)) {
|
||||||
$entity->set('syncStatus', 'unclean');
|
$entity->set('syncStatus', 'unclean');
|
||||||
|
$entity->set('aiSyncStatus', 'unclean');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$hasUnsynced = false;
|
// Timestamps
|
||||||
$hasFailed = false;
|
$maxAdvLastSync = null;
|
||||||
|
$maxAiLastSync = null;
|
||||||
|
|
||||||
|
// Status-Tracker
|
||||||
|
$advHasFailed = false;
|
||||||
|
$advHasUnsynced = false;
|
||||||
|
$aiHasFailed = false;
|
||||||
|
$aiHasUnsynced = false;
|
||||||
|
|
||||||
foreach ($rows as $row) {
|
foreach ($rows as $row) {
|
||||||
$status = $row['sync_status'] ?? null;
|
// Advoware: neuester Timestamp
|
||||||
|
if (!empty($row['maxAdvLastSync'])) {
|
||||||
if ($status === 'failed') {
|
if ($maxAdvLastSync === null || $row['maxAdvLastSync'] > $maxAdvLastSync) {
|
||||||
$hasFailed = true;
|
$maxAdvLastSync = $row['maxAdvLastSync'];
|
||||||
}
|
|
||||||
|
|
||||||
if ($status === 'new' || $status === 'unclean' || $status === null || $status === '') {
|
|
||||||
$hasUnsynced = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($hasFailed) {
|
// AI: neuester Timestamp
|
||||||
|
if (!empty($row['maxAiLastSync'])) {
|
||||||
|
if ($maxAiLastSync === null || $row['maxAiLastSync'] > $maxAiLastSync) {
|
||||||
|
$maxAiLastSync = $row['maxAiLastSync'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Advoware: schlechtester Status
|
||||||
|
$advStatus = $row['sync_status'] ?? null;
|
||||||
|
if ($advStatus === 'failed') {
|
||||||
|
$advHasFailed = true;
|
||||||
|
} elseif ($advStatus === 'new' || $advStatus === 'unclean' || $advStatus === null || $advStatus === '') {
|
||||||
|
$advHasUnsynced = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// AI: schlechtester Status
|
||||||
|
$aiStatus = $row['ai_sync_status'] ?? null;
|
||||||
|
if ($aiStatus === 'failed') {
|
||||||
|
$aiHasFailed = true;
|
||||||
|
} elseif ($aiStatus === 'new' || $aiStatus === 'unclean' || $aiStatus === null || $aiStatus === '') {
|
||||||
|
$aiHasUnsynced = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Advoware Timestamp setzen
|
||||||
|
if ($maxAdvLastSync !== null) {
|
||||||
|
$entity->set('lastSync', $maxAdvLastSync);
|
||||||
|
}
|
||||||
|
|
||||||
|
// AI Timestamp setzen
|
||||||
|
if ($maxAiLastSync !== null) {
|
||||||
|
$entity->set('aiLastSync', $maxAiLastSync);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Advoware Status setzen (worst-case)
|
||||||
|
if ($advHasFailed) {
|
||||||
$entity->set('syncStatus', 'failed');
|
$entity->set('syncStatus', 'failed');
|
||||||
} elseif ($hasUnsynced) {
|
} elseif ($advHasUnsynced) {
|
||||||
$entity->set('syncStatus', 'unclean');
|
$entity->set('syncStatus', 'unclean');
|
||||||
} else {
|
} else {
|
||||||
$entity->set('syncStatus', 'synced');
|
$entity->set('syncStatus', 'synced');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AI Status setzen (worst-case)
|
||||||
|
if ($aiHasFailed) {
|
||||||
|
$entity->set('aiSyncStatus', 'failed');
|
||||||
|
} elseif ($aiHasUnsynced) {
|
||||||
|
$entity->set('aiSyncStatus', 'unclean');
|
||||||
|
} else {
|
||||||
|
$entity->set('aiSyncStatus', 'synced');
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$GLOBALS['log']->error('CAkten UpdateLastSyncFromDocuments Hook Error: ' . $e->getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
return [
|
return [
|
||||||
'cacheTimestamp' => 1774530224,
|
'cacheTimestamp' => 1774530556,
|
||||||
'microtimeState' => 1774530224.017606,
|
'microtimeState' => 1774530556.202809,
|
||||||
'currencyRates' => [
|
'currencyRates' => [
|
||||||
'EUR' => 1.0
|
'EUR' => 1.0
|
||||||
],
|
],
|
||||||
|
|||||||
Reference in New Issue
Block a user