@@ -50,6 +50,22 @@ export class TasksController {
5050 private readonly attachmentsService : AttachmentsService ,
5151 ) { }
5252
53+ private async resolveTaskMutationUserId (
54+ authContext : AuthContextType ,
55+ organizationId : string ,
56+ missingUserMessage : string ,
57+ ) : Promise < string > {
58+ if ( authContext . userId ) {
59+ return authContext . userId ;
60+ }
61+
62+ if ( authContext . isApiKey ) {
63+ return this . tasksService . getApiKeyActorUserId ( organizationId ) ;
64+ }
65+
66+ throw new BadRequestException ( missingUserMessage ) ;
67+ }
68+
5369 // ==================== TASKS ====================
5470
5571 @Get ( )
@@ -171,13 +187,11 @@ export class TasksController {
171187 }
172188 }
173189
174- // Get userId from auth context
175- if ( ! authContext . userId ) {
176- throw new BadRequestException (
177- 'User ID is required. Bulk operations require authenticated user session.' ,
178- ) ;
179- }
180- const userId = authContext . userId ;
190+ const userId = await this . resolveTaskMutationUserId (
191+ authContext ,
192+ organizationId ,
193+ 'User ID is required. Bulk operations require authenticated user session.' ,
194+ ) ;
181195
182196 return await this . tasksService . updateTasksStatus (
183197 organizationId ,
@@ -241,13 +255,11 @@ export class TasksController {
241255 throw new BadRequestException ( 'taskIds must be a non-empty array' ) ;
242256 }
243257
244- // Get userId from auth context
245- if ( ! authContext . userId ) {
246- throw new BadRequestException (
247- 'User ID is required. Bulk operations require authenticated user session.' ,
248- ) ;
249- }
250- const userId = authContext . userId ;
258+ const userId = await this . resolveTaskMutationUserId (
259+ authContext ,
260+ organizationId ,
261+ 'User ID is required. Bulk operations require authenticated user session.' ,
262+ ) ;
251263
252264 return await this . tasksService . updateTasksAssignee (
253265 organizationId ,
@@ -517,13 +529,11 @@ export class TasksController {
517529 reviewDate ?: string ;
518530 } ,
519531 ) : Promise < TaskResponseDto > {
520- // Get userId from auth context
521- if ( ! authContext . userId ) {
522- throw new BadRequestException (
523- 'User ID is required. Task updates require authenticated user session.' ,
524- ) ;
525- }
526- const userId = authContext . userId ;
532+ const userId = await this . resolveTaskMutationUserId (
533+ authContext ,
534+ organizationId ,
535+ 'User ID is required. Task updates require authenticated user session.' ,
536+ ) ;
527537
528538 let parsedReviewDate : Date | null | undefined ;
529539 if ( body . reviewDate !== undefined ) {
0 commit comments