From 0afbdcad4ebe612b0e5264eeb54564ce4af3960e Mon Sep 17 00:00:00 2001 From: eduardogomez97 Date: Wed, 24 Jan 2024 15:45:02 +0100 Subject: [PATCH] Installation request to Dooplugins (#299) * Installation request to Dooplugins --- ApiClient/ManagementClient.php | 2 +- .../Adminhtml/Integration/CreateStore.php | 52 ++++++++----------- Helper/StoreConfig.php | 2 +- composer.json | 2 +- etc/module.xml | 2 +- 5 files changed, 25 insertions(+), 35 deletions(-) diff --git a/ApiClient/ManagementClient.php b/ApiClient/ManagementClient.php index b879fcf0..be110b1b 100644 --- a/ApiClient/ManagementClient.php +++ b/ApiClient/ManagementClient.php @@ -67,7 +67,7 @@ public function listSearchEngines(): array */ public function createStore(array $storeData): array { - $response = $this->client->post('/plugins/create-store', $storeData); + $response = $this->client->post('/install', $storeData); return json_decode($response, true); } diff --git a/Controller/Adminhtml/Integration/CreateStore.php b/Controller/Adminhtml/Integration/CreateStore.php index ef4a5ee0..e082df6f 100644 --- a/Controller/Adminhtml/Integration/CreateStore.php +++ b/Controller/Adminhtml/Integration/CreateStore.php @@ -101,21 +101,22 @@ public function generateDoofinderStores() $websiteId = (int)$storeGroup->getWebsiteId(); $searchEngineData = $this->generateSearchEngineData($storeGroupId); $storeOptions = $this->generateStoreOptions($websiteId); + $primary_language = $this->storeConfig->getLanguageFromStore($storeGroup->getDefaultStore()); $storeGroupConfig = [ "name" => $storeGroup->getName(), "platform" => "magento2", - "primary_language" => $this->storeConfig->getLanguageFromStore($storeGroup->getDefaultStore()), + "primary_language" => $primary_language, "skip_indexation" => false, - "callback_urls" => $searchEngineData["callbackUrls"], "sector" => $this->storeConfig->getValueFromConfig(StoreConfig::SECTOR_VALUE_CONFIG), + "site_url" => $this->get_primary_site_url_in_se($searchEngineData["searchEngineConfig"], $primary_language), "search_engines" => $searchEngineData["searchEngineConfig"], "options" => $storeOptions, "query_input" => "#search" ]; $response = $this->storeConfig->createStore($storeGroupConfig); $this->saveInstallationConfig((int)$storeGroupId, $response["installation_id"], $response["script"]); - $this->saveSearchEngineConfig($searchEngineData["storesConfig"], $response["search_engines"]); + $this->saveSearchEngineConfig($searchEngineData["storesConfig"], $response["config"]["search_engines"]); } catch (Exception $e) { $success = false; $this->logger->error('Error creating store for store group "' . $storeGroup->getName() . @@ -132,41 +133,31 @@ public function generateSearchEngineData($storeGroupId) { $searchEngineConfig = []; $storesConfig = []; - $callbackUrls = []; foreach ($this->storeConfig->getStoreGroupStores($storeGroupId) as $store) { $language = $this->storeConfig->getLanguageFromStore($store); $currency = strtoupper($store->getCurrentCurrency()->getCode()); + $store_id = $store->getId(); + $base_url = $store->getBaseUrl(); // store_id field refers to store_view's id. $searchEngineConfig[] = [ "name" => $store->getName(), "language" => $language, "currency" => $currency, - "site_url" => $store->getBaseUrl(), - "datatypes" => [ - [ - "name" => "product", - "preset" => "product", - 'datasources' => [ - [ - 'type' => 'magento2', - 'options' => [ - 'url' => $this->urlInterface->getBaseUrl() . 'rest/' . $store->getCode() . '/V1/', - 'store_id' => $store->getId() - ] - ] - ] - ] + "site_url" => $base_url, + "callback_url" => $base_url . 'doofinderfeed/setup/processCallback?storeId=' . $store_id, + "options" => [ + "store_id" => $store_id, + "index_url" => $base_url . 'rest/' . $store->getCode() . '/V1/' ] ]; - $storesConfig[$language][$currency] = (int)$store->getId(); - $callbackUrls[$language][$currency] = $this->getProcessCallbackUrl($store); + + $storesConfig[$language][$currency] = (int)$store_id; } return [ "searchEngineConfig" => $searchEngineConfig, - "storesConfig" => $storesConfig, - "callbackUrls" => $callbackUrls + "storesConfig" => $storesConfig ]; } @@ -268,14 +259,13 @@ private function cleanCache() } /** - * Get Process Callback URL - * - * @param StoreInterface $store - * - * @return string + * We obtain the url associated with the main language search_engine */ - private function getProcessCallbackUrl(StoreInterface $store): string - { - return $store->getBaseUrl() . 'doofinderfeed/setup/processCallback?storeId=' . $store->getId(); + private function get_primary_site_url_in_se($search_engines, $primary_language) { + $primary_search_engine = array_values(array_filter($search_engines, function ($search_engine) use ($primary_language) { + return $search_engine["language"] == $primary_language; + }))[0]; + + return $primary_search_engine["site_url"]; } } diff --git a/Helper/StoreConfig.php b/Helper/StoreConfig.php index 6f6d631c..c9938566 100644 --- a/Helper/StoreConfig.php +++ b/Helper/StoreConfig.php @@ -236,7 +236,7 @@ public function __construct( */ public function createStore(array $storeData): array { - $managementClient = $this->managementClientFactory->create(['apiType' => 'admin']); + $managementClient = $this->managementClientFactory->create(['apiType' => 'dooplugins']); return $managementClient->createStore($storeData); } diff --git a/composer.json b/composer.json index 773bfa2e..bcdccd21 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "doofinder/doofinder-magento2", - "version": "0.13.8", + "version": "0.13.9", "description": "Doofinder module for Magento 2", "type": "magento2-module", "require": { diff --git a/etc/module.xml b/etc/module.xml index 94f4eeb3..a1d418d3 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,6 +1,6 @@ - +