From 62fb3339ad888d0d532434f6f714825d473557a9 Mon Sep 17 00:00:00 2001 From: "John.R" Date: Wed, 29 Nov 2023 17:05:15 +0100 Subject: [PATCH 01/13] feat: add debounce --- ps_eventbus.php | 16 +++++--- sql/install.sql | 6 +++ sql/uninstall.sql | 1 + src/Repository/LiveSyncRepository.php | 56 ++++++++++++++++++++++++++ src/Service/SynchronizationService.php | 25 ++++++++++++ 5 files changed, 99 insertions(+), 5 deletions(-) create mode 100644 src/Repository/LiveSyncRepository.php diff --git a/ps_eventbus.php b/ps_eventbus.php index 4f46e245..fa190e7b 100644 --- a/ps_eventbus.php +++ b/ps_eventbus.php @@ -1339,11 +1339,17 @@ private function sendLiveSync($shopContents, $shopContentId, $action) if ((int) $shopContentId === 0) { return; } - try { - /** @var \PrestaShop\Module\PsEventbus\Api\LiveSyncApiClient $liveSyncApiClient */ - $liveSyncApiClient = $this->getService(\PrestaShop\Module\PsEventbus\Api\LiveSyncApiClient::class); - $liveSyncApiClient->liveSync($shopContents, (int) $shopContentId, $action); - } catch (\Exception $e) { + + /** @var \PrestaShop\Module\PsEventbus\Service\SynchronizationService $synchronizationService */ + $synchronizationService = $this->getService(PrestaShop\Module\PsEventbus\Service\SynchronizationService::class); + + if ($synchronizationService->debounceLiveSync($shopContents)) { + try { + /** @var \PrestaShop\Module\PsEventbus\Api\LiveSyncApiClient $liveSyncApiClient */ + $liveSyncApiClient = $this->getService(\PrestaShop\Module\PsEventbus\Api\LiveSyncApiClient::class); + $liveSyncApiClient->liveSync($shopContents, (int) $shopContentId, $action); + } catch (\Exception $e) { + } } } diff --git a/sql/install.sql b/sql/install.sql index 0b0f5d09..19cb6239 100644 --- a/sql/install.sql +++ b/sql/install.sql @@ -36,3 +36,9 @@ CREATE TABLE IF NOT EXISTS `PREFIX_eventbus_incremental_sync` PRIMARY KEY (`type`, `id_object`, `id_shop`, `lang_iso`) ) ENGINE = ENGINE_TYPE DEFAULT CHARSET = utf8; + +CREATE TABLE IF NOT EXISTS `PREFIX_eventbus_live_sync` +( + `shop_content` VARCHAR(50) NOT NULL, + `last_change_at` DATETIME NOT NULL, +) diff --git a/sql/uninstall.sql b/sql/uninstall.sql index 73456a13..436d0d28 100644 --- a/sql/uninstall.sql +++ b/sql/uninstall.sql @@ -2,3 +2,4 @@ DROP TABLE IF EXISTS `PREFIX_eventbus_type_sync`; DROP TABLE IF EXISTS `PREFIX_eventbus_job`; DROP TABLE IF EXISTS `PREFIX_eventbus_deleted_objects`; DROP TABLE IF EXISTS `PREFIX_eventbus_incremental_sync`; +DROP TABLE IF EXISTS `PREFIX_eventbus_live_sync`; diff --git a/src/Repository/LiveSyncRepository.php b/src/Repository/LiveSyncRepository.php new file mode 100644 index 00000000..e8612041 --- /dev/null +++ b/src/Repository/LiveSyncRepository.php @@ -0,0 +1,56 @@ +db = $db; + } + + /** + * @param string $shopContent + * + * @return array|bool|\mysqli_result|\PDOStatement|resource|null + * + * @throws \PrestaShopDatabaseException + */ + public function getLastChangeAtByShopContent(string $shopContent) + { + $query = ' + SELECT `last_change_at` + FROM `' . _DB_PREFIX_ . 'eventbus_live_sync_debounce` `eb_debounce` + WHERE `eb_debounce`.`shop_content` = "' . pSQL($shopContent) . '" + LIMIT 1 + '; + + $result = $this->db->executeS($query); + + return $result ? $result[0]['last_change_at'] : null; + } + + /** + * @param string $shopContent + * @param Date $lastChangeAt + * + * @return bool|\mysqli_result|\PDOStatement|resource + * + * @throws \PrestaShopDatabaseException + */ + public function upsertDebounce(string $shopContent, string $lastChangeAt) + { + $query = ' + INSERT INTO `' . _DB_PREFIX_ . 'eventbus_live_sync_debounce` (`shop_content`, `last_change_at`) + VALUES ("' . pSQL($shopContent) . '", "' . pSQL($lastChangeAt) . '") + ON DUPLICATE KEY UPDATE `last_change_at` = "' . pSQL($lastChangeAt) . '" + '; + + return $this->db->execute($query); + } +} diff --git a/src/Service/SynchronizationService.php b/src/Service/SynchronizationService.php index ca6a6987..8f8d40a3 100644 --- a/src/Service/SynchronizationService.php +++ b/src/Service/SynchronizationService.php @@ -8,6 +8,7 @@ use PrestaShop\Module\PsEventbus\Provider\PaginatedApiDataProviderInterface; use PrestaShop\Module\PsEventbus\Repository\EventbusSyncRepository; use PrestaShop\Module\PsEventbus\Repository\IncrementalSyncRepository; +use PrestaShop\Module\PsEventbus\Repository\LiveSyncRepository; class SynchronizationService { @@ -19,6 +20,10 @@ class SynchronizationService * @var IncrementalSyncRepository */ private $incrementalSyncRepository; + /** + * @var LiveSyncRepository + */ + private $liveSyncRepository; /** * @var ProxyServiceInterface */ @@ -31,11 +36,13 @@ class SynchronizationService public function __construct( EventbusSyncRepository $eventbusSyncRepository, IncrementalSyncRepository $incrementalSyncRepository, + LiveSyncRepository $liveSyncRepository, ProxyServiceInterface $proxyService, PayloadDecorator $payloadDecorator ) { $this->eventbusSyncRepository = $eventbusSyncRepository; $this->incrementalSyncRepository = $incrementalSyncRepository; + $this->liveSyncRepository = $liveSyncRepository; $this->proxyService = $proxyService; $this->payloadDecorator = $payloadDecorator; } @@ -129,6 +136,24 @@ public function handleIncrementalSync(PaginatedApiDataProviderInterface $dataPro return $this->returnSyncResponse($data, $response, $remainingObjects); } + public function debounceLiveSync($shopContents) + { + $dateNow = date('Y-m-d H:i:s'); + $lastChangeAt = $this->liveSyncRepository->getLastChangeAtByShopContent($shopContents); + + if ($lastChangeAt !== null) { + $lastChangeAt = strtotime($lastChangeAt); + $dateNow = strtotime($dateNow); + $diff = $dateNow - $lastChangeAt; + + if ($diff < 60 * 5) { + return false; + } + + return $this->liveSyncRepository->upsertDebounce($shopContents, $dateNow); + } + } + /** * @param array $data * @param array $syncResponse From c496b985b4d5b64e2b4066c48bebac85821bf17c Mon Sep 17 00:00:00 2001 From: "John.R" Date: Thu, 30 Nov 2023 10:09:07 +0100 Subject: [PATCH 02/13] fix: lint --- src/Repository/LiveSyncRepository.php | 4 ++-- src/Service/SynchronizationService.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Repository/LiveSyncRepository.php b/src/Repository/LiveSyncRepository.php index e8612041..3d70a693 100644 --- a/src/Repository/LiveSyncRepository.php +++ b/src/Repository/LiveSyncRepository.php @@ -29,7 +29,7 @@ public function getLastChangeAtByShopContent(string $shopContent) WHERE `eb_debounce`.`shop_content` = "' . pSQL($shopContent) . '" LIMIT 1 '; - + $result = $this->db->executeS($query); return $result ? $result[0]['last_change_at'] : null; @@ -50,7 +50,7 @@ public function upsertDebounce(string $shopContent, string $lastChangeAt) VALUES ("' . pSQL($shopContent) . '", "' . pSQL($lastChangeAt) . '") ON DUPLICATE KEY UPDATE `last_change_at` = "' . pSQL($lastChangeAt) . '" '; - + return $this->db->execute($query); } } diff --git a/src/Service/SynchronizationService.php b/src/Service/SynchronizationService.php index 8f8d40a3..3e0e9541 100644 --- a/src/Service/SynchronizationService.php +++ b/src/Service/SynchronizationService.php @@ -136,7 +136,7 @@ public function handleIncrementalSync(PaginatedApiDataProviderInterface $dataPro return $this->returnSyncResponse($data, $response, $remainingObjects); } - public function debounceLiveSync($shopContents) + public function debounceLiveSync($shopContents) { $dateNow = date('Y-m-d H:i:s'); $lastChangeAt = $this->liveSyncRepository->getLastChangeAtByShopContent($shopContents); From b7993da882963544629e6282154ef79328c61b10 Mon Sep 17 00:00:00 2001 From: "John.R" Date: Thu, 30 Nov 2023 11:27:18 +0100 Subject: [PATCH 03/13] feat: rework debounce --- config/front/repository.yml | 6 +++++ ps_eventbus.php | 6 +++-- src/Repository/LiveSyncRepository.php | 23 +++++++++++------ src/Service/SynchronizationService.php | 35 ++++++++++++++++++-------- 4 files changed, 50 insertions(+), 20 deletions(-) diff --git a/config/front/repository.yml b/config/front/repository.yml index ed957ec2..e6be2fe4 100644 --- a/config/front/repository.yml +++ b/config/front/repository.yml @@ -258,3 +258,9 @@ services: public: true arguments: - "@ps_eventbus.db" + + PrestaShop\Module\PsEventbus\Repository\LiveSyncRepository: + class: PrestaShop\Module\PsEventbus\Repository\LiveSyncRepository + public: true + arguments: + - "@ps_eventbus.db" diff --git a/ps_eventbus.php b/ps_eventbus.php index fa190e7b..c6e01490 100644 --- a/ps_eventbus.php +++ b/ps_eventbus.php @@ -1343,11 +1343,13 @@ private function sendLiveSync($shopContents, $shopContentId, $action) /** @var \PrestaShop\Module\PsEventbus\Service\SynchronizationService $synchronizationService */ $synchronizationService = $this->getService(PrestaShop\Module\PsEventbus\Service\SynchronizationService::class); - if ($synchronizationService->debounceLiveSync($shopContents)) { + $shopContentNeedsSync = $synchronizationService->debounceLiveSync($shopContents); + + if (count($shopContentNeedsSync) > 0) { try { /** @var \PrestaShop\Module\PsEventbus\Api\LiveSyncApiClient $liveSyncApiClient */ $liveSyncApiClient = $this->getService(\PrestaShop\Module\PsEventbus\Api\LiveSyncApiClient::class); - $liveSyncApiClient->liveSync($shopContents, (int) $shopContentId, $action); + $liveSyncApiClient->liveSync($shopContentNeedsSync, (int) $shopContentId, $action); } catch (\Exception $e) { } } diff --git a/src/Repository/LiveSyncRepository.php b/src/Repository/LiveSyncRepository.php index 3d70a693..563ee118 100644 --- a/src/Repository/LiveSyncRepository.php +++ b/src/Repository/LiveSyncRepository.php @@ -9,7 +9,10 @@ class LiveSyncRepository */ private $db; - public function __construct(\Db $db, \Context $context) + /** + * @param \Db $db + */ + public function __construct(\Db $db) { $this->db = $db; } @@ -17,7 +20,7 @@ public function __construct(\Db $db, \Context $context) /** * @param string $shopContent * - * @return array|bool|\mysqli_result|\PDOStatement|resource|null + * @return string|null * * @throws \PrestaShopDatabaseException */ @@ -25,28 +28,32 @@ public function getLastChangeAtByShopContent(string $shopContent) { $query = ' SELECT `last_change_at` - FROM `' . _DB_PREFIX_ . 'eventbus_live_sync_debounce` `eb_debounce` - WHERE `eb_debounce`.`shop_content` = "' . pSQL($shopContent) . '" + FROM `' . _DB_PREFIX_ . 'eventbus_live_sync` `eb_ls` + WHERE `eb_ls`.`shop_content` = "' . pSQL($shopContent) . '" LIMIT 1 '; $result = $this->db->executeS($query); - return $result ? $result[0]['last_change_at'] : null; + if (is_array($result)) { + return $result[0]['last_change_at']; + } + + return null; } /** * @param string $shopContent - * @param Date $lastChangeAt + * @param string $lastChangeAt * - * @return bool|\mysqli_result|\PDOStatement|resource + * @return bool * * @throws \PrestaShopDatabaseException */ public function upsertDebounce(string $shopContent, string $lastChangeAt) { $query = ' - INSERT INTO `' . _DB_PREFIX_ . 'eventbus_live_sync_debounce` (`shop_content`, `last_change_at`) + INSERT INTO `' . _DB_PREFIX_ . 'eventbus_live_sync` (`shop_content`, `last_change_at`) VALUES ("' . pSQL($shopContent) . '", "' . pSQL($lastChangeAt) . '") ON DUPLICATE KEY UPDATE `last_change_at` = "' . pSQL($lastChangeAt) . '" '; diff --git a/src/Service/SynchronizationService.php b/src/Service/SynchronizationService.php index 3e0e9541..b87f351e 100644 --- a/src/Service/SynchronizationService.php +++ b/src/Service/SynchronizationService.php @@ -136,22 +136,37 @@ public function handleIncrementalSync(PaginatedApiDataProviderInterface $dataPro return $this->returnSyncResponse($data, $response, $remainingObjects); } - public function debounceLiveSync($shopContents) + /** + * @param array $shopContents + * + * @return array + * + * @throws \PrestaShopDatabaseException + */ + public function debounceLiveSync(array $shopContents) { $dateNow = date('Y-m-d H:i:s'); - $lastChangeAt = $this->liveSyncRepository->getLastChangeAtByShopContent($shopContents); + $shopContentsUpdated = []; - if ($lastChangeAt !== null) { - $lastChangeAt = strtotime($lastChangeAt); - $dateNow = strtotime($dateNow); - $diff = $dateNow - $lastChangeAt; + foreach ($shopContents as $shopContent) { + $lastChangeAt = $this->liveSyncRepository->getLastChangeAtByShopContent($shopContent); - if ($diff < 60 * 5) { - return false; - } + if ($lastChangeAt !== null) { + $lastChangeAtInt = strtotime((string) $lastChangeAt); + $dateNowInt = strtotime((string) $dateNow); + $diff = $dateNowInt - $lastChangeAtInt; + + if ($diff > 60 * 5) { + $result = $this->liveSyncRepository->upsertDebounce($shopContent, $dateNow); - return $this->liveSyncRepository->upsertDebounce($shopContents, $dateNow); + if ($result === true) { + array_push($shopContentsUpdated, $shopContent); + } + } + } } + + return $shopContentsUpdated; } /** From dab5100c2c2a8df7faee50e37543dff7ffb9f52b Mon Sep 17 00:00:00 2001 From: "John.R" Date: Tue, 5 Dec 2023 17:18:17 +0100 Subject: [PATCH 04/13] fix: debounce --- config/front/services.yml | 1 + ps_eventbus.php | 99 +++++++++++++------------- sql/install.sql | 5 +- src/Repository/LiveSyncRepository.php | 6 +- src/Service/SynchronizationService.php | 29 +++----- 5 files changed, 71 insertions(+), 69 deletions(-) diff --git a/config/front/services.yml b/config/front/services.yml index 60c94539..da27c2a2 100644 --- a/config/front/services.yml +++ b/config/front/services.yml @@ -61,6 +61,7 @@ services: arguments: - '@PrestaShop\Module\PsEventbus\Repository\EventbusSyncRepository' - '@PrestaShop\Module\PsEventbus\Repository\IncrementalSyncRepository' + - '@PrestaShop\Module\PsEventbus\Repository\LiveSyncRepository' - '@PrestaShop\Module\PsEventbus\Service\ProxyServiceInterface' - '@PrestaShop\Module\PsEventbus\Decorator\PayloadDecorator' diff --git a/ps_eventbus.php b/ps_eventbus.php index c6e01490..b733af10 100644 --- a/ps_eventbus.php +++ b/ps_eventbus.php @@ -241,7 +241,7 @@ public function hookActionObjectImageDeleteAfter($parameters) { $image = $parameters['object']; if (isset($image->id_product)) { - $this->sendLiveSync(['products'], $image->id_product, 'delete'); + $this->sendLiveSync('products', $image->id_product, 'delete'); $this->insertIncrementalSyncObject( $image->id_product, Config::COLLECTION_PRODUCTS, @@ -261,7 +261,7 @@ public function hookActionObjectImageAddAfter($parameters) { $image = $parameters['object']; if (isset($image->id_product)) { - $this->sendLiveSync(['products'], $image->id_product, 'upsert'); + $this->sendLiveSync('products', $image->id_product, 'upsert'); $this->insertIncrementalSyncObject( $image->id_product, Config::COLLECTION_PRODUCTS, @@ -281,7 +281,7 @@ public function hookActionObjectImageUpdateAfter($parameters) { $image = $parameters['object']; if (isset($image->id_product)) { - $this->sendLiveSync(['products'], $image->id_product, 'upsert'); + $this->sendLiveSync('products', $image->id_product, 'upsert'); $this->insertIncrementalSyncObject( $image->id_product, Config::COLLECTION_PRODUCTS, @@ -301,7 +301,7 @@ public function hookActionObjectLanguageDeleteAfter($parameters) { $language = $parameters['object']; if (isset($language->id)) { - $this->sendLiveSync(['languages'], $language->id, 'delete'); + $this->sendLiveSync('languages', $language->id, 'delete'); $this->insertDeletedObject( $language->id, Config::COLLECTION_LANGUAGES, @@ -320,7 +320,7 @@ public function hookActionObjectLanguageAddAfter($parameters) { $language = $parameters['object']; if (isset($language->id) && isset($language->id_product)) { - $this->sendLiveSync(['languages'], $language->id_product, 'upsert'); + $this->sendLiveSync('languages', $language->id_product, 'upsert'); $this->insertIncrementalSyncObject( $language->id, Config::COLLECTION_LANGUAGES, @@ -340,7 +340,7 @@ public function hookActionObjectLanguageUpdateAfter($parameters) { $language = $parameters['object']; if (isset($language->id) && isset($language->id_product)) { - $this->sendLiveSync(['languages'], $language->id_product, 'upsert'); + $this->sendLiveSync('languages', $language->id_product, 'upsert'); $this->insertIncrementalSyncObject( $language->id, Config::COLLECTION_LANGUAGES, @@ -360,7 +360,7 @@ public function hookActionObjectManufacturerDeleteAfter($parameters) { $manufacturer = $parameters['object']; if (isset($manufacturer->id)) { - $this->sendLiveSync(['manufacturers'], $manufacturer->id, 'delete'); + $this->sendLiveSync('manufacturers', $manufacturer->id, 'delete'); $this->insertDeletedObject( $manufacturer->id, Config::COLLECTION_MANUFACTURERS, @@ -379,7 +379,7 @@ public function hookActionObjectManufacturerAddAfter($parameters) { $manufacturer = $parameters['object']; if (isset($manufacturer->id)) { - $this->sendLiveSync(['manufacturers'], $manufacturer->id, 'upsert'); + $this->sendLiveSync('manufacturers', $manufacturer->id, 'upsert'); $this->insertIncrementalSyncObject( $manufacturer->id, Config::COLLECTION_MANUFACTURERS, @@ -399,7 +399,7 @@ public function hookActionObjectManufacturerUpdateAfter($parameters) { $manufacturer = $parameters['object']; if (isset($manufacturer->id)) { - $this->sendLiveSync(['manufacturers'], $manufacturer->id, 'upsert'); + $this->sendLiveSync('manufacturers', $manufacturer->id, 'upsert'); $this->insertIncrementalSyncObject( $manufacturer->id, Config::COLLECTION_MANUFACTURERS, @@ -419,7 +419,7 @@ public function hookActionObjectSupplierDeleteAfter($parameters) { $supplier = $parameters['object']; if (isset($supplier->id)) { - $this->sendLiveSync(['suppliers'], $supplier->id, 'delete'); + $this->sendLiveSync('suppliers', $supplier->id, 'delete'); $this->insertDeletedObject( $supplier->id, Config::COLLECTION_SUPPLIERS, @@ -438,7 +438,7 @@ public function hookActionObjectSupplierAddAfter($parameters) { $supplier = $parameters['object']; if (isset($supplier->id)) { - $this->sendLiveSync(['suppliers'], $supplier->id, 'upsert'); + $this->sendLiveSync('suppliers', $supplier->id, 'upsert'); $this->insertIncrementalSyncObject( $supplier->id, Config::COLLECTION_SUPPLIERS, @@ -458,7 +458,7 @@ public function hookActionObjectSupplierUpdateAfter($parameters) { $supplier = $parameters['object']; if (isset($supplier->id)) { - $this->sendLiveSync(['suppliers'], $supplier->id, 'upsert'); + $this->sendLiveSync('suppliers', $supplier->id, 'upsert'); $this->insertIncrementalSyncObject( $supplier->id, Config::COLLECTION_SUPPLIERS, @@ -479,7 +479,7 @@ public function hookActionObjectProductDeleteAfter($parameters) $product = $parameters['object']; if (isset($product->id)) { - $this->sendLiveSync(['products'], $product->id, 'delete'); + $this->sendLiveSync('products', $product->id, 'delete'); $this->insertDeletedObject( $product->id, Config::COLLECTION_PRODUCTS, @@ -498,7 +498,9 @@ public function hookActionObjectProductAddAfter($parameters) { $product = $parameters['object']; if (isset($product->id)) { - $this->sendLiveSync(['products', 'custom-product-carriers', 'stocks'], $product->id, 'upsert'); + $this->sendLiveSync('products', $product->id, 'upsert'); + $this->sendLiveSync('custom-product-carriers', $product->id, 'upsert'); + $this->sendLiveSync('stocks', $product->id, 'upsert'); $this->insertIncrementalSyncObject( $product->id, @@ -537,7 +539,10 @@ public function hookActionObjectProductUpdateAfter($parameters) $product = $parameters['object']; if (isset($product->id)) { - $this->sendLiveSync(['products', 'custom-product-carriers', 'stocks'], $product->id, 'upsert'); + $this->sendLiveSync('products', $product->id, 'upsert'); + $this->sendLiveSync('custom-product-carriers', $product->id, 'upsert'); + $this->sendLiveSync('stocks', $product->id, 'upsert'); + $this->insertIncrementalSyncObject( $product->id, Config::COLLECTION_PRODUCTS, @@ -571,7 +576,7 @@ public function hookActionObjectWishlistDeleteAfter($parameters) { $wishlist = $parameters['object']; if (isset($wishlist->id)) { - $this->sendLiveSync(['wishlists'], $wishlist->id, 'delete'); + $this->sendLiveSync('wishlists', $wishlist->id, 'delete'); $this->insertDeletedObject( $wishlist->id, Config::COLLECTION_WISHLISTS, @@ -590,7 +595,7 @@ public function hookActionObjectWishlistAddAfter($parameters) { $wishlist = $parameters['object']; if (isset($wishlist->id)) { - $this->sendLiveSync(['wishlists'], $wishlist->id, 'upsert'); + $this->sendLiveSync('wishlists', $wishlist->id, 'upsert'); $this->insertIncrementalSyncObject( $wishlist->id, Config::COLLECTION_WISHLISTS, @@ -610,7 +615,7 @@ public function hookActionObjectWishlistUpdateAfter($parameters) { $wishlist = $parameters['object']; if (isset($wishlist->id)) { - $this->sendLiveSync(['wishlists'], $wishlist->id, 'upsert'); + $this->sendLiveSync('wishlists', $wishlist->id, 'upsert'); $this->insertIncrementalSyncObject( $wishlist->id, Config::COLLECTION_WISHLISTS, @@ -630,7 +635,7 @@ public function hookActionObjectStockAddAfter($parameters) { $stock = $parameters['object']; if (isset($stock->id)) { - $this->sendLiveSync(['stocks'], $stock->id, 'upsert'); + $this->sendLiveSync('stocks', $stock->id, 'upsert'); $this->insertIncrementalSyncObject( $stock->id, Config::COLLECTION_STOCKS, @@ -650,7 +655,7 @@ public function hookActionObjectStockUpdateAfter($parameters) { $stock = $parameters['object']; if (isset($stock->id)) { - $this->sendLiveSync(['stocks'], $stock->id, 'upsert'); + $this->sendLiveSync('stocks', $stock->id, 'upsert'); $this->insertIncrementalSyncObject( $stock->id, Config::COLLECTION_STOCKS, @@ -670,7 +675,7 @@ public function hookActionObjectStoreDeleteAfter($parameters) { $product = $parameters['object']; if (isset($product->id)) { - $this->sendLiveSync(['stores'], $product->id, 'delete'); + $this->sendLiveSync('stores', $product->id, 'delete'); $this->insertDeletedObject( $product->id, Config::COLLECTION_STORES, @@ -689,7 +694,7 @@ public function hookActionObjectStoreAddAfter($parameters) { $product = $parameters['object']; if (isset($product->id)) { - $this->sendLiveSync(['stores'], $product->id, 'upsert'); + $this->sendLiveSync('stores', $product->id, 'upsert'); $this->insertIncrementalSyncObject( $product->id, Config::COLLECTION_STORES, @@ -709,7 +714,7 @@ public function hookActionObjectStoreUpdateAfter($parameters) { $store = $parameters['object']; if (isset($store->id)) { - $this->sendLiveSync(['stores'], $store->id, 'upsert'); + $this->sendLiveSync('stores', $store->id, 'upsert'); $this->insertIncrementalSyncObject( $store->id, Config::COLLECTION_STORES, @@ -731,7 +736,7 @@ public function hookActionObjectCombinationDeleteAfter($parameters) $combination = $parameters['object']; if (isset($combination->id)) { - $this->sendLiveSync(['products'], $combination->id, 'delete'); + $this->sendLiveSync('products', $combination->id, 'delete'); $this->insertDeletedObject( $combination->id, Config::COLLECTION_PRODUCT_ATTRIBUTES, @@ -751,7 +756,7 @@ public function hookActionObjectCategoryAddAfter($parameters) $category = $parameters['object']; if (isset($category->id)) { - $this->sendLiveSync(['categories'], $category->id, 'upsert'); + $this->sendLiveSync('categories', $category->id, 'upsert'); $this->insertIncrementalSyncObject( $category->id, Config::COLLECTION_CATEGORIES, @@ -772,7 +777,7 @@ public function hookActionObjectCategoryUpdateAfter($parameters) $category = $parameters['object']; if (isset($category->id)) { - $this->sendLiveSync(['categories'], $category->id, 'upsert'); + $this->sendLiveSync('categories', $category->id, 'upsert'); $this->insertIncrementalSyncObject( $category->id, Config::COLLECTION_CATEGORIES, @@ -793,7 +798,7 @@ public function hookActionObjectCategoryDeleteAfter($parameters) $category = $parameters['object']; if (isset($category->id)) { - $this->sendLiveSync(['categories'], $category->id, 'delete'); + $this->sendLiveSync('categories', $category->id, 'delete'); $this->insertDeletedObject( $category->id, Config::COLLECTION_CATEGORIES, @@ -813,7 +818,7 @@ public function hookActionObjectCustomerAddAfter($parameters) $customer = $parameters['object']; if (isset($customer->id)) { - $this->sendLiveSync(['customers'], $customer->id, 'upsert'); + $this->sendLiveSync('customers', $customer->id, 'upsert'); $this->insertIncrementalSyncObject( $customer->id, Config::COLLECTION_CUSTOMERS, @@ -834,7 +839,7 @@ public function hookActionObjectCustomerUpdateAfter($parameters) $customer = $parameters['object']; if (isset($customer->id)) { - $this->sendLiveSync(['customers'], $customer->id, 'upsert'); + $this->sendLiveSync('customers', $customer->id, 'upsert'); $this->insertIncrementalSyncObject( $customer->id, Config::COLLECTION_CUSTOMERS, @@ -855,7 +860,7 @@ public function hookActionObjectCustomerDeleteAfter($parameters) $customer = $parameters['object']; if (isset($customer->id)) { - $this->sendLiveSync(['customers'], $customer->id, 'delete'); + $this->sendLiveSync('customers', $customer->id, 'delete'); $this->insertDeletedObject( $customer->id, Config::COLLECTION_CUSTOMERS, @@ -875,7 +880,7 @@ public function hookActionObjectCurrencyAddAfter($parameters) $currency = $parameters['object']; if (isset($currency->id)) { - $this->sendLiveSync(['currencies'], $currency->id, 'upsert'); + $this->sendLiveSync('currencies', $currency->id, 'upsert'); $this->insertIncrementalSyncObject( $currency->id, Config::COLLECTION_CURRENCIES, @@ -896,7 +901,7 @@ public function hookActionObjectCurrencyUpdateAfter($parameters) $currency = $parameters['object']; if (isset($currency->id)) { - $this->sendLiveSync(['currencies'], $currency->id, 'upsert'); + $this->sendLiveSync('currencies', $currency->id, 'upsert'); $this->insertIncrementalSyncObject( $currency->id, Config::COLLECTION_CURRENCIES, @@ -917,7 +922,7 @@ public function hookActionObjectCartAddAfter($parameters) $cart = $parameters['object']; if (isset($cart->id)) { - $this->sendLiveSync(['carts'], $cart->id, 'upsert'); + $this->sendLiveSync('carts', $cart->id, 'upsert'); $this->insertIncrementalSyncObject( $cart->id, Config::COLLECTION_CARTS, @@ -937,7 +942,7 @@ public function hookActionObjectCartUpdateAfter($parameters) $cart = $parameters['object']; if (isset($cart->id)) { - $this->sendLiveSync(['carts'], $cart->id, 'upsert'); + $this->sendLiveSync('carts', $cart->id, 'upsert'); $this->insertIncrementalSyncObject( $cart->id, Config::COLLECTION_CARTS, @@ -957,7 +962,7 @@ public function hookActionObjectOrderAddAfter($parameters) $order = $parameters['object']; if (isset($order->id)) { - $this->sendLiveSync(['orders'], $order->id, 'upsert'); + $this->sendLiveSync('orders', $order->id, 'upsert'); $this->insertIncrementalSyncObject( $order->id, Config::COLLECTION_ORDERS, @@ -977,7 +982,7 @@ public function hookActionObjectOrderUpdateAfter($parameters) $order = $parameters['object']; if (isset($order->id)) { - $this->sendLiveSync(['orders'], $order->id, 'upsert'); + $this->sendLiveSync('orders', $order->id, 'upsert'); $this->insertIncrementalSyncObject( $order->id, Config::COLLECTION_ORDERS, @@ -998,7 +1003,7 @@ public function hookActionObjectCarrierAddAfter($parameters) $carrier = $parameters['object']; if (isset($carrier->id)) { - $this->sendLiveSync(['carriers'], $carrier->id, 'upsert'); + $this->sendLiveSync('carriers', $carrier->id, 'upsert'); $this->insertIncrementalSyncObject( $carrier->id, Config::COLLECTION_CARRIERS, @@ -1019,7 +1024,7 @@ public function hookActionObjectCarrierUpdateAfter($parameters) $carrier = $parameters['object']; if (isset($carrier->id)) { - $this->sendLiveSync(['carriers'], $carrier->id, 'upsert'); + $this->sendLiveSync('carriers', $carrier->id, 'upsert'); $this->insertIncrementalSyncObject( $carrier->id, Config::COLLECTION_CARRIERS, @@ -1040,7 +1045,7 @@ public function hookActionObjectCarrierDeleteAfter($parameters) $carrier = $parameters['object']; if (isset($carrier->id)) { - $this->sendLiveSync(['carriers'], $carrier->id, 'delete'); + $this->sendLiveSync('carriers', $carrier->id, 'delete'); $this->insertIncrementalSyncObject( $carrier->id, Config::COLLECTION_CARRIERS, @@ -1270,7 +1275,7 @@ public function hookActionObjectSpecificPriceAddAfter($parameters) if ($specificPrice instanceof SpecificPrice) { if (isset($specificPrice->id)) { - $this->sendLiveSync(['specific-prices'], $specificPrice->id, 'upsert'); + $this->sendLiveSync('specific-prics', $specificPrice->id, 'upsert'); $this->insertIncrementalSyncObject( $specificPrice->id, Config::COLLECTION_SPECIFIC_PRICES, @@ -1293,7 +1298,7 @@ public function hookActionObjectSpecificPriceUpdateAfter($parameters) if ($specificPrice instanceof SpecificPrice) { if (isset($specificPrice->id)) { - $this->sendLiveSync(['specific-prices'], $specificPrice->id, 'upsert'); + $this->sendLiveSync('specific-prics', $specificPrice->id, 'upsert'); $this->insertIncrementalSyncObject( $specificPrice->id, Config::COLLECTION_SPECIFIC_PRICES, @@ -1316,7 +1321,7 @@ public function hookActionObjectSpecificPriceDeleteAfter($parameters) if ($specificPrice instanceof SpecificPrice) { if (isset($specificPrice->id)) { - $this->sendLiveSync(['specific-prices'], $specificPrice->id, 'delete'); + $this->sendLiveSync('specific-prics', $specificPrice->id, 'delete'); $this->insertDeletedObject( $specificPrice->id, Config::COLLECTION_SPECIFIC_PRICES, @@ -1328,13 +1333,13 @@ public function hookActionObjectSpecificPriceDeleteAfter($parameters) } /** - * @param array $shopContents + * @param string $shopContent * @param int $shopContentId * @param string $action * * @return void */ - private function sendLiveSync($shopContents, $shopContentId, $action) + private function sendLiveSync(string $shopContent, int $shopContentId, string $action) { if ((int) $shopContentId === 0) { return; @@ -1343,13 +1348,11 @@ private function sendLiveSync($shopContents, $shopContentId, $action) /** @var \PrestaShop\Module\PsEventbus\Service\SynchronizationService $synchronizationService */ $synchronizationService = $this->getService(PrestaShop\Module\PsEventbus\Service\SynchronizationService::class); - $shopContentNeedsSync = $synchronizationService->debounceLiveSync($shopContents); - - if (count($shopContentNeedsSync) > 0) { + if ($synchronizationService->debounceLiveSync($shopContent)) { try { /** @var \PrestaShop\Module\PsEventbus\Api\LiveSyncApiClient $liveSyncApiClient */ $liveSyncApiClient = $this->getService(\PrestaShop\Module\PsEventbus\Api\LiveSyncApiClient::class); - $liveSyncApiClient->liveSync($shopContentNeedsSync, (int) $shopContentId, $action); + $liveSyncApiClient->liveSync($shopContent, (int) $shopContentId, $action); } catch (\Exception $e) { } } diff --git a/sql/install.sql b/sql/install.sql index 19cb6239..a30b00c5 100644 --- a/sql/install.sql +++ b/sql/install.sql @@ -40,5 +40,6 @@ CREATE TABLE IF NOT EXISTS `PREFIX_eventbus_incremental_sync` CREATE TABLE IF NOT EXISTS `PREFIX_eventbus_live_sync` ( `shop_content` VARCHAR(50) NOT NULL, - `last_change_at` DATETIME NOT NULL, -) + `last_change_at` DATETIME NOT NULL +) ENGINE = ENGINE_TYPE + DEFAULT CHARSET = utf8; diff --git a/src/Repository/LiveSyncRepository.php b/src/Repository/LiveSyncRepository.php index 563ee118..30f34554 100644 --- a/src/Repository/LiveSyncRepository.php +++ b/src/Repository/LiveSyncRepository.php @@ -2,6 +2,10 @@ namespace PrestaShop\Module\PsEventbus\Repository; +use mysqli_result; +use PDOStatement; +use PrestaShopDatabaseException; + class LiveSyncRepository { /** @@ -35,7 +39,7 @@ public function getLastChangeAtByShopContent(string $shopContent) $result = $this->db->executeS($query); - if (is_array($result)) { + if (count($result) > 0) { return $result[0]['last_change_at']; } diff --git a/src/Service/SynchronizationService.php b/src/Service/SynchronizationService.php index b87f351e..4442bd33 100644 --- a/src/Service/SynchronizationService.php +++ b/src/Service/SynchronizationService.php @@ -137,36 +137,29 @@ public function handleIncrementalSync(PaginatedApiDataProviderInterface $dataPro } /** - * @param array $shopContents + * @param string $shopContent * - * @return array + * @return bool * * @throws \PrestaShopDatabaseException */ - public function debounceLiveSync(array $shopContents) + public function debounceLiveSync(string $shopContent) { $dateNow = date('Y-m-d H:i:s'); - $shopContentsUpdated = []; - foreach ($shopContents as $shopContent) { - $lastChangeAt = $this->liveSyncRepository->getLastChangeAtByShopContent($shopContent); + $lastChangeAt = $this->liveSyncRepository->getLastChangeAtByShopContent($shopContent); - if ($lastChangeAt !== null) { - $lastChangeAtInt = strtotime((string) $lastChangeAt); - $dateNowInt = strtotime((string) $dateNow); - $diff = $dateNowInt - $lastChangeAtInt; + $lastChangeAtInt = strtotime((string) $lastChangeAt === null ? $dateNow : $lastChangeAt); + $dateNowInt = strtotime((string) $dateNow); + $diff = $dateNowInt - $lastChangeAtInt; - if ($diff > 60 * 5) { - $result = $this->liveSyncRepository->upsertDebounce($shopContent, $dateNow); + if ($diff > 60 * 5) { + $this->liveSyncRepository->upsertDebounce($shopContent, $dateNow); - if ($result === true) { - array_push($shopContentsUpdated, $shopContent); - } - } - } + return true; } - return $shopContentsUpdated; + return false; } /** From c8c651a0b9c80108ce25cd386fe61b340a4e534f Mon Sep 17 00:00:00 2001 From: "John.R" Date: Wed, 6 Dec 2023 09:38:26 +0100 Subject: [PATCH 05/13] fix: phpstan --- src/Api/LiveSyncApiClient.php | 6 +++--- src/Repository/LiveSyncRepository.php | 2 +- src/Service/SynchronizationService.php | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Api/LiveSyncApiClient.php b/src/Api/LiveSyncApiClient.php index 6103471b..98262026 100644 --- a/src/Api/LiveSyncApiClient.php +++ b/src/Api/LiveSyncApiClient.php @@ -65,13 +65,13 @@ private function getClient($timeout = Config::SYNC_API_MAX_TIMEOUT) } /** - * @param array $shopContents + * @param string $shopContent * @param int $shopContentId * @param string $action * * @return array */ - public function liveSync($shopContents, $shopContentId, $action) + public function liveSync(string $shopContent, int $shopContentId, string $action) { $rawResponse = $this->getClient(3)->sendRequest( new Request( @@ -83,7 +83,7 @@ public function liveSync($shopContents, $shopContentId, $action) 'User-Agent' => 'ps-eventbus/' . $this->module->version, 'Content-Type' => 'application/json', ], - '{"shopContents":' . json_encode($shopContents) . ', "shopContentId": ' . $shopContentId . ', "action": "' . $action . '"}' + '{"shopContents":' . json_encode($shopContent) . ', "shopContentId": ' . $shopContentId . ', "action": "' . $action . '"}' ) ); diff --git a/src/Repository/LiveSyncRepository.php b/src/Repository/LiveSyncRepository.php index 30f34554..3ed40134 100644 --- a/src/Repository/LiveSyncRepository.php +++ b/src/Repository/LiveSyncRepository.php @@ -39,7 +39,7 @@ public function getLastChangeAtByShopContent(string $shopContent) $result = $this->db->executeS($query); - if (count($result) > 0) { + if (is_array($result) && count($result) > 0) { return $result[0]['last_change_at']; } diff --git a/src/Service/SynchronizationService.php b/src/Service/SynchronizationService.php index 4442bd33..b8d5ae7c 100644 --- a/src/Service/SynchronizationService.php +++ b/src/Service/SynchronizationService.php @@ -149,7 +149,7 @@ public function debounceLiveSync(string $shopContent) $lastChangeAt = $this->liveSyncRepository->getLastChangeAtByShopContent($shopContent); - $lastChangeAtInt = strtotime((string) $lastChangeAt === null ? $dateNow : $lastChangeAt); + $lastChangeAtInt = strtotime((string) $lastChangeAt == null ? (string) $dateNow : (string) $lastChangeAt); $dateNowInt = strtotime((string) $dateNow); $diff = $dateNowInt - $lastChangeAtInt; From 19058f5b31b7e3f98bd09163f807f05336b40777 Mon Sep 17 00:00:00 2001 From: "John.R" Date: Wed, 6 Dec 2023 09:53:33 +0100 Subject: [PATCH 06/13] fix: php-cs-fixer --- src/Repository/LiveSyncRepository.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Repository/LiveSyncRepository.php b/src/Repository/LiveSyncRepository.php index 3ed40134..ba4df6c7 100644 --- a/src/Repository/LiveSyncRepository.php +++ b/src/Repository/LiveSyncRepository.php @@ -2,10 +2,6 @@ namespace PrestaShop\Module\PsEventbus\Repository; -use mysqli_result; -use PDOStatement; -use PrestaShopDatabaseException; - class LiveSyncRepository { /** From 133ee52d51a6188a54c76bb274f74a30e855f68a Mon Sep 17 00:00:00 2001 From: "John.R" Date: Wed, 6 Dec 2023 17:07:51 +0100 Subject: [PATCH 07/13] fix: livesync debounce --- sql/install.sql | 3 ++- src/Repository/LiveSyncRepository.php | 11 +++++------ src/Service/SynchronizationService.php | 14 ++++++-------- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/sql/install.sql b/sql/install.sql index a30b00c5..45b38aab 100644 --- a/sql/install.sql +++ b/sql/install.sql @@ -40,6 +40,7 @@ CREATE TABLE IF NOT EXISTS `PREFIX_eventbus_incremental_sync` CREATE TABLE IF NOT EXISTS `PREFIX_eventbus_live_sync` ( `shop_content` VARCHAR(50) NOT NULL, - `last_change_at` DATETIME NOT NULL + `last_change_at` DATETIME NOT NULL, + PRIMARY KEY (`shop_content`) ) ENGINE = ENGINE_TYPE DEFAULT CHARSET = utf8; diff --git a/src/Repository/LiveSyncRepository.php b/src/Repository/LiveSyncRepository.php index ba4df6c7..47eb1e3f 100644 --- a/src/Repository/LiveSyncRepository.php +++ b/src/Repository/LiveSyncRepository.php @@ -24,19 +24,18 @@ public function __construct(\Db $db) * * @throws \PrestaShopDatabaseException */ - public function getLastChangeAtByShopContent(string $shopContent) + public function getShopContentInfos(string $shopContent) { $query = ' - SELECT `last_change_at` + SELECT `eb_ls`.`shop_content`, `eb_ls`.`last_change_at` FROM `' . _DB_PREFIX_ . 'eventbus_live_sync` `eb_ls` - WHERE `eb_ls`.`shop_content` = "' . pSQL($shopContent) . '" - LIMIT 1 + WHERE `eb_ls`.`shop_content` = "' . pSQL($shopContent) . '"; '; $result = $this->db->executeS($query); if (is_array($result) && count($result) > 0) { - return $result[0]['last_change_at']; + return $result[0]; } return null; @@ -55,7 +54,7 @@ public function upsertDebounce(string $shopContent, string $lastChangeAt) $query = ' INSERT INTO `' . _DB_PREFIX_ . 'eventbus_live_sync` (`shop_content`, `last_change_at`) VALUES ("' . pSQL($shopContent) . '", "' . pSQL($lastChangeAt) . '") - ON DUPLICATE KEY UPDATE `last_change_at` = "' . pSQL($lastChangeAt) . '" + ON DUPLICATE KEY UPDATE `last_change_at` = "' . pSQL($lastChangeAt) . '"; '; return $this->db->execute($query); diff --git a/src/Service/SynchronizationService.php b/src/Service/SynchronizationService.php index b8d5ae7c..344ede43 100644 --- a/src/Service/SynchronizationService.php +++ b/src/Service/SynchronizationService.php @@ -143,19 +143,17 @@ public function handleIncrementalSync(PaginatedApiDataProviderInterface $dataPro * * @throws \PrestaShopDatabaseException */ - public function debounceLiveSync(string $shopContent) + public function debounceLiveSync(string $shopContentName) { $dateNow = date('Y-m-d H:i:s'); - $lastChangeAt = $this->liveSyncRepository->getLastChangeAtByShopContent($shopContent); + $shopContent = $this->liveSyncRepository->getShopContentInfos($shopContentName); - $lastChangeAtInt = strtotime((string) $lastChangeAt == null ? (string) $dateNow : (string) $lastChangeAt); - $dateNowInt = strtotime((string) $dateNow); - $diff = $dateNowInt - $lastChangeAtInt; - - if ($diff > 60 * 5) { - $this->liveSyncRepository->upsertDebounce($shopContent, $dateNow); + $lastChangeAt = $shopContent != null ? (string) $shopContent['last_change_at'] : (string) $dateNow; + $diff = strtotime((string) $dateNow) - strtotime((string) $lastChangeAt); + if ($shopContent == null || $diff > 60 * 5) { + $this->liveSyncRepository->upsertDebounce($shopContentName, $dateNow); return true; } From 5a4ad26202225eeeecbb2a31bf0a90d3b4cd9c0a Mon Sep 17 00:00:00 2001 From: "John.R" Date: Wed, 6 Dec 2023 17:10:16 +0100 Subject: [PATCH 08/13] fix: phpstan and phpcsfixer --- src/Repository/LiveSyncRepository.php | 2 +- src/Service/SynchronizationService.php | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Repository/LiveSyncRepository.php b/src/Repository/LiveSyncRepository.php index 47eb1e3f..5b9f982b 100644 --- a/src/Repository/LiveSyncRepository.php +++ b/src/Repository/LiveSyncRepository.php @@ -20,7 +20,7 @@ public function __construct(\Db $db) /** * @param string $shopContent * - * @return string|null + * @return array|null * * @throws \PrestaShopDatabaseException */ diff --git a/src/Service/SynchronizationService.php b/src/Service/SynchronizationService.php index 344ede43..92a1acd2 100644 --- a/src/Service/SynchronizationService.php +++ b/src/Service/SynchronizationService.php @@ -137,7 +137,7 @@ public function handleIncrementalSync(PaginatedApiDataProviderInterface $dataPro } /** - * @param string $shopContent + * @param string $shopContentName * * @return bool * @@ -154,6 +154,7 @@ public function debounceLiveSync(string $shopContentName) if ($shopContent == null || $diff > 60 * 5) { $this->liveSyncRepository->upsertDebounce($shopContentName, $dateNow); + return true; } From e6c728922b9fc78e94d6f77b32b37c5c24f165f4 Mon Sep 17 00:00:00 2001 From: "John.R" Date: Thu, 7 Dec 2023 17:05:11 +0100 Subject: [PATCH 09/13] fix: add upgrade script --- upgrade/Upgrade-2.3.5.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 upgrade/Upgrade-2.3.5.php diff --git a/upgrade/Upgrade-2.3.5.php b/upgrade/Upgrade-2.3.5.php new file mode 100644 index 00000000..211a8d26 --- /dev/null +++ b/upgrade/Upgrade-2.3.5.php @@ -0,0 +1,20 @@ +execute($query); + + return true; +} From f11eccfc8ad4414adee63b9622d219a0181d4128 Mon Sep 17 00:00:00 2001 From: "John.R" Date: Thu, 7 Dec 2023 17:07:23 +0100 Subject: [PATCH 10/13] fix: interpolate var --- src/Api/LiveSyncApiClient.php | 2 +- src/Api/SyncApiClient.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Api/LiveSyncApiClient.php b/src/Api/LiveSyncApiClient.php index 98262026..1e2e41f4 100644 --- a/src/Api/LiveSyncApiClient.php +++ b/src/Api/LiveSyncApiClient.php @@ -83,7 +83,7 @@ public function liveSync(string $shopContent, int $shopContentId, string $action 'User-Agent' => 'ps-eventbus/' . $this->module->version, 'Content-Type' => 'application/json', ], - '{"shopContents":' . json_encode($shopContent) . ', "shopContentId": ' . $shopContentId . ', "action": "' . $action . '"}' + '{"shopContents": ["' . $shopContent . '"], "shopContentId": ' . $shopContentId . ', "action": "' . $action . '"}' ) ); diff --git a/src/Api/SyncApiClient.php b/src/Api/SyncApiClient.php index cf0ce975..8a4bedda 100644 --- a/src/Api/SyncApiClient.php +++ b/src/Api/SyncApiClient.php @@ -90,13 +90,13 @@ public function validateJobId($jobId) } /** - * @param array $shopContents + * @param array $shopContent * @param int $shopContentId * @param string $action * * @return array */ - public function liveSync($shopContents, $shopContentId, $action) + public function liveSync($shopContent, $shopContentId, $action) { $rawResponse = $this->getClient(3)->sendRequest( new Request( @@ -108,7 +108,7 @@ public function liveSync($shopContents, $shopContentId, $action) 'User-Agent' => 'ps-eventbus/' . $this->module->version, 'Content-Type' => 'application/json', ], - '{"shopContents":' . json_encode($shopContents) . ', "shopContentId": ' . $shopContentId . ', "action": "' . $action . '"}' + '{"shopContents": ["' . $shopContent . '"], "shopContentId": ' . $shopContentId . ', "action": "' . $action . '"}' ) ); From 169f223bddaae2c9c696b668f75cf483f3f60015 Mon Sep 17 00:00:00 2001 From: "John.R" Date: Thu, 7 Dec 2023 17:08:08 +0100 Subject: [PATCH 11/13] fix: remove 's' in 'infos' word --- src/Repository/LiveSyncRepository.php | 2 +- src/Service/SynchronizationService.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Repository/LiveSyncRepository.php b/src/Repository/LiveSyncRepository.php index 5b9f982b..7cf5539a 100644 --- a/src/Repository/LiveSyncRepository.php +++ b/src/Repository/LiveSyncRepository.php @@ -24,7 +24,7 @@ public function __construct(\Db $db) * * @throws \PrestaShopDatabaseException */ - public function getShopContentInfos(string $shopContent) + public function getShopContentInfo(string $shopContent) { $query = ' SELECT `eb_ls`.`shop_content`, `eb_ls`.`last_change_at` diff --git a/src/Service/SynchronizationService.php b/src/Service/SynchronizationService.php index 92a1acd2..add29253 100644 --- a/src/Service/SynchronizationService.php +++ b/src/Service/SynchronizationService.php @@ -147,7 +147,7 @@ public function debounceLiveSync(string $shopContentName) { $dateNow = date('Y-m-d H:i:s'); - $shopContent = $this->liveSyncRepository->getShopContentInfos($shopContentName); + $shopContent = $this->liveSyncRepository->getShopContentInfo($shopContentName); $lastChangeAt = $shopContent != null ? (string) $shopContent['last_change_at'] : (string) $dateNow; $diff = strtotime((string) $dateNow) - strtotime((string) $lastChangeAt); From 5a2f1199a852a7e86feef9a6a02c05001600ef5b Mon Sep 17 00:00:00 2001 From: "John.R" Date: Thu, 7 Dec 2023 17:17:59 +0100 Subject: [PATCH 12/13] fix: revert interpolate var --- src/Api/LiveSyncApiClient.php | 2 +- src/Api/SyncApiClient.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Api/LiveSyncApiClient.php b/src/Api/LiveSyncApiClient.php index 1e2e41f4..98262026 100644 --- a/src/Api/LiveSyncApiClient.php +++ b/src/Api/LiveSyncApiClient.php @@ -83,7 +83,7 @@ public function liveSync(string $shopContent, int $shopContentId, string $action 'User-Agent' => 'ps-eventbus/' . $this->module->version, 'Content-Type' => 'application/json', ], - '{"shopContents": ["' . $shopContent . '"], "shopContentId": ' . $shopContentId . ', "action": "' . $action . '"}' + '{"shopContents":' . json_encode($shopContent) . ', "shopContentId": ' . $shopContentId . ', "action": "' . $action . '"}' ) ); diff --git a/src/Api/SyncApiClient.php b/src/Api/SyncApiClient.php index 8a4bedda..29f75e76 100644 --- a/src/Api/SyncApiClient.php +++ b/src/Api/SyncApiClient.php @@ -108,7 +108,7 @@ public function liveSync($shopContent, $shopContentId, $action) 'User-Agent' => 'ps-eventbus/' . $this->module->version, 'Content-Type' => 'application/json', ], - '{"shopContents": ["' . $shopContent . '"], "shopContentId": ' . $shopContentId . ', "action": "' . $action . '"}' + '{"shopContents":' . json_encode($shopContent) . ', "shopContentId": ' . $shopContentId . ', "action": "' . $action . '"}' ) ); From e00ece74237432ee4c269691dda83b95e73a232b Mon Sep 17 00:00:00 2001 From: "John.R" Date: Thu, 4 Jan 2024 11:31:03 +0100 Subject: [PATCH 13/13] fix: try to fix shop content type --- src/Api/LiveSyncApiClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Api/LiveSyncApiClient.php b/src/Api/LiveSyncApiClient.php index 98262026..1e2e41f4 100644 --- a/src/Api/LiveSyncApiClient.php +++ b/src/Api/LiveSyncApiClient.php @@ -83,7 +83,7 @@ public function liveSync(string $shopContent, int $shopContentId, string $action 'User-Agent' => 'ps-eventbus/' . $this->module->version, 'Content-Type' => 'application/json', ], - '{"shopContents":' . json_encode($shopContent) . ', "shopContentId": ' . $shopContentId . ', "action": "' . $action . '"}' + '{"shopContents": ["' . $shopContent . '"], "shopContentId": ' . $shopContentId . ', "action": "' . $action . '"}' ) );