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.
This commit is contained in:
root 2026-01-26 14:48:37 -05:00
parent 8e0ca0a63b
commit bc01a6aafb

View file

@ -275,6 +275,13 @@ function mapProject(data: any): Record<string, any> {
* Map Resource entity
*/
function mapResource(data: any): Record<string, any> {
// 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<string, any> {
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,