-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
187 lines (151 loc) · 9.62 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
const axios = require('axios');
const xml2js = require('xml2js');
const isXML = require('is-xml');
const zlib = require("zlib");
const util = require('util');
const gunzip = util.promisify(zlib.gunzip);
/**
* Request publicity records from AADE for a specific company VATID [https://www.aade.gr/epiheiriseis/forologikes-ypiresies/mitroo/anazitisi-basikon-stoiheion-mitrooy-epiheiriseon]
* @param {string} search_vatid - Company VATID that you want to request publicity records (Required)
* @param {string} aade_publicity_username - AADE Publicity username that used for API call (Required) [https://www1.aade.gr/sgsisapps/tokenservices/protected/displayConsole.htm]
* @param {string} aade_publicity_password - AADE Publicity password that used for API call (Required) [https://www1.aade.gr/sgsisapps/tokenservices/protected/displayConsole.htm]
* @param {string} searched_by_vatid - Publicity records requested by VATID (Optional)
* @param {boolean} debug - Logging steps until finish, if you enable this (Optional)
*/
function convertArrayValues(object){
Object.entries(Object.keys(object)).forEach(entry => {
const [key, value] = entry;
if(typeof object[value][0] === "object"){
object[value] = null;
}else{
object[value] = object[value][0];
object[value] = object[value].trim();
}
});
}
function parseXml(xml, debug = false) {
return new Promise((resolve, reject) => {
xml2js.parseString(xml, (err, result) => {
if (err) {
reject(err);
} else {
let final_arr_return = [];
if(err != null){
return (Error ('Error parsing XML Response from AADE (if you believe is this a bug, open a issue)'))
}
const final_json_text = JSON.stringify(result, null, 4);
const final_json = JSON.parse(final_json_text);
const call_seq_id = final_json['env:Envelope']['env:Body'][0]['srvc:rgWsPublic2AfmMethodResponse'][0]['srvc:result'][0]['rg_ws_public2_result_rtType'][0]['call_seq_id'][0];
let error_code = null;
if(final_json['env:Envelope']['env:Body'][0]['srvc:rgWsPublic2AfmMethodResponse'][0]['srvc:result'][0]['rg_ws_public2_result_rtType'][0]['error_rec'][0]['error_code'][0]['$'] === undefined){
error_code = final_json['env:Envelope']['env:Body'][0]['srvc:rgWsPublic2AfmMethodResponse'][0]['srvc:result'][0]['rg_ws_public2_result_rtType'][0]['error_rec'][0]['error_code'][0]
}
let error_descr = null;
if(final_json['env:Envelope']['env:Body'][0]['srvc:rgWsPublic2AfmMethodResponse'][0]['srvc:result'][0]['rg_ws_public2_result_rtType'][0]['error_rec'][0]['error_descr'][0]['$'] === undefined){
error_descr = final_json['env:Envelope']['env:Body'][0]['srvc:rgWsPublic2AfmMethodResponse'][0]['srvc:result'][0]['rg_ws_public2_result_rtType'][0]['error_rec'][0]['error_descr'][0]
}
let company_data = null;
let company_sectors = null
if(error_code == null && error_descr == null){
company_data = final_json['env:Envelope']['env:Body'][0]['srvc:rgWsPublic2AfmMethodResponse'][0]['srvc:result'][0]['rg_ws_public2_result_rtType'][0]['basic_rec'][0];
convertArrayValues(company_data)
if(final_json['env:Envelope']['env:Body'][0]['srvc:rgWsPublic2AfmMethodResponse'][0]['srvc:result'][0]['rg_ws_public2_result_rtType'][0]['firm_act_tab'] !== undefined){
company_sectors = final_json['env:Envelope']['env:Body'][0]['srvc:rgWsPublic2AfmMethodResponse'][0]['srvc:result'][0]['rg_ws_public2_result_rtType'][0]['firm_act_tab'][0]['item'];
company_sectors.forEach(e => {
convertArrayValues(e)
});
}
company_data.company_sectors = company_sectors;
}
final_arr_return['call_seq_id'] = call_seq_id;
if((error_code == null && error_descr == null) && company_data != null){
final_arr_return['have_errors'] = false;
final_arr_return['errors_info'] = [];
final_arr_return['errors_info']['code'] = null;
final_arr_return['errors_info']['descr'] = null;
final_arr_return['data'] = company_data;
}else{
final_arr_return['have_errors'] = true;
final_arr_return['errors_info'] = [];
final_arr_return['errors_info']['code'] = error_code;
final_arr_return['errors_info']['descr'] = error_descr;
final_arr_return['data'] = null;
}
if(debug){
console.log("[*][aade-publicity-search] Parsing AADE Response");
}
resolve(final_arr_return);
}
});
});
}
async function getCompanyPublicityByAADE(search_vatid, aade_publicity_username, aade_publicity_password, searched_by_vatid = null, debug = false){
try {
let data_xml = "";
let final_arr_return = [];
let final_result = [];
if(debug){
console.log("[*][aade-publicity-search] Starting...");
}
if(search_vatid == null || search_vatid == ""){
return (Error ('You need to pass a VATID for publicity search'))
}
if((aade_publicity_username == null || aade_publicity_username == "") || (aade_publicity_password == null || aade_publicity_password == "")){
return (Error ('You need to provide special credentials to call AADE API. More info: https://www.aade.gr/epiheiriseis/forologikes-ypiresies/mitroo/anazitisi-basikon-stoiheion-mitrooy-epiheiriseon'))
}
if(typeof search_vatid != "string"){
return (Error ('Parameter search_vatid must be string'))
}
if(typeof aade_publicity_username != "string"){
return (Error ('Parameter aade_publicity_username must be string'))
}
if(typeof aade_publicity_password != "string"){
return (Error ('Parameter aade_publicity_password must be string'))
}
if(typeof searched_by_vatid != "string" && searched_by_vatid != null){
return (Error ('Parameter searched_by_vatid must be string'))
}
if(typeof debug != "boolean"){
return (Error ('Parameter debug must be boolean'))
}
if(debug){
console.log("[*][aade-publicity-search] Validation check for all params ...");
}
if(searched_by_vatid == null || searched_by_vatid == ""){
if(debug){
console.log("[*][aade-publicity-search] Structure XML Data without vatid called_by");
}
data_xml = '<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:ns2="http://rgwspublic2/RgWsPublic2Service" xmlns:ns3="http://rgwspublic2/RgWsPublic2"><env:Header><ns1:Security><ns1:UsernameToken><ns1:Username>'+ aade_publicity_username +'</ns1:Username><ns1:Password>'+ aade_publicity_password +'</ns1:Password></ns1:UsernameToken></ns1:Security></env:Header><env:Body><ns2:rgWsPublic2AfmMethod><ns2:INPUT_REC><ns3:afm_called_by/><ns3:afm_called_for>'+ search_vatid +'</ns3:afm_called_for></ns2:INPUT_REC></ns2:rgWsPublic2AfmMethod></env:Body></env:Envelope>';
}else{
if(debug){
console.log("[*][aade-publicity-search] Structure XML Data with vatid called_by");
}
data_xml = '<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:ns2="http://rgwspublic2/RgWsPublic2Service" xmlns:ns3="http://rgwspublic2/RgWsPublic2"><env:Header><ns1:Security><ns1:UsernameToken><ns1:Username>'+ aade_publicity_username +'</ns1:Username><ns1:Password>'+ aade_publicity_password +'</ns1:Password></ns1:UsernameToken></ns1:Security></env:Header><env:Body><ns2:rgWsPublic2AfmMethod><ns2:INPUT_REC><ns3:afm_called_by>'+ searched_by_vatid +'<ns3:afm_called_by/><ns3:afm_called_for>'+ search_vatid +'</ns3:afm_called_for></ns2:INPUT_REC></ns2:rgWsPublic2AfmMethod></env:Body></env:Envelope>';
}
if(debug){
console.log("[*][aade-publicity-search] Preparing AADE Request");
}
let config = {
method: 'post',
url: 'https://www1.gsis.gr/wsaade/RgWsPublic2/RgWsPublic2',
headers: {
'Content-Type': 'application/soap+xml; charset=utf8',
'User-Agent': '@georgetomzaridis/aade-publicity-search',
'Accept-Encoding': 'gzip',
'Connection': 'keep-alive',
'Accept': '*/*',
},
data : data_xml,
responseType: "arraybuffer",
decompress: true,
};
const {data, error} = await axios.request(config);
const decompress_data = await gunzip(data);
const final_dec_data = decompress_data.toString('utf8');
final_result = parseXml(final_dec_data, debug);
return final_result;
}catch (error){
return (Error ('Something went wrong: ' + error + ' (if you believe is this a bug, open a issue)'))
}
}
module.exports = getCompanyPublicityByAADE;