Skip to content

Commit

Permalink
Installation request to Dooplugins (#299)
Browse files Browse the repository at this point in the history
* Installation request to Dooplugins
  • Loading branch information
eduardogomez97 committed Jan 24, 2024
1 parent 254ab8d commit 0afbdca
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 35 deletions.
2 changes: 1 addition & 1 deletion ApiClient/ManagementClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
52 changes: 21 additions & 31 deletions Controller/Adminhtml/Integration/CreateStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() .
Expand All @@ -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
];
}

Expand Down Expand Up @@ -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"];
}
}
2 changes: 1 addition & 1 deletion Helper/StoreConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Doofinder_Feed" setup_version="0.13.8">
<module name="Doofinder_Feed" setup_version="0.13.9">
<sequence>
<module name="Magento_Integration" />
</sequence>
Expand Down

0 comments on commit 0afbdca

Please sign in to comment.