From e40f05e30445923913087f216fd57b4cd7f568c4 Mon Sep 17 00:00:00 2001 From: Jonathan Renard <1273438+fox-john@users.noreply.github.com> Date: Thu, 29 Aug 2024 14:47:42 +0200 Subject: [PATCH] feat: pseventbus v4 order cart rules (#347) --- README.md | 4 +- composer.lock | 2 +- config/admin/services.yml | 1 + config/common/new-repository.yml | 8 ++ config/common/repository.yml | 8 -- config/front/services.yml | 9 +- controllers/front/apiFront.php | 5 +- e2e-env/docker-compose.yml | 2 +- e2e/README.md | 4 +- e2e/src/full-sync.spec.ts | 12 +- e2e/src/helpers/data-helper.ts | 2 +- e2e/src/helpers/mock-probe.ts | 2 +- e2e/src/helpers/shop-contents.ts | 2 +- e2e/src/reject-invalid-job-id.spec.ts | 2 +- .../AbstractRepository.php | 2 +- .../NewRepository/OrderCartRuleRepository.php | 95 ++++++++++++++ .../OrderRepository.php | 71 +++++++--- .../NewRepository}/RepositoryInterface.php | 2 +- src/Repository/OrderCartRuleRepository.php | 78 ----------- src/Service/FrontApiService.php | 29 ++++- src/Service/ProxyService.php | 1 - .../ProxyServiceInterface.php | 2 +- .../ShopContent/OrderCartRulesService.php | 123 ++++++++++++++++++ src/Service/ShopContent/OrdersService.php | 3 +- .../ShopContentServiceInterface.php | 2 +- src/Service/SynchronizationService.php | 7 +- 26 files changed, 339 insertions(+), 139 deletions(-) create mode 100644 config/common/new-repository.yml rename src/Repository/{NewRepositoryTemp => NewRepository}/AbstractRepository.php (95%) create mode 100644 src/Repository/NewRepository/OrderCartRuleRepository.php rename src/Repository/{NewRepositoryTemp => NewRepository}/OrderRepository.php (56%) rename src/{Interfaces => Repository/NewRepository}/RepositoryInterface.php (92%) delete mode 100644 src/Repository/OrderCartRuleRepository.php rename src/{Interfaces => Service}/ProxyServiceInterface.php (90%) create mode 100644 src/Service/ShopContent/OrderCartRulesService.php rename src/{Interfaces => Service/ShopContent}/ShopContentServiceInterface.php (92%) diff --git a/README.md b/README.md index ac254564..66b7c371 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ To check the module healthiness (authentified route): ```sh BASE_URL="http://localhost:8000" -curl -s -L "$BASE_URL/index.php?fc=module&module=ps_eventbus&controller=apiFront&shop_content=healthcheck&job_id=valid-job-stuff" | jq . +curl -s -L "$BASE_URL/index.php?fc=module&module=ps_eventbus&controller=apiFront&is_e2e=1&shop_content=healthcheck&job_id=valid-job-stuff" | jq . { "prestashop_version": "1.6.1.24", "ps_eventbus_version": "0.0.0", @@ -66,7 +66,7 @@ To check the fallback route (unauthentified): ```sh BASE_URL="http://localhost:8000" -curl -s -L "$BASE_URL/index.php?fc=module&module=ps_eventbus&controller=apiFront&shop_content=healthcheck" | jq . +curl -s -L "$BASE_URL/index.php?fc=module&module=ps_eventbus&controller=apiFront&is_e2e=1&shop_content=healthcheck" | jq . { "ps_account": true, "is_valid_jwt": true, diff --git a/composer.lock b/composer.lock index cba5142c..d7659e4b 100644 --- a/composer.lock +++ b/composer.lock @@ -1504,5 +1504,5 @@ "platform-overrides": { "php": "5.6" }, - "plugin-api-version": "2.6.0" + "plugin-api-version": "2.3.0" } diff --git a/config/admin/services.yml b/config/admin/services.yml index 521fb78a..0489a1c6 100644 --- a/config/admin/services.yml +++ b/config/admin/services.yml @@ -1,6 +1,7 @@ imports: - { resource: ../common/common.yml } - { resource: ../common/repository.yml } + - { resource: ../common/new-repository.yml } - { resource: ../front/services.yml } services: diff --git a/config/common/new-repository.yml b/config/common/new-repository.yml new file mode 100644 index 00000000..9a91b7d0 --- /dev/null +++ b/config/common/new-repository.yml @@ -0,0 +1,8 @@ +services: + PrestaShop\Module\PsEventbus\Repository\NewRepository\OrderRepository: + class: PrestaShop\Module\PsEventbus\Repository\NewRepository\OrderRepository + public: true + + PrestaShop\Module\PsEventbus\Repository\NewRepository\OrderCartRuleRepository: + class: PrestaShop\Module\PsEventbus\Repository\NewRepository\OrderCartRuleRepository + public: true diff --git a/config/common/repository.yml b/config/common/repository.yml index e51f00e3..d2b8412f 100644 --- a/config/common/repository.yml +++ b/config/common/repository.yml @@ -68,10 +68,6 @@ services: arguments: - '@=service("prestashop.adapter.legacy.context").getContext()' - PrestaShop\Module\PsEventbus\Repository\NewRepositoryTemp\OrderRepository: - class: PrestaShop\Module\PsEventbus\Repository\NewRepositoryTemp\OrderRepository - public: true - PrestaShop\Module\PsEventbus\Repository\OrderDetailsRepository: class: PrestaShop\Module\PsEventbus\Repository\OrderDetailsRepository public: true @@ -82,10 +78,6 @@ services: class: PrestaShop\Module\PsEventbus\Repository\OrderHistoryRepository public: true - PrestaShop\Module\PsEventbus\Repository\OrderCartRuleRepository: - class: PrestaShop\Module\PsEventbus\Repository\OrderCartRuleRepository - public: true - PrestaShop\Module\PsEventbus\Repository\CartRepository: class: PrestaShop\Module\PsEventbus\Repository\CartRepository public: true diff --git a/config/front/services.yml b/config/front/services.yml index cf0bbc98..ae6dfbb7 100644 --- a/config/front/services.yml +++ b/config/front/services.yml @@ -3,6 +3,7 @@ imports: - { resource: ../common/common.yml } - { resource: ../common/decorator.yml } - { resource: ../common/repository.yml } + - { resource: ../common/new-repository.yml } - { resource: ../common/builder.yml } services: @@ -56,6 +57,12 @@ services: class: PrestaShop\Module\PsEventbus\Service\ShopContent\OrdersService public: true arguments: - - '@PrestaShop\Module\PsEventbus\Repository\NewRepositoryTemp\OrderRepository' + - '@PrestaShop\Module\PsEventbus\Repository\NewRepository\OrderRepository' - '@PrestaShop\Module\PsEventbus\Repository\OrderHistoryRepository' - '@PrestaShop\Module\PsEventbus\Formatter\ArrayFormatter' + + PrestaShop\Module\PsEventbus\Service\ShopContent\OrderCartRulesService: + class: PrestaShop\Module\PsEventbus\Service\ShopContent\OrderCartRulesService + public: true + arguments: + - '@PrestaShop\Module\PsEventbus\Repository\NewRepository\OrderCartRuleRepository' diff --git a/controllers/front/apiFront.php b/controllers/front/apiFront.php index 969d8058..e2344373 100644 --- a/controllers/front/apiFront.php +++ b/controllers/front/apiFront.php @@ -29,6 +29,9 @@ public function postProcess() /** @var bool $debug */ $debug = Tools::getValue('debug') == 1; + /** @var bool $ise2e */ + $ise2e = Tools::getValue('is_e2e') == 1; + /** @var Ps_eventbus $module */ $module = Module::getInstanceByName('ps_eventbus'); @@ -38,6 +41,6 @@ public function postProcess() // edit shopContent for matching Config.php const $shopContentEdited = str_replace('-', '_', $shopContent); - $frontApiService->handleDataSync($shopContentEdited, $jobId, $langIso, $limit, $isFull, $debug); + $frontApiService->handleDataSync($shopContentEdited, $jobId, $langIso, $limit, $isFull, $debug, $ise2e); } } diff --git a/e2e-env/docker-compose.yml b/e2e-env/docker-compose.yml index 155c0af7..add5990e 100644 --- a/e2e-env/docker-compose.yml +++ b/e2e-env/docker-compose.yml @@ -12,7 +12,7 @@ services: "-sI", "-H", "Host: localhost:8000", - "http://localhost:80/index.php?fc=module&module=ps_eventbus&controller=apiFront&shop_content=healthcheck", + "http://localhost:80/index.php?fc=module&module=ps_eventbus&controller=apiFront&is_e2e=1&shop_content=healthcheck", ] interval: 30s volumes: diff --git a/e2e/README.md b/e2e/README.md index 1ae8fee1..08bfa19d 100644 --- a/e2e/README.md +++ b/e2e/README.md @@ -18,12 +18,12 @@ import testConfig from './helpers/test.config'; import request from 'supertest'; const controller = 'categories'; -const endpoint = `/index.php?fc=module&module=ps_eventbus&controller=apiFront&shop_content=${shopContent}&limit=5`; +const endpoint = `/index.php?fc=module&module=ps_eventbus&controller=apiFront&is_e2e=1&shop_content=${shopContent}&limit=5`; describe('CategoriesShopContent', () => { it(`${shopContent} should upload to collector`, async () => { // arrange - const url = `${testConfig.prestashopUrl}/index.php?fc=module&module=ps_eventbus&controller=apiFront&shop_content=${shopContent}&limit=5&full=1&job_id=${jobId}`; + const url = `${testConfig.prestashopUrl}/index.php?fc=module&module=ps_eventbus&controller=apiFront&is_e2e=1&shop_content=${shopContent}&limit=5&full=1&job_id=${jobId}`; // jobId starting with "valid-job-" will be considered valid by the mock sync-api and will always return 201; // other values will be rejected by the mock const jobId = 'valid-job-1' diff --git a/e2e/src/full-sync.spec.ts b/e2e/src/full-sync.spec.ts index 737ecd94..78041e9e 100644 --- a/e2e/src/full-sync.spec.ts +++ b/e2e/src/full-sync.spec.ts @@ -15,7 +15,7 @@ import { ShopContent, shopContentList } from "./helpers/shop-contents"; expect.extend(matchers); // these ShopContent will be excluded from the following test suite -const EXCLUDED_API: ShopContent[] = ["google-taxonomies"]; +const EXCLUDED_API: ShopContent[] = ["taxonomies"]; // FIXME : these api can't send anything to the mock api because the database is empty from the factory const MISSING_TEST_DATA: ShopContent[] = [ @@ -64,8 +64,8 @@ describe('Full Sync', () => { }); // TODO : some versions of prestashop include ps_facebook out of the box, this test can't reliably be run for all versions - describe.skip("google-taxonomies", () => { - const shoContent = "google-taxonomies"; + describe.skip("taxonomies", () => { + const shoContent = "taxonomies"; // TODO : apiGoogleTaxonomies requires an additional module to be present : devise a specific test setup for this endpoint it.skip(`${shoContent} should accept full sync`, async () => {}); @@ -74,7 +74,7 @@ describe('Full Sync', () => { it(`${shoContent} should reject full sync when ps_facebook is not installed`, async () => { // arrange - const url = `${testConfig.prestashopUrl}/index.php?fc=module&module=ps_eventbus&controller=apiFront&shop_content=${shoContent}&limit=5&full=1&job_id=${jobId}`; + const url = `${testConfig.prestashopUrl}/index.php?fc=module&module=ps_eventbus&controller=apiFront&is_e2e=1&shop_content=${shoContent}&limit=5&full=1&job_id=${jobId}`; const callId = { 'call_id': Math.random().toString(36).substring(2, 11) }; @@ -101,7 +101,7 @@ describe('Full Sync', () => { describe.each(shopContents)("%s", (shopContent) => { it(`${shopContent} should accept full sync`, async () => { // arrange - const url = `${testConfig.prestashopUrl}/index.php?fc=module&module=ps_eventbus&controller=apiFront&shop_content=${shopContent}&limit=5&full=1&job_id=${jobId}`; + const url = `${testConfig.prestashopUrl}/index.php?fc=module&module=ps_eventbus&controller=apiFront&is_e2e=1&shop_content=${shopContent}&limit=5&full=1&job_id=${jobId}`; const callId = { 'call_id': Math.random().toString(36).substring(2, 11) }; @@ -133,7 +133,7 @@ describe('Full Sync', () => { } else { it(`${shopContent} should upload to collector`, async () => { // arrange - const url = `${testConfig.prestashopUrl}/index.php?fc=module&module=ps_eventbus&controller=apiFront&shop_content=${shopContent}&limit=5&full=1&job_id=${jobId}`; + const url = `${testConfig.prestashopUrl}/index.php?fc=module&module=ps_eventbus&controller=apiFront&is_e2e=1&shop_content=${shopContent}&limit=5&full=1&job_id=${jobId}`; const message$ = probe({ url: `/upload/${jobId}` }); const callId = { 'call_id': Math.random().toString(36).substring(2, 11) }; diff --git a/e2e/src/helpers/data-helper.ts b/e2e/src/helpers/data-helper.ts index 04cae9fc..fb05825c 100644 --- a/e2e/src/helpers/data-helper.ts +++ b/e2e/src/helpers/data-helper.ts @@ -56,7 +56,7 @@ export async function getShopHealthCheck(options?: { healthCheck = cachedHealthCheck; } else { const res = await axios.get( - `${testConfig.prestashopUrl}/index.php?fc=module&module=ps_eventbus&controller=apiFront&shop_content=healthcheck&job_id=valid-job-healthcheck` + `${testConfig.prestashopUrl}/index.php?fc=module&module=ps_eventbus&controller=apiFront&is_e2e=1&shop_content=healthcheck&job_id=valid-job-healthcheck` ); healthCheck = res.data; cachedHealthCheck = healthCheck; diff --git a/e2e/src/helpers/mock-probe.ts b/e2e/src/helpers/mock-probe.ts index cae8710a..645c2ff4 100644 --- a/e2e/src/helpers/mock-probe.ts +++ b/e2e/src/helpers/mock-probe.ts @@ -83,7 +83,7 @@ export function doFullSync(jobId: string, shopContent: ShopContent, options?: Mo const callId = { 'call_id': Math.random().toString(36).substring(2, 11) }; const requestNext = (full: number) => axios.post( - `${testConfig.prestashopUrl}/index.php?fc=module&module=ps_eventbus&controller=apiFront&shop_content=${shopContent}&limit=5&full=${full}&job_id=${jobId}`, + `${testConfig.prestashopUrl}/index.php?fc=module&module=ps_eventbus&controller=apiFront&is_e2e=1&shop_content=${shopContent}&limit=5&full=${full}&job_id=${jobId}`, callId, { headers: { diff --git a/e2e/src/helpers/shop-contents.ts b/e2e/src/helpers/shop-contents.ts index abd72f5b..d73d13ee 100644 --- a/e2e/src/helpers/shop-contents.ts +++ b/e2e/src/helpers/shop-contents.ts @@ -11,7 +11,7 @@ export const shopContentMapping = { 'specific_prices': 'specific-prices', 'custom_product_carriers' : 'custom-product-carriers', 'customers': 'customers', - 'taxonomies': 'google-taxonomies', + 'taxonomies': 'taxonomies', 'modules': 'modules', 'orders': 'orders', 'order_details': 'order-details', diff --git a/e2e/src/reject-invalid-job-id.spec.ts b/e2e/src/reject-invalid-job-id.spec.ts index ecd7472e..807e133f 100644 --- a/e2e/src/reject-invalid-job-id.spec.ts +++ b/e2e/src/reject-invalid-job-id.spec.ts @@ -19,7 +19,7 @@ describe('Reject invalid job-id', () => { it.each(shopContents)(`%s should return 454 with an invalid job id (sync-api status 454)`, async (shopContent) => { expect.assertions(6); // arrange - const url = `${testConfig.prestashopUrl}/index.php?fc=module&module=ps_eventbus&controller=apiFront&shop_content=${shopContent}&limit=5&job_id=${jobId}` + const url = `${testConfig.prestashopUrl}/index.php?fc=module&module=ps_eventbus&controller=apiFront&is_e2e=1&shop_content=${shopContent}&limit=5&job_id=${jobId}` const message$ = probe({params: {id: jobId}}); //act diff --git a/src/Repository/NewRepositoryTemp/AbstractRepository.php b/src/Repository/NewRepository/AbstractRepository.php similarity index 95% rename from src/Repository/NewRepositoryTemp/AbstractRepository.php rename to src/Repository/NewRepository/AbstractRepository.php index 0770ab93..a54de804 100644 --- a/src/Repository/NewRepositoryTemp/AbstractRepository.php +++ b/src/Repository/NewRepository/AbstractRepository.php @@ -1,6 +1,6 @@ query = new \DbQuery(); + + $this->query->from(self::TABLE_NAME, 'ocr'); + + $this->query->select('ocr.id_order_cart_rule'); + $this->query->select('ocr.id_order'); + $this->query->select('ocr.id_cart_rule'); + $this->query->select('ocr.id_order_invoice'); + $this->query->select('ocr.name'); + $this->query->select('ocr.value'); + $this->query->select('ocr.value_tax_excl'); + $this->query->select('ocr.free_shipping'); + + if (defined('_PS_VERSION_') && version_compare(_PS_VERSION_, '1.7.7.0', '>=')) { + $this->query->select('ocr.deleted'); + } + } + + /** + * @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->generateBaseQuery(); + + $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->generateBaseQuery(); + + $this->query->where('ocr.id_order IN(' . implode(',', array_map('intval', $contentIds)) . ')') + ->limit($limit); + + return $this->runQuery($debug); + } + + /** + * @param int $offset + * @param string $langIso + * @param bool $debug + * + * @return int + * + * @throws \PrestaShopException + * @throws \PrestaShopDatabaseException + */ + public function countFullSyncContentLeft($offset, $langIso, $debug) + { + $result = $this->getContentsForFull($offset, 1, $langIso, $debug); + + if (!is_array($result) || empty($result)) { + return 0; + } + + return count($result); + } +} diff --git a/src/Repository/NewRepositoryTemp/OrderRepository.php b/src/Repository/NewRepository/OrderRepository.php similarity index 56% rename from src/Repository/NewRepositoryTemp/OrderRepository.php rename to src/Repository/NewRepository/OrderRepository.php index f757c3c2..d8cf2242 100644 --- a/src/Repository/NewRepositoryTemp/OrderRepository.php +++ b/src/Repository/NewRepository/OrderRepository.php @@ -1,9 +1,6 @@ where('o.id_shop = ' . (int) parent::getShopId()) ->groupBy('o.id_order'); - $this->query->select('o.id_order, o.reference, o.id_customer, o.id_cart, o.current_state'); - $this->query->select('o.conversion_rate, o.total_paid_tax_excl, o.total_paid_tax_incl'); - $this->query->select('IF((SELECT so.id_order FROM `' . _DB_PREFIX_ . 'orders` so WHERE so.id_customer = o.id_customer AND so.id_order < o.id_order LIMIT 1) > 0, 0, 1) as new_customer'); - $this->query->select('c.iso_code as currency, SUM(os.total_products_tax_incl + os.total_shipping_tax_incl) as refund'); - $this->query->select('SUM(os.total_products_tax_excl + os.total_shipping_tax_excl) as refund_tax_excl, o.module as payment_module'); - $this->query->select('o.payment as payment_mode, o.total_paid_real, o.total_shipping as shipping_cost, o.date_add as created_at'); - $this->query->select('o.date_upd as updated_at, o.id_carrier'); + $this->query->select('o.id_order'); + $this->query->select('o.reference'); + $this->query->select('o.id_customer'); + $this->query->select('o.id_cart'); + $this->query->select('o.current_state'); + $this->query->select('o.conversion_rate'); + $this->query->select('o.total_paid_tax_excl'); + $this->query->select('o.total_paid_tax_incl'); + + $this->query->select('c.iso_code as currency'); + $this->query->select('o.module as payment_module'); + $this->query->select('o.payment as payment_mode'); + $this->query->select('o.total_paid_real'); + $this->query->select('o.total_shipping as shipping_cost'); + $this->query->select('o.date_add as created_at'); + $this->query->select('o.date_upd as updated_at'); + $this->query->select('o.id_carrier'); $this->query->select('o.payment as payment_name'); - $this->query->select('CONCAT(CONCAT("delivery", ":", cntd.iso_code), ",", CONCAT("invoice", ":", cnti.iso_code)) as address_iso'); $this->query->select('o.valid as is_validated'); $this->query->select('ost.paid as is_paid'); $this->query->select('ost.shipped as is_shipped'); $this->query->select('osl.name as status_label'); $this->query->select('o.module as payment_name'); - - $this->query->select('o.id_shop_group, o.id_shop, o.id_lang, o.id_currency, o.recyclable, o.gift'); - $this->query->select('o.total_discounts, o.total_discounts_tax_incl, o.total_discounts_tax_excl'); - $this->query->select('o.total_products, o.total_products_wt, o.total_shipping_tax_incl, o.total_shipping_tax_excl'); - $this->query->select('o.carrier_tax_rate, o.total_wrapping, o.total_wrapping_tax_incl, o.total_wrapping_tax_excl'); - $this->query->select('o.round_mode, o.round_type, o.invoice_number, o.delivery_number, o.invoice_date, o.delivery_date, o.valid'); + $this->query->select('o.id_shop_group'); + $this->query->select('o.id_shop'); + $this->query->select('o.id_lang'); + $this->query->select('o.id_currency'); + $this->query->select('o.recyclable'); + $this->query->select('o.gift'); + $this->query->select('o.total_discounts'); + $this->query->select('o.total_discounts_tax_incl'); + $this->query->select('o.total_discounts_tax_excl'); + $this->query->select('o.total_products'); + $this->query->select('o.total_products_wt'); + $this->query->select('o.total_shipping_tax_incl'); + $this->query->select('o.total_shipping_tax_excl'); + $this->query->select('o.carrier_tax_rate'); + $this->query->select('o.total_wrapping'); + $this->query->select('o.total_wrapping_tax_incl'); + $this->query->select('o.total_wrapping_tax_excl'); + $this->query->select('o.round_mode'); + $this->query->select('o.round_type'); + $this->query->select('o.invoice_number'); + $this->query->select('o.delivery_number'); + $this->query->select('o.invoice_date'); + $this->query->select('o.delivery_date'); + $this->query->select('o.valid'); + + $this->query->select('SUM(os.total_products_tax_incl + os.total_shipping_tax_incl) as refund'); + $this->query->select('SUM(os.total_products_tax_excl + os.total_shipping_tax_excl) as refund_tax_excl'); + $this->query->select('CONCAT(CONCAT("delivery", ":", cntd.iso_code), ",", CONCAT("invoice", ":", cnti.iso_code)) as address_iso'); + $this->query->select('IF((SELECT so.id_order FROM `' . _DB_PREFIX_ . 'orders` so WHERE so.id_customer = o.id_customer AND so.id_order < o.id_order LIMIT 1) > 0, 0, 1) as new_customer'); } /** @@ -105,12 +134,12 @@ public function getContentsForIncremental($limit, $contentIds, $langIso, $debug) */ public function countFullSyncContentLeft($offset, $langIso, $debug) { - $orders = $this->getContentsForFull($offset, 1, $langIso, $debug); + $result = $this->getContentsForFull($offset, 1, $langIso, $debug); - if (!is_array($orders) || empty($orders)) { + if (!is_array($result) || empty($result)) { return 0; } - return count($orders); + return count($result); } } diff --git a/src/Interfaces/RepositoryInterface.php b/src/Repository/NewRepository/RepositoryInterface.php similarity index 92% rename from src/Interfaces/RepositoryInterface.php rename to src/Repository/NewRepository/RepositoryInterface.php index 9723ab2d..2d484318 100644 --- a/src/Interfaces/RepositoryInterface.php +++ b/src/Repository/NewRepository/RepositoryInterface.php @@ -1,6 +1,6 @@ db = \Db::getInstance(); - } - - /** - * @return \DbQuery - */ - public function getBaseQuery() - { - $query = new \DbQuery(); - - $query->from(self::TABLE_NAME, 'ocr'); - - return $query; - } - - /** - * @param array $orderIds - * - * @return array|bool|\mysqli_result|\PDOStatement|resource|null - * - * @throws \PrestaShopDatabaseException - */ - public function getOrderCartRules($orderIds) - { - if (!$orderIds) { - return []; - } - - $query = $this->getBaseQuery(); - - $query->select('ocr.id_order_cart_rule,ocr.id_order,ocr.id_cart_rule,ocr.id_order_invoice,ocr.name,ocr.value,ocr.value_tax_excl, ocr.free_shipping'); - - if (defined('_PS_VERSION_') && version_compare(_PS_VERSION_, '1.7.7.0', '>=')) { - $query->select('ocr.deleted'); - } - $query->where('ocr.id_order IN (' . implode(',', array_map('intval', $orderIds)) . ')'); - - return $this->db->executeS($query); - } - - /** - * @param array $orderIds - * - * @return array - * - * @throws \PrestaShopDatabaseException - */ - public function getOrderCartRuleIdsByOrderIds($orderIds) - { - if (!$orderIds) { - return []; - } - - $query = $this->getBaseQuery(); - - $query->select('ocr.id_order_cart_rule as id'); - $query->where('ocr.id_order IN (' . implode(',', array_map('intval', $orderIds)) . ')'); - - $result = $this->db->executeS($query); - - return is_array($result) ? $result : []; - } -} diff --git a/src/Service/FrontApiService.php b/src/Service/FrontApiService.php index 1f240050..cf9f7ea7 100644 --- a/src/Service/FrontApiService.php +++ b/src/Service/FrontApiService.php @@ -11,6 +11,7 @@ use PrestaShop\Module\PsEventbus\Repository\EventbusSyncRepository; use PrestaShop\Module\PsEventbus\Repository\IncrementalSyncRepository; use PrestaShop\Module\PsEventbus\Repository\LanguageRepository; +use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; class FrontApiService { @@ -71,10 +72,11 @@ public function __construct( * @param int $limit * @param bool $isFull * @param bool $debug + * @param bool $ise2e * * @return void */ - public function handleDataSync($shopContent, $jobId, $langIso, $limit, $isFull, $debug) + public function handleDataSync($shopContent, $jobId, $langIso, $limit, $isFull, $debug, $ise2e) { try { if (!in_array($shopContent, array_merge(Config::SHOP_CONTENTS, [Config::COLLECTION_HEALTHCHECK]), true)) { @@ -176,10 +178,31 @@ public function handleDataSync($shopContent, $jobId, $langIso, $limit, $isFull, } catch (FirebaseException $exception) { $this->errorHandler->handle($exception); CommonService::exitWithExceptionMessage($exception); + } catch (ServiceNotFoundException $exception) { + $this->catchGenericException($exception, $ise2e); } catch (\Exception $exception) { - $this->errorHandler->handle($exception); - CommonService::dieWithResponse(['message' => 'An error occured. Please check shop logs for more information'], 500); + $this->catchGenericException($exception, $ise2e); + } + } + + /** + * @param mixed $exception + * @param mixed $ise2e + * + * @return void + * + * @throws \Exception + */ + private function catchGenericException($exception, $ise2e) + { + $this->errorHandler->handle($exception); + + // if debug mode enabled, print error + if (_PS_MODE_DEV_ == true && $ise2e == false) { + throw $exception; } + + CommonService::dieWithResponse(['message' => 'An error occured. Please check shop logs for more information'], 500); } /** diff --git a/src/Service/ProxyService.php b/src/Service/ProxyService.php index 2f8e9091..e804aa50 100644 --- a/src/Service/ProxyService.php +++ b/src/Service/ProxyService.php @@ -8,7 +8,6 @@ use PrestaShop\Module\PsEventbus\Exception\EnvVarException; use PrestaShop\Module\PsEventbus\Formatter\JsonFormatter; use PrestaShop\Module\PsEventbus\Handler\ErrorHandler\ErrorHandlerInterface; -use PrestaShop\Module\PsEventbus\Interfaces\ProxyServiceInterface; class ProxyService implements ProxyServiceInterface { diff --git a/src/Interfaces/ProxyServiceInterface.php b/src/Service/ProxyServiceInterface.php similarity index 90% rename from src/Interfaces/ProxyServiceInterface.php rename to src/Service/ProxyServiceInterface.php index 528ff932..bcf1bc95 100644 --- a/src/Interfaces/ProxyServiceInterface.php +++ b/src/Service/ProxyServiceInterface.php @@ -1,6 +1,6 @@ orderCartRuleRepository = $orderCartRuleRepository; + } + + /** + * @param int $offset + * @param int $limit + * @param string $langIso + * @param bool $debug + * + * @return array + */ + public function getContentsForFull($offset, $limit, $langIso, $debug) + { + $orders = $this->orderCartRuleRepository->getContentsForFull($offset, $limit, $langIso, $debug); + + if (empty($orders)) { + return []; + } + + $this->castOrderCartRules($orders, $langIso); + + return array_map(function ($order) { + return [ + 'id' => $order['id_order'], + 'collection' => Config::COLLECTION_ORDERS, + 'properties' => $order, + ]; + }, $orders); + } + + /** + * @param int $limit + * @param array $contentIds + * @param string $langIso + * @param bool $debug + * + * @return array + */ + public function getContentsForIncremental($limit, $contentIds, $langIso, $debug) + { + $orders = $this->orderCartRuleRepository->getContentsForIncremental($limit, $contentIds, $langIso, $debug); + + if (empty($orders)) { + return []; + } + + $this->castOrderCartRules($orders, $langIso); + + return array_map(function ($order) { + return [ + 'id' => $order['id_order'], + 'collection' => Config::COLLECTION_ORDERS, + 'properties' => $order, + ]; + }, $orders); + } + + /** + * @param int $offset + * @param string $langIso + * @param bool $debug + * + * @return int + */ + public function countFullSyncContentLeft($offset, $langIso, $debug) + { + return (int) $this->orderCartRuleRepository->countFullSyncContentLeft($offset, $langIso, $debug); + } + + /** + * @param array $cartRules + * @param string $langIso + * + * @return void + */ + public function castOrderCartRules(&$cartRules, $langIso) + { + 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']; + } + } +} diff --git a/src/Service/ShopContent/OrdersService.php b/src/Service/ShopContent/OrdersService.php index f9a1b24f..b54178a2 100644 --- a/src/Service/ShopContent/OrdersService.php +++ b/src/Service/ShopContent/OrdersService.php @@ -4,9 +4,8 @@ use PrestaShop\Module\PsEventbus\Config\Config; use PrestaShop\Module\PsEventbus\Formatter\ArrayFormatter; -use PrestaShop\Module\PsEventbus\Interfaces\ShopContentServiceInterface; +use PrestaShop\Module\PsEventbus\Repository\NewRepository\OrderRepository; use PrestaShop\Module\PsEventbus\Repository\OrderHistoryRepository; -use PrestaShop\Module\PsEventbus\Repository\NewRepositoryTemp\OrderRepository; class OrdersService implements ShopContentServiceInterface { diff --git a/src/Interfaces/ShopContentServiceInterface.php b/src/Service/ShopContent/ShopContentServiceInterface.php similarity index 92% rename from src/Interfaces/ShopContentServiceInterface.php rename to src/Service/ShopContent/ShopContentServiceInterface.php index 9223815c..718e7fa3 100644 --- a/src/Interfaces/ShopContentServiceInterface.php +++ b/src/Service/ShopContent/ShopContentServiceInterface.php @@ -1,6 +1,6 @@