@open-mercato/scheduler 0.6.7-develop.6756.1.5300c41145 → 0.6.7-develop.6758.1.697eade236

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.
Files changed (24) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/dist/modules/scheduler/api/targets/route.js +5 -4
  3. package/dist/modules/scheduler/api/targets/route.js.map +2 -2
  4. package/dist/modules/scheduler/commands/jobs.js +10 -3
  5. package/dist/modules/scheduler/commands/jobs.js.map +2 -2
  6. package/dist/modules/scheduler/data/validators.js +27 -1
  7. package/dist/modules/scheduler/data/validators.js.map +2 -2
  8. package/dist/modules/scheduler/lib/scheduler-safe-commands.js +73 -0
  9. package/dist/modules/scheduler/lib/scheduler-safe-commands.js.map +7 -0
  10. package/dist/modules/scheduler/services/localSchedulerService.js +9 -0
  11. package/dist/modules/scheduler/services/localSchedulerService.js.map +2 -2
  12. package/dist/modules/scheduler/workers/execute-schedule.worker.js +9 -0
  13. package/dist/modules/scheduler/workers/execute-schedule.worker.js.map +2 -2
  14. package/package.json +5 -5
  15. package/src/modules/scheduler/api/targets/__tests__/route.test.ts +76 -0
  16. package/src/modules/scheduler/api/targets/route.ts +7 -7
  17. package/src/modules/scheduler/commands/jobs.ts +11 -3
  18. package/src/modules/scheduler/data/__tests__/validators.test.ts +40 -8
  19. package/src/modules/scheduler/data/validators.ts +30 -1
  20. package/src/modules/scheduler/lib/scheduler-safe-commands.ts +98 -0
  21. package/src/modules/scheduler/services/__tests__/localSchedulerService.test.ts +97 -1
  22. package/src/modules/scheduler/services/localSchedulerService.ts +11 -0
  23. package/src/modules/scheduler/workers/__tests__/execute-schedule.worker.test.ts +12 -0
  24. package/src/modules/scheduler/workers/execute-schedule.worker.ts +15 -0
@@ -7,6 +7,7 @@ import { LocalLockStrategy } from '../lib/localLockStrategy'
7
7
  import { recalculateNextRun } from '../lib/nextRunCalculator'
8
8
  import { emitSchedulerEvent } from '../events.js'
9
9
  import { getGlobalEventBus } from '@open-mercato/shared/modules/events'
10
+ import { assertSchedulerSafeCommandAuthorized } from '../lib/scheduler-safe-commands.js'
10
11
  import { buildScheduledCommandContext } from '../lib/commandContext.js'
11
12
  import { buildQueueTargetPayload, buildSchedulerIdempotencyKey } from '../lib/queueTargetPayload.js'
12
13
  import { createLogger } from '@open-mercato/shared/lib/logger'
@@ -15,6 +16,7 @@ const logger = createLogger('scheduler').child({ component: 'local' })
15
16
 
16
17
  export interface RbacServiceLike {
17
18
  tenantHasFeature(tenantId: string | null | undefined, feature: string, opts?: { organizationId?: string | null }): Promise<boolean>
19
+ userHasAllFeatures(userId: string, required: readonly string[], scope: { tenantId: string | null; organizationId: string | null }): Promise<boolean>
18
20
  }
19
21
 
20
22
  export interface LocalSchedulerConfig {
@@ -276,6 +278,14 @@ export class LocalSchedulerService {
276
278
  }
277
279
 
278
280
  const commandBus = new CommandBus()
281
+ const actorUserId = typeof schedule.createdByUserId === 'string' ? schedule.createdByUserId.trim() : ''
282
+ await assertSchedulerSafeCommandAuthorized({
283
+ commandId: schedule.targetCommand,
284
+ actorUserId,
285
+ tenantId: schedule.tenantId,
286
+ organizationId: schedule.organizationId,
287
+ rbacService: this.rbacService,
288
+ })
279
289
 
280
290
  const commandInput = {
281
291
  ...((schedule.targetPayload as Record<string, unknown>) || {}),
@@ -283,6 +293,7 @@ export class LocalSchedulerService {
283
293
  organizationId: schedule.organizationId,
284
294
  }
285
295
 
296
+ // Build the schedule-scoped command context after the allowlist/RBAC gate.
286
297
  const commandCtx = buildScheduledCommandContext(
287
298
  schedule,
288
299
  {
@@ -2,6 +2,7 @@ import { CrudHttpError } from '@open-mercato/shared/lib/crud/errors'
2
2
  import { ensureTenantScope } from '@open-mercato/shared/lib/commands/scope'
3
3
  import { createQueue } from '@open-mercato/queue'
4
4
  import { ScheduledJob } from '../../data/entities'
5
+ import { registerSchedulerSafeCommands } from '../../lib/scheduler-safe-commands'
5
6
  import executeScheduleWorker from '../execute-schedule.worker'
6
7
 
7
8
  const mockCommandExecute = jest.fn()
@@ -41,6 +42,7 @@ function buildCommandSchedule(overrides: Partial<ScheduledJob> = {}): ScheduledJ
41
42
  schedule.scheduleValue = '* * * * *'
42
43
  schedule.timezone = 'UTC'
43
44
  schedule.sourceType = 'user'
45
+ schedule.createdByUserId = 'user-a'
44
46
  schedule.createdAt = new Date('2026-01-01T00:00:00.000Z')
45
47
  schedule.updatedAt = new Date('2026-01-01T00:00:00.000Z')
46
48
  Object.assign(schedule, overrides)
@@ -54,6 +56,7 @@ function buildWorkerContext(schedule: ScheduledJob) {
54
56
  }
55
57
  const rbacService = {
56
58
  tenantHasFeature: jest.fn(async () => true),
59
+ userHasAllFeatures: jest.fn(async () => true),
57
60
  }
58
61
  return {
59
62
  context: {
@@ -71,6 +74,15 @@ function buildWorkerContext(schedule: ScheduledJob) {
71
74
  }
72
75
 
73
76
  describe('executeScheduleWorker command scope', () => {
77
+ beforeAll(() => {
78
+ registerSchedulerSafeCommands([
79
+ {
80
+ commandId: 'scheduler.test.assert-tenant-scope',
81
+ requiredFeatures: ['scheduler.jobs.manage'],
82
+ },
83
+ ])
84
+ })
85
+
74
86
  afterEach(() => {
75
87
  mockCommandExecute.mockReset()
76
88
  })
@@ -6,6 +6,7 @@ import { ScheduledJob } from '../data/entities.js'
6
6
  import { CommandBus } from '@open-mercato/shared/lib/commands'
7
7
  import type { AppContainer } from '@open-mercato/shared/lib/di/container'
8
8
  import { emitSchedulerEvent } from '../events.js'
9
+ import { assertSchedulerSafeCommandAuthorized } from '../lib/scheduler-safe-commands.js'
9
10
  import { buildScheduledCommandContext } from '../lib/commandContext.js'
10
11
  import { buildQueueTargetPayload, buildSchedulerIdempotencyKey } from '../lib/queueTargetPayload.js'
11
12
  import { createLogger } from '@open-mercato/shared/lib/logger'
@@ -34,6 +35,11 @@ type RbacServiceLike = {
34
35
  feature: string,
35
36
  opts?: { organizationId?: string | null },
36
37
  ): Promise<boolean>
38
+ userHasAllFeatures(
39
+ userId: string,
40
+ required: readonly string[],
41
+ scope: { tenantId: string | null; organizationId: string | null },
42
+ ): Promise<boolean>
37
43
  }
38
44
 
39
45
  /**
@@ -205,6 +211,14 @@ export default async function executeScheduleWorker(
205
211
 
206
212
  } else if (schedule.targetType === 'command' && schedule.targetCommand) {
207
213
  const commandBus = new CommandBus()
214
+ const actorUserId = typeof schedule.createdByUserId === 'string' ? schedule.createdByUserId.trim() : ''
215
+ await assertSchedulerSafeCommandAuthorized({
216
+ commandId: schedule.targetCommand,
217
+ actorUserId,
218
+ tenantId: schedule.tenantId,
219
+ organizationId: schedule.organizationId,
220
+ rbacService,
221
+ })
208
222
 
209
223
  const commandInput = {
210
224
  ...((schedule.targetPayload as Record<string, unknown>) || {}),
@@ -212,6 +226,7 @@ export default async function executeScheduleWorker(
212
226
  organizationId: schedule.organizationId,
213
227
  }
214
228
 
229
+ // Build the schedule-scoped command context after the allowlist/RBAC gate.
215
230
  const commandCtx = buildScheduledCommandContext(schedule, ctx as unknown as AppContainer)
216
231
 
217
232
  const commandResult = await commandBus.execute(schedule.targetCommand, {