diff --git a/lib/services/entity-sync.ts b/lib/services/entity-sync.ts index e4b9bbb..b791572 100644 --- a/lib/services/entity-sync.ts +++ b/lib/services/entity-sync.ts @@ -274,16 +274,17 @@ export class EntitySyncService { } } - // Validate project foreign keys for tasks + // Validate project and resource foreign keys for tasks if (entity === EntityType.TASKS) { const initialCount = mappedRecords.length; - // Get all valid project IDs from database + // Get all valid project IDs and resource IDs from database const validProjectIds = await this.getValidProjectIds(); + const validResourceIds = await this.getValidResourceIds(); - // Filter tasks with invalid project references + // Filter tasks with invalid foreign key references mappedRecords = mappedRecords.map(task => { - // Set invalid project IDs to null instead of filtering out the entire task + // Set invalid project IDs to null if (task.project_id && !validProjectIds.has(task.project_id)) { entityLogger.debug(`Invalid project_id, setting to null`, { taskId: task.id, @@ -291,12 +292,35 @@ export class EntitySyncService { }); task.project_id = null; } + + // Set invalid resource IDs to null + if (task.assigned_resource_id && !validResourceIds.has(task.assigned_resource_id)) { + entityLogger.debug(`Invalid assigned_resource_id, setting to null`, { + taskId: task.id, + invalidResourceId: task.assigned_resource_id, + }); + task.assigned_resource_id = null; + } + if (task.creator_resource_id && !validResourceIds.has(task.creator_resource_id)) { + task.creator_resource_id = null; + } + if (task.completed_by_resource_id && !validResourceIds.has(task.completed_by_resource_id)) { + task.completed_by_resource_id = null; + } + if (task.last_activity_resource_id && !validResourceIds.has(task.last_activity_resource_id)) { + task.last_activity_resource_id = null; + } + return task; }); - const nullifiedCount = initialCount - mappedRecords.filter(t => t.project_id).length; - if (nullifiedCount > 0) { - entityLogger.warn('Nullified invalid project references', { nullifiedCount }); + const nullifiedProjectCount = initialCount - mappedRecords.filter(t => t.project_id).length; + const nullifiedResourceCount = initialCount - mappedRecords.filter(t => t.assigned_resource_id).length; + if (nullifiedProjectCount > 0 || nullifiedResourceCount > 0) { + entityLogger.warn('Nullified invalid foreign key references', { + nullifiedProjectCount, + nullifiedResourceCount + }); } }