From bc01a6aafb16fee508d3a80777097edfc5cc17f1 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 26 Jan 2026 14:48:37 -0500 Subject: [PATCH] fix: handle non-numeric values in Resources integer fields Autotask API returns string values like 'PRIMARY' for some integer fields (e.g., emailTypeCode). Added safeInt() helper function to: - Parse numeric values correctly - Return null for non-numeric strings - Handle null/undefined/empty values This prevents database errors when syncing Resources with non-standard field values from Autotask API. --- lib/utils/entity-mapper.ts | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/utils/entity-mapper.ts b/lib/utils/entity-mapper.ts index a844084..0abbde7 100644 --- a/lib/utils/entity-mapper.ts +++ b/lib/utils/entity-mapper.ts @@ -275,6 +275,13 @@ function mapProject(data: any): Record { * Map Resource entity */ function mapResource(data: any): Record { + // Helper to safely parse integer values (returns null for non-numeric strings) + const safeInt = (value: any): number | null => { + if (value === null || value === undefined || value === '') return null; + const num = typeof value === 'number' ? value : parseInt(value, 10); + return isNaN(num) ? null : num; + }; + return { id: data.id, // Name fields @@ -300,29 +307,29 @@ function mapResource(data: any): Record { is_active: data.isActive !== undefined ? data.isActive : true, // Employment details - resource_type: data.resourceType, + resource_type: safeInt(data.resourceType), payroll_identifier: data.payrollIdentifier, pay_roll_identifier: data.payrollIdentifier, // Keep for backward compatibility - payroll_type: data.payrollType, + payroll_type: safeInt(data.payrollType), accounting_reference_id: data.accountingReferenceID, internal_cost: data.internalCost, hire_date: data.hireDate, // Location and availability - location_id: data.locationID, + location_id: safeInt(data.locationID), travel_availability_pct: data.travelAvailabilityPct, - default_service_desk_role_id: data.defaultServiceDeskRoleID, + default_service_desk_role_id: safeInt(data.defaultServiceDeskRoleID), // System preferences - email_type_code: data.emailTypeCode, + email_type_code: safeInt(data.emailTypeCode), number_format: data.numberFormat, time_format: data.timeFormat, date_format: data.dateFormat, // Demographics and security - gender: data.gender, - license_type: data.licenseType, - security_level: data.securityLevel, + gender: safeInt(data.gender), + license_type: safeInt(data.licenseType), + security_level: safeInt(data.securityLevel), // Ratings survey_resource_rating: data.surveyResourceRating,