UNPKG

n8n-nodes-cigotracker

Version:

n8n node for CigoTracker API integration - manage deliveries, routes, and field service operations

74 lines (73 loc) 2.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.cigoTrackerApiRequest = cigoTrackerApiRequest; exports.formatDateForApi = formatDateForApi; exports.validateTimeFrame = validateTimeFrame; exports.parseCoordinates = parseCoordinates; const n8n_workflow_1 = require("n8n-workflow"); async function cigoTrackerApiRequest(method, endpoint, body = {}, qs = {}) { const credentials = await this.getCredentials('cigoTrackerApi'); const baseUrl = credentials.environment === 'production' ? 'https://app.cigotracker.com' : 'https://app-demo.cigotracker.com'; const options = { method, url: `${baseUrl}/api/v1${endpoint}`, headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', }, auth: { username: credentials.apiAccountId, password: credentials.apiAuthKey, }, body, qs, json: true, }; if (method === 'GET' || method === 'DELETE') { delete options.body; } if (options.body && Object.keys(options.body).length === 0) { delete options.body; } try { return await this.helpers.httpRequest(options); } catch (error) { console.error('CigoTracker API Error:', error); if (error.response) { console.error('Response data:', error.response.data); console.error('Response status:', error.response.status); } throw new n8n_workflow_1.NodeApiError(this.getNode(), error, { message: `CigoTracker API request failed: ${error.message || 'Unknown error'}`, }); } } function formatDateForApi(date) { const dateObj = new Date(date); return dateObj.toISOString().split('T')[0]; } function validateTimeFrame(timeFrame) { if (!timeFrame || typeof timeFrame !== 'object') { return false; } if (!timeFrame.start || !timeFrame.end) { return false; } const timeRegex = /^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$/; return timeRegex.test(timeFrame.start) && timeRegex.test(timeFrame.end); } function parseCoordinates(coordinates) { try { const cleaned = coordinates.replace(/[\[\]]/g, '').trim(); const parts = cleaned.split(',').map(p => parseFloat(p.trim())); if (parts.length === 2 && !isNaN(parts[0]) && !isNaN(parts[1])) { return [parts[0], parts[1]]; } } catch (error) { } return undefined; }