diff --git a/.changes/unreleased/Feature-20230712-180023.yaml b/.changes/unreleased/Feature-20230712-180023.yaml new file mode 100644 index 000000000..4610dcdbc --- /dev/null +++ b/.changes/unreleased/Feature-20230712-180023.yaml @@ -0,0 +1,6 @@ +kind: Feature +body: | + [addresses] Add a cronjob to re-associate addresses with addresses reference every 6 hours +time: 2023-07-12T18:00:23.037677413+02:00 +custom: + Issue: "112" diff --git a/src/Bundle/ChillMainBundle/Service/AddressGeographicalUnit/CollateAddressWithReferenceOrPostalCode.php b/src/Bundle/ChillMainBundle/Service/AddressGeographicalUnit/CollateAddressWithReferenceOrPostalCode.php index 656751eeb..2a5dda3f7 100644 --- a/src/Bundle/ChillMainBundle/Service/AddressGeographicalUnit/CollateAddressWithReferenceOrPostalCode.php +++ b/src/Bundle/ChillMainBundle/Service/AddressGeographicalUnit/CollateAddressWithReferenceOrPostalCode.php @@ -14,7 +14,7 @@ namespace Chill\MainBundle\Service\AddressGeographicalUnit; use Doctrine\DBAL\Connection; use Psr\Log\LoggerInterface; -final readonly class CollateAddressWithReferenceOrPostalCode +final readonly class CollateAddressWithReferenceOrPostalCode implements CollateAddressWithReferenceOrPostalCodeInterface { private const LOG_PREFIX = '[collate addresses] '; /** @@ -62,8 +62,6 @@ final readonly class CollateAddressWithReferenceOrPostalCode JOIN chill_main_postal_code cmpc on cma.postcode_id = cmpc.id, chill_main_address_reference cmar JOIN chill_main_postal_code cmpc_reference ON cmar.postcode_id = cmpc_reference.id WHERE - -- only where the reference is null - -- cma.addressreference_id IS NULL cma.addressreference_id != cmar.id -- only if cmpc is a reference (must be matched before executing this query) AND cma.postcode_id = cmar.postcode_id @@ -113,9 +111,9 @@ final readonly class CollateAddressWithReferenceOrPostalCode $pointUpdates, $lastId, ] = $this->connection->transactional(function () use ($sinceId) { - $postCodeSetReferenceFromMostSimilar = $this->connection->executeQuery(self::FORCE_ORIGINAL_POSTAL_CODE, ['since_id' => $sinceId]); - $addressReferenceMatch = $this->connection->executeQuery(self::FORCE_MOST_SIMILAR_ADDRESS_REFERENCE, ['since_id' => $sinceId]); - $pointUpdates = $this->connection->executeQuery(self::UPDATE_POINT, ['since_id' => $sinceId]); + $postCodeSetReferenceFromMostSimilar = $this->connection->executeStatement(self::FORCE_ORIGINAL_POSTAL_CODE, ['since_id' => $sinceId]); + $addressReferenceMatch = $this->connection->executeStatement(self::FORCE_MOST_SIMILAR_ADDRESS_REFERENCE, ['since_id' => $sinceId]); + $pointUpdates = $this->connection->executeStatement(self::UPDATE_POINT, ['since_id' => $sinceId]); $lastId = $this->connection->fetchOne(self::MAX_ADDRESS_ID); return [ diff --git a/src/Bundle/ChillMainBundle/Service/AddressGeographicalUnit/CollateAddressWithReferenceOrPostalCodeCronJob.php b/src/Bundle/ChillMainBundle/Service/AddressGeographicalUnit/CollateAddressWithReferenceOrPostalCodeCronJob.php new file mode 100644 index 000000000..b35a687dd --- /dev/null +++ b/src/Bundle/ChillMainBundle/Service/AddressGeographicalUnit/CollateAddressWithReferenceOrPostalCodeCronJob.php @@ -0,0 +1,46 @@ +clock->now(); + + return $now->sub(new \DateInterval('PT6H')) > $cronJobExecution->getLastStart(); + } + + public function getKey(): string + { + return 'collate-address'; + } + + public function run(array $lastExecutionData): null|array + { + $maxId = ($this->collateAddressWithReferenceOrPostalCode)($lastExecutionData[self::LAST_MAX_ID] ?? 0); + + return [self::LAST_MAX_ID => $maxId]; + } +} diff --git a/src/Bundle/ChillMainBundle/Service/AddressGeographicalUnit/CollateAddressWithReferenceOrPostalCodeInterface.php b/src/Bundle/ChillMainBundle/Service/AddressGeographicalUnit/CollateAddressWithReferenceOrPostalCodeInterface.php new file mode 100644 index 000000000..cd3f2606f --- /dev/null +++ b/src/Bundle/ChillMainBundle/Service/AddressGeographicalUnit/CollateAddressWithReferenceOrPostalCodeInterface.php @@ -0,0 +1,20 @@ +setLastStart($lastExecution); + + $clock = new MockClock($now); + $collator = $this->prophesize(CollateAddressWithReferenceOrPostalCodeInterface::class); + + $job = new CollateAddressWithReferenceOrPostalCodeCronJob($clock, $collator->reveal()); + + self::assertEquals($expected, $job->canRun($execution)); + } + + public function testRun(): void + { + $clock = new MockClock(); + $collator = $this->prophesize(CollateAddressWithReferenceOrPostalCodeInterface::class); + $collator->__invoke(0)->shouldBeCalledOnce(); + $collator->__invoke(0)->willReturn(1); + + $job = new CollateAddressWithReferenceOrPostalCodeCronJob($clock, $collator->reveal()); + + $actual = $job->run(['last-max-id' => 0]); + self::assertEquals(['last-max-id' => 1], $actual); + } + + public static function provideDataCanRun(): iterable + { + yield [new \DateTimeImmutable('2023-07-10T12:00:00'), new \DateTimeImmutable('2023-07-10T11:00:00'), false]; + yield [new \DateTimeImmutable('2023-07-10T12:00:00'), new \DateTimeImmutable('2023-07-10T05:00:00'), true]; + yield [new \DateTimeImmutable('2023-07-10T12:00:00'), new \DateTimeImmutable('2023-07-01T12:00:00'), true]; + } + +}