Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show pickup point full address, removed by: #26 #80

Open
wants to merge 12 commits into
base: 1.x
Choose a base branch
from
18 changes: 18 additions & 0 deletions src/Doctrine/ORM/PickupPointRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
8 changes: 8 additions & 0 deletions src/EventListener/AddIndicesSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 9 additions & 1 deletion src/Message/Handler/LoadPickupPointsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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());
Expand All @@ -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) {
Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/Model/PickupPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class PickupPoint implements PickupPointInterface

protected ?float $longitude = null;

protected ?\DateTimeInterface $updatedAt = null;

public function getId(): ?int
{
return $this->id;
Expand Down Expand Up @@ -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;
}
}
4 changes: 4 additions & 0 deletions src/Model/PickupPointInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
5 changes: 5 additions & 0 deletions src/Provider/CachedProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/Provider/FakerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
5 changes: 5 additions & 0 deletions src/Provider/LocalProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,9 @@ public function getName(): string
{
return $this->decoratedProvider->getName();
}

public function cleanupOnLoadPickupPoints(): bool
{
return $this->decoratedProvider->cleanupOnLoadPickupPoints();
}
}
5 changes: 5 additions & 0 deletions src/Provider/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ public function __toString(): string
{
return $this->getCode();
}

public function cleanupOnLoadPickupPoints(): bool
{
return false;
}
}
2 changes: 2 additions & 0 deletions src/Provider/ProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ public function findPickupPoint(PickupPointCode $code): ?PickupPointInterface;
* @return iterable<PickupPointInterface>|PickupPointInterface[]
*/
public function findAllPickupPoints(): iterable;

public function cleanupOnLoadPickupPoints(): bool;
}
2 changes: 2 additions & 0 deletions src/Repository/PickupPointRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ public function findOneByCode(PickupPointCode $code): ?PickupPointInterface;
* @psalm-return list<PickupPointInterface>
*/
public function findByOrder(OrderInterface $order, string $provider): array;

public function deleteOlderThan(\DateTimeInterface $dateTime, string $provider): void;
}
1 change: 1 addition & 0 deletions src/Resources/config/doctrine/model/PickupPoint.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
<field name="country" column="country" type="string"/>
<field name="latitude" nullable="true" type="decimal" precision="10" scale="8"/>
<field name="longitude" nullable="true" type="decimal" precision="11" scale="8"/>
<field name="updatedAt" nullable="true" type="datetime"/>
</mapped-superclass>
</doctrine-mapping>
24 changes: 24 additions & 0 deletions src/Resources/public/js/setono-pickup-point.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 += `<i>${result.full_address}</i>`;
element.style.display = element.style.display === 'none' ? 'block' : 'none';
}
};
xhttp.open('GET', url, false);
xhttp.send();
},
};