From 70a5daa3d082f0751d7abdd7b2db374a3c7f31cb Mon Sep 17 00:00:00 2001 From: Khaliq Date: Mon, 6 Jan 2025 11:09:52 +0200 Subject: [PATCH] fix(sync-status): [nan-2207] add connection_id to response (#3224) Currently, we can request sync statuses from all connection IDs, but we don't know to which connection they belong to, making them unusable. This adds the `connection_id` to the response: ``` { "syncs": [ { "id": "4d8069c7-71d7-4147-b36a-01b7c0327718", "connection_id": "u-1", "type": "INITIAL", "nextScheduledSyncAt": null, "name": "fake-data", "status": "PAUSED", "frequency": "every 5m", "recordCount": { "Test2": 0 } } ] } ``` Updates docs to reflect the update. NAN-2207 --- docs-v2/reference/sdks/node.mdx | 1 + docs-v2/spec.yaml | 3 +++ packages/shared/lib/models/Sync.ts | 3 +++ .../lib/services/sync/manager.service.ts | 21 +++++++++++++++---- .../shared/lib/services/sync/sync.service.ts | 12 ++++++----- 5 files changed, 31 insertions(+), 9 deletions(-) diff --git a/docs-v2/reference/sdks/node.mdx b/docs-v2/reference/sdks/node.mdx index ff0cf12c1c7..9532f43fdb2 100644 --- a/docs-v2/reference/sdks/node.mdx +++ b/docs-v2/reference/sdks/node.mdx @@ -1002,6 +1002,7 @@ await nango.syncStatus('', ['SYNC_NAME1', 'SYNC_NAME2'], '", + "connection_id": "", "name": "", "status": "RUNNING", "type": "INCREMENTAL", diff --git a/docs-v2/spec.yaml b/docs-v2/spec.yaml index f7dd6590520..a4c9ac21d5a 100644 --- a/docs-v2/spec.yaml +++ b/docs-v2/spec.yaml @@ -1265,6 +1265,9 @@ paths: id: type: string description: The unique identifier for the sync. + connection_id: + type: string + description: The ID of the connection name: type: string description: The name of the sync. diff --git a/packages/shared/lib/models/Sync.ts b/packages/shared/lib/models/Sync.ts index 79b6b114494..ac61a31f6de 100644 --- a/packages/shared/lib/models/Sync.ts +++ b/packages/shared/lib/models/Sync.ts @@ -26,6 +26,8 @@ export interface SyncResult { export type SyncResultByModel = Record; +export type SyncWithConnectionId = Sync & { connection_id: string }; + export interface Sync extends TimestampsAndDeleted { id: string; nango_connection_id: number; @@ -60,6 +62,7 @@ export interface ReportedSyncJobStatus { id?: string; type: SyncType | 'INITIAL'; name?: string; + connection_id?: string; status: SyncStatus; latestResult?: SyncResultByModel | undefined; jobStatus?: SyncStatus; diff --git a/packages/shared/lib/services/sync/manager.service.ts b/packages/shared/lib/services/sync/manager.service.ts index c1f07c6d4ed..a05a49995c0 100644 --- a/packages/shared/lib/services/sync/manager.service.ts +++ b/packages/shared/lib/services/sync/manager.service.ts @@ -13,7 +13,7 @@ import { import { errorNotificationService } from '../notification/error.service.js'; import configService from '../config.service.js'; import type { Connection, NangoConnection } from '../../models/Connection.js'; -import type { Sync, ReportedSyncJobStatus, SyncCommand } from '../../models/Sync.js'; +import type { SyncWithConnectionId, ReportedSyncJobStatus, SyncCommand } from '../../models/Sync.js'; import { SyncType, SyncStatus } from '../../models/Sync.js'; import { NangoError } from '../../utils/error.js'; import type { Config as ProviderConfig } from '../../models/Provider.js'; @@ -334,12 +334,24 @@ export class SyncManagerService { continue; } - const reportedStatus = await this.syncStatus({ sync, environmentId, providerConfigKey, includeJobStatus, orchestrator, recordsService }); + const syncWithConnectionId: SyncWithConnectionId = { + ...sync, + connection_id: connection.connection_id + }; + + const reportedStatus = await this.syncStatus({ + sync: syncWithConnectionId, + environmentId, + providerConfigKey, + includeJobStatus, + orchestrator, + recordsService + }); syncsWithStatus.push(reportedStatus); } } else { - const syncs = + const syncs: SyncWithConnectionId[] = syncNames.length > 0 ? await getSyncsByProviderConfigAndSyncNames(environmentId, providerConfigKey, syncNames) : await getSyncsByProviderConfigKey(environmentId, providerConfigKey); @@ -421,7 +433,7 @@ export class SyncManagerService { orchestrator, recordsService }: { - sync: Sync; + sync: SyncWithConnectionId; environmentId: number; providerConfigKey: string; includeJobStatus: boolean; @@ -458,6 +470,7 @@ export class SyncManagerService { return { id: sync.id, + connection_id: sync.connection_id, type: latestJob?.type === SyncType.INCREMENTAL ? latestJob.type : 'INITIAL', finishedAt: latestJob?.updated_at, nextScheduledSyncAt: schedule.nextDueDate, diff --git a/packages/shared/lib/services/sync/sync.service.ts b/packages/shared/lib/services/sync/sync.service.ts index e8434f80b1e..64b7c02cd90 100644 --- a/packages/shared/lib/services/sync/sync.service.ts +++ b/packages/shared/lib/services/sync/sync.service.ts @@ -1,6 +1,6 @@ import { v4 as uuidv4 } from 'uuid'; import db, { schema, dbNamespace } from '@nangohq/database'; -import type { Sync, SyncConfig, Job as SyncJob } from '../../models/Sync.js'; +import type { Sync, SyncWithConnectionId, SyncConfig, Job as SyncJob } from '../../models/Sync.js'; import { SyncStatus } from '../../models/Sync.js'; import type { Connection, NangoConnection } from '../../models/Connection.js'; import type { ActiveLog, IncomingFlowConfig, SlimAction, SlimSync, SyncAndActionDifferences, SyncTypeLiteral } from '@nangohq/types'; @@ -241,8 +241,6 @@ export const getSyncsByConnectionId = async (nangoConnectionId: number): Promise return null; }; -type SyncWithConnectionId = Sync & { connection_id: string }; - export const getSyncsByProviderConfigKey = async (environment_id: number, providerConfigKey: string): Promise => { const results = await db.knex .select(`${TABLE}.*`, `${TABLE}.name`, `_nango_connections.connection_id`, `${TABLE}.created_at`, `${TABLE}.updated_at`, `${TABLE}.last_sync_date`) @@ -284,9 +282,13 @@ export const getSyncNamesByConnectionId = async (nangoConnectionId: number): Pro return []; }; -export const getSyncsByProviderConfigAndSyncNames = async (environment_id: number, providerConfigKey: string, syncNames: string[]): Promise => { +export const getSyncsByProviderConfigAndSyncNames = async ( + environment_id: number, + providerConfigKey: string, + syncNames: string[] +): Promise => { const results = await db.knex - .select(`${TABLE}.*`) + .select(`${TABLE}.*`, '_nango_connections.connection_id') .from(TABLE) .join('_nango_connections', '_nango_connections.id', `${TABLE}.nango_connection_id`) .where({