Skip to content

Commit

Permalink
Catalog config update script after one catalog decision (#426)
Browse files Browse the repository at this point in the history
* Product set ids cleared while uninstalling integration

* Cyclic module removed

* Post FBE onboarding Catalog sync triggered

* Added ajx call to trigger all products and category upload after MBE onboarding flow

* Unused config removed

* Post one catalog migration flow catalog update script added

* Commerce sync flag updated

* Typo fixed

* Static errors fixed

* Catalog sync enabled for commerce partner integration id

* static errors fixed

* PR comments fixed

* exceptions being caught

* Catalog config update script added

* Redundant code removed

* Shipping profile sync disabled based on meta commerce sync disabled flag

* phpdocs added

* static errors resolved

* Unit test passed

* unit tests fixed

* Catalog code moved to catalog module

* Do not call comment added for script

* Commerce sync disable code removed

* Extra line removed

* events updated

---------

Co-authored-by: Alex Zarichnyi <zlik@fb.com>
  • Loading branch information
singhramanpreett and zlik committed Sep 12, 2023
1 parent c873aff commit a58e1d7
Show file tree
Hide file tree
Showing 10 changed files with 303 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
namespace Meta\BusinessExtension\Controller\Adminhtml\Ajax;

use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Event\ManagerInterface as EventManager;
use Magento\Framework\Exception\LocalizedException;
use Meta\BusinessExtension\Helper\FBEHelper;
use Magento\Framework\App\Action\HttpDeleteActionInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Security\Model\AdminSessionsManager;
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
use Magento\Framework\App\ResourceConnection;
use Meta\BusinessExtension\Model\ResourceModel\FacebookInstalledFeature;

class Fbdeleteasset implements HttpDeleteActionInterface
Expand All @@ -53,11 +53,6 @@ class Fbdeleteasset implements HttpDeleteActionInterface
*/
private $adminSessionManager;

/**
* @var ResourceConnection
*/
private $resourceConnection;

/**
* @var SystemConfig
*/
Expand All @@ -73,31 +68,36 @@ class Fbdeleteasset implements HttpDeleteActionInterface
*/
private $fbeInstalledFeatureResource;

/**
* @var EventManager
*/
private EventManager $eventManager;

/**
* @param JsonFactory $resultJsonFactory
* @param FBEHelper $fbeHelper
* @param AdminSessionsManager $adminSessionManager
* @param ResourceConnection $resourceConnection
* @param SystemConfig $systemConfig
* @param RequestInterface $request
* @param FacebookInstalledFeature $fbeInstalledFeatureResource
* @param EventManager $eventManager
*/
public function __construct(
JsonFactory $resultJsonFactory,
FBEHelper $fbeHelper,
AdminSessionsManager $adminSessionManager,
ResourceConnection $resourceConnection,
SystemConfig $systemConfig,
RequestInterface $request,
FacebookInstalledFeature $fbeInstalledFeatureResource
FacebookInstalledFeature $fbeInstalledFeatureResource,
EventManager $eventManager
) {
$this->resultJsonFactory = $resultJsonFactory;
$this->fbeHelper = $fbeHelper;
$this->adminSessionManager = $adminSessionManager;
$this->resourceConnection = $resourceConnection;
$this->systemConfig = $systemConfig;
$this->request = $request;
$this->fbeInstalledFeatureResource = $fbeInstalledFeatureResource;
$this->eventManager = $eventManager;
}

/**
Expand Down Expand Up @@ -146,6 +146,9 @@ public function executeForJson()
try {
$this->deleteConfigKeys($storeId);
$this->deleteInstalledFeatures($storeId);

$this->eventManager->dispatch('facebook_delete_assets_after', ['store_id' => $storeId]);

$response = [
'success' => true,
'message' => __('' . self::DELETE_SUCCESS_MESSAGE),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class PostFBEOnboardingSync extends AbstractAjax
* @param JsonFactory $resultJsonFactory
* @param FBEHelper $fbeHelper
* @param SystemConfig $systemConfig
* @param EventManager $eventManager
*/
public function __construct(
Context $context,
Expand Down Expand Up @@ -99,7 +100,6 @@ public function executeForJson(): array
));
return $response;
}

// Dispatch the facebook_post_fbe_onboarding_sync event,
// so observers in other Meta modules can subscribe and trigger their syncs,
// such as full catalog sync, and shipping profiles sync
Expand Down
132 changes: 132 additions & 0 deletions app/code/Meta/BusinessExtension/Helper/CatalogConfigUpdateHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Meta\BusinessExtension\Helper;

use Magento\Framework\Event\ManagerInterface as EventManager;
use Meta\BusinessExtension\Model\System\Config as SystemConfig;

class CatalogConfigUpdateHelper
{
/**
* @var FBEHelper
*/
private FBEHelper $fbeHelper;

/**
* @var SystemConfig
*/
private SystemConfig $systemConfig;

/**
* @var EventManager
*/
private EventManager $eventManager;

/**
* @param FBEHelper $fbeHelper
* @param SystemConfig $systemConfig
* @param EventManager $eventManager
*/
public function __construct(
FBEHelper $fbeHelper,
SystemConfig $systemConfig,
EventManager $eventManager
) {
$this->fbeHelper = $fbeHelper;
$this->systemConfig = $systemConfig;
$this->eventManager = $eventManager;
}

/**
* Function updates catalog config if updated on Meta side after onboarding flow completion on Meta side.
* Function enables Meta Commerce Sync and do full catalog sync.
* Do not call it apart from catalog update webhook event, it may cause stale state for catalog integration.
*
* @param int $storeId
* @param string $catalogId
* @param string $commercePartnerIntegrationId
* @param string $pixelId
* @return void
*/
public function updateCatalogConfiguration(
int $storeId,
string $catalogId,
string $commercePartnerIntegrationId,
string $pixelId
): void {
$oldCatalogId = $this->systemConfig->getCatalogId($storeId);
try {
// if Catalog id is updated, only then we update the config
if ($oldCatalogId != $catalogId) {
// updates catalog id
$this->systemConfig->saveConfig(
SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_CATALOG_ID,
$catalogId,
$storeId
);
// updates commerce partner integration id
$this->systemConfig->saveConfig(
SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_COMMERCE_PARTNER_INTEGRATION_ID,
$commercePartnerIntegrationId,
$storeId
);
// updates pixel id
$this->systemConfig->saveConfig(
SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_PIXEL_ID,
$pixelId,
$storeId
);
// delete feed id as new catalog would be having new feed id.
$this->systemConfig->deleteConfig(
SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_FEED_ID,
$storeId
);
// Dispatch the facebook_update_catalog_configuration event
// so observer in other Meta modules can subscribe and update required configurations
// along with configuration updated in this file.
// for e.g. clears all the product set ids for categories for old catalog
$this->eventManager->dispatch('facebook_update_catalog_configuration', ['store_id' => $storeId]);

$this->fbeHelper->log('Catalog configuration updated and stale product set ids cleared --- '
. $commercePartnerIntegrationId);

}

$this->systemConfig->cleanCache();

// Dispatch the facebook_update_catalog_configuration_after event,
// so observers in other Meta modules can subscribe and trigger their syncs,
// such as full catalog sync, and shipping profiles sync
$this->eventManager->dispatch('facebook_update_catalog_configuration_after', ['store_id' => $storeId]);
} catch (\Throwable $e) {
$context = [
'store_id' => $storeId,
'event' => 'update_catalog_config',
'event_type' => 'update_catalog_config',
'catalog_id' => $catalogId,
'old_catalog_id' => $oldCatalogId,
'commerce_partner_integration_id' => $commercePartnerIntegrationId,
'pixel_id' => $pixelId
];
$this->fbeHelper->logExceptionImmediatelyToMeta($e, $context);
}
}
}
2 changes: 2 additions & 0 deletions app/code/Meta/BusinessExtension/Model/System/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
*/
class Config
{
public const META_PRODUCT_SET_ID = 'meta_product_set_id';
public const CATEGORY_SYNC_TO_FACEBOOK = 'sync_to_facebook_catalog';
private const VERSION_CACHE_KEY = 'meta-business-extension-version';
private const EXTENSION_PACKAGE_NAME = 'meta/meta-for-magento2';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
use Magento\Security\Model\AdminSessionsManager;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Event\ManagerInterface as EventManager;
use Meta\BusinessExtension\Helper\FBEHelper;
use Meta\BusinessExtension\Controller\Adminhtml\Ajax\Fbdeleteasset;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Magento\Framework\App\ResourceConnection;
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
use Meta\BusinessExtension\Model\ResourceModel\FacebookInstalledFeature;

Expand Down Expand Up @@ -77,18 +77,18 @@ public function setUp(): void
$resultJsonFactory = $this->createMock(JsonFactory::class);
$this->fbeHelper = $this->createMock(FBEHelper::class);
$sessionManager = $this->createMock(AdminSessionsManager::class);
$resourceConnection = $this->createMock(ResourceConnection::class);
$this->systemConfig = $this->createMock(SystemConfig::class);
$this->fbeInstalledFeatureResource = $this->createMock(FacebookInstalledFeature::class);
$this->request = $this->createMock(\Magento\Framework\App\RequestInterface::class);
$eventManager = $this->createMock(EventManager::class);
$this->fbdeleteasset = new Fbdeleteasset(
$resultJsonFactory,
$this->fbeHelper,
$sessionManager,
$resourceConnection,
$this->systemConfig,
$this->request,
$this->fbeInstalledFeatureResource
$this->fbeInstalledFeatureResource,
$eventManager
);
}

Expand Down

0 comments on commit a58e1d7

Please sign in to comment.