cambios de desarrollo

This commit is contained in:
abiandev
2026-06-01 16:11:02 +02:00
parent 48b349d68b
commit 6b7c7ec462
10 changed files with 1282 additions and 23 deletions
+113
View File
@@ -0,0 +1,113 @@
const DEFAULT_AGHEERA_PUSH_URL = 'https://push-test.agheera.com/Telematics/Positions';
let httpClientOverride = null;
const getPushUrl = () =>
String(process.env.AGHEERA_PUSH_URL || DEFAULT_AGHEERA_PUSH_URL).trim();
const getApiKey = () =>
String(process.env.AGHEERA_API_KEY || '').trim();
const formatMeasurementTime = (dateValue) => {
const date = dateValue instanceof Date ? dateValue : new Date(dateValue);
return date.toISOString().replace(/\.\d{3}Z$/, 'Z');
};
const normalizeNumber = (rawValue) => {
const numericValue = Number(rawValue);
return Number.isFinite(numericValue) ? numericValue : null;
};
const getHttpClient = () => {
if (typeof httpClientOverride === 'function') {
return httpClientOverride;
}
if (typeof fetch === 'function') {
return fetch;
}
return null;
};
const buildPositionPayload = ({
latitude,
longitude,
vehicleId,
licensePlate,
measurementTime
}) => ({
Vehicles: [
{
latitude: normalizeNumber(latitude),
longitude: normalizeNumber(longitude),
vehicleId: String(vehicleId || '').trim(),
licensePlate: String(licensePlate || '').trim(),
measurementTime: formatMeasurementTime(measurementTime)
}
]
});
const pushPosition = async ({
latitude,
longitude,
vehicleId,
licensePlate,
measurementTime
}) => {
const httpClient = getHttpClient();
if (!httpClient) {
throw new Error('Agheera HTTP client unavailable');
}
const apiKey = getApiKey();
if (!apiKey) {
throw new Error('Agheera API key missing');
}
const url = getPushUrl();
const payload = buildPositionPayload({
latitude,
longitude,
vehicleId,
licensePlate,
measurementTime
});
const response = await httpClient(url, {
method: 'POST',
headers: {
apiKey,
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
});
const responseBody = typeof response?.text === 'function' ? await response.text() : '';
if (!response?.ok) {
const error = new Error('Agheera push failed');
error.status = response?.status || null;
error.body = responseBody;
throw error;
}
return {
status: response.status,
body: responseBody
};
};
const __setHttpClientForTests = (httpClient) => {
httpClientOverride = httpClient;
};
const __resetHttpClientForTests = () => {
httpClientOverride = null;
};
module.exports = {
DEFAULT_AGHEERA_PUSH_URL,
pushPosition,
__setHttpClientForTests,
__resetHttpClientForTests
};