fix: derive contingent change signal from the sync diff
This commit is contained in:
@@ -53,6 +53,10 @@ class ContingentSnapshotManager
|
||||
|
||||
$state = $this->syncStateRepository->findOneByAccommodation($accommodation) ?? new ContingentSyncState($accommodation);
|
||||
|
||||
// Read before recordSuccess() moves it: days beyond the horizon the previous run reached
|
||||
// are the window growing, not the contingent situation changing.
|
||||
$previousHorizonTo = $state->getHorizonTo();
|
||||
|
||||
try {
|
||||
$calendar = $this->contingentsClient->getContingentCalendar(
|
||||
$hotelCode,
|
||||
@@ -72,6 +76,7 @@ class ContingentSnapshotManager
|
||||
}
|
||||
|
||||
$added = 0;
|
||||
$extended = 0;
|
||||
$updated = 0;
|
||||
$seen = [];
|
||||
|
||||
@@ -102,7 +107,12 @@ class ContingentSnapshotManager
|
||||
->setDate($date);
|
||||
|
||||
$this->entityManager->persist($day);
|
||||
++$added;
|
||||
|
||||
if (null !== $previousHorizonTo && $date > $previousHorizonTo) {
|
||||
++$extended;
|
||||
} else {
|
||||
++$added;
|
||||
}
|
||||
} elseif ($day->getStatus() === $entry->status) {
|
||||
continue;
|
||||
} else {
|
||||
@@ -124,26 +134,21 @@ class ContingentSnapshotManager
|
||||
$this->entityManager->flush();
|
||||
|
||||
$now = CarbonImmutable::now()->toDateTimeImmutable();
|
||||
$hash = $this->fingerprint($accommodation);
|
||||
$changed = $hash !== $state->getContentHash();
|
||||
|
||||
// The diff is the change signal. A digest of the stored snapshot cannot be one: the window
|
||||
// rolls forward a day at a time, so every digest would cover a different span than the one
|
||||
// it is compared against and every run would report a change.
|
||||
$changed = $added > 0 || $updated > 0 || $removed > 0;
|
||||
|
||||
if ($changed) {
|
||||
$state->setContentHash($hash)->setChangedAt($now);
|
||||
$state->setChangedAt($now);
|
||||
}
|
||||
|
||||
$state->recordSuccess($now, $dateTo);
|
||||
$this->entityManager->persist($state);
|
||||
$this->entityManager->flush();
|
||||
|
||||
return ContingentSyncResult::synced($changed, $added, $updated, $removed);
|
||||
}
|
||||
|
||||
/**
|
||||
* sha256 over the accommodation's complete stored snapshot, contingent status only.
|
||||
*/
|
||||
public function fingerprint(Accommodation $accommodation): string
|
||||
{
|
||||
return hash('sha256', implode('|', $this->dayRepository->findStatusFingerprintParts($accommodation)));
|
||||
return ContingentSyncResult::synced($changed, $added, $extended, $updated, $removed);
|
||||
}
|
||||
|
||||
private function recordFailure(ContingentSyncState $state, string $hotelCode, string $error): ContingentSyncResult
|
||||
|
||||
Reference in New Issue
Block a user