diff --git a/config/front/repository.yml b/config/front/repository.yml index ac6c45f2..9d521d9e 100644 --- a/config/front/repository.yml +++ b/config/front/repository.yml @@ -271,3 +271,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/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 bc4c102f..b42f50cc 100644 --- a/ps_eventbus.php +++ b/ps_eventbus.php @@ -240,7 +240,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, @@ -260,7 +260,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, @@ -280,7 +280,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, @@ -300,7 +300,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, @@ -319,7 +319,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, @@ -339,7 +339,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, @@ -359,7 +359,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, @@ -378,7 +378,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, @@ -398,7 +398,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, @@ -418,7 +418,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, @@ -437,7 +437,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, @@ -457,7 +457,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, @@ -478,7 +478,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, @@ -497,7 +497,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, @@ -536,7 +538,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, @@ -570,7 +575,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, @@ -589,7 +594,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, @@ -609,7 +614,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, @@ -629,7 +634,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, @@ -649,7 +654,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, @@ -669,7 +674,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, @@ -688,7 +693,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, @@ -708,7 +713,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, @@ -730,7 +735,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, @@ -750,7 +755,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, @@ -771,7 +776,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, @@ -792,7 +797,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, @@ -812,7 +817,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, @@ -833,7 +838,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, @@ -854,7 +859,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, @@ -874,7 +879,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, @@ -895,7 +900,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, @@ -916,7 +921,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, @@ -936,7 +941,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, @@ -956,7 +961,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, @@ -976,7 +981,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, @@ -997,7 +1002,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, @@ -1018,7 +1023,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, @@ -1039,7 +1044,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, @@ -1269,7 +1274,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, @@ -1292,7 +1297,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, @@ -1315,7 +1320,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, @@ -1327,22 +1332,28 @@ 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; } - 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($shopContent)) { + try { + /** @var \PrestaShop\Module\PsEventbus\Api\LiveSyncApiClient $liveSyncApiClient */ + $liveSyncApiClient = $this->getService(\PrestaShop\Module\PsEventbus\Api\LiveSyncApiClient::class); + $liveSyncApiClient->liveSync($shopContent, (int) $shopContentId, $action); + } catch (\Exception $e) { + } } } diff --git a/sql/install.sql b/sql/install.sql index 0b0f5d09..45b38aab 100644 --- a/sql/install.sql +++ b/sql/install.sql @@ -36,3 +36,11 @@ 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, + PRIMARY KEY (`shop_content`) +) ENGINE = ENGINE_TYPE + DEFAULT CHARSET = utf8; 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/Api/LiveSyncApiClient.php b/src/Api/LiveSyncApiClient.php index 6103471b..1e2e41f4 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": ["' . $shopContent . '"], "shopContentId": ' . $shopContentId . ', "action": "' . $action . '"}' ) ); diff --git a/src/Api/SyncApiClient.php b/src/Api/SyncApiClient.php index cf0ce975..29f75e76 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":' . json_encode($shopContent) . ', "shopContentId": ' . $shopContentId . ', "action": "' . $action . '"}' ) ); diff --git a/src/Repository/LiveSyncRepository.php b/src/Repository/LiveSyncRepository.php new file mode 100644 index 00000000..7cf5539a --- /dev/null +++ b/src/Repository/LiveSyncRepository.php @@ -0,0 +1,62 @@ +db = $db; + } + + /** + * @param string $shopContent + * + * @return array|null + * + * @throws \PrestaShopDatabaseException + */ + public function getShopContentInfo(string $shopContent) + { + $query = ' + 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) . '"; + '; + + $result = $this->db->executeS($query); + + if (is_array($result) && count($result) > 0) { + return $result[0]; + } + + return null; + } + + /** + * @param string $shopContent + * @param string $lastChangeAt + * + * @return bool + * + * @throws \PrestaShopDatabaseException + */ + 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) . '"; + '; + + return $this->db->execute($query); + } +} diff --git a/src/Service/SynchronizationService.php b/src/Service/SynchronizationService.php index ca6a6987..add29253 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,31 @@ public function handleIncrementalSync(PaginatedApiDataProviderInterface $dataPro return $this->returnSyncResponse($data, $response, $remainingObjects); } + /** + * @param string $shopContentName + * + * @return bool + * + * @throws \PrestaShopDatabaseException + */ + public function debounceLiveSync(string $shopContentName) + { + $dateNow = date('Y-m-d H:i:s'); + + $shopContent = $this->liveSyncRepository->getShopContentInfo($shopContentName); + + $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; + } + + return false; + } + /** * @param array $data * @param array $syncResponse 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; +}