From e9ba15e8bcf6fb84de89a22163f6518b93b102d6 Mon Sep 17 00:00:00 2001 From: Bharat Kunwar Date: Wed, 2 Oct 2024 14:23:58 +0100 Subject: [PATCH] CET-461/feat: Support new company search in Netherlands --- Api/Config/RepositoryInterface.php | 4 +- Model/Config/Repository.php | 10 ++-- Model/Ui/ConfigProvider.php | 2 +- .../web/js/view/address-autocomplete.js | 54 ++++++------------- .../payment/method-renderer/two_payment.js | 42 +++++++-------- 5 files changed, 41 insertions(+), 71 deletions(-) diff --git a/Api/Config/RepositoryInterface.php b/Api/Config/RepositoryInterface.php index df12bb3c..3e161322 100755 --- a/Api/Config/RepositoryInterface.php +++ b/Api/Config/RepositoryInterface.php @@ -217,11 +217,11 @@ public function getWeightUnit(?int $storeId = null): string; public function getUrls(string $route, ?array $params = []): string; /** - * Get search host urls + * Get search host url * * @return array */ - public function getSearchHostUrls(): array; + public function getSearchHostUrl(): array; /** * Get checkout API url diff --git a/Model/Config/Repository.php b/Model/Config/Repository.php index 5f0a474a..ac0c3bdb 100755 --- a/Model/Config/Repository.php +++ b/Model/Config/Repository.php @@ -239,13 +239,11 @@ public function getUrls(string $route, ?array $params = []): string /** * @inheritDoc */ - public function getSearchHostUrls(): array + public function getSearchHostUrl(): array { - return [ - 'gb' => 'https://gb.search.two.inc', - 'no' => 'https://no.search.two.inc', - 'se' => 'https://se.search.two.inc' - ]; + $mode = $mode ?: $this->getMode(); + $prefix = $mode == 'production' ? 'search' : ('search.' . $mode); + return sprintf(self::URL_TEMPLATE, $prefix); } /** diff --git a/Model/Ui/ConfigProvider.php b/Model/Ui/ConfigProvider.php index c735bd42..01ca9a4d 100755 --- a/Model/Ui/ConfigProvider.php +++ b/Model/Ui/ConfigProvider.php @@ -49,7 +49,7 @@ public function __construct( public function getConfig(): array { $companySearchConfig = [ - 'searchHosts' => $this->configRepository->getSearchHostUrls(), + 'searchHost' => $this->configRepository->getSearchHostUrl(), 'searchLimit' => 50, ]; diff --git a/view/frontend/web/js/view/address-autocomplete.js b/view/frontend/web/js/view/address-autocomplete.js index 6b50bfef..3602bf9d 100755 --- a/view/frontend/web/js/view/address-autocomplete.js +++ b/view/frontend/web/js/view/address-autocomplete.js @@ -51,12 +51,7 @@ define([ const countryCode = $(this.countrySelector).val().toLowerCase(); customerData.set('countryCode', countryCode); let field = $(this.companyNameSelector).closest('.field'); - if (countryCode in config.companySearchConfig.searchHosts) { - field.show(); - } else { - field.hide(); - this.setCompanyData(); - } + field.show(); }, setCompanyData: function (companyId = '', companyName = '') { console.debug({ logger: 'addressAutocomplete.setCompanyData', companyId, companyName }); @@ -88,22 +83,15 @@ define([ dataType: 'json', delay: 400, url: function (params) { - var searchHosts = config.companySearchConfig.searchHosts, - selectedCountryCode = $(self.countrySelector).val(), - searchHost = ''; - if (selectedCountryCode.toLowerCase() in searchHosts) { - searchHost = searchHosts[selectedCountryCode.toLowerCase()]; - } - params.page = params.page || 1; - return ( - searchHost + - '/search?limit=' + - searchLimit + - '&offset=' + - (params.page - 1) * searchLimit + - '&q=' + - unescape(params.term) - ); + const queryParams = new URLSearchParams({ + country: $(self.countrySelector).val(), + limit: searchLimit, + offset: ((params.page || 1) - 1) * searchLimit, + q: unescape(params.term) + }); + return `${ + config.companySearchConfig.searchHost + }/companies/v1/company?${queryParams.toString()}`; }, processResults: function (response, params) { var items = []; @@ -113,8 +101,8 @@ define([ items.push({ id: item.name, text: item.name, - html: `${item.highlight} (${item.id})`, - companyId: item.id + html: `${item.highlight} (${item.national_identifier.id})`, + companyId: item.national_identifier.id }); } } @@ -154,22 +142,12 @@ define([ $('.select2-selection__rendered').text(selectedItem.id); self.setCompanyData(selectedItem.companyId, selectedItem.text); if (self.isAddressSearchEnabled) { - let countryId = $(self.countrySelector).val(); - if ( - _.indexOf( - self.supportedCountryCodes, - countryId.toLowerCase() - ) != -1 - ) { + const companyId = selectedItem.companyId; + const countryCode = $(self.countrySelector).val().toLowerCase(); + if (_.indexOf(self.supportedCountryCodes, countryCode) != -1) { const addressResponse = $.ajax({ dataType: 'json', - url: - config.checkoutApiUrl + - '/v1/' + - countryId.toUpperCase() + - '/company/' + - selectedItem.companyId + - '/address' + url: `${config.checkoutApiUrl}/v1/${countryCode}/company/${companyId}/address` }); addressResponse.done(function (response) { if (response.address) { diff --git a/view/frontend/web/js/view/payment/method-renderer/two_payment.js b/view/frontend/web/js/view/payment/method-renderer/two_payment.js index 42ad8ccf..86ada80c 100755 --- a/view/frontend/web/js/view/payment/method-renderer/two_payment.js +++ b/view/frontend/web/js/view/payment/method-renderer/two_payment.js @@ -368,14 +368,13 @@ define([ console.debug({ logger: 'twoPayment.placeOrderIntent', orderIntentRequestBody }); + const queryParams = new URLSearchParams({ + client: config.orderIntentConfig.extensionPlatformName, + client_v: config.orderIntentConfig.extensionDBVersion + }); + return $.ajax({ - url: - config.checkoutApiUrl + - '/v1/order_intent?' + - 'client=' + - config.orderIntentConfig.extensionPlatformName + - '&client_v=' + - config.orderIntentConfig.extensionDBVersion, + url: `${config.checkoutApiUrl}/v1/order_intent?${queryParams.toString()}`, type: 'POST', global: true, contentType: 'application/json', @@ -427,20 +426,15 @@ define([ dataType: 'json', delay: 400, url: function (params) { - var searchHosts = config.companySearchConfig.searchHosts, - billingAddress = quote.billingAddress(), - searchHost = searchHosts[self.countryCode()]; - if (!searchHost) return; - params.page = params.page || 1; - return ( - searchHost + - '/search?limit=' + - searchLimit + - '&offset=' + - (params.page - 1) * searchLimit + - '&q=' + - unescape(params.term) - ); + const queryParams = new URLSearchParams({ + country: self.countryCode(), + limit: searchLimit, + offset: ((params.page || 1) - 1) * searchLimit, + q: unescape(params.term) + }); + return `${ + config.companySearchConfig.searchHost + }/companies/v1/company?${queryParams.toString()}`; }, processResults: function (response, params) { var items = []; @@ -450,8 +444,8 @@ define([ items.push({ id: item.name, text: item.name, - html: `${item.highlight} (${item.id})`, - companyId: item.id + html: `${item.highlight} (${item.national_identifier.id})`, + companyId: item.national_identifier.id }); } } @@ -635,7 +629,7 @@ define([ }, getCurrentBuyer() { - const URL = config.checkoutApiUrl + '/autofill/v1/buyer/current'; + const URL = `${config.checkoutApiUrl}/autofill/v1/buyer/current`; const OPTIONS = { credentials: 'include', headers: {