diff --git a/src/Doctrine/ORM/PickupPointRepository.php b/src/Doctrine/ORM/PickupPointRepository.php index e210c6e..eac40d0 100644 --- a/src/Doctrine/ORM/PickupPointRepository.php +++ b/src/Doctrine/ORM/PickupPointRepository.php @@ -58,4 +58,22 @@ public function findByOrder(OrderInterface $order, string $provider): array ->getResult() ; } + + public function deleteOlderThan(\DateTimeInterface $dateTime, string $provider): void + { + $qb = $this->createQueryBuilder('o'); + $qb + ->andWhere('o.code.provider = :provider') + ->andWhere($qb->expr()->orX( + $qb->expr()->lt('o.updatedAt', ':dateTime'), + $qb->expr()->isNull('o.updatedAt') + )) + ->setParameters([ + 'dateTime' => $dateTime, + 'provider' => $provider, + ]) + ->delete() + ->getQuery() + ->execute(); + } } diff --git a/src/EventListener/AddIndicesSubscriber.php b/src/EventListener/AddIndicesSubscriber.php index b6317cd..220d252 100644 --- a/src/EventListener/AddIndicesSubscriber.php +++ b/src/EventListener/AddIndicesSubscriber.php @@ -36,6 +36,14 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $event): void ], ], ], + 'indexes' => [ + 'updated_at_idx' => [ + 'columns' => [ + 'code_provider', + 'updatedAt' + ], + ], + ], ]; $metadata->table = array_merge_recursive($tableConfig, $metadata->table); diff --git a/src/Message/Handler/LoadPickupPointsHandler.php b/src/Message/Handler/LoadPickupPointsHandler.php index e260293..934c43d 100644 --- a/src/Message/Handler/LoadPickupPointsHandler.php +++ b/src/Message/Handler/LoadPickupPointsHandler.php @@ -39,6 +39,8 @@ public function __invoke(LoadPickupPoints $message): void $i = 1; + $timestamp = new \DateTimeImmutable(); + foreach ($pickupPoints as $pickupPoint) { $pickupPointCode = $pickupPoint->getCode(); Assert::notNull($pickupPointCode); @@ -47,6 +49,7 @@ public function __invoke(LoadPickupPoints $message): void // if it's found, we will update the properties, else we will just persist this object if (null === $localPickupPoint) { + $pickupPoint->setUpdatedAt($timestamp); $this->pickupPointManager->persist($pickupPoint); } else { $localPickupPoint->setName($pickupPoint->getName()); @@ -56,6 +59,7 @@ public function __invoke(LoadPickupPoints $message): void $localPickupPoint->setCountry($pickupPoint->getCountry()); $localPickupPoint->setLatitude($pickupPoint->getLatitude()); $localPickupPoint->setLongitude($pickupPoint->getLongitude()); + $localPickupPoint->setUpdatedAt($timestamp); } if ($i % 50 === 0) { @@ -64,8 +68,12 @@ public function __invoke(LoadPickupPoints $message): void ++$i; } - $this->flush(); + if ($provider->cleanupOnLoadPickupPoints()) { + $this->pickupPointRepository->deleteOlderThan($timestamp, $provider->getCode()); + $this->flush(); + } + } private function flush(): void diff --git a/src/Model/PickupPoint.php b/src/Model/PickupPoint.php index 0153e4f..ff2a61e 100644 --- a/src/Model/PickupPoint.php +++ b/src/Model/PickupPoint.php @@ -27,6 +27,8 @@ class PickupPoint implements PickupPointInterface protected ?float $longitude = null; + protected ?\DateTimeInterface $updatedAt = null; + public function getId(): ?int { return $this->id; @@ -125,4 +127,14 @@ public function getFullAddress(): string $this->getCity() ); } + + public function getUpdatedAt(): ?\DateTimeInterface + { + return $this->updatedAt; + } + + public function setUpdatedAt(?\DateTimeInterface $updatedAt): void + { + $this->updatedAt = $updatedAt; + } } diff --git a/src/Model/PickupPointInterface.php b/src/Model/PickupPointInterface.php index c18621d..142f56a 100644 --- a/src/Model/PickupPointInterface.php +++ b/src/Model/PickupPointInterface.php @@ -50,4 +50,8 @@ public function getLongitude(): ?float; public function setLongitude(?float $longitude): void; public function getFullAddress(): string; + + public function setUpdatedAt(\DateTimeInterface $updatedAt): void; + + public function getUpdatedAt(): ?\DateTimeInterface; } diff --git a/src/Provider/CachedProvider.php b/src/Provider/CachedProvider.php index 4bb681f..215a18d 100644 --- a/src/Provider/CachedProvider.php +++ b/src/Provider/CachedProvider.php @@ -93,6 +93,11 @@ public function getName(): string return $this->provider->getName(); } + public function cleanupOnLoadPickupPoints(): bool + { + return $this->provider->cleanupOnLoadPickupPoints(); + } + private function buildOrderCacheKey(OrderInterface $order): string { $shippingAddress = $order->getShippingAddress(); diff --git a/src/Provider/FakerProvider.php b/src/Provider/FakerProvider.php index 8c7e469..eac2ac1 100644 --- a/src/Provider/FakerProvider.php +++ b/src/Provider/FakerProvider.php @@ -82,6 +82,7 @@ private function createFakePickupPoint(string $index, ?string $countryCode = nul $pickupPoint->setCountry($countryCode); $pickupPoint->setLatitude($this->faker->latitude); $pickupPoint->setLongitude($this->faker->longitude); + $pickupPoint->setUpdatedAt(new \DateTime()); return $pickupPoint; } diff --git a/src/Provider/LocalProvider.php b/src/Provider/LocalProvider.php index b6a2e4e..520a281 100644 --- a/src/Provider/LocalProvider.php +++ b/src/Provider/LocalProvider.php @@ -54,4 +54,9 @@ public function getName(): string { return $this->decoratedProvider->getName(); } + + public function cleanupOnLoadPickupPoints(): bool + { + return $this->decoratedProvider->cleanupOnLoadPickupPoints(); + } } diff --git a/src/Provider/Provider.php b/src/Provider/Provider.php index 04f30d4..ed66662 100644 --- a/src/Provider/Provider.php +++ b/src/Provider/Provider.php @@ -10,4 +10,9 @@ public function __toString(): string { return $this->getCode(); } + + public function cleanupOnLoadPickupPoints(): bool + { + return false; + } } diff --git a/src/Provider/ProviderInterface.php b/src/Provider/ProviderInterface.php index 5e2e13e..bab5cf1 100644 --- a/src/Provider/ProviderInterface.php +++ b/src/Provider/ProviderInterface.php @@ -37,4 +37,6 @@ public function findPickupPoint(PickupPointCode $code): ?PickupPointInterface; * @return iterable|PickupPointInterface[] */ public function findAllPickupPoints(): iterable; + + public function cleanupOnLoadPickupPoints(): bool; } diff --git a/src/Repository/PickupPointRepositoryInterface.php b/src/Repository/PickupPointRepositoryInterface.php index dbafce7..b4617f0 100644 --- a/src/Repository/PickupPointRepositoryInterface.php +++ b/src/Repository/PickupPointRepositoryInterface.php @@ -17,4 +17,6 @@ public function findOneByCode(PickupPointCode $code): ?PickupPointInterface; * @psalm-return list */ public function findByOrder(OrderInterface $order, string $provider): array; + + public function deleteOlderThan(\DateTimeInterface $dateTime, string $provider): void; } diff --git a/src/Resources/config/doctrine/model/PickupPoint.orm.xml b/src/Resources/config/doctrine/model/PickupPoint.orm.xml index 5e9d65d..3c18c45 100644 --- a/src/Resources/config/doctrine/model/PickupPoint.orm.xml +++ b/src/Resources/config/doctrine/model/PickupPoint.orm.xml @@ -15,5 +15,6 @@ + diff --git a/src/Resources/public/js/setono-pickup-point.js b/src/Resources/public/js/setono-pickup-point.js index 271300f..62542f4 100644 --- a/src/Resources/public/js/setono-pickup-point.js +++ b/src/Resources/public/js/setono-pickup-point.js @@ -7,9 +7,16 @@ let pickupPoints = { shippingMethods: document.querySelectorAll('input.input-shipping-method'), pickupPointChoices: {}, lastChosenPickupPointId: null, + pickupPointLabel: document.querySelectorAll('label.setono-sylius-pickup-point-label'), init: function (args) { this.searchUrl = args.searchUrl; + if (this.pickupPointLabel.length > 0) { + this.pickupPointLabel.forEach((element) => { + this.showLabel(element); + }); + } + if (0 === this.pickupPointShippingMethods.length) { return; } @@ -100,4 +107,21 @@ let pickupPoints = { return content; }, + showLabel: function (element) { + const url = element.getAttribute('data-url'); + if (url === null) { + return; + } + const xhttp = new XMLHttpRequest(); + // eslint-disable-next-line func-names + xhttp.onreadystatechange = function () { + if (xhttp.readyState === 4 && xhttp.status === 200) { + const result = JSON.parse(xhttp.response); + element.innerHTML += `${result.full_address}`; + element.style.display = element.style.display === 'none' ? 'block' : 'none'; + } + }; + xhttp.open('GET', url, false); + xhttp.send(); + }, };