From a11a6ee4f9bd44bc9fa2418ccee9cd85c4ba2df8 Mon Sep 17 00:00:00 2001 From: Jonathan Renard <1273438+fox-john@users.noreply.github.com> Date: Fri, 13 Sep 2024 10:18:04 +0200 Subject: [PATCH] feat: pseventbus v4 cart rules (#358) --- config/common/new-repository.yml | 4 + config/common/repository.yml | 10 -- config/front/services.yml | 6 + e2e/src/helpers/shop-contents.ts | 1 + src/Repository/CartRuleRepository.php | 121 ---------------- .../NewRepository/CartRuleRepository.php | 131 ++++++++++++++++++ src/Service/ShopContent/CartRulesService.php | 121 ++++++++++++++++ 7 files changed, 263 insertions(+), 131 deletions(-) delete mode 100644 src/Repository/CartRuleRepository.php create mode 100644 src/Repository/NewRepository/CartRuleRepository.php create mode 100644 src/Service/ShopContent/CartRulesService.php diff --git a/config/common/new-repository.yml b/config/common/new-repository.yml index 574aff63..58a8124e 100644 --- a/config/common/new-repository.yml +++ b/config/common/new-repository.yml @@ -26,3 +26,7 @@ services: PrestaShop\Module\PsEventbus\Repository\NewRepository\CartProductRepository: class: PrestaShop\Module\PsEventbus\Repository\NewRepository\CartProductRepository public: true + + PrestaShop\Module\PsEventbus\Repository\NewRepository\CartRuleRepository: + class: PrestaShop\Module\PsEventbus\Repository\NewRepository\CartRuleRepository + public: true diff --git a/config/common/repository.yml b/config/common/repository.yml index f850c11d..35eea5f8 100644 --- a/config/common/repository.yml +++ b/config/common/repository.yml @@ -68,16 +68,6 @@ services: arguments: - '@=service("prestashop.adapter.legacy.context").getContext()' - PrestaShop\Module\PsEventbus\Repository\NewRepository\OrderDetailRepository: - class: PrestaShop\Module\PsEventbus\Repository\NewRepository\OrderDetailRepository - public: true - arguments: - - '@=service("prestashop.adapter.legacy.context").getContext()' - - PrestaShop\Module\PsEventbus\Repository\CartRuleRepository: - class: PrestaShop\Module\PsEventbus\Repository\CartRuleRepository - public: true - PrestaShop\Module\PsEventbus\Repository\GoogleTaxonomyRepository: class: PrestaShop\Module\PsEventbus\Repository\GoogleTaxonomyRepository public: true diff --git a/config/front/services.yml b/config/front/services.yml index 67f31ff2..bc81d958 100644 --- a/config/front/services.yml +++ b/config/front/services.yml @@ -108,3 +108,9 @@ services: public: true arguments: - '@PrestaShop\Module\PsEventbus\Repository\NewRepository\CartProductRepository' + + PrestaShop\Module\PsEventbus\Service\ShopContent\CartRulesService: + class: PrestaShop\Module\PsEventbus\Service\ShopContent\CartRulesService + public: true + arguments: + - '@PrestaShop\Module\PsEventbus\Repository\NewRepository\CartRuleRepository' diff --git a/e2e/src/helpers/shop-contents.ts b/e2e/src/helpers/shop-contents.ts index a5ad147c..a548114c 100644 --- a/e2e/src/helpers/shop-contents.ts +++ b/e2e/src/helpers/shop-contents.ts @@ -39,6 +39,7 @@ export const shopContentMapping = { 'carrier_taxes': 'carrier-taxes', 'carts': 'carts', 'cart_products': 'cart-products', + 'cart_rules': 'cart-rules', 'orders': 'orders', 'order_cart_rules': 'order-cart-rules', 'order_details': 'order-details', diff --git a/src/Repository/CartRuleRepository.php b/src/Repository/CartRuleRepository.php deleted file mode 100644 index 6dab844d..00000000 --- a/src/Repository/CartRuleRepository.php +++ /dev/null @@ -1,121 +0,0 @@ -db = \Db::getInstance(); - } - - /** - * @return \DbQuery - */ - public function getBaseQuery() - { - $query = new \DbQuery(); - - $query->from('cart_rule', 'cr'); - - return $query; - } - - /** - * @param int $limit - * @param int $offset - * - * @return array|bool|\mysqli_result|\PDOStatement|resource|null - * - * @throws \PrestaShopDatabaseException - */ - public function getCartRules($limit, $offset) - { - $query = $this->getBaseQuery(); - - $query->select('cr.id_cart_rule,cr.id_customer, cr.code, cr.date_from AS "from",cr.date_to AS "to",cr.description,cr.quantity'); - $query->select('cr.quantity_per_user,cr.priority,cr.partial_use,cr.minimum_amount,cr.minimum_amount_tax,cr.minimum_amount_currency'); - $query->select('cr.minimum_amount_shipping,cr.country_restriction,cr.carrier_restriction,cr.group_restriction,cr.cart_rule_restriction'); - $query->select('cr.product_restriction,cr.shop_restriction,cr.free_shipping,cr.reduction_percent,cr.reduction_amount,cr.reduction_tax'); - $query->select('cr.reduction_currency,cr.reduction_product,cr.gift_product,cr.gift_product_attribute'); - $query->select('cr.highlight,cr.active,cr.date_add AS created_at,cr.date_upd AS updated_at'); - - if (defined('_PS_VERSION_') && version_compare(_PS_VERSION_, '1.7', '>=')) { - $query->select('cr.reduction_exclude_special'); - } - - $query->limit($limit, $offset); - - return $this->db->executeS($query); - } - - /** - * @param int $limit - * @param array $cartRuleIds - * - * @return array|bool|\mysqli_result|\PDOStatement|resource|null - * - * @throws \PrestaShopDatabaseException - */ - public function getCartRulesIncremental($limit, $cartRuleIds) - { - $query = $this->getBaseQuery(); - - $query->where('cr.id_cart_rule IN(' . implode(',', array_map('intval', $cartRuleIds)) . ')') - ->limit($limit); - - return $this->db->executeS($query); - } - - /** - * @param int $offset - * - * @return int - */ - public function getRemainingCartRulesCount($offset) - { - $query = $this->getBaseQuery(); - - $query->select('(COUNT(cr.id_cart_rule) - ' . (int) $offset . ') as count'); - - return (int) $this->db->getValue($query); - } - - /** - * @param int $limit - * @param int $offset - * - * @return array - * - * @throws \PrestaShopDatabaseException - */ - public function getQueryForDebug($limit, $offset) - { - $query = $this->getBaseQuery(); - - $query->select('cr.id_cart_rule,cr.id_customer, cr.code, cr.date_from AS "from",cr.date_to AS "to",cr.description,cr.quantity'); - $query->select('cr.quantity_per_user,cr.priority,cr.partial_use,cr.minimum_amount,cr.minimum_amount_tax,cr.minimum_amount_currency'); - $query->select('cr.minimum_amount_shipping,cr.country_restriction,cr.carrier_restriction,cr.group_restriction,cr.cart_rule_restriction'); - $query->select('cr.product_restriction,cr.shop_restriction,cr.free_shipping,cr.reduction_percent,cr.reduction_amount,cr.reduction_tax'); - $query->select('cr.reduction_currency,cr.reduction_product,cr.gift_product,cr.gift_product_attribute'); - $query->select('cr.highlight,cr.active,cr.date_add AS created_at,cr.date_upd AS updated_at'); - - if (defined('_PS_VERSION_') && version_compare(_PS_VERSION_, '1.7', '>=')) { - $query->select('cr.reduction_exclude_special'); - } - - $query->limit($limit, $offset); - - $queryStringified = preg_replace('/\s+/', ' ', $query->build()); - - return array_merge( - (array) $query, - ['queryStringified' => $queryStringified] - ); - } -} diff --git a/src/Repository/NewRepository/CartRuleRepository.php b/src/Repository/NewRepository/CartRuleRepository.php new file mode 100644 index 00000000..48d63d7b --- /dev/null +++ b/src/Repository/NewRepository/CartRuleRepository.php @@ -0,0 +1,131 @@ +query = new \DbQuery(); + + $this->query->from(self::TABLE_NAME, 'cr'); + } + + /** + * @param string $langIso + * + * @return mixed + * + * @throws \PrestaShopException + */ + public function generateFullQuery($langIso) + { + $this->generateMinimalQuery(); + + $this->query + ->select('cr.id_cart_rule') + ->select('cr.id_customer') + ->select('cr.code') + ->select('cr.date_from AS "from"') + ->select('cr.date_to AS "to"') + ->select('cr.description') + ->select('cr.quantity') + ->select('cr.quantity_per_user') + ->select('cr.priority') + ->select('cr.partial_use') + ->select('cr.minimum_amount') + ->select('cr.minimum_amount_tax') + ->select('cr.minimum_amount_currency') + ->select('cr.minimum_amount_shipping') + ->select('cr.country_restriction') + ->select('cr.carrier_restriction') + ->select('cr.group_restriction') + ->select('cr.cart_rule_restriction') + ->select('cr.product_restriction') + ->select('cr.shop_restriction') + ->select('cr.free_shipping') + ->select('cr.reduction_percent') + ->select('cr.reduction_amount') + ->select('cr.reduction_tax') + ->select('cr.reduction_currency') + ->select('cr.reduction_product') + ->select('cr.gift_product') + ->select('cr.gift_product_attribute') + ->select('cr.highlight') + ->select('cr.active') + ->select('cr.date_add AS created_at') + ->select('cr.date_upd AS updated_at') + ; + + if (defined('_PS_VERSION_') && version_compare(_PS_VERSION_, '1.7', '>=')) { + $this->query->select('cr.reduction_exclude_special'); + } + } + + /** + * @param int $offset + * @param int $limit + * @param string $langIso + * @param bool $debug + * + * @return array + * + * @throws \PrestaShopException + * @throws \PrestaShopDatabaseException + */ + public function getContentsForFull($offset, $limit, $langIso, $debug) + { + $this->generateFullQuery($langIso); + + $this->query->limit((int) $limit, (int) $offset); + + return $this->runQuery($debug); + } + + /** + * @param int $limit + * @param array $contentIds + * @param string $langIso + * @param bool $debug + * + * @return array + * + * @throws \PrestaShopException + * @throws \PrestaShopDatabaseException + */ + public function getContentsForIncremental($limit, $contentIds, $langIso, $debug) + { + $this->generateFullQuery($langIso); + + $this->query + ->where('cr.id_cart_rule IN(' . implode(',', array_map('intval', $contentIds)) . ')') + ->limit($limit) + ; + + return $this->runQuery($debug); + } + + /** + * @param int $offset + * + * @return int + * + * @throws \PrestaShopException + * @throws \PrestaShopDatabaseException + */ + public function countFullSyncContentLeft($offset) + { + $this->generateMinimalQuery(); + + $this->query->select('(COUNT(cr.id_cart_rule) - ' . (int) $offset . ') as count'); + + $result = $this->runQuery(false); + + return $result[0]['count']; + } +} diff --git a/src/Service/ShopContent/CartRulesService.php b/src/Service/ShopContent/CartRulesService.php new file mode 100644 index 00000000..2806bdab --- /dev/null +++ b/src/Service/ShopContent/CartRulesService.php @@ -0,0 +1,121 @@ +cartRuleRepository = $cartRuleRepository; + } + + /** + * @param int $offset + * @param int $limit + * @param string $langIso + * @param bool $debug + * + * @return array + */ + public function getContentsForFull($offset, $limit, $langIso, $debug) + { + $result = $this->cartRuleRepository->getContentsForFull($offset, $limit, $langIso, $debug); + + if (empty($result)) { + return []; + } + + $this->castCartRules($result); + + return array_map(function ($item) { + return [ + 'id' => $item['id_cart_rule'], + 'collection' => Config::COLLECTION_CART_RULES, + 'properties' => $item, + ]; + }, $result); + } + + /** + * @param int $limit + * @param array $contentIds + * @param string $langIso + * @param bool $debug + * + * @return array + */ + public function getContentsForIncremental($limit, $contentIds, $langIso, $debug) + { + $result = $this->cartRuleRepository->getContentsForIncremental($limit, $contentIds, $langIso, $debug); + + if (empty($result)) { + return []; + } + + $this->castCartRules($result); + + return array_map(function ($item) { + return [ + 'id' => $item['id_cart_rule'], + 'collection' => Config::COLLECTION_CART_RULES, + 'properties' => $item, + ]; + }, $result); + } + + /** + * @param int $offset + * @param int $limit + * @param string $langIso + * + * @return int + */ + public function countFullSyncContentLeft($offset, $limit, $langIso) + { + return $this->cartRuleRepository->countFullSyncContentLeft($offset); + } + + /** + * @param array $cartRules + * + * @return void + */ + private function castCartRules(&$cartRules) + { + foreach ($cartRules as &$cartRule) { + $cartRule['id_cart_rule'] = (int) $cartRule['id_cart_rule']; + $cartRule['id_customer'] = (int) $cartRule['id_customer']; + $cartRule['quantity'] = (int) $cartRule['quantity']; + $cartRule['quantity_per_user'] = (int) $cartRule['quantity_per_user']; + $cartRule['priority'] = (int) $cartRule['priority']; + $cartRule['partial_use'] = (bool) $cartRule['partial_use']; + $cartRule['minimum_amount'] = (float) $cartRule['minimum_amount']; + $cartRule['minimum_amount_tax'] = (bool) $cartRule['minimum_amount_tax']; + $cartRule['minimum_amount_currency'] = (int) $cartRule['minimum_amount_currency']; + $cartRule['minimum_amount_shipping'] = (bool) $cartRule['minimum_amount_shipping']; + $cartRule['country_restriction'] = (bool) $cartRule['country_restriction']; + $cartRule['carrier_restriction'] = (bool) $cartRule['carrier_restriction']; + $cartRule['group_restriction'] = (bool) $cartRule['group_restriction']; + $cartRule['cart_rule_restriction'] = (bool) $cartRule['cart_rule_restriction']; + $cartRule['product_restriction'] = (bool) $cartRule['product_restriction']; + $cartRule['shop_restriction'] = (bool) $cartRule['shop_restriction']; + $cartRule['free_shipping'] = (bool) $cartRule['free_shipping']; + $cartRule['reduction_percent'] = (float) $cartRule['reduction_percent']; + $cartRule['reduction_amount'] = (float) $cartRule['reduction_amount']; + $cartRule['reduction_tax'] = (bool) $cartRule['reduction_tax']; + $cartRule['reduction_currency'] = (int) $cartRule['reduction_currency']; + $cartRule['reduction_product'] = (int) $cartRule['reduction_product']; + $cartRule['reduction_exclude_special'] = (bool) $cartRule['reduction_exclude_special']; + $cartRule['gift_product'] = (int) $cartRule['gift_product']; + $cartRule['gift_product_attribute'] = (int) $cartRule['gift_product_attribute']; + $cartRule['highlight'] = (bool) $cartRule['highlight']; + $cartRule['active'] = (bool) $cartRule['active']; + } + } +}