-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
161 lines (131 loc) · 5 KB
/
Copy pathserver.js
File metadata and controls
161 lines (131 loc) · 5 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
const express = require('express');
const app = express();
// Questo permette all'API di capire i dati in formato JSON
app.use(express.json());
// Endpoint base per controllare se il server è vivo
app.get('/', (req, res) => {
res.json({ message: 'API Normalizzazione Dati Italiani - Attiva' });
});
// Il tuo primo vero endpoint API
app.post('/normalize/phone', (req, res) => {
const { phone } = req.body;
// Controllo se l'utente ha mandato il numero
if (!phone) {
return res.status(400).json({ error: 'Campo "phone" mancante nella richiesta' });
}
// 1. Rimuovo tutto ciò che non è un numero o il segno +
let normalized = phone.replace(/[^\d+]/g, '');
// 2. Se inizia con 00, lo trasformo in +
if (normalized.startsWith('00')) {
normalized = '+' + normalized.slice(2);
}
// 3. Se non ha il prefisso +39 ma ha una lunghezza da cellulare, lo aggiungo
if (!normalized.startsWith('+39') && normalized.length >= 9) {
normalized = '+39' + normalized.replace(/^\+/, '');
}
// Restituisco il JSON pulito
res.json({
original: phone,
normalized: normalized,
valid: normalized.length >= 12 && normalized.startsWith('+39')
});
});
// Endpoint per validare la Partita IVA italiana
app.post('/validate/vat', (req, res) => {
let { vat } = req.body;
if (!vat) {
return res.status(400).json({ error: 'Campo "vat" mancante nella richiesta' });
}
// Pulisco: rimuovo spazi e il prefisso "IT" se l'utente lo ha inserito
vat = vat.replace(/\s+/g, '').toUpperCase();
if (vat.startsWith('IT')) {
vat = vat.slice(2);
}
// 1. Deve essere lunga esattamente 11 caratteri e contenere solo numeri
if (vat.length !== 11 || !/^\d{11}$/.test(vat)) {
return res.json({ original: req.body.vat, valid: false, error: 'Lunghezza o formato non valido' });
}
// 2. Controllo l'algoritmo di validazione (Modulo 10)
let sum = 0;
for (let i = 0; i < 10; i++) {
let digit = parseInt(vat[i], 10);
// Le posizioni pari (indice dispari) vengono raddoppiate
if (i % 2 === 1) {
digit *= 2;
// Se il doppio supera 9, si sottrae 9 (es. 7*2=14 -> 14-9=5)
if (digit > 9) digit -= 9;
}
sum += digit;
}
// Calcolo il carattere di controllo atteso
const expectedCheckDigit = (10 - (sum % 10)) % 10;
const actualCheckDigit = parseInt(vat[10], 10);
// Se l'ultima cifra coincide con quella attesa, è valida
const isValid = expectedCheckDigit === actualCheckDigit;
res.json({
original: req.body.vat,
clean_vat: vat,
valid: isValid,
error: isValid ? null : 'Carattere di controllo errato (Partita IVA finta o scritta male)'
});
});
// Endpoint per normalizzare l'indirizzo italiano
app.post('/normalize/address', (req, res) => {
const { street, city, province, zip } = req.body;
// Un oggetto vuoto dove metteremo i dati puliti
let normalized = {};
let errors = [];
// 1. Normalizzazione della Via (Street)
if (street) {
let cleanStreet = street.trim();
// Metto la lettera maiuscola a ogni parola (es. "via roma" -> "Via Roma")
cleanStreet = cleanStreet.replace(/\b\w/g, char => char.toUpperCase());
// Correzione abbreviazioni comuni italiane
cleanStreet = cleanStreet.replace(/^V\.\s/i, 'Via ');
cleanStreet = cleanStreet.replace(/^P\.ZZA\s/i, 'Piazza ');
cleanStreet = cleanStreet.replace(/^C\.SO\s/i, 'Corso ');
cleanStreet = cleanStreet.replace(/^V\.LE\s/i, 'Viale ');
normalized.street = cleanStreet;
}
// 2. Normalizzazione della Città
if (city) {
let cleanCity = city.trim().toLowerCase();
// Maiuscola solo la prima lettera di ogni parola
cleanCity = cleanCity.replace(/\b\w/g, char => char.toUpperCase());
normalized.city = cleanCity;
}
// 3. Normalizzazione del CAP (deve avere esattamente 5 numeri)
if (zip) {
let cleanZip = zip.toString().trim();
if (/^\d{5}$/.test(cleanZip)) {
normalized.zip = cleanZip;
} else if (/^\d{4}$/.test(cleanZip)) {
// Se hanno scritto un CAP di 4 cifre, probabilmente manca lo zero iniziale (es. Roma o centri minori)
normalized.zip = '0' + cleanZip;
} else {
normalized.zip = null;
errors.push('Il CAP deve essere di 5 cifre');
}
}
// 4. Normalizzazione della Provincia (deve essere di 2 lettere e maiuscola)
if (province) {
let cleanProv = province.trim().toUpperCase();
if (/^[A-Z]{2}$/.test(cleanProv)) {
normalized.province = cleanProv;
} else {
normalized.province = null;
errors.push('La sigla della provincia deve essere di 2 lettere (es. RM)');
}
}
// Restituiamo il risultato
res.json({
original: req.body,
normalized: normalized,
valid: errors.length === 0,
errors: errors.length > 0 ? errors : null
});
});
// Avvio del server
app.listen(3000, () => {
console.log('Server attivo su http://localhost:3000');
});