@wrcb/cb-common 1.0.630 → 1.0.631
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/helpers/geocodeAddress.d.ts +27 -0
- package/build/helpers/geocodeAddress.js +108 -0
- package/build/server.d.ts +1 -0
- package/build/server.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
interface AddressData {
|
|
2
|
+
cep: string;
|
|
3
|
+
street: string;
|
|
4
|
+
number: string;
|
|
5
|
+
city: string;
|
|
6
|
+
state: string;
|
|
7
|
+
}
|
|
8
|
+
interface Coordinates {
|
|
9
|
+
lat: number;
|
|
10
|
+
lng: number;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Geocodifica um endereço brasileiro usando ViaCEP + Nominatim
|
|
14
|
+
* @param address - Dados do endereço
|
|
15
|
+
* @returns Coordenadas geográficas (lat/lng)
|
|
16
|
+
*/
|
|
17
|
+
export declare function geocodeAddress(address: AddressData): Promise<Coordinates>;
|
|
18
|
+
/**
|
|
19
|
+
* Calcula distância entre duas coordenadas usando fórmula de Haversine
|
|
20
|
+
* @param lat1 - Latitude ponto 1
|
|
21
|
+
* @param lng1 - Longitude ponto 1
|
|
22
|
+
* @param lat2 - Latitude ponto 2
|
|
23
|
+
* @param lng2 - Longitude ponto 2
|
|
24
|
+
* @returns Distância em quilômetros
|
|
25
|
+
*/
|
|
26
|
+
export declare function calculateDistance(lat1: number, lng1: number, lat2: number, lng2: number): number;
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.geocodeAddress = geocodeAddress;
|
|
13
|
+
exports.calculateDistance = calculateDistance;
|
|
14
|
+
// services/geocodeAddress.ts
|
|
15
|
+
const server_1 = require("@wrcb/cb-common/server");
|
|
16
|
+
/**
|
|
17
|
+
* Geocodifica um endereço brasileiro usando ViaCEP + Nominatim
|
|
18
|
+
* @param address - Dados do endereço
|
|
19
|
+
* @returns Coordenadas geográficas (lat/lng)
|
|
20
|
+
*/
|
|
21
|
+
function geocodeAddress(address) {
|
|
22
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
23
|
+
try {
|
|
24
|
+
// 1. Validar e limpar CEP
|
|
25
|
+
const cleanCep = address.cep.replace(/\D/g, '');
|
|
26
|
+
if (cleanCep.length !== 8) {
|
|
27
|
+
throw new server_1.BadRequestError('InvalidCEP');
|
|
28
|
+
}
|
|
29
|
+
// 2. Buscar endereço completo no ViaCEP
|
|
30
|
+
const viaCepResponse = yield fetch(`https://viacep.com.br/ws/${cleanCep}/json/`);
|
|
31
|
+
if (!viaCepResponse.ok) {
|
|
32
|
+
throw new server_1.BadRequestError('ViaCEPUnavailable');
|
|
33
|
+
}
|
|
34
|
+
const viaCepData = yield viaCepResponse.json();
|
|
35
|
+
if (viaCepData.erro) {
|
|
36
|
+
throw new server_1.BadRequestError('CEPNotFound');
|
|
37
|
+
}
|
|
38
|
+
// 3. Montar endereço completo para geocodificação
|
|
39
|
+
const fullAddress = `${address.street}, ${address.number}, ${viaCepData.bairro}, ${address.city}, ${address.state}, Brasil`;
|
|
40
|
+
// 4. Geocodificar com Nominatim (OpenStreetMap)
|
|
41
|
+
const nominatimResponse = yield fetch(`https://nominatim.openstreetmap.org/search?` +
|
|
42
|
+
`q=${encodeURIComponent(fullAddress)}` +
|
|
43
|
+
`&format=json` +
|
|
44
|
+
`&limit=1` +
|
|
45
|
+
`&countrycodes=br`, {
|
|
46
|
+
headers: {
|
|
47
|
+
'User-Agent': 'Compranomia-App/1.0' // Nominatim requer User-Agent
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
if (!nominatimResponse.ok) {
|
|
51
|
+
throw new server_1.BadRequestError('NominatimUnavailable');
|
|
52
|
+
}
|
|
53
|
+
const nominatimData = yield nominatimResponse.json();
|
|
54
|
+
if (!nominatimData || nominatimData.length === 0) {
|
|
55
|
+
// Fallback: tentar só com CEP
|
|
56
|
+
const fallbackResponse = yield fetch(`https://nominatim.openstreetmap.org/search?` +
|
|
57
|
+
`postalcode=${cleanCep}` +
|
|
58
|
+
`&country=brazil` +
|
|
59
|
+
`&format=json` +
|
|
60
|
+
`&limit=1`, {
|
|
61
|
+
headers: {
|
|
62
|
+
'User-Agent': 'Compranomia-App/1.0'
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
const fallbackData = yield fallbackResponse.json();
|
|
66
|
+
if (!fallbackData || fallbackData.length === 0) {
|
|
67
|
+
throw new server_1.BadRequestError('CouldNotGeocodeAddress');
|
|
68
|
+
}
|
|
69
|
+
return {
|
|
70
|
+
lat: parseFloat(fallbackData[0].lat),
|
|
71
|
+
lng: parseFloat(fallbackData[0].lon)
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
lat: parseFloat(nominatimData[0].lat),
|
|
76
|
+
lng: parseFloat(nominatimData[0].lon)
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
console.error('Geocoding error:', error.message);
|
|
81
|
+
throw new server_1.BadRequestError(error.message || 'GeocodingFailed');
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Calcula distância entre duas coordenadas usando fórmula de Haversine
|
|
87
|
+
* @param lat1 - Latitude ponto 1
|
|
88
|
+
* @param lng1 - Longitude ponto 1
|
|
89
|
+
* @param lat2 - Latitude ponto 2
|
|
90
|
+
* @param lng2 - Longitude ponto 2
|
|
91
|
+
* @returns Distância em quilômetros
|
|
92
|
+
*/
|
|
93
|
+
function calculateDistance(lat1, lng1, lat2, lng2) {
|
|
94
|
+
const R = 6371; // Raio da Terra em km
|
|
95
|
+
const dLat = toRad(lat2 - lat1);
|
|
96
|
+
const dLng = toRad(lng2 - lng1);
|
|
97
|
+
const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
|
|
98
|
+
Math.cos(toRad(lat1)) *
|
|
99
|
+
Math.cos(toRad(lat2)) *
|
|
100
|
+
Math.sin(dLng / 2) *
|
|
101
|
+
Math.sin(dLng / 2);
|
|
102
|
+
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
|
103
|
+
const distance = R * c;
|
|
104
|
+
return Math.round(distance * 10) / 10; // Arredondar para 1 casa decimal
|
|
105
|
+
}
|
|
106
|
+
function toRad(degrees) {
|
|
107
|
+
return (degrees * Math.PI) / 180;
|
|
108
|
+
}
|
package/build/server.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export * from './helpers/authHelper';
|
|
|
3
3
|
export * from './helpers/isValidMongoId';
|
|
4
4
|
export * from './helpers/isValidTenant';
|
|
5
5
|
export * from './helpers/calculateBonus';
|
|
6
|
+
export * from './helpers/geocodeAddress';
|
|
6
7
|
export * from './errors/badRequestError';
|
|
7
8
|
export * from './errors/customError';
|
|
8
9
|
export * from './errors/databaseConnectionError';
|
package/build/server.js
CHANGED
|
@@ -19,6 +19,7 @@ __exportStar(require("./helpers/authHelper"), exports);
|
|
|
19
19
|
__exportStar(require("./helpers/isValidMongoId"), exports);
|
|
20
20
|
__exportStar(require("./helpers/isValidTenant"), exports);
|
|
21
21
|
__exportStar(require("./helpers/calculateBonus"), exports);
|
|
22
|
+
__exportStar(require("./helpers/geocodeAddress"), exports);
|
|
22
23
|
__exportStar(require("./errors/badRequestError"), exports);
|
|
23
24
|
__exportStar(require("./errors/customError"), exports);
|
|
24
25
|
__exportStar(require("./errors/databaseConnectionError"), exports);
|