diff --git a/lib/services/entity-sync.ts b/lib/services/entity-sync.ts index b791572..ff6d274 100644 --- a/lib/services/entity-sync.ts +++ b/lib/services/entity-sync.ts @@ -727,13 +727,21 @@ export class EntitySyncService { picklistLogger.info('Found picklist values', { recordCount: records.length }); - // Upsert to database + // Get existing values to calculate added vs updated const tableName = getTableName(EntityType.STATUSES); - const upsertedCount = await postgresClient.bulkUpsert(tableName, records, ['value']); + const existingQuery = `SELECT value FROM ${tableName}`; + const existingResult = await postgresClient.query<{ value: number }>(existingQuery); + const existingValues = new Set(existingResult.rows.map((r: any) => r.value)); + + const recordsAdded = records.filter((r: any) => !existingValues.has(r.value)).length; + const recordsUpdated = records.filter((r: any) => existingValues.has(r.value)).length; + + // Upsert to database + await postgresClient.bulkUpsert(tableName, records, ['value']); const stats: EntitySyncStats = { - recordsAdded: upsertedCount, - recordsUpdated: 0, + recordsAdded, + recordsUpdated, recordsDeleted: 0, }; @@ -772,13 +780,21 @@ export class EntitySyncService { picklistLogger.info('Found picklist values', { recordCount: records.length }); - // Upsert to database + // Get existing values to calculate added vs updated const tableName = getTableName(EntityType.ISSUE_TYPES); - const upsertedCount = await postgresClient.bulkUpsert(tableName, records, ['value']); + const existingQuery = `SELECT value FROM ${tableName}`; + const existingResult = await postgresClient.query<{ value: number }>(existingQuery); + const existingValues = new Set(existingResult.rows.map((r: any) => r.value)); + + const recordsAdded = records.filter((r: any) => !existingValues.has(r.value)).length; + const recordsUpdated = records.filter((r: any) => existingValues.has(r.value)).length; + + // Upsert to database + await postgresClient.bulkUpsert(tableName, records, ['value']); const stats: EntitySyncStats = { - recordsAdded: upsertedCount, - recordsUpdated: 0, + recordsAdded, + recordsUpdated, recordsDeleted: 0, }; @@ -834,13 +850,21 @@ export class EntitySyncService { picklistLogger.info('Found picklist values', { recordCount: records.length }); - // Upsert to database + // Get existing values to calculate added vs updated const tableName = getTableName(EntityType.SUB_ISSUE_TYPES); - const upsertedCount = await postgresClient.bulkUpsert(tableName, records, ['value']); + const existingQuery = `SELECT value FROM ${tableName}`; + const existingResult = await postgresClient.query<{ value: number }>(existingQuery); + const existingValues = new Set(existingResult.rows.map(r => r.value)); + + const recordsAdded = records.filter((r: any) => !existingValues.has(r.value)).length; + const recordsUpdated = records.filter((r: any) => existingValues.has(r.value)).length; + + // Upsert to database + await postgresClient.bulkUpsert(tableName, records, ['value']); const stats: EntitySyncStats = { - recordsAdded: upsertedCount, - recordsUpdated: 0, + recordsAdded, + recordsUpdated, recordsDeleted: 0, }; @@ -879,13 +903,21 @@ export class EntitySyncService { picklistLogger.info('Found picklist values', { recordCount: records.length }); - // Upsert to database + // Get existing values to calculate added vs updated const tableName = getTableName(EntityType.WORK_TYPES); - const upsertedCount = await postgresClient.bulkUpsert(tableName, records, ['value']); + const existingQuery = `SELECT value FROM ${tableName}`; + const existingResult = await postgresClient.query<{ value: number }>(existingQuery); + const existingValues = new Set(existingResult.rows.map((r: any) => r.value)); + + const recordsAdded = records.filter((r: any) => !existingValues.has(r.value)).length; + const recordsUpdated = records.filter((r: any) => existingValues.has(r.value)).length; + + // Upsert to database + await postgresClient.bulkUpsert(tableName, records, ['value']); const stats: EntitySyncStats = { - recordsAdded: upsertedCount, - recordsUpdated: 0, + recordsAdded, + recordsUpdated, recordsDeleted: 0, };