@open-mercato/core 0.7.1-develop.7153.1.7145d295e6 → 0.7.1-develop.7154.1.981330c924

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -86,8 +86,31 @@ function createSyncScheduleService(em, schedulerService) {
86
86
  expected: input.expectedUpdatedAt
87
87
  });
88
88
  }
89
+ const id = existing?.id ?? randomUUID();
90
+ const scheduledJobId = existing?.scheduledJobId ?? id;
91
+ await requireScheduler().register({
92
+ id: scheduledJobId,
93
+ name: buildScheduleName(input),
94
+ description: buildScheduleDescription(input),
95
+ scopeType: "organization",
96
+ organizationId: scope.organizationId,
97
+ tenantId: scope.tenantId,
98
+ scheduleType: input.scheduleType,
99
+ scheduleValue: input.scheduleValue,
100
+ timezone: input.timezone,
101
+ targetType: "queue",
102
+ targetQueue: "data-sync-scheduled",
103
+ targetPayload: {
104
+ scheduleId: id,
105
+ scope
106
+ },
107
+ requireFeature: "data_sync.run",
108
+ sourceType: "module",
109
+ sourceModule: "data_sync",
110
+ isEnabled: input.isEnabled
111
+ });
89
112
  const row = existing ?? em.create(SyncSchedule, {
90
- id: randomUUID(),
113
+ id,
91
114
  integrationId: input.integrationId,
92
115
  entityType: input.entityType,
93
116
  direction: input.direction,
@@ -107,32 +130,11 @@ function createSyncScheduleService(em, schedulerService) {
107
130
  row.timezone = input.timezone;
108
131
  row.fullSync = input.fullSync;
109
132
  row.isEnabled = input.isEnabled;
110
- row.scheduledJobId = row.scheduledJobId ?? row.id;
133
+ row.scheduledJobId = scheduledJobId;
111
134
  if (!existing) {
112
135
  em.persist(row);
113
136
  }
114
137
  await em.flush();
115
- await requireScheduler().register({
116
- id: row.scheduledJobId,
117
- name: buildScheduleName(row),
118
- description: buildScheduleDescription(row),
119
- scopeType: "organization",
120
- organizationId: scope.organizationId,
121
- tenantId: scope.tenantId,
122
- scheduleType: row.scheduleType,
123
- scheduleValue: row.scheduleValue,
124
- timezone: row.timezone,
125
- targetType: "queue",
126
- targetQueue: "data-sync-scheduled",
127
- targetPayload: {
128
- scheduleId: row.id,
129
- scope
130
- },
131
- requireFeature: "data_sync.run",
132
- sourceType: "module",
133
- sourceModule: "data_sync",
134
- isEnabled: row.isEnabled
135
- });
136
138
  return row;
137
139
  },
138
140
  async deleteSchedule(id, scope, container, expectedUpdatedAt) {
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../src/modules/data_sync/lib/sync-schedule-service.ts"],
4
- "sourcesContent": ["import { randomUUID } from 'node:crypto'\nimport type { AwilixContainer } from 'awilix'\nimport type { EntityManager, FilterQuery } from '@mikro-orm/postgresql'\nimport { findAndCountWithDecryption, findOneWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport { enforceCommandOptimisticLockWithGuards, enforceRecordGoneIsConflict } from '@open-mercato/shared/lib/crud/optimistic-lock-command'\nimport { SyncSchedule } from '../data/entities'\n\ntype SyncScope = {\n organizationId: string\n tenantId: string\n}\n\ntype SchedulerServiceLike = {\n register: (registration: {\n id: string\n name: string\n description?: string\n scopeType: 'organization'\n organizationId: string\n tenantId: string\n scheduleType: 'cron' | 'interval'\n scheduleValue: string\n timezone?: string\n targetType: 'queue'\n targetQueue: string\n targetPayload: Record<string, unknown>\n requireFeature?: string\n sourceType: 'module'\n sourceModule: string\n isEnabled?: boolean\n }) => Promise<void>\n unregister: (scheduleId: string) => Promise<void>\n}\n\nexport function createSyncScheduleService(em: EntityManager, schedulerService?: SchedulerServiceLike) {\n function requireScheduler(): SchedulerServiceLike {\n if (!schedulerService) {\n throw new Error('Scheduler module is not available')\n }\n return schedulerService\n }\n\n function buildScheduleName(row: SyncSchedule): string {\n return `Data sync: ${row.integrationId} ${row.entityType} ${row.direction}`\n }\n\n function buildScheduleDescription(row: SyncSchedule): string {\n return `Scheduled ${row.direction} for ${row.integrationId} (${row.entityType})`\n }\n\n async function getById(id: string, scope: SyncScope): Promise<SyncSchedule | null> {\n return findOneWithDecryption(\n em,\n SyncSchedule,\n {\n id,\n organizationId: scope.organizationId,\n tenantId: scope.tenantId,\n deletedAt: null,\n },\n undefined,\n scope,\n )\n }\n\n async function getByKey(\n integrationId: string,\n entityType: string,\n direction: 'import' | 'export',\n scope: SyncScope,\n ): Promise<SyncSchedule | null> {\n return findOneWithDecryption(\n em,\n SyncSchedule,\n {\n integrationId,\n entityType,\n direction,\n organizationId: scope.organizationId,\n tenantId: scope.tenantId,\n deletedAt: null,\n },\n undefined,\n scope,\n )\n }\n\n return {\n getById,\n getByKey,\n\n async listSchedules(query: {\n integrationId?: string\n entityType?: string\n direction?: 'import' | 'export'\n page: number\n pageSize: number\n }, scope: SyncScope): Promise<{ items: SyncSchedule[]; total: number }> {\n const where: FilterQuery<SyncSchedule> = {\n organizationId: scope.organizationId,\n tenantId: scope.tenantId,\n deletedAt: null,\n }\n\n if (query.integrationId) where.integrationId = query.integrationId\n if (query.entityType) where.entityType = query.entityType\n if (query.direction) where.direction = query.direction\n\n const [items, total] = await findAndCountWithDecryption(\n em,\n SyncSchedule,\n where,\n {\n orderBy: { createdAt: 'DESC' },\n limit: query.pageSize,\n offset: (query.page - 1) * query.pageSize,\n },\n scope,\n )\n\n return { items, total }\n },\n\n async saveSchedule(input: {\n id?: string\n integrationId: string\n entityType: string\n direction: 'import' | 'export'\n scheduleType: 'cron' | 'interval'\n scheduleValue: string\n timezone: string\n fullSync: boolean\n isEnabled: boolean\n expectedUpdatedAt?: string | null\n }, scope: SyncScope, container?: AwilixContainer): Promise<SyncSchedule> {\n const existing = input.id\n ? await getById(input.id, scope)\n : await getByKey(input.integrationId, input.entityType, input.direction, scope)\n\n if (existing && container) {\n await enforceCommandOptimisticLockWithGuards(container, {\n resourceKind: 'data_sync.schedule',\n resourceId: existing.id,\n current: existing.updatedAt ?? null,\n expected: input.expectedUpdatedAt ?? null,\n })\n } else if (!existing && input.expectedUpdatedAt) {\n // Concurrent delete-then-edit race: the client edited a schedule that was\n // removed before this keyed upsert ran. Surface the unified conflict\n // instead of silently re-creating it. No-op when no expected version was\n // sent (a genuine create) or when OM_OPTIMISTIC_LOCK is off.\n enforceRecordGoneIsConflict({\n resourceKind: 'data_sync.schedule',\n resourceId: input.id ?? `${input.integrationId}:${input.entityType}:${input.direction}`,\n expected: input.expectedUpdatedAt,\n })\n }\n\n const row = existing ?? em.create(SyncSchedule, {\n id: randomUUID(),\n integrationId: input.integrationId,\n entityType: input.entityType,\n direction: input.direction,\n scheduleType: input.scheduleType,\n scheduleValue: input.scheduleValue,\n timezone: input.timezone,\n fullSync: input.fullSync,\n isEnabled: input.isEnabled,\n organizationId: scope.organizationId,\n tenantId: scope.tenantId,\n })\n\n row.integrationId = input.integrationId\n row.entityType = input.entityType\n row.direction = input.direction\n row.scheduleType = input.scheduleType\n row.scheduleValue = input.scheduleValue\n row.timezone = input.timezone\n row.fullSync = input.fullSync\n row.isEnabled = input.isEnabled\n row.scheduledJobId = row.scheduledJobId ?? row.id\n\n if (!existing) {\n em.persist(row)\n }\n\n await em.flush()\n\n await requireScheduler().register({\n id: row.scheduledJobId,\n name: buildScheduleName(row),\n description: buildScheduleDescription(row),\n scopeType: 'organization',\n organizationId: scope.organizationId,\n tenantId: scope.tenantId,\n scheduleType: row.scheduleType,\n scheduleValue: row.scheduleValue,\n timezone: row.timezone,\n targetType: 'queue',\n targetQueue: 'data-sync-scheduled',\n targetPayload: {\n scheduleId: row.id,\n scope,\n },\n requireFeature: 'data_sync.run',\n sourceType: 'module',\n sourceModule: 'data_sync',\n isEnabled: row.isEnabled,\n })\n\n return row\n },\n\n async deleteSchedule(\n id: string,\n scope: SyncScope,\n container?: AwilixContainer,\n expectedUpdatedAt?: string | null,\n ): Promise<boolean> {\n const row = await getById(id, scope)\n if (!row) return false\n\n if (container) {\n await enforceCommandOptimisticLockWithGuards(container, {\n resourceKind: 'data_sync.schedule',\n resourceId: row.id,\n current: row.updatedAt ?? null,\n expected: expectedUpdatedAt ?? null,\n })\n }\n\n const scheduledJobId = row.scheduledJobId ?? row.id\n await requireScheduler().unregister(scheduledJobId)\n\n row.deletedAt = new Date()\n row.isEnabled = false\n await em.flush()\n return true\n },\n }\n}\n\nexport type SyncScheduleService = ReturnType<typeof createSyncScheduleService>\n"],
5
- "mappings": "AAAA,SAAS,kBAAkB;AAG3B,SAAS,4BAA4B,6BAA6B;AAClE,SAAS,wCAAwC,mCAAmC;AACpF,SAAS,oBAAoB;AA6BtB,SAAS,0BAA0B,IAAmB,kBAAyC;AACpG,WAAS,mBAAyC;AAChD,QAAI,CAAC,kBAAkB;AACrB,YAAM,IAAI,MAAM,mCAAmC;AAAA,IACrD;AACA,WAAO;AAAA,EACT;AAEA,WAAS,kBAAkB,KAA2B;AACpD,WAAO,cAAc,IAAI,aAAa,IAAI,IAAI,UAAU,IAAI,IAAI,SAAS;AAAA,EAC3E;AAEA,WAAS,yBAAyB,KAA2B;AAC3D,WAAO,aAAa,IAAI,SAAS,QAAQ,IAAI,aAAa,KAAK,IAAI,UAAU;AAAA,EAC/E;AAEA,iBAAe,QAAQ,IAAY,OAAgD;AACjF,WAAO;AAAA,MACL;AAAA,MACA;AAAA,MACA;AAAA,QACE;AAAA,QACA,gBAAgB,MAAM;AAAA,QACtB,UAAU,MAAM;AAAA,QAChB,WAAW;AAAA,MACb;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAEA,iBAAe,SACb,eACA,YACA,WACA,OAC8B;AAC9B,WAAO;AAAA,MACL;AAAA,MACA;AAAA,MACA;AAAA,QACE;AAAA,QACA;AAAA,QACA;AAAA,QACA,gBAAgB,MAAM;AAAA,QACtB,UAAU,MAAM;AAAA,QAChB,WAAW;AAAA,MACb;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IAEA,MAAM,cAAc,OAMjB,OAAqE;AACtE,YAAM,QAAmC;AAAA,QACvC,gBAAgB,MAAM;AAAA,QACtB,UAAU,MAAM;AAAA,QAChB,WAAW;AAAA,MACb;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YAAY,oBAAI,KAAK;AACzB,UAAI,YAAY;AAChB,YAAM,GAAG,MAAM;AACf,aAAO;AAAA,IACT;AAAA,EACF;AACF;",
4
+ "sourcesContent": ["import { randomUUID } from 'node:crypto'\nimport type { AwilixContainer } from 'awilix'\nimport type { EntityManager, FilterQuery } from '@mikro-orm/postgresql'\nimport { findAndCountWithDecryption, findOneWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport { enforceCommandOptimisticLockWithGuards, enforceRecordGoneIsConflict } from '@open-mercato/shared/lib/crud/optimistic-lock-command'\nimport { SyncSchedule } from '../data/entities'\n\ntype SyncScope = {\n organizationId: string\n tenantId: string\n}\n\ntype SchedulerServiceLike = {\n register: (registration: {\n id: string\n name: string\n description?: string\n scopeType: 'organization'\n organizationId: string\n tenantId: string\n scheduleType: 'cron' | 'interval'\n scheduleValue: string\n timezone?: string\n targetType: 'queue'\n targetQueue: string\n targetPayload: Record<string, unknown>\n requireFeature?: string\n sourceType: 'module'\n sourceModule: string\n isEnabled?: boolean\n }) => Promise<void>\n unregister: (scheduleId: string) => Promise<void>\n}\n\nexport function createSyncScheduleService(em: EntityManager, schedulerService?: SchedulerServiceLike) {\n function requireScheduler(): SchedulerServiceLike {\n if (!schedulerService) {\n throw new Error('Scheduler module is not available')\n }\n return schedulerService\n }\n\n function buildScheduleName(row: { integrationId: string; entityType: string; direction: 'import' | 'export' }): string {\n return `Data sync: ${row.integrationId} ${row.entityType} ${row.direction}`\n }\n\n function buildScheduleDescription(row: { integrationId: string; entityType: string; direction: 'import' | 'export' }): string {\n return `Scheduled ${row.direction} for ${row.integrationId} (${row.entityType})`\n }\n\n async function getById(id: string, scope: SyncScope): Promise<SyncSchedule | null> {\n return findOneWithDecryption(\n em,\n SyncSchedule,\n {\n id,\n organizationId: scope.organizationId,\n tenantId: scope.tenantId,\n deletedAt: null,\n },\n undefined,\n scope,\n )\n }\n\n async function getByKey(\n integrationId: string,\n entityType: string,\n direction: 'import' | 'export',\n scope: SyncScope,\n ): Promise<SyncSchedule | null> {\n return findOneWithDecryption(\n em,\n SyncSchedule,\n {\n integrationId,\n entityType,\n direction,\n organizationId: scope.organizationId,\n tenantId: scope.tenantId,\n deletedAt: null,\n },\n undefined,\n scope,\n )\n }\n\n return {\n getById,\n getByKey,\n\n async listSchedules(query: {\n integrationId?: string\n entityType?: string\n direction?: 'import' | 'export'\n page: number\n pageSize: number\n }, scope: SyncScope): Promise<{ items: SyncSchedule[]; total: number }> {\n const where: FilterQuery<SyncSchedule> = {\n organizationId: scope.organizationId,\n tenantId: scope.tenantId,\n deletedAt: null,\n }\n\n if (query.integrationId) where.integrationId = query.integrationId\n if (query.entityType) where.entityType = query.entityType\n if (query.direction) where.direction = query.direction\n\n const [items, total] = await findAndCountWithDecryption(\n em,\n SyncSchedule,\n where,\n {\n orderBy: { createdAt: 'DESC' },\n limit: query.pageSize,\n offset: (query.page - 1) * query.pageSize,\n },\n scope,\n )\n\n return { items, total }\n },\n\n async saveSchedule(input: {\n id?: string\n integrationId: string\n entityType: string\n direction: 'import' | 'export'\n scheduleType: 'cron' | 'interval'\n scheduleValue: string\n timezone: string\n fullSync: boolean\n isEnabled: boolean\n expectedUpdatedAt?: string | null\n }, scope: SyncScope, container?: AwilixContainer): Promise<SyncSchedule> {\n const existing = input.id\n ? await getById(input.id, scope)\n : await getByKey(input.integrationId, input.entityType, input.direction, scope)\n\n if (existing && container) {\n await enforceCommandOptimisticLockWithGuards(container, {\n resourceKind: 'data_sync.schedule',\n resourceId: existing.id,\n current: existing.updatedAt ?? null,\n expected: input.expectedUpdatedAt ?? null,\n })\n } else if (!existing && input.expectedUpdatedAt) {\n // Concurrent delete-then-edit race: the client edited a schedule that was\n // removed before this keyed upsert ran. Surface the unified conflict\n // instead of silently re-creating it. No-op when no expected version was\n // sent (a genuine create) or when OM_OPTIMISTIC_LOCK is off.\n enforceRecordGoneIsConflict({\n resourceKind: 'data_sync.schedule',\n resourceId: input.id ?? `${input.integrationId}:${input.entityType}:${input.direction}`,\n expected: input.expectedUpdatedAt,\n })\n }\n\n const id = existing?.id ?? randomUUID()\n const scheduledJobId = existing?.scheduledJobId ?? id\n\n // Validate the schedule (and register it with the scheduler) before writing\n // the SyncSchedule row \u2014 an unparseable scheduleValue must not leave a\n // persisted row with no working schedule behind it.\n await requireScheduler().register({\n id: scheduledJobId,\n name: buildScheduleName(input),\n description: buildScheduleDescription(input),\n scopeType: 'organization',\n organizationId: scope.organizationId,\n tenantId: scope.tenantId,\n scheduleType: input.scheduleType,\n scheduleValue: input.scheduleValue,\n timezone: input.timezone,\n targetType: 'queue',\n targetQueue: 'data-sync-scheduled',\n targetPayload: {\n scheduleId: id,\n scope,\n },\n requireFeature: 'data_sync.run',\n sourceType: 'module',\n sourceModule: 'data_sync',\n isEnabled: input.isEnabled,\n })\n\n const row = existing ?? em.create(SyncSchedule, {\n id,\n integrationId: input.integrationId,\n entityType: input.entityType,\n direction: input.direction,\n scheduleType: input.scheduleType,\n scheduleValue: input.scheduleValue,\n timezone: input.timezone,\n fullSync: input.fullSync,\n isEnabled: input.isEnabled,\n organizationId: scope.organizationId,\n tenantId: scope.tenantId,\n })\n\n row.integrationId = input.integrationId\n row.entityType = input.entityType\n row.direction = input.direction\n row.scheduleType = input.scheduleType\n row.scheduleValue = input.scheduleValue\n row.timezone = input.timezone\n row.fullSync = input.fullSync\n row.isEnabled = input.isEnabled\n row.scheduledJobId = scheduledJobId\n\n if (!existing) {\n em.persist(row)\n }\n\n await em.flush()\n\n return row\n },\n\n async deleteSchedule(\n id: string,\n scope: SyncScope,\n container?: AwilixContainer,\n expectedUpdatedAt?: string | null,\n ): Promise<boolean> {\n const row = await getById(id, scope)\n if (!row) return false\n\n if (container) {\n await enforceCommandOptimisticLockWithGuards(container, {\n resourceKind: 'data_sync.schedule',\n resourceId: row.id,\n current: row.updatedAt ?? null,\n expected: expectedUpdatedAt ?? null,\n })\n }\n\n const scheduledJobId = row.scheduledJobId ?? row.id\n await requireScheduler().unregister(scheduledJobId)\n\n row.deletedAt = new Date()\n row.isEnabled = false\n await em.flush()\n return true\n },\n }\n}\n\nexport type SyncScheduleService = ReturnType<typeof createSyncScheduleService>\n"],
5
+ "mappings": "AAAA,SAAS,kBAAkB;AAG3B,SAAS,4BAA4B,6BAA6B;AAClE,SAAS,wCAAwC,mCAAmC;AACpF,SAAS,oBAAoB;AA6BtB,SAAS,0BAA0B,IAAmB,kBAAyC;AACpG,WAAS,mBAAyC;AAChD,QAAI,CAAC,kBAAkB;AACrB,YAAM,IAAI,MAAM,mCAAmC;AAAA,IACrD;AACA,WAAO;AAAA,EACT;AAEA,WAAS,kBAAkB,KAA4F;AACrH,WAAO,cAAc,IAAI,aAAa,IAAI,IAAI,UAAU,IAAI,IAAI,SAAS;AAAA,EAC3E;AAEA,WAAS,yBAAyB,KAA4F;AAC5H,WAAO,aAAa,IAAI,SAAS,QAAQ,IAAI,aAAa,KAAK,IAAI,UAAU;AAAA,EAC/E;AAEA,iBAAe,QAAQ,IAAY,OAAgD;AACjF,WAAO;AAAA,MACL;AAAA,MACA;AAAA,MACA;AAAA,QACE;AAAA,QACA,gBAAgB,MAAM;AAAA,QACtB,UAAU,MAAM;AAAA,QAChB,WAAW;AAAA,MACb;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAEA,iBAAe,SACb,eACA,YACA,WACA,OAC8B;AAC9B,WAAO;AAAA,MACL;AAAA,MACA;AAAA,MACA;AAAA,QACE;AAAA,QACA;AAAA,QACA;AAAA,QACA,gBAAgB,MAAM;AAAA,QACtB,UAAU,MAAM;AAAA,QAChB,WAAW;AAAA,MACb;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IAEA,MAAM,cAAc,OAMjB,OAAqE;AACtE,YAAM,QAAmC;AAAA,QACvC,gBAAgB,MAAM;AAAA,QACtB,UAAU,MAAM;AAAA,QAChB,WAAW;AAAA,MACb;AAEA,UAAI,MAAM,cAAe,OAAM,g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AAY,oBAAI,KAAK;AACzB,UAAI,YAAY;AAChB,YAAM,GAAG,MAAM;AACf,aAAO;AAAA,IACT;AAAA,EACF;AACF;",
6
6
  "names": []
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-mercato/core",
3
- "version": "0.7.1-develop.7153.1.7145d295e6",
3
+ "version": "0.7.1-develop.7154.1.981330c924",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -252,16 +252,16 @@
252
252
  "zod": "^4.4.3"
253
253
  },
254
254
  "peerDependencies": {
255
- "@open-mercato/ai-assistant": "0.7.1-develop.7153.1.7145d295e6",
256
- "@open-mercato/shared": "0.7.1-develop.7153.1.7145d295e6",
257
- "@open-mercato/ui": "0.7.1-develop.7153.1.7145d295e6",
255
+ "@open-mercato/ai-assistant": "0.7.1-develop.7154.1.981330c924",
256
+ "@open-mercato/shared": "0.7.1-develop.7154.1.981330c924",
257
+ "@open-mercato/ui": "0.7.1-develop.7154.1.981330c924",
258
258
  "react": "^19.0.0",
259
259
  "react-dom": "^19.0.0"
260
260
  },
261
261
  "devDependencies": {
262
- "@open-mercato/ai-assistant": "0.7.1-develop.7153.1.7145d295e6",
263
- "@open-mercato/shared": "0.7.1-develop.7153.1.7145d295e6",
264
- "@open-mercato/ui": "0.7.1-develop.7153.1.7145d295e6",
262
+ "@open-mercato/ai-assistant": "0.7.1-develop.7154.1.981330c924",
263
+ "@open-mercato/shared": "0.7.1-develop.7154.1.981330c924",
264
+ "@open-mercato/ui": "0.7.1-develop.7154.1.981330c924",
265
265
  "@testing-library/dom": "^10.4.1",
266
266
  "@testing-library/jest-dom": "^7.0.0",
267
267
  "@testing-library/react": "^16.3.1",
@@ -40,11 +40,11 @@ export function createSyncScheduleService(em: EntityManager, schedulerService?:
40
40
  return schedulerService
41
41
  }
42
42
 
43
- function buildScheduleName(row: SyncSchedule): string {
43
+ function buildScheduleName(row: { integrationId: string; entityType: string; direction: 'import' | 'export' }): string {
44
44
  return `Data sync: ${row.integrationId} ${row.entityType} ${row.direction}`
45
45
  }
46
46
 
47
- function buildScheduleDescription(row: SyncSchedule): string {
47
+ function buildScheduleDescription(row: { integrationId: string; entityType: string; direction: 'import' | 'export' }): string {
48
48
  return `Scheduled ${row.direction} for ${row.integrationId} (${row.entityType})`
49
49
  }
50
50
 
@@ -156,8 +156,36 @@ export function createSyncScheduleService(em: EntityManager, schedulerService?:
156
156
  })
157
157
  }
158
158
 
159
+ const id = existing?.id ?? randomUUID()
160
+ const scheduledJobId = existing?.scheduledJobId ?? id
161
+
162
+ // Validate the schedule (and register it with the scheduler) before writing
163
+ // the SyncSchedule row — an unparseable scheduleValue must not leave a
164
+ // persisted row with no working schedule behind it.
165
+ await requireScheduler().register({
166
+ id: scheduledJobId,
167
+ name: buildScheduleName(input),
168
+ description: buildScheduleDescription(input),
169
+ scopeType: 'organization',
170
+ organizationId: scope.organizationId,
171
+ tenantId: scope.tenantId,
172
+ scheduleType: input.scheduleType,
173
+ scheduleValue: input.scheduleValue,
174
+ timezone: input.timezone,
175
+ targetType: 'queue',
176
+ targetQueue: 'data-sync-scheduled',
177
+ targetPayload: {
178
+ scheduleId: id,
179
+ scope,
180
+ },
181
+ requireFeature: 'data_sync.run',
182
+ sourceType: 'module',
183
+ sourceModule: 'data_sync',
184
+ isEnabled: input.isEnabled,
185
+ })
186
+
159
187
  const row = existing ?? em.create(SyncSchedule, {
160
- id: randomUUID(),
188
+ id,
161
189
  integrationId: input.integrationId,
162
190
  entityType: input.entityType,
163
191
  direction: input.direction,
@@ -178,7 +206,7 @@ export function createSyncScheduleService(em: EntityManager, schedulerService?:
178
206
  row.timezone = input.timezone
179
207
  row.fullSync = input.fullSync
180
208
  row.isEnabled = input.isEnabled
181
- row.scheduledJobId = row.scheduledJobId ?? row.id
209
+ row.scheduledJobId = scheduledJobId
182
210
 
183
211
  if (!existing) {
184
212
  em.persist(row)
@@ -186,28 +214,6 @@ export function createSyncScheduleService(em: EntityManager, schedulerService?:
186
214
 
187
215
  await em.flush()
188
216
 
189
- await requireScheduler().register({
190
- id: row.scheduledJobId,
191
- name: buildScheduleName(row),
192
- description: buildScheduleDescription(row),
193
- scopeType: 'organization',
194
- organizationId: scope.organizationId,
195
- tenantId: scope.tenantId,
196
- scheduleType: row.scheduleType,
197
- scheduleValue: row.scheduleValue,
198
- timezone: row.timezone,
199
- targetType: 'queue',
200
- targetQueue: 'data-sync-scheduled',
201
- targetPayload: {
202
- scheduleId: row.id,
203
- scope,
204
- },
205
- requireFeature: 'data_sync.run',
206
- sourceType: 'module',
207
- sourceModule: 'data_sync',
208
- isEnabled: row.isEnabled,
209
- })
210
-
211
217
  return row
212
218
  },
213
219