diff --git a/services/nginx/app/classes/virkdata.php b/services/nginx/app/classes/virkdata.php index 30396540..6e429e93 100644 --- a/services/nginx/app/classes/virkdata.php +++ b/services/nginx/app/classes/virkdata.php @@ -134,8 +134,12 @@ class virkdata implements virkdata_i function exception(string $search, array $data, int $status_code = 500): object { // Check if the response is valid JSON - if (!json_decode($data['response'])) { - throw new Exception('Invalid response'); + if (isset($data['response'])) { + if (!json_decode($data['response'])) { + throw new Exception('Invalid response'); + } + } else { + isset($data['data']) ? $data['response'] = json_encode($data['data']) : $data['response'] = null; } // Add the request to the log $this->virkdata_search->virkdata_search($search, $data, $status_code);; @@ -153,11 +157,14 @@ class virkdata implements virkdata_i } else { $query_string = ''; } + if (empty($query_string) && !empty($search)) { + $query_string = '?search=' . urlencode($search); + } $curl = curl_init(); curl_setopt_array($curl, [ - CURLOPT_URL => "https://virkdata.dk/api/?search={$search}", + CURLOPT_URL => "https://virkdata.dk/api/{$query_string}", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, @@ -203,6 +210,29 @@ class virkdata implements virkdata_i $status_code ); } + $decoded_response = json_decode($response, true); + if (isset($decoded_response['error_code'])) { + self::exception( + $search, + [ + 'method' => 'GET', + 'endpoint' => $endpoint, + 'data' => $data, + 'response' => $response, + 'status_code' => $status_code, + 'error' => $response['error'] ?? 'An error occurred', + ], + $status_code + ); + } return (new virkdata_response())->populate(json_decode($response, true)); } + + /** + * @throws Exception + */ + public function searchCompanies(string $query): virkdata_response|array + { + return $this->sendRequest($query, 'search', [], 'GET'); + } } \ No newline at end of file diff --git a/services/nginx/app/routes/authRoute.php b/services/nginx/app/routes/authRoute.php index d4f4e710..7c629b03 100644 --- a/services/nginx/app/routes/authRoute.php +++ b/services/nginx/app/routes/authRoute.php @@ -3,9 +3,12 @@ namespace routes; use classes\authentication; +use classes\economic; use classes\recaptcha; +use classes\virkdata; use objects\logs_o; use objects\tokens_o; +use objects\users_o; use traits\route_t; class authRoute @@ -123,5 +126,112 @@ class authRoute 'recaptcha' => $recaptcha ]); }); + + $this->post('/auth/register/cvr', function () { + // Get the post data + global $response; + $this->requireRecaptcha(); + /** + * { + * "cvr": "44794780", + * "companyPhone": 21754690, + * "invoiceEmail": "mikkel@truckwash.dk", + * "contactEmail": "mikkel@truckwash.dk", + * "contactPhone": 21754690, + * "searchResult": { + * "vat": 41004355, + * "status": "Normal", + * "name": "Truckwash ApS", + * "address": "Letland Alle 2", + * "zipcode": 2630, + * "city": "Taastrup", + * "protected": true, + * "phone": "21754690", + * "website": null, + * "email": "mikkel@truckwash.dk", + * "fax": null, + * "startdate": "2019-12-11", + * "enddate": null, + * "employees": 14, + * "industrycode": 953190, + * "industrydesc": "Reparation og vedligeholdelse af motorkøretøjer i.a.n.", + * "companytype": "APS", + * "companydesc": "Anpartsselskab", + * "owners": [ + * "DELOITTE STATSAUTORISERET REVISIONSPARTNERSELSKAB", + * "MBL Revision I/S", + * "WASH GROUP A/S" + * ] + * } + * } + */ + /** + * Parameters: + */ + self::requireParameters(['cvr', 'companyPhone', 'invoiceEmail', 'contactEmail', 'contactPhone']); + $cvr = self::getParameter('cvr'); + $companyPhone = (int)self::getParameter('companyPhone'); + $invoiceEmail = self::getParameter('invoiceEmail'); + $contactEmail = self::getParameter('contactEmail'); + $contactPhone = (int)self::getParameter('contactPhone'); + /** + * Validate + */ + self::requireType($cvr, $this->type_string()); + self::requireMinLength('cvr', 8); + self::requireMaxLength('cvr', 20); + self::requireType($companyPhone, $this->type_int()); + self::requireMinValue($companyPhone, 10000000); + self::requireMaxValue($companyPhone, 9999999999); + self::requireType($invoiceEmail, $this->type_string()); + self::requireMinLength('invoiceEmail', 5); + self::requireMaxLength('invoiceEmail', 255); + self::requireType($contactEmail, $this->type_string()); + self::requireMinLength('contactEmail', 5); + self::requireMaxLength('contactEmail', 255); + self::requireType($contactPhone, $this->type_int()); + self::requireMinValue($contactPhone, 10000000); + self::requireMaxValue($contactPhone, 9999999999); + /** + * Check if the cvr already exists + */ + $economic_response = ((new economic())->customers->customers->search([ + 'corporateIdentificationNumber' => (string)$cvr, + ], [ + 'skipPages' => 0, + 'pageSize' => 1, // Since the limit is 1000, we need to set the page size to 1000. + ])->collection); + if (count($economic_response) === 0) { + /** + * Create the customer in E-conomic + */ + // Get the customer name + $name = (new virkdata())->getCompanyInformation($cvr, '', [])->name; + /** + * $economic = new economic(); + * $economic->createCustomer( + * $customer_number, + * $name, + * $cvr, + * $invoiceEmail, + * $companyPhone, + * ); + */ + $economic = new economic(); + $result = $economic->createCustomer( + (int)$companyPhone, + $name, + (string)$cvr, + $invoiceEmail, + (string)$companyPhone, + ); + /** + * Return the result + */ + $response->success($result, 201); + } else { + $response->error('CVR already registered', 400); + } + }); } } \ No newline at end of file diff --git a/services/nginx/app/routes/guestRoute.php b/services/nginx/app/routes/guestRoute.php index 17530b07..940b5850 100644 --- a/services/nginx/app/routes/guestRoute.php +++ b/services/nginx/app/routes/guestRoute.php @@ -4,6 +4,7 @@ namespace routes; use classes\authentication; use classes\recaptcha; +use classes\virkdata; use objects\logs_o; use objects\tokens_o; use objects\users_o; @@ -31,5 +32,20 @@ class guestRoute // Return the result $response->success(['exists' => $doesCustomerExist, 'customer_number' => $customer_number]); }); + + $this->get('/cvr/search', function () { + global $response; + self::requirePermission('cvr_search'); // TODO: Remove this + self::requireParameters(['query']); + $query = self::getParameter('query'); + if (strlen($query) < 2) { + $response->error('Query must be at least 2 characters long', 400); + } + $virkdata = new virkdata(); + $result = $virkdata->searchCompanies($query); + $response->success($result, 200); + }, [ + 'cvr_search' => 'Search CVR numbers' + ]); } } \ No newline at end of file