diff --git a/src/instructor/instructor.controller.ts b/src/instructor/instructor.controller.ts index 65a4bc6..95f8f28 100644 --- a/src/instructor/instructor.controller.ts +++ b/src/instructor/instructor.controller.ts @@ -218,6 +218,11 @@ export class InstructorController { ); } + @Post('platforms/access-sync') + syncPlatformAccess(@Req() request: Request) { + return this.instructorService.syncPlatformAccess(this.getBearerToken(request)); + } + @Delete('platforms/:platformId') deletePlatform( @Req() request: Request, diff --git a/src/instructor/instructor.service.ts b/src/instructor/instructor.service.ts index e841cd0..1ce71ac 100644 --- a/src/instructor/instructor.service.ts +++ b/src/instructor/instructor.service.ts @@ -2927,9 +2927,20 @@ export class InstructorService { }); } + async syncPlatformAccess(accessToken: string | undefined) { + const { authUser } = await this.getInstructor(accessToken); + const organizationId = authUser.primaryAffiliation?.organizationId; + const organizationName = authUser.primaryAffiliation?.organizationName?.trim(); + if (!organizationName) throw new BadRequestException('Instructor organization is required'); + + const assignmentCount = await this.syncPlatformAccessPolicy(organizationId, organizationName, true); + return { ok: true, assignmentCount, syncedAt: new Date().toISOString() }; + } + private async syncPlatformAccessPolicy( organizationId: string | undefined, organizationName: string, + throwOnError = false, ) { const [platforms, groups] = await Promise.all([ this.prisma.instructorPlatform.findMany({ @@ -2987,7 +2998,8 @@ export class InstructorService { }; }); - await this.platformGatewayClient.syncAccess(assignments); + await this.platformGatewayClient.syncAccess(assignments, { throwOnError }); + return assignments.length; } private async findInstructorGroup( diff --git a/src/platform-control/platform-gateway.client.ts b/src/platform-control/platform-gateway.client.ts index 58bbd09..d947637 100644 --- a/src/platform-control/platform-gateway.client.ts +++ b/src/platform-control/platform-gateway.client.ts @@ -178,14 +178,23 @@ export class PlatformGatewayClient { }); } - async syncAccess(assignments: GatewayAccessAssignment[]): Promise { - if (!this.enabled) return; - await this.request('/api/lms/access-sync', { - method: 'POST', - body: JSON.stringify({ assignments }), - }).catch((error) => { + async syncAccess(assignments: GatewayAccessAssignment[], options: { throwOnError?: boolean } = {}): Promise { + if (!this.enabled) { + if (options.throwOnError) throw new Error('Platform Gateway integration is not configured.'); + return false; + } + + try { + await this.request('/api/lms/access-sync', { + method: 'POST', + body: JSON.stringify({ assignments }), + }); + return true; + } catch (error) { + if (options.throwOnError) throw error; this.logger.warn(`Platform access sync failed. message=${error instanceof Error ? error.message : String(error)}`); - }); + return false; + } } private async request(path: string, init: RequestInit): Promise {