Skip to content

Commit

Permalink
feat : add owner_email & owner_uid values on link shop
Browse files Browse the repository at this point in the history
  • Loading branch information
hschoenenberger committed Feb 13, 2024
1 parent 1ced5b6 commit 48c436b
Show file tree
Hide file tree
Showing 19 changed files with 144 additions and 128 deletions.
2 changes: 1 addition & 1 deletion config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
<is_configurable>1</is_configurable>
<need_instance>0</need_instance>
<limited_countries></limited_countries>
</module>
</module>
6 changes: 6 additions & 0 deletions config/common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ services:
arguments:
- '@PrestaShop\Module\PsAccounts\Repository\ConfigurationRepository'

PrestaShop\Module\PsAccounts\Factory\CircuitBreakerFactory:
class: PrestaShop\Module\PsAccounts\Factory\CircuitBreakerFactory
public: true
arguments:
- '@PrestaShop\Module\PsAccounts\Adapter\Configuration'

#####################
# handlers

Expand Down
2 changes: 2 additions & 0 deletions controllers/front/apiV1ShopLinkAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public function update(Shop $shop, UpdateShopLinkAccountRequest $request)
new LinkShop([
'shopId' => $request->shop_id,
'uid' => $request->uid,
'ownerUid' => $request->owner_uid,
'ownerEmail' => $request->owner_email,
'employeeId' => $request->employee_id,
])
));
Expand Down
29 changes: 25 additions & 4 deletions ps_accounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ class Ps_accounts extends Module
// \PrestaShop\Module\PsAccounts\Hook\ActionAdminControllerInitBefore::class,
\PrestaShop\Module\PsAccounts\Hook\ActionAdminLoginControllerLoginAfter::class,
\PrestaShop\Module\PsAccounts\Hook\ActionAdminLoginControllerSetMedia::class,
\PrestaShop\Module\PsAccounts\Hook\ActionModuleInstallAfter::class,
\PrestaShop\Module\PsAccounts\Hook\ActionObjectShopAddAfter::class,
\PrestaShop\Module\PsAccounts\Hook\ActionObjectShopDeleteAfter::class,
\PrestaShop\Module\PsAccounts\Hook\ActionObjectShopDeleteBefore::class,
Expand Down Expand Up @@ -173,9 +172,7 @@ public function install()
&& $this->addCustomHooks($this->customHooks)
&& $this->registerHook($this->getHooksToRegister());

$this->installEventBus();
$this->fixMultiShopConfig();
$this->autoReonboardOnV5();
$this->onModuleInstallAfter();

$this->getLogger()->info('Install - Loading ' . $this->name . ' Env : [' . $this->getModuleEnv() . ']');

Expand Down Expand Up @@ -517,4 +514,28 @@ private function installEventBus()
$moduleInstaller->installModule('ps_eventbus');
}
}

/**
* @return void
*
* @throws Exception
*/
private function onModuleInstallAfter()
{
/** @var \PrestaShop\Module\PsAccounts\Factory\CircuitBreakerFactory $circuitBreakerFactory */
$circuitBreakerFactory = $this->getService(\PrestaShop\Module\PsAccounts\Factory\CircuitBreakerFactory::class);
$circuitBreakerFactory->resetAll();

/** @var \PrestaShop\Module\PsAccounts\Cqrs\CommandBus $commandBus */
$commandBus = $this->getService(\PrestaShop\Module\PsAccounts\Cqrs\CommandBus::class);
$commandBus->handle(new \PrestaShop\Module\PsAccounts\Account\Command\UpdateModuleCommand(
new \PrestaShop\Module\PsAccounts\Account\Dto\UpdateModule([
'version' => \Ps_accounts::VERSION,
])
));

$this->installEventBus();
$this->fixMultiShopConfig();
$this->autoReonboardOnV5();
}
}
12 changes: 6 additions & 6 deletions src/Account/Dto/LinkShop.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ class LinkShop extends Dto
public $uid;
/** @var string */
public $employeeId = '';

// /** @var string */
// public $ownerUid;
//
// /** @var string */
// public $ownerEmail;
/** @var string */
public $ownerUid;
/** @var string */
public $ownerEmail;

protected $mandatory = [
'shopId',
'uid',
// 'ownerUid',
// 'ownerEmail',
];
}
12 changes: 7 additions & 5 deletions src/Account/LinkShop.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public function delete()
{
$this->setShopUuid(null);
$this->setEmployeeId(null);
$this->setOwnerUuid(null);
$this->setOwnerEmail(null);
}

/**
Expand All @@ -60,6 +62,8 @@ public function update(Dto\LinkShop $payload)
{
$this->setShopUuid($payload->uid);
$this->setEmployeeId((int) $payload->employeeId ?: null);
$this->setOwnerUuid($payload->ownerUid);
$this->setOwnerEmail($payload->ownerEmail);
}

/**
Expand Down Expand Up @@ -123,12 +127,11 @@ public function setEmployeeId($employeeId)
}

/**
* @return string|null
* @return int
*/
public function getOwnerUuid()
{
// FIXME: implement method
//return (int) $this->configuration->getEmployeeId();
return (int) $this->configuration->getUserFirebaseUuid();
}

/**
Expand All @@ -138,8 +141,7 @@ public function getOwnerUuid()
*/
public function setOwnerUuid($uuid)
{
// FIXME: implement method
//$this->configuration->updateEmployeeId((string) $uuid);
$this->configuration->updateUserFirebaseUuid((string) $uuid);
}

/**
Expand Down
6 changes: 0 additions & 6 deletions src/Account/Session/Firebase/OwnerSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ public function getToken()
public function cleanup()
{
$this->configurationRepository->updateUserFirebaseIdToken('');
$this->configurationRepository->updateFirebaseEmail('');
}

/**
Expand All @@ -93,11 +92,6 @@ public function cleanup()
*/
public function setToken($token, $refreshToken = null)
{
$parsed = (new Parser())->parse((string) $token);

$uuid = $parsed->claims()->get(Token::ID_OWNER_CLAIM);
$this->configurationRepository->updateUserFirebaseIdToken($token);

$this->configurationRepository->updateFirebaseEmail($parsed->claims()->get('email'));
}
}
4 changes: 0 additions & 4 deletions src/Account/Session/Firebase/ShopSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public function getToken()
*/
public function cleanup()
{
$this->configurationRepository->updateShopUuid('');
$this->configurationRepository->updateFirebaseIdAndRefreshTokens('', '');
}

Expand All @@ -93,9 +92,6 @@ public function cleanup()
*/
public function setToken($token, $refreshToken = null)
{
$parsed = (new Parser())->parse($token);

$this->configurationRepository->updateShopUuid($parsed->claims()->get(Token::ID_OWNER_CLAIM));
$this->configurationRepository->updateFirebaseIdAndRefreshTokens($token, $refreshToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/

namespace PrestaShop\Module\PsAccounts\Repository;
namespace PrestaShop\Module\PsAccounts\Adapter;

use PrestaShop\Module\PsAccounts\Type\Enum;

Expand All @@ -31,6 +31,7 @@ class ConfigurationKeys extends Enum

// PS User Account
const PS_ACCOUNTS_USER_FIREBASE_ID_TOKEN = 'PS_ACCOUNTS_USER_FIREBASE_ID_TOKEN';
const PS_ACCOUNTS_USER_FIREBASE_UUID = 'PS_ACCOUNTS_USER_FIREBASE_UUID';
/* @deprecated */
const PS_ACCOUNTS_FIREBASE_EMAIL = 'PS_ACCOUNTS_FIREBASE_EMAIL';

Expand Down
12 changes: 4 additions & 8 deletions src/Api/Controller/Request/UpdateShopLinkAccountRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,13 @@ class UpdateShopLinkAccountRequest extends Request
/** @var string */
public $shop_id;
/** @var string */
public $shop_refresh_token;
/** @var string */
public $user_refresh_token;
/** @var string */
public $shop_token;
/** @var string */
public $user_token;
/** @var string */
public $employee_id = '';
/** @var string */
public $uid;
/** @var string */
public $owner_uid;
/** @var string */
public $owner_email;

/**
* @var string[]
Expand Down
73 changes: 57 additions & 16 deletions src/Factory/CircuitBreakerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,29 @@ class CircuitBreakerFactory
/**
* @var array
*/
private static $provides = [
private $provides = [
AccountsClient::class,
IndirectChannelClient::class,
];

/**
* @var array
*/
private static $instances = [];
private $instances = [];

/**
* @var Configuration
*/
private $configStorage;

/**
* @param Configuration $configStorage
*/
public function __construct(
Configuration $configStorage
) {
$this->configStorage = $configStorage;
}

/**
* @param string $resourceId
Expand All @@ -48,20 +62,19 @@ class CircuitBreakerFactory
*
* @throws \Exception
*/
public static function create($resourceId)
public function createInstance($resourceId)
{
/** @var \Ps_accounts $module */
$module = \Module::getInstanceByName('ps_accounts');
/** @var Configuration $config */
$config = $module->getService(Configuration::class);

$instance = new PersistentCircuitBreaker($resourceId, 'PS_ACCOUNTS', $config);
$instance = new PersistentCircuitBreaker(
static::className($resourceId),
'PS_ACCOUNTS',
$this->configStorage
);
$instance->setDefaultFallbackResponse([
'status' => false,
'httpCode' => 500,
'body' => ['message' => 'Circuit Breaker Open'],
]);
static::$instances[$resourceId] = $instance;
$this->instances[$resourceId] = $instance;

return $instance;
}
Expand All @@ -73,24 +86,52 @@ public static function create($resourceId)
*
* @throws \Exception
*/
public static function getOrCreate($resourceId)
public function getOrCreate($resourceId)
{
if (!array_key_exists($resourceId, static::$instances)) {
static::create($resourceId);
if (!array_key_exists($resourceId, $this->instances)) {
static::createInstance($resourceId);
}

return static::$instances[$resourceId];
return $this->instances[$resourceId];
}

/**
* @return void
*
* @throws \Exception
*/
public static function resetAll()
public function resetAll()
{
foreach (static::$provides as $class) {
foreach ($this->provides as $class) {
static::getOrCreate($class)->reset();
}
}

/**
* @param string $resourceId
*
* @return CircuitBreaker
*
* @throws \Exception
*/
public static function create($resourceId)
{
/** @var \Ps_accounts $module */
$module = \Module::getInstanceByName('ps_accounts');

/** @var CircuitBreakerFactory $factory */
$factory = $module->getService(CircuitBreakerFactory::class);

return $factory->createInstance($resourceId);
}

/**
* @param string $className
*
* @return string
*/
protected function className($className)
{
return strtoupper(preg_replace(["/^.*\\\\/", "/([^A-Z])([A-Z])/"], ["", "$1_$2"], $className));
}
}
45 changes: 0 additions & 45 deletions src/Hook/ActionModuleInstallAfter.php

This file was deleted.

Loading

0 comments on commit 48c436b

Please sign in to comment.