Skip to content

Commit

Permalink
Merge pull request #316 from PrestaShopCorp/ACCOUNT-1664/feat/hook-ev…
Browse files Browse the repository at this point in the history
…ent-link-unlink

[ACCOUNT-1664] feat: custom hooks for shop link & unlink events
  • Loading branch information
hschoenenberger authored Apr 12, 2023
2 parents 2a1f681 + e71d1bd commit 1c02f04
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 8 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,12 @@ Those API has been removed:
- `/orders`
- `/products`
- `/themes`

## Custom hooks

Here are listed custom hooks provided with this module :

| Hook name | Payload | Description |
|------------------------------|------------------|------------------------------------------------------|
| actionShopAccountLinkAfter | shopId, shopUuid | Triggered after link has been acknowledged by shop |
| actionShopAccountUnlinkAfter | shopId, shopUuid | Triggered after unlink has been acknowledged by shop |
2 changes: 1 addition & 1 deletion _dev/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ps_accounts",
"version": "6.1.4",
"version": "6.1.5",
"private": true,
"scripts": {
"dev": "vite",
Expand Down
4 changes: 2 additions & 2 deletions config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<module>
<name>ps_accounts</name>
<displayName><![CDATA[PrestaShop Account]]></displayName>
<version><![CDATA[6.0.0]]></version>
<version><![CDATA[6.1.6]]></version>
<description><![CDATA[Associate your shop with your PrestaShop account to activate and manage your subscriptions in your back office. Do not uninstall this module if you have a current subscription.]]></description>
<author><![CDATA[PrestaShop]]></author>
<tab><![CDATA[administration]]></tab>
<confirmUninstall><![CDATA[This action will prevent immediately your PrestaShop services and Community services from working as they are using PrestaShop Accounts module for authentication.]]></confirmUninstall>
<is_configurable>1</is_configurable>
<need_instance>0</need_instance>
</module>
</module>
24 changes: 23 additions & 1 deletion controllers/front/apiV1ShopLinkAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use PrestaShop\Module\PsAccounts\Controller\AbstractShopRestController;
use PrestaShop\Module\PsAccounts\DTO\Api\UpdateShopLinkAccountRequest;
use PrestaShop\Module\PsAccounts\Exception\RefreshTokenException;
use PrestaShop\Module\PsAccounts\Service\PsAccountsService;
use PrestaShop\Module\PsAccounts\Service\ShopLinkAccountService;

class ps_AccountsApiV1ShopLinkAccountModuleFrontController extends AbstractShopRestController
Expand All @@ -30,6 +31,11 @@ class ps_AccountsApiV1ShopLinkAccountModuleFrontController extends AbstractShopR
*/
private $shopLinkAccountService;

/**
* @var PsAccountsService
*/
private $psAccountsService;

/**
* ps_AccountsApiV1ShopLinkAccountModuleFrontController constructor.
*
Expand All @@ -40,6 +46,8 @@ public function __construct()
parent::__construct();

$this->shopLinkAccountService = $this->module->getService(ShopLinkAccountService::class);

$this->psAccountsService = $this->module->getService(PsAccountsService::class);
}

/**
Expand All @@ -48,7 +56,7 @@ public function __construct()
*
* @return array
*
* @throws RefreshTokenException
* @throws RefreshTokenException|PrestaShopException
*/
public function update(Shop $shop, UpdateShopLinkAccountRequest $request): array
{
Expand All @@ -57,6 +65,11 @@ public function update(Shop $shop, UpdateShopLinkAccountRequest $request): array
$this->module->getParameter('ps_accounts.verify_account_tokens')
);

Hook::exec(Ps_accounts::HOOK_ACTION_SHOP_ACCOUNT_LINK_AFTER, [
'shopUuid' => $this->psAccountsService->getShopUuid(),
'shopId' => $shop->id,
]);

return [
'success' => true,
'message' => 'Link Account stored successfully',
Expand All @@ -68,11 +81,20 @@ public function update(Shop $shop, UpdateShopLinkAccountRequest $request): array
* @param array $payload
*
* @return array
*
* @throws PrestaShopException
*/
public function delete(Shop $shop, array $payload): array
{
$hookData = [
'shopUuid' => $this->psAccountsService->getShopUuid(),
'shopId' => $shop->id,
];

$this->shopLinkAccountService->resetLinkAccount();

Hook::exec(Ps_accounts::HOOK_ACTION_SHOP_ACCOUNT_UNLINK_AFTER, $hookData);

return [
'success' => true,
'message' => 'Link Account deleted successfully',
Expand Down
50 changes: 46 additions & 4 deletions ps_accounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ class Ps_accounts extends Module

// Needed in order to retrieve the module version easier (in api call headers) than instanciate
// the module each time to get the version
const VERSION = '6.1.4';
const VERSION = '6.1.6';

const HOOK_ACTION_SHOP_ACCOUNT_LINK_AFTER = 'actionShopAccountLinkAfter';
const HOOK_ACTION_SHOP_ACCOUNT_UNLINK_AFTER = 'actionShopAccountUnlinkAfter';
const HOOK_DISPLAY_ACCOUNT_UPDATE_WARNING = 'displayAccountUpdateWarning';

/**
* @var array
Expand All @@ -50,9 +54,11 @@ class Ps_accounts extends Module
'actionObjectShopDeleteAfter',
'actionObjectShopUrlUpdateAfter',
'displayDashboardTop',
'displayAccountUpdateWarning',
'actionAdminLoginControllerLoginAfter',
'actionAdminControllerInitBefore',
self::HOOK_DISPLAY_ACCOUNT_UPDATE_WARNING,
self::HOOK_ACTION_SHOP_ACCOUNT_LINK_AFTER,
self::HOOK_ACTION_SHOP_ACCOUNT_UNLINK_AFTER,
];

/**
Expand All @@ -62,12 +68,24 @@ class Ps_accounts extends Module
*/
private $customHooks = [
[
'name' => 'displayAccountUpdateWarning',
'name' => self::HOOK_DISPLAY_ACCOUNT_UPDATE_WARNING,
'title' => 'Display account update warning',
'description' => 'Show a warning message when the user wants to'
. ' update his shop configuration',
'position' => 1,
],
[
'name' => self::HOOK_ACTION_SHOP_ACCOUNT_LINK_AFTER,
'title' => 'Shop linked event',
'description' => 'Shop linked with PrestaShop Account',
'position' => 1,
],
[
'name' => self::HOOK_ACTION_SHOP_ACCOUNT_UNLINK_AFTER,
'title' => 'Shop unlinked event',
'description' => 'Shop unlinked with PrestaShop Account',
'position' => 1,
],
];

/**
Expand All @@ -88,7 +106,7 @@ public function __construct()

// We cannot use the const VERSION because the const is not computed by addons marketplace
// when the zip is uploaded
$this->version = '6.1.4';
$this->version = '6.1.6';

$this->module_key = 'abf2cd758b4d629b2944d3922ef9db73';

Expand Down Expand Up @@ -691,6 +709,30 @@ public function hookActionAdminControllerInitBefore($params)
}
}

/**
* @param array{shopUuid: string, shopId: string} $params
*
* @return void
*
* @throws Exception
*/
public function hookActionShopAccountLinkAfter($params)
{
// Not implemented here
}

/**
* @param array{shopUuid: string, shopId: string} $params
*
* @return void
*
* @throws Exception
*/
public function hookActionShopAccountUnlinkAfter($params)
{
// Not implemented here
}

/**
* @return string
*/
Expand Down
16 changes: 16 additions & 0 deletions upgrade/upgrade-6.1.6.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/**
* @param Ps_accounts $module
*
* @return bool
*
* @throws Exception
*/
function upgrade_module_6_1_6($module)
{
$module->addCustomHooks($module->getCustomHooks());
$module->registerHook($module->getHookToInstall());

return true;
}

0 comments on commit 1c02f04

Please sign in to comment.