Skip to content

Commit

Permalink
Merge pull request #28 from AfterShip/webhook
Browse files Browse the repository at this point in the history
catch all exception when getting extension filed on stores api
  • Loading branch information
yangyao committed Jun 19, 2024
2 parents f0a6199 + ea7a95c commit 3aa81bb
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Constants
public const WEBHOOK_RESOURCE_ORDERS = 'orders';
public const WEBHOOK_RESOURCE_PRODUCTS = 'products';

public const AFTERSHIP_TIKTOK_SHOP_VERSION = '1.0.16';
public const AFTERSHIP_TIKTOK_SHOP_VERSION = '1.0.17';

public const WEBHOOK_CONFIG_SCOPE_PATH = 'aftership/webhooks/webhooks';

Expand Down
68 changes: 46 additions & 22 deletions Plugin/StoreConfigExtensionAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Magento\Store\Api\StoreConfigManagerInterface;
use Magento\Store\Api\Data\StoreConfigExtensionFactory;
use AfterShip\TikTokShop\Constants;
use Psr\Log\LoggerInterface;

/**
* Display permission & plugin version on store api.
Expand All @@ -26,6 +27,10 @@
*/
class StoreConfigExtensionAttributes
{
/**
* @var LoggerInterface
*/
protected $logger;
/**
* UserContextInterface Instance.
*
Expand Down Expand Up @@ -53,11 +58,12 @@ class StoreConfigExtensionAttributes
* @param IntegrationServiceInterface $integrationService
*/
public function __construct(
LoggerInterface $logger,
StoreConfigExtensionFactory $storeConfigExtensionFactory,
UserContextInterface $userContext,
IntegrationServiceInterface $integrationService
) {

$this->logger = $logger;
$this->storeConfigExtensionFactory = $storeConfigExtensionFactory;
$this->userContext = $userContext;
$this->integrationService = $integrationService;
Expand All @@ -70,11 +76,20 @@ public function __construct(
*/
public function getApiScopes()
{
$integrationId = $this->userContext->getUserId();
$apiScopes = '';
if ($integrationId) {
$scopes = $this->integrationService->getSelectedResources($integrationId);
$apiScopes = is_array($scopes) ? implode(',', $scopes) : $scopes;
try{
$integrationId = $this->userContext->getUserId();
if ($integrationId) {
$scopes = $this->integrationService->getSelectedResources($integrationId);
$apiScopes = is_array($scopes) ? implode(',', $scopes) : $scopes;
}
}catch (\Exception $e){
$this->logger->error(
sprintf(
'[AfterShip TikTokShop] Failed to get api scopes for integration, %s',
$e->getMessage()
)
);
}
return $apiScopes;
}
Expand All @@ -99,24 +114,33 @@ public function getVersion()
*/
public function afterGetStoreConfigs(StoreConfigManagerInterface $subject, $result)
{
foreach ($result as $store) {
$extensionAttributes = $store->getExtensionAttributes();
if (!$extensionAttributes) {
$extensionAttributes = $this->storeConfigExtensionFactory->create();
}
// setPermissions method is generated by extension_attributes.xml.
if (method_exists($extensionAttributes, 'setPermissions')) {
call_user_func_array([$extensionAttributes, 'setPermissions'], [$this->getApiScopes()]);
}
if (method_exists($extensionAttributes, 'setAftershipTiktok')) {
call_user_func_array([$extensionAttributes, 'setAftershipTiktok'], [$this->getVersion()]);
}
// Pass Upgrade compatibility tool check.
if (method_exists($extensionAttributes, 'setData')) {
call_user_func_array([$extensionAttributes, 'setData'], ['permissions', $this->getApiScopes()]);
call_user_func_array([$extensionAttributes, 'setData'], ['aftership_tiktok', $this->getVersion()]);
try {
foreach ($result as $store) {
$extensionAttributes = $store->getExtensionAttributes();
if (!$extensionAttributes) {
$extensionAttributes = $this->storeConfigExtensionFactory->create();
}
// setPermissions method is generated by extension_attributes.xml.
if (method_exists($extensionAttributes, 'setPermissions')) {
call_user_func_array([$extensionAttributes, 'setPermissions'], [$this->getApiScopes()]);
}
if (method_exists($extensionAttributes, 'setAftershipTiktok')) {
call_user_func_array([$extensionAttributes, 'setAftershipTiktok'], [$this->getVersion()]);
}
// Pass Upgrade compatibility tool check.
if (method_exists($extensionAttributes, 'setData')) {
call_user_func_array([$extensionAttributes, 'setData'], ['permissions', $this->getApiScopes()]);
call_user_func_array([$extensionAttributes, 'setData'], ['aftership_tiktok', $this->getVersion()]);
}
$store->setExtensionAttributes($extensionAttributes);
}
$store->setExtensionAttributes($extensionAttributes);
}catch (\Exception $e){
$this->logger->error(
sprintf(
'[AfterShip TikTokShop] Failed to set extension attributes for store API, %s',
$e->getMessage()
)
);
}
return $result;
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "aftership/tiktok-shop",
"description": "Aftership TikTok Shop extension for Magento 2. Allows connect with TikTok Shop and more.",
"type": "magento2-module",
"version": "1.0.16",
"version": "1.0.17",
"minimum-stability": "stable",
"license": "MIT",
"keywords": ["aftership", "magento", "magento2", "magento-2", "tiktok", "shopping", "shop", "feed"],
Expand Down
6 changes: 0 additions & 6 deletions etc/di.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Store\Api\StoreConfigManagerInterface">
<plugin name="add_tts_version_to_store" type="AfterShip\TikTokShop\Plugin\StoreConfigExtensionAttributes"/>
</type>
<type name="Magento\Sales\Api\OrderRepositoryInterface">
<plugin name="inventory_reservations_placement" type="AfterShip\TikTokShop\Plugin\CreateReservationsAfterPlaceOrder"/>
</type>
<preference for="AfterShip\TikTokShop\Api\WebhookEntityInterface" type="AfterShip\TikTokShop\Model\Api\WebhookRequest"/>
<preference for="AfterShip\TikTokShop\Api\WebhookManagementInterface" type="AfterShip\TikTokShop\Model\Api\WebhookManagement"/>
<preference for="AfterShip\TikTokShop\Api\Data\WebhookEventInterface" type="AfterShip\TikTokShop\Model\Api\WebhookEvent"/>
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?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="AfterShip_TikTokShop" setup_version="1.0.16" schema_version="1.0.16">
<module name="AfterShip_TikTokShop" setup_version="1.0.17" schema_version="1.0.17">
</module>
</config>
9 changes: 9 additions & 0 deletions etc/webapi_rest/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Store\Api\StoreConfigManagerInterface">
<plugin name="add_tts_version_to_store" type="AfterShip\TikTokShop\Plugin\StoreConfigExtensionAttributes"/>
</type>
<type name="Magento\Sales\Api\OrderRepositoryInterface">
<plugin name="inventory_reservations_placement" type="AfterShip\TikTokShop\Plugin\CreateReservationsAfterPlaceOrder"/>
</type>
</config>

0 comments on commit 3aa81bb

Please sign in to comment.