Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes: RecordCollectors - catching and translation messages #17228

Open
wants to merge 5 commits into
base: developer
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 30 additions & 2 deletions app/RecordCollectors/Base.php
Expand Up @@ -60,8 +60,7 @@ class Base
protected $modulesFieldsMap = [];

/** @var array Form mapping for loading record data. */
public $formFieldsToRecordMap = [];

public array $formFieldsToRecordMap = [];
/**
* Constructor.
*/
Expand Down Expand Up @@ -149,6 +148,7 @@ public function getFieldsModule(string $moduleName): array
return $this->modulesFieldsMap[$moduleName];
}


/**
* Check whether it is active.
*
Expand Down Expand Up @@ -239,6 +239,7 @@ public function loadData(): void
}
}
}
$this->response['error'] = isset($additional['error'], $additional['message']) ? $additional['message'] : false;
$this->response['fields'] = $fieldsData;
$this->response['skip'] = $skip;
$this->response['keys'] = array_keys($rows);
Expand Down Expand Up @@ -296,4 +297,31 @@ protected function getRecordModel(): \Vtiger_Record_Model
}
return $recordModel;
}

/**
* @param string $message
* @return string
*/
protected function getTranslationResponseMessage(string $message): string
{
switch ($message) {
case 'Not Found':
$translatedMessage = \App\Language::translate('LBL_NO_FOUND_RECORD', 'Other.RecordCollector', null, false);
break;
case 'TOO_MANY_OPTIONS':
$translatedMessage = \App\Language::translate('LBL_TOO_MANY_OPTIONS', 'Other.RecordCollector', null, false);
break;
case 'NO_SEARCH':
$translatedMessage = \App\Language::translate('LBL_NO_FILLED_DATA', 'Other.RecordCollector', null, false);
break;
case 'Bad Request':
$translatedMessage = \App\Language::translate('LBL_BAD_REQUEST', 'Other.RecordCollector', null, false);
break;
default:
$translatedMessage = $message;
break;
}

return $translatedMessage;
}
}
45 changes: 30 additions & 15 deletions app/RecordCollectors/BrReceitaWsCnpj.php
Expand Up @@ -45,7 +45,7 @@ class BrReceitaWsCnpj extends Base

/** {@inheritdoc} */
public $settingsFields = [
'api_key' => ['required' => 0, 'purifyType' => 'Text', 'label' => 'LBL_API_KEY_OPTIONAL'],
'api_key' => ['required' => 1, 'purifyType' => 'Text', 'label' => 'LBL_API_KEY_OPTIONAL'],
];

/** {@inheritdoc} */
Expand All @@ -71,7 +71,7 @@ class BrReceitaWsCnpj extends Base
];

/** {@inheritdoc} */
public $formFieldsToRecordMap = [
public array $formFieldsToRecordMap = [
'Accounts' => [
'nome' => 'accountname',
'fantasia' => 'accountname',
Expand Down Expand Up @@ -135,11 +135,11 @@ class BrReceitaWsCnpj extends Base
/** {@inheritdoc} */
public function search(): array
{
$cnpj = str_replace([' ', '/', '.', '-'], '', $this->request->getByType('cnpj', 'Text'));
if (!$this->isActive() || empty($cnpj)) {
$cnpjNumber = str_replace([' ', '/', '.', '-'], '', $this->request->getByType('cnpj', 'Text'));
if (empty($cnpjNumber) || !$this->isActive()) {
return [];
}
$this->getDataFromApi($cnpj);
$this->getDataFromApi($cnpjNumber);
$this->loadData();
return $this->response;
}
Expand All @@ -151,7 +151,7 @@ public function search(): array
*
* @return void
*/
private function getDataFromApi(string $cnpj): void
private function getDataFromApi(string $cnpjNumber): void
{
try {
$this->setApiKey();
Expand All @@ -162,23 +162,17 @@ private function getDataFromApi(string $cnpj): void
]
];
}
$response = \App\RequestHttp::getClient()->get($this->url . $cnpj, $options ?? []);
$response = \App\RequestHttp::getClient()->get($this->url . $cnpjNumber, $options ?? []);
$data = $this->parseData(\App\Json::decode($response->getBody()->getContents()));
if (isset($data['status']) && 'ERROR' === $data['status']) {
$this->response['error'] = $data['message'];
$this->response['error'] = $this->getTranslationResponseMessage($data['message']);
unset($this->data['fields']);
} else {
$this->data = $data;
}
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
\App\Log::warning($e->getMessage(), 'RecordCollectors');
if (429 === $e->getCode()) {
$this->response['error'] = \App\Language::translate('LBL_BR_RECITA_WS_CNPJ_ERROR', 'Other.RecordCollector');
} elseif ($e->getCode() > 400) {
$this->response['error'] = $e->getResponse()->getReasonPhrase();
} else {
$this->response['error'] = $e->getMessage();
}
$this->response['error'] = $this->getTranslationResponseMessage($this->response['error'] ?? $e->getResponse()->getReasonPhrase());
}
}

Expand All @@ -205,4 +199,25 @@ private function setApiKey(): void
$this->apiKey = $params['api_key'];
}
}

/**
* @param string $message
* @return string
*/
protected function getTranslationResponseMessage(string $message): string
{
switch ($message) {
case 'CNPJ inválido':
$translatedMessage = \App\Language::translate('LBL_BR_RECITA_WS_CNPJ_INVALIDATE', 'Other.RecordCollector', null, false);
break;
case 'Too Many Requests':
$translatedMessage = \App\Language::translate('LBL_TOO_MANY_REQUESTS', 'Other.RecordCollector', null, false);
break;
default :
$translatedMessage = $message;
break;
}

return $translatedMessage;
}
}
6 changes: 3 additions & 3 deletions app/RecordCollectors/ChZefix.php
Expand Up @@ -86,7 +86,7 @@ class ChZefix extends Base
];

/** {@inheritdoc} */
public $formFieldsToRecordMap = [
public array $formFieldsToRecordMap = [
'Accounts' => [
'name' => 'accountname',
'translation1' => 'accountname',
Expand Down Expand Up @@ -214,7 +214,7 @@ private function getCompanyById(string $companyId): void
}
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
\App\Log::warning($e->getMessage(), 'RecordCollectors');
$this->response['error'] = $e->getResponse()->getReasonPhrase();
$this->response['error'] = $this->getTranslationResponseMessage($e->getResponse()->getReasonPhrase());
}
}

Expand Down Expand Up @@ -247,7 +247,7 @@ private function getCompaniesByName(string $companyName): void
}
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
\App\Log::warning($e->getMessage(), 'RecordCollectors');
$this->response['error'] = $e->getResponse()->getReasonPhrase();
$this->response['error'] = $this->getTranslationResponseMessage($e->getResponse()->getReasonPhrase());
}
}

Expand Down
22 changes: 12 additions & 10 deletions app/RecordCollectors/DkCvr.php
Expand Up @@ -49,7 +49,7 @@ class DkCvr extends Base

/** {@inheritdoc} */
public $settingsFields = [
'token' => ['required' => 0, 'purifyType' => 'Text', 'label' => 'LBL_API_KEY_OPTIONAL'],
'token' => ['required' => 1, 'purifyType' => 'Text', 'label' => 'LBL_API_KEY_OPTIONAL'],
];

/** {@inheritdoc} */
Expand Down Expand Up @@ -99,7 +99,7 @@ class DkCvr extends Base
];

/** {@inheritdoc} */
public $formFieldsToRecordMap = [
public array $formFieldsToRecordMap = [
'Accounts' => [
'name' => 'accountname',
'vat' => 'vat_id',
Expand Down Expand Up @@ -196,7 +196,7 @@ public function search(): array
if ($phone = $this->request->getByType('phone', 'Text')) {
$params['phone'] = $phone;
}
if (!$this->isActive() && empty($params)) {
if (empty($params) && !$this->isActive()) {
return [];
}
$params['country'] = $this->request->getByType('country', 'Text');
Expand Down Expand Up @@ -225,24 +225,26 @@ private function setToken(): void
*
* @return void
*/
private function getDataFromApi($params): void
private function getDataFromApi(array $params): void
{
$params['format'] = 'json';
if (!empty($this->token)) {
$params['token'] = $this->token;
}
try {
$response = \App\RequestHttp::getClient()->get($this->url . http_build_query($params));
if (200 === $response->getStatusCode()) {
$this->data = $this->parseData(\App\Json::decode($response->getBody()->getContents()));
if (isset($this->data['name'])) {
$this->response['links'][0] = self::EXTERNAL_URL . $params['country'] . '/' . urlencode(str_replace(' ', '-', $this->data['name'])) . '/' . urlencode($this->data['vat']);
}
$this->data = $this->parseData(\App\Json::decode($response->getBody()->getContents()));
if (isset($this->data['error'])) {
$this->data['error'] = $this->getTranslationResponseMessage($this->data['error']);
}
if (isset($this->data['name'])) {
$this->response['links'][0] = self::EXTERNAL_URL . $params['country'] . '/' . urlencode(str_replace(' ', '-', $this->data['name'])) . '/' . urlencode($this->data['vat']);
}
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
\App\Log::warning($e->getMessage(), 'RecordCollectors');
$this->response['error'] = $e->getResponse()->getReasonPhrase();
$this->response['error'] = $this->getTranslationResponseMessage($this->response['error'] ?? $e->getResponse()->getReasonPhrase());
}

if ($this->data && empty($this->data['error'])) {
switch ($params['country']) {
case 'no':
Expand Down
32 changes: 27 additions & 5 deletions app/RecordCollectors/FrEnterpriseGouv.php
Expand Up @@ -21,6 +21,9 @@
*/
class FrEnterpriseGouv extends Base
{
/** @var int Number of items returned */
public const LIMIT = 4;

/** {@inheritdoc} */
public $allowedModules = ['Accounts', 'Leads', 'Vendors', 'Partners', 'Competition'];

Expand Down Expand Up @@ -84,7 +87,7 @@ class FrEnterpriseGouv extends Base
];

/** {@inheritdoc} */
public $formFieldsToRecordMap = [
public array $formFieldsToRecordMap = [
'Accounts' => [
'nom_complet' => 'accountname',
'siren' => 'vat_id',
Expand Down Expand Up @@ -127,9 +130,6 @@ class FrEnterpriseGouv extends Base
],
];

/** @var int Number of items returned */
const LIMIT = 4;

/** {@inheritdoc} */
public function search(): array
{
Expand Down Expand Up @@ -174,11 +174,12 @@ private function getDataFromApi(array $query): void
$data = isset($response) ? \App\Json::decode($response->getBody()->getContents()) : [];
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
\App\Log::warning($e->getMessage(), 'RecordCollectors');
$this->response['error'] = $e->getMessage();
$this->response['error'] = $this->getTranslationResponseMessage($e->getResponse()->getReasonPhrase());
}
if (empty($data)) {
return;
}

foreach ($data['results'] as $key => $result) {
$this->data[$key] = $this->parseData($result);
}
Expand All @@ -195,4 +196,25 @@ private function parseData(array $data): array
{
return \App\Utils::flattenKeys($data, 'ucfirst');
}

/**
* @param string $message
* @return string
*/
protected function getTranslationResponseMessage(string $message): string
{
switch ($message) {
case 'Not Found':
$translatedMessage = \App\Language::translate('LBL_NO_FOUND_RECORD', 'Other.RecordCollector', null, false);
break;
case 'Bad Request':
$translatedMessage = \App\Language::translate('LBL_FR_GOUV_BAD_REQUEST', 'Other.RecordCollector', null, false);
break;
default :
$translatedMessage = $message;
break;
}

return $translatedMessage;
}
}
4 changes: 2 additions & 2 deletions app/RecordCollectors/Gus.php
Expand Up @@ -83,7 +83,7 @@ class Gus extends Base
];

/** {@inheritdoc} */
public $formFieldsToRecordMap = [
public array $formFieldsToRecordMap = [
'Accounts' => [
'Nazwa' => 'accountname',
'Regon' => 'registration_number_2',
Expand Down Expand Up @@ -116,7 +116,6 @@ class Gus extends Base
'Powiat' => 'addresslevel3a',
'Wojewodztwo' => 'addresslevel2a',
'Kraj' => 'addresslevel1a',
'NumerBudynku' => 'buildingnumbera',
'NumerTelefonu' => 'phone',
'NumerFaksu' => 'fax',
'AdresEmail' => 'email',
Expand Down Expand Up @@ -176,6 +175,7 @@ public function search(): array
$response = [];
$moduleName = $this->request->getModule();
$client = \App\RecordCollectors\Helper\GusClient::getInstance($this->getClientParams($moduleName));

try {
$infoFromGus = $client->search($vatId, $ncr, $taxNumber);
$response['recordModel'] = $this->getRecordModel();
Expand Down
17 changes: 6 additions & 11 deletions app/RecordCollectors/NoBrregEnhetsregisteret.php
Expand Up @@ -65,7 +65,7 @@ class NoBrregEnhetsregisteret extends Base
];

/** {@inheritdoc} */
public $formFieldsToRecordMap = [
public array $formFieldsToRecordMap = [
'Accounts' => [
'navn' => 'accountname',
'organisasjonsnummer' => 'registration_number_1',
Expand Down Expand Up @@ -117,7 +117,7 @@ public function search(): array
{
$companyNumber = str_replace([' ', ',', '.', '-'], '', $this->request->getByType('companyNumber', 'Text'));

if (!$this->isActive() && empty($companyNumber)) {
if (empty($companyNumber) && !$this->isActive()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't it be "||" instead of "&&"?

return [];
}

Expand All @@ -140,19 +140,14 @@ private function getDataFromApi(string $companyNumber): void
$response = \App\RequestHttp::getClient()->get($this->url . $companyNumber);
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
\App\Log::warning($e->getMessage(), 'RecordCollectors');
$this->response['error'] = $e->getMessage();
if (400 === $e->getCode()) {
$this->response['error'] = \App\Language::translate('LBL_NO_BRREG_ENHETSREGISTERET_400', 'Other.RecordCollector');
$this->response['error'] = \App\Language::translate('LBL_NO_BRREG_ENHETSREGISTERET_400', 'Other.RecordCollector', null, false);
return;
}
}
if (empty($response)) {
$this->response['error'] = \App\Language::translate('LBL_COMPANY_NOT_FOUND', 'Other.RecordCollector');
} else {
$this->data = $this->parseData(\App\Json::decode($response->getBody()->getContents()));
$this->response['links'][0] = self::EXTERNAL_URL . $companyNumber;
unset($this->data['_linksSelfHref']);
}
$this->data = !empty($response) ? $this->parseData(\App\Json::decode($response->getBody()->getContents())) : [];
$this->response['links'][0] = self::EXTERNAL_URL . $companyNumber;
unset($this->data['_linksSelfHref']);
}

/**
Expand Down