service.js
8.42 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Import Axios HTTP Library
import axios from 'axios';
import createHistory from 'history/createBrowserHistory'
import {toast} from 'react-toastify';
const history = createHistory();
// esporto il base Url per poterlo utilizzare in altri files
export const baseURLName = "http://localhost:3001";
// Public class exported globally
class LemaApi {
constructor() {
//this.productOptionsUrl = "profilation/GetProductOptions";
//this.jobsOptionsUrl = "profilation/GetJobsOptions";
this.webClientConfigDataUrl = "profilation/GetWebClientConfigData";
this.activationFileDataUrl = "activation/SaveSessionFile";
this.createThumbnailsUrl = "activation/GetThumbnail";
this.activationDataUrl = "activation/SaveActivationData";
this.deleteFileDataUrl = "activation/DeleteSessionFile";
this.subscriptionSessionUrl = "profilation/GetSubscriptionSession";
this.profilationDataUrl = "profilation/SaveProfilationData";
this.getProfilationDataUrl = "profilation/GetProfilationData";
this.saveContactDataUrl = "contact/SaveContactData";
this.savePrivacyInfoUrl = "activation/SavePrivacyData";
this.checkIbanUrl = "activation/CheckIban";
this.getConsumptionEstimateUrl = "activation/ConsumptionsEstimate";
// Axios singleton instance
this._axios = axios.create({
withCredentials: true,
baseURL: baseURLName + '/api/',
timeout: 60000,
headers: {
'Content-Type': 'application/json' // i dati sono nel body della request in formato Json
}
});
// Interceptor
this._axios.interceptors.response.use(function (response) {
// Do something with response data
return response;
}, function (error) {
console.log(error.config);
if(typeof error.response == "undefined") {
toast.error("Il server remoto non è raggiungibile. Controllare il servizio LeMA.Choso!" +
"Server: " + baseURLName, {position: toast.POSITION.BOTTOM_CENTER});
// esco immediatamente
return Promise.reject("Server Error");
}
if (error.response.status === 500)
{
toast.error("Errore dal server remoto", {position: toast.POSITION.BOTTOM_CENTER});
}
return Promise.reject(error.response);
});
}
// Post dei dati di privacy
savePrivacyInfo(request) {
return this._axios.post(this.savePrivacyInfoUrl, request);
}
loadWebClientConfigData() {
return this._axios.get(this.webClientConfigDataUrl)
.then(response => {
// Salvo i dati nel local storage
localStorage.setItem("CONFIG_DATA", JSON.stringify(response.data));
// ritorno una promise risolta
return Promise.resolve('Data loaded successfully');
}
);
}
// Lista dei prodotti
static getProductOptions() {
console.log("Property ProductOptions called...");
let data = JSON.parse(localStorage.getItem("CONFIG_DATA")) || {};
return data.productOptions;
};
// Lista dei settori merceologici
static getBusinessDescriptions() {
console.log("Property BusinessDescriptions called...");
let data = JSON.parse(localStorage.getItem("CONFIG_DATA")) || {};
return data.businessDescriptions
};
// Lista dei tipi jobs
static getJobTypes(){
console.log("Property JobsTypes called...");
let data = JSON.parse(localStorage.getItem("CONFIG_DATA")) || {};
return data.jobTypes
}
// Lista dei tipi jobs
static getAtecoCodes(){
console.log("Property AtecoCodes called...");
let data = JSON.parse(localStorage.getItem("CONFIG_DATA")) || {};
return data.atecoOptions
}
getSubscriptionSession() {
return this._axios.get(this.subscriptionSessionUrl);
}
// Controlla che esista un iban, e nel caso restituisce info inerenti
checkIban(iban) {
// Debug
console.log("IBAN: " + iban);
return this._axios.get(this.checkIbanUrl + "/?iban=" + iban);
}
getConsumptionEstimate(ee_amount, gas_amount, period, expend) {
return this._axios.get(this.getConsumptionEstimateUrl + "/?ee_amount=" + ee_amount + "&gas_amount=" + gas_amount + "&period=" + period + "&expend=" + expend);
}
// Post dei dati di contatti
saveContactData(request) {
// Debug
console.log("SAVE CONTACTS DEBUG:");
console.log(request);
return this._axios.post(this.saveContactDataUrl, request)
.then(response => {
console.log("Contacts saved successfully");
})
.catch(err => {
// TODO: segnalare all'utente errori nel salvataggio
console.error(err);
}
);
}
// Post dei files di attivazione al server
saveActivationFiles(request, fileKind) {
// Debug
console.log("SAVE ACTIVATION FILES DEBUG:");
console.log(request);
return this._axios.post(this.activationFileDataUrl + "/" + fileKind, request)
.then(response => response.data)
.catch(err => {
// TODO: segnalare all'utente errori nel salvataggio
console.error(err);
}
);
}
// Creazione thumbnails a seguito della creazione di un file
createThumbnails(request) {
// Debug
console.log("THUMBNAILS CREATION DEBUG:");
console.log(request);
return this._axios.post(this.createThumbnailsUrl, request)
.then(response => response.data)
.catch(err => {
// TODO: segnalare all'utente errori nel salvataggio
console.error(err);
}
);
}
// Post dei dati di attivazione al server
saveActivationData(request) {
// Debug
console.log("SAVE ACTIVATION DATA DEBUG:");
console.log(request);
return this._axios.post(this.activationDataUrl, request)
.then(response => {
console.log("Activation data saved successfully");
})
.catch(err => {
// TODO: segnalare all'utente errori nel salvataggio
console.error(err);
}
);
}
// Post dei files di attivazione al server
deleteActivationFiles(idFile) {
// Debug
console.log("DELETE ACTIVATION FILES DEBUG:");
console.log(idFile);
return this._axios.delete(this.deleteFileDataUrl + "/" + idFile)
.then(response => {
console.log("Activation data deleted successfully");
})
.catch(err => {
// TODO: segnalare all'utente errori nel salvataggio
console.error(err);
}
);
}
saveProfilationData(request) {
// Debug
console.log("SAVE PROFILATION DATA DEBUG:");
console.log(request);
return this._axios.post(this.profilationDataUrl, request);
}
getProfilationData() {
// Debug
console.log("GET PROFILATION DATA DEBUG:");
return this._axios.get(this.getProfilationDataUrl)
.then(response => {
console.log("Profilation data retrieved successfully");
// ritorno una promise risolta contenente i dati di profilazione
return Promise.resolve(response.data);
})
.catch(err => {
console.error(err);
}
);
}
}
// Export singleton Service Class
export default LemaApi;