This commit is contained in:
2026-08-03 14:03:00 +09:00
parent 5e7cffa901
commit b7e910dcf7
24 changed files with 1081 additions and 111 deletions
+64
View File
@@ -0,0 +1,64 @@
import { Prisma } from '@prisma/client';
export type CourseMapPayload = {
key: string;
platformKey: 'NAV2';
name: string;
description: string;
worldFile: string;
viewerModel: 'turtlebot3_world' | 'turtlebot3_house';
custom?: boolean;
layout?: Prisma.JsonValue;
};
export const NAV2_SYSTEM_MAPS: CourseMapPayload[] = [
{
key: 'nav2-turtlebot3-world',
platformKey: 'NAV2',
name: '기본 월드',
description: 'TurtleBot3 기본 장애물 환경',
worldFile: 'turtlebot3_world.world',
viewerModel: 'turtlebot3_world',
},
{
key: 'nav2-turtlebot3-house',
platformKey: 'NAV2',
name: '실내 환경',
description: '방과 복도로 구성된 TurtleBot3 House',
worldFile: 'turtlebot3_house.world',
viewerModel: 'turtlebot3_house',
},
];
export function serializeCourseMaps(
selectedKeys: string[],
customMaps: Array<{
key: string;
name: string;
description: string | null;
platformKey: string;
worldFile: string;
viewerModel: string;
layout: Prisma.JsonValue;
}>,
): CourseMapPayload[] {
const systemByKey = new Map(NAV2_SYSTEM_MAPS.map((map) => [map.key, map]));
const customByKey = new Map(customMaps.map((map) => [map.key, map]));
return selectedKeys.flatMap((key) => {
const systemMap = systemByKey.get(key);
if (systemMap) return [systemMap];
const customMap = customByKey.get(key);
if (!customMap || customMap.platformKey.toUpperCase() !== 'NAV2') return [];
return [{
key: customMap.key,
platformKey: 'NAV2' as const,
name: customMap.name,
description: customMap.description ?? '강사가 구성한 학습 맵',
worldFile: customMap.worldFile,
viewerModel: customMap.viewerModel === 'turtlebot3_house' ? 'turtlebot3_house' as const : 'turtlebot3_world' as const,
custom: true,
layout: customMap.layout,
}];
});
}
+19 -3
View File
@@ -6,6 +6,7 @@ import { AuthService } from '../auth/auth.service';
import { PlatformControlService } from '../platform-control/platform-control.service';
import { PrismaService } from '../prisma/prisma.service';
import { R2PublicFileService } from '../storage/r2-public-file.service';
import { serializeCourseMaps } from './course-maps';
const COURSE_REFERENCE_UPLOAD_ROOT = join(process.cwd(), 'uploads', 'course-references');
const PUBLIC_GUIDE_COURSE_SLUG = 'autonomy-studio-lms-guide';
@@ -159,6 +160,12 @@ export class CurriculumService {
environmentProfiles: {
where: { enabled: true },
},
maps: {
orderBy: { createdAt: 'asc' },
},
weekMaps: {
orderBy: { sortOrder: 'asc' },
},
studentProgress: {
where: {
user: {
@@ -183,6 +190,12 @@ export class CurriculumService {
platform: true,
},
});
const mapKeysByWeek = new Map<number, string[]>();
course.weekMaps.forEach((selection) => {
const items = mapKeysByWeek.get(selection.week) ?? [];
items.push(selection.mapKey);
mapKeysByWeek.set(selection.week, items);
});
const environmentProfileByWeekType = new Map(course.environmentProfiles.map((profile) => [`${profile.week}:${profile.profileType.toUpperCase()}`, profile]));
const visualizationProfilesByWeek = new Map<number, Array<{
id: string;
@@ -414,6 +427,7 @@ export class CurriculumService {
const moduleWithArtifacts = module as typeof module & {
sourceType?: string | null;
prebuiltArtifacts?: unknown;
defaultParameters?: unknown;
};
return {
id: module.id,
@@ -424,10 +438,12 @@ export class CurriculumService {
templateRepositoryUrl: module.templateRepositoryUrl,
sourceType: moduleWithArtifacts.sourceType ?? 'GIT',
prebuiltArtifacts: moduleWithArtifacts.prebuiltArtifacts ?? [],
defaultParameters: moduleWithArtifacts.defaultParameters ?? [],
dockerImage: module.dockerImage,
};
}),
environmentProfiles: visualizationProfilesByWeek.get(week.week) ?? [],
maps: serializeCourseMaps(mapKeysByWeek.get(week.week) ?? [], course.maps),
};
});
const addedWeeks = overrides
@@ -464,6 +480,7 @@ export class CurriculumService {
sourceSubmittedAt: weekProgress?.submittedAt?.toISOString() ?? null,
},
environmentProfiles: visualizationProfilesByWeek.get(override.week) ?? [],
maps: serializeCourseMaps(mapKeysByWeek.get(override.week) ?? [], course.maps),
};
});
@@ -763,7 +780,7 @@ export class CurriculumService {
? '비상 정지'
: '오류 발생'
: '상태 확인 중',
action: blocked ? '연결 불가' : '제어권 요청',
action: blocked ? '연결 불가' : '제어요청',
state: blocked ? 'locked' : 'ready',
};
});
@@ -925,10 +942,9 @@ export class CurriculumService {
private formatLearningHours(learningMinutes: number) {
const totalMinutes = Math.max(0, Math.floor(learningMinutes));
if (totalMinutes < 60) return `${totalMinutes}m`;
const hours = Math.floor(totalMinutes / 60);
const minutes = totalMinutes % 60;
return minutes > 0 ? `${hours}h ${minutes}m` : `${hours}h`;
return `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}`;
}
}
@@ -0,0 +1,16 @@
import { IsOptional, IsString, MaxLength } from 'class-validator';
export class CreateInstructorCourseMapDto {
@IsOptional()
@IsString()
courseId?: string;
@IsString()
@MaxLength(80)
name!: string;
@IsOptional()
@IsString()
@MaxLength(240)
description?: string;
}
@@ -87,8 +87,32 @@ export class UpdateInstructorCurriculumDto {
@IsString({ each: true })
disabledModuleIds?: string[];
@IsOptional()
@IsArray()
moduleDefaultParameters?: Array<{
moduleId?: string;
parameters?: string[];
}>;
@IsOptional()
@IsArray()
environmentModuleSets?: Array<{
platformKey?: string;
moduleIds?: string[];
modules?: Array<{
id?: string;
defaultParameters?: string[];
dockerImage?: string | null;
}>;
}>;
@IsOptional()
@IsArray()
@IsString({ each: true })
visualizationPlatformKeys?: string[];
@IsOptional()
@IsArray()
@IsString({ each: true })
mapKeys?: string[];
}
+21
View File
@@ -5,6 +5,7 @@ import { Request } from 'express';
import { InstructorService } from './instructor.service';
import { AssignInstructorPlatformDto } from './dto/assign-instructor-platform.dto';
import { CreateInstructorPlatformDto } from './dto/create-instructor-platform.dto';
import { CreateInstructorCourseMapDto } from './dto/create-instructor-course-map.dto';
import { CreateInstructorStudentDto } from './dto/create-instructor-student.dto';
import { SaveInstructorGroupDto } from './dto/save-instructor-group.dto';
import { SaveSubmissionScoreDto } from './dto/save-submission-score.dto';
@@ -85,6 +86,17 @@ export class InstructorController {
);
}
@Post('curriculum/maps')
createCurriculumMap(
@Req() request: Request,
@Body() body: CreateInstructorCourseMapDto,
) {
return this.instructorService.createCurriculumMap(
this.getBearerToken(request),
body,
);
}
@Delete('curriculum/weeks/:week')
deleteCurriculumWeek(
@Req() request: Request,
@@ -152,6 +164,15 @@ export class InstructorController {
);
}
@Post('platform-registration-code')
issuePlatformRegistrationCode(
@Req() request: Request,
) {
return this.instructorService.issuePlatformRegistrationCode(
this.getBearerToken(request),
);
}
@Put('platforms/:platformId')
updatePlatform(
@Req() request: Request,
+364 -69
View File
@@ -4,6 +4,7 @@ import {
ConflictException,
ForbiddenException,
Injectable,
Logger,
NotFoundException,
UnauthorizedException,
} from '@nestjs/common';
@@ -24,6 +25,8 @@ import { SaveInstructorGroupDto } from './dto/save-instructor-group.dto';
import { SaveSubmissionScoreDto } from './dto/save-submission-score.dto';
import { UpdateInstructorCurriculumDto } from './dto/update-instructor-curriculum.dto';
import { UpdateInstructorPlatformDto } from './dto/update-instructor-platform.dto';
import { CreateInstructorCourseMapDto } from './dto/create-instructor-course-map.dto';
import { NAV2_SYSTEM_MAPS } from '../curriculum/course-maps';
const INSTRUCTOR_ROLES = new Set(['INSTRUCTOR', 'LMS_ADMIN']);
const DEFAULT_SUBMISSION_BOT_EMAIL = 'lms-bot@futuredrive.net';
@@ -59,6 +62,8 @@ type StudentIssueItem = {
@Injectable()
export class InstructorService {
private readonly logger = new Logger(InstructorService.name);
constructor(
private readonly authService: AuthService,
private readonly chatGateway: ChatGateway,
@@ -734,7 +739,7 @@ export class InstructorService {
}
if (group && organizationName) {
await this.syncPlatformAccessPolicy(organizationId, organizationName);
this.queuePlatformAccessPolicy(organizationId, organizationName);
}
return this.listStudents(accessToken, course.id);
}
@@ -789,7 +794,7 @@ export class InstructorService {
]);
if (organizationName) {
await this.syncPlatformAccessPolicy(organizationId, organizationName);
this.queuePlatformAccessPolicy(organizationId, organizationName);
}
return this.listStudents(accessToken);
}
@@ -1123,6 +1128,27 @@ export class InstructorService {
items.push(selection);
visualizationSelectionsByWeek.set(selection.week, items);
});
const courseMaps = await this.prisma.courseMap.findMany({
where: { courseId: course.id },
orderBy: { createdAt: 'asc' },
});
const weekMapSelections = await this.prisma.courseWeekMap.findMany({
where: { courseId: course.id },
orderBy: { sortOrder: 'asc' },
});
const mapKeysByWeek = new Map<number, string[]>();
weekMapSelections.forEach((selection) => {
const items = mapKeysByWeek.get(selection.week) ?? [];
items.push(selection.mapKey);
mapKeysByWeek.set(selection.week, items);
});
const environmentProfiles = await this.prisma.courseEnvironmentProfile.findMany({
where: { courseId: course.id, enabled: true },
orderBy: [{ platformKey: 'asc' }, { profileType: 'asc' }],
});
const environmentProfileByWeekPlatformKey = new Map(
environmentProfiles.map((profile) => [`${profile.week}:${profile.platformKey}`, profile]),
);
const overrideByWeek = new Map(overrides.map((override) => [override.week, override]));
const baseWeekNumbers = new Set(course.weeks.map((week) => week.week));
const baseWeeks = course.weeks.map((week) => {
@@ -1172,8 +1198,19 @@ export class InstructorService {
folderName: module.folderName,
templateRepositoryUrl: module.templateRepositoryUrl,
dockerImage: module.dockerImage,
defaultParameters: this.stringArray(module.defaultParameters) ?? [],
enabled: !disabledModuleIdSet.has(module.id),
})),
environmentProfiles: (visualizationSelectionsByWeek.get(week.week) ?? []).map((selection) => {
const profile = environmentProfileByWeekPlatformKey.get(`${week.week}:${selection.platformKey}`);
return {
id: profile?.id ?? `${week.week}:${selection.platformKey}`,
platformKey: selection.platformKey,
profileType: profile?.profileType ?? selection.platform.type,
name: profile?.name ?? selection.platform.displayName,
config: profile?.config ?? selection.platform.defaultConfig,
};
}),
visualizationPlatforms: (visualizationSelectionsByWeek.get(week.week) ?? []).map((selection) => ({
key: selection.platform.key,
type: selection.platform.type,
@@ -1181,6 +1218,7 @@ export class InstructorService {
description: selection.platform.description,
config: selection.platform.defaultConfig,
})),
mapKeys: mapKeysByWeek.get(week.week) ?? [],
};
});
const addedWeeks = overrides
@@ -1221,6 +1259,7 @@ export class InstructorService {
description: selection.platform.description,
config: selection.platform.defaultConfig,
})),
mapKeys: mapKeysByWeek.get(override.week) ?? [],
};
});
@@ -1241,6 +1280,19 @@ export class InstructorService {
description: platform.description,
config: platform.defaultConfig,
})),
mapOptions: [
...NAV2_SYSTEM_MAPS,
...courseMaps.map((map) => ({
key: map.key,
platformKey: 'NAV2' as const,
name: map.name,
description: map.description ?? '강사가 구성한 학습 맵',
worldFile: map.worldFile,
viewerModel: map.viewerModel === 'turtlebot3_house' ? 'turtlebot3_house' as const : 'turtlebot3_world' as const,
custom: true,
layout: map.layout,
})),
],
setting: {
groupId: setting?.groupId ?? null,
activeWeek: setting?.activeWeek ?? course.weeks.find((week) => week.state === 'current')?.week ?? course.weeks[0]?.week ?? 1,
@@ -1300,6 +1352,135 @@ export class InstructorService {
const practiceContents = this.normalizePracticeContents(input.practiceContents);
const disabledModuleIds = input.disabledModuleIds?.map((moduleId) => moduleId.trim()).filter(Boolean);
const moduleDefaultParameters = input.moduleDefaultParameters === undefined
? undefined
: input.moduleDefaultParameters.reduce<Array<{ moduleId: string; parameters: string[] }>>((items, entry) => {
const moduleId = entry?.moduleId?.trim();
if (!moduleId) return items;
items.push({
moduleId,
parameters: Array.isArray(entry.parameters)
? entry.parameters.map((parameter) => parameter.trim()).filter(Boolean)
: [],
});
return items;
}, []);
if (moduleDefaultParameters !== undefined) {
const moduleIdSet = new Set(targetWeek?.modules.map((module) => module.id) ?? []);
const invalidModule = moduleDefaultParameters.find((entry) => !moduleIdSet.has(entry.moduleId));
if (invalidModule) throw new BadRequestException('선택한 주차에 없는 모듈입니다.');
await this.prisma.$transaction(moduleDefaultParameters.map((entry) => (
this.prisma.courseWeekModule.update({
where: { id: entry.moduleId },
data: { defaultParameters: entry.parameters },
})
)));
}
if (input.environmentModuleSets !== undefined) {
const moduleIdSet = new Set(targetWeek?.modules.map((module) => module.id) ?? []);
const requestedSets = input.environmentModuleSets.reduce<Array<{
platformKey: string;
modules: Array<{ id: string; defaultParameters: string[]; dockerImage?: string | null }>;
}>>((items, entry) => {
const platformKey = entry?.platformKey?.trim();
if (!platformKey) return items;
const moduleMap = new Map<string, { defaultParameters: string[]; dockerImage?: string | null }>();
if (Array.isArray(entry.modules)) {
entry.modules.forEach((module) => {
const id = module?.id?.trim();
if (!id) return;
moduleMap.set(id, {
defaultParameters: Array.isArray(module.defaultParameters)
? module.defaultParameters.map((parameter) => parameter.trim()).filter(Boolean)
: [],
dockerImage: typeof module.dockerImage === 'string'
? module.dockerImage.trim() || null
: module.dockerImage === null ? null : undefined,
});
});
} else {
(entry.moduleIds ?? []).forEach((moduleId) => {
const id = moduleId.trim();
if (id) moduleMap.set(id, { defaultParameters: [] });
});
}
if ([...moduleMap.keys()].some((moduleId) => !moduleIdSet.has(moduleId))) {
throw new BadRequestException('선택한 주차에 없는 모듈입니다.');
}
items.push({
platformKey,
modules: [...moduleMap.entries()].map(([id, value]) => ({ id, ...value })),
});
return items;
}, []);
const selections = await this.prisma.courseWeekVisualizationPlatform.findMany({
where: {
courseId: course.id,
week: input.activeWeek,
enabled: true,
platformKey: { in: requestedSets.map((item) => item.platformKey) },
},
include: { platform: true },
});
const selectionsByPlatformKey = new Map(selections.map((selection) => [selection.platformKey, selection]));
if (requestedSets.some((item) => !selectionsByPlatformKey.has(item.platformKey))) {
throw new BadRequestException('선택한 환경 프로필을 찾을 수 없습니다.');
}
const profiles = await this.prisma.courseEnvironmentProfile.findMany({
where: { courseId: course.id, week: input.activeWeek },
});
const profilesByPlatformKey = new Map(profiles.map((profile) => [profile.platformKey, profile]));
await this.prisma.$transaction(requestedSets.map((item) => {
const selection = selectionsByPlatformKey.get(item.platformKey)!;
const existingProfile = profilesByPlatformKey.get(item.platformKey);
const existingConfig = existingProfile?.config && typeof existingProfile.config === 'object' && !Array.isArray(existingProfile.config)
? existingProfile.config as Prisma.JsonObject
: {};
const defaultConfig = selection.platform.defaultConfig && typeof selection.platform.defaultConfig === 'object' && !Array.isArray(selection.platform.defaultConfig)
? selection.platform.defaultConfig as Prisma.JsonObject
: {};
const config = {
...defaultConfig,
...existingConfig,
moduleSet: {
target: defaultConfig.target === 'remote-jetson' ? 'platform' : 'local',
includeUnlisted: false,
modules: item.modules.map((module) => ({
id: module.id,
...(module.defaultParameters.length ? { defaultParameters: module.defaultParameters } : {}),
...(module.dockerImage !== undefined ? { dockerImage: module.dockerImage } : {}),
})),
},
};
return this.prisma.courseEnvironmentProfile.upsert({
where: {
courseId_week_platformKey: {
courseId: course.id,
week: input.activeWeek,
platformKey: item.platformKey,
},
},
create: {
courseId: course.id,
week: input.activeWeek,
platformKey: item.platformKey,
profileType: selection.platform.type,
name: selection.platform.displayName,
config,
enabled: true,
},
update: {
profileType: selection.platform.type,
name: selection.platform.displayName,
config,
enabled: true,
},
});
}));
}
let selectedTagLabel: string | undefined;
if (input.tagLabel !== undefined) {
const tagLabel = input.tagLabel.trim();
@@ -1357,7 +1538,7 @@ export class InstructorService {
});
}
if (input.visualizationPlatformKeys) {
if (input.visualizationPlatformKeys !== undefined) {
const selectedKeys = Array.from(new Set(input.visualizationPlatformKeys.map((key) => key.trim().toUpperCase()).filter(Boolean)));
const validPlatforms = selectedKeys.length
? await this.prisma.visualizationPlatform.findMany({
@@ -1400,6 +1581,34 @@ export class InstructorService {
)));
}
if (input.mapKeys !== undefined) {
const selectedKeys = Array.from(new Set(input.mapKeys.map((key) => key.trim()).filter(Boolean)));
const customMaps = selectedKeys.length
? await this.prisma.courseMap.findMany({
where: { courseId: course.id, key: { in: selectedKeys }, platformKey: 'NAV2' },
select: { key: true },
})
: [];
const validKeys = new Set([
...NAV2_SYSTEM_MAPS.map((map) => map.key),
...customMaps.map((map) => map.key),
]);
const mapKeys = selectedKeys.filter((key) => validKeys.has(key));
await this.prisma.courseWeekMap.deleteMany({
where: {
courseId: course.id,
week: input.activeWeek,
mapKey: { notIn: mapKeys },
},
});
await Promise.all(mapKeys.map((mapKey, sortOrder) => this.prisma.courseWeekMap.upsert({
where: { courseId_week_mapKey: { courseId: course.id, week: input.activeWeek, mapKey } },
create: { courseId: course.id, week: input.activeWeek, mapKey, sortOrder },
update: { sortOrder },
})));
}
if (applyToStudents) {
const targetGroup = selectedGroup;
if (!targetGroup) throw new BadRequestException('강의 진행을 적용하려면 그룹을 선택해 주세요.');
@@ -1449,6 +1658,28 @@ export class InstructorService {
return this.getCurriculum(accessToken, selectedGroup?.id ?? input.groupId ?? null, course.id);
}
async createCurriculumMap(accessToken: string | undefined, input: CreateInstructorCourseMapDto) {
const { authUser, instructor } = await this.getInstructor(accessToken);
const organizationName = authUser.primaryAffiliation?.organizationName?.trim();
if (!organizationName) throw new BadRequestException('Instructor organization is required');
const course = await this.findInstructorCourse(organizationName, instructor.id, input.courseId);
if (!course) throw new NotFoundException('Course not found');
const name = input.name.trim();
if (!name) throw new BadRequestException('맵 이름을 입력해 주세요.');
const slug = name.toLowerCase().replace(/[^a-z0-9가-힣]+/g, '-').replace(/^-+|-+$/g, '') || 'map';
const key = `nav2-custom-${slug}-${randomUUID().slice(0, 8)}`;
return this.prisma.courseMap.create({
data: {
courseId: course.id,
key,
name,
description: this.optionalTrim(input.description) ?? '강사가 구성한 학습 맵',
layout: { version: 1, objects: [] },
},
});
}
async deleteCurriculumWeek(accessToken: string | undefined, week: number) {
const { authUser, instructor } = await this.getInstructor(accessToken);
const organizationId = authUser.primaryAffiliation?.organizationId;
@@ -1708,6 +1939,40 @@ export class InstructorService {
return this.listPlatforms(accessToken);
}
async issuePlatformRegistrationCode(accessToken: string | undefined) {
if (!accessToken) throw new UnauthorizedException('Bearer token required');
const launcherApiBaseUrl = this.launcherApiBaseUrl();
let response: Response;
try {
response = await fetch(`${launcherApiBaseUrl}/provision/device-authorizations`, {
method: 'POST',
headers: {
authorization: `Bearer ${accessToken}`,
'content-type': 'application/json',
},
body: JSON.stringify({
usageType: 'class',
}),
});
} catch {
throw new BadGatewayException('Platform registration code request failed');
}
const payload = await response.json().catch((): Record<string, unknown> => ({}));
if (!response.ok || typeof payload.code !== 'string') {
throw new BadGatewayException(
typeof payload.message === 'string'
? payload.message
: 'Platform registration code request failed',
);
}
return {
code: payload.code,
expiresAt: typeof payload.expiresAt === 'string' ? payload.expiresAt : null,
};
}
async updatePlatform(
accessToken: string | undefined,
platformId: string,
@@ -1812,7 +2077,8 @@ export class InstructorService {
});
if (!platform) throw new NotFoundException('Platform not found');
const group = await this.prisma.studentGroup.findFirst({
const unassign = groupId === 'unassigned';
const group = unassign ? null : await this.prisma.studentGroup.findFirst({
where: {
id: groupId,
...this.organizationWhere<Prisma.StudentGroupWhereInput>(
@@ -1826,8 +2092,8 @@ export class InstructorService {
instructorPlatformId: true,
},
});
if (!group) throw new NotFoundException('Group not found');
if (group.instructorPlatformId && group.instructorPlatformId !== platform.id) {
if (!unassign && !group) throw new NotFoundException('Group not found');
if (group?.instructorPlatformId && group.instructorPlatformId !== platform.id) {
throw new BadRequestException('이미 플랫폼이 배정된 그룹입니다.');
}
@@ -1871,20 +2137,22 @@ export class InstructorService {
organizationName,
),
instructorPlatformId: platform.id,
NOT: { id: group.id },
...(group ? { NOT: { id: group.id } } : {}),
},
data: {
instructorPlatformId: null,
platformName: null,
},
});
await tx.studentGroup.update({
where: { id: group.id },
data: {
instructorPlatformId: platform.id,
platformName: platform.displayName,
},
});
if (group) {
await tx.studentGroup.update({
where: { id: group.id },
data: {
instructorPlatformId: platform.id,
platformName: platform.displayName,
},
});
}
if (activeSession && force) {
await tx.platformControlSession.updateMany({
where: {
@@ -1916,12 +2184,14 @@ export class InstructorService {
sessionId: activeSession.id,
platformId: null,
instructorPlatformId: platform.id,
reason: 'GROUP_ASSIGNMENT_CHANGED',
message: `${platform.displayName} 플랫폼 배정이 변경되어 사용이 종료되었습니다.`,
reason: group ? 'GROUP_ASSIGNMENT_CHANGED' : 'GROUP_ASSIGNMENT_REMOVED',
message: group
? `${platform.displayName} 플랫폼 배정이 변경되어 사용이 종료되었습니다.`
: `${platform.displayName} 플랫폼 배정이 해제되어 사용이 종료되었습니다.`,
endedAt: now.toISOString(),
});
}
await this.syncPlatformAccessPolicy(organizationId, organizationName);
this.queuePlatformAccessPolicy(organizationId, organizationName);
return this.listPlatforms(accessToken);
}
@@ -2040,7 +2310,7 @@ export class InstructorService {
}),
]);
await this.syncPlatformAccessPolicy(organizationId, organizationName);
this.queuePlatformAccessPolicy(organizationId, organizationName);
if (terminatedSessionNotice) {
this.chatGateway.notifyUser(terminatedSessionNotice.userId, {
type: 'platform.control.terminated',
@@ -2116,7 +2386,7 @@ export class InstructorService {
throw error;
}
await this.syncPlatformAccessPolicy(organizationId, organizationName);
this.queuePlatformAccessPolicy(organizationId, organizationName);
return this.listGroups(accessToken, input.courseId);
}
@@ -2221,7 +2491,7 @@ export class InstructorService {
throw error;
}
await this.syncPlatformAccessPolicy(organizationId, organizationName);
this.queuePlatformAccessPolicy(organizationId, organizationName);
this.notifyPlatformTerminatedSessions(termination.notices, termination.endedAt, termination.reason);
return this.listGroups(accessToken, input.courseId);
}
@@ -2281,7 +2551,7 @@ export class InstructorService {
});
});
await this.syncPlatformAccessPolicy(organizationId, organizationName);
this.queuePlatformAccessPolicy(organizationId, organizationName);
this.notifyPlatformTerminatedSessions(termination.notices, termination.endedAt, termination.reason);
return this.listGroups(accessToken, courseId);
}
@@ -2408,57 +2678,75 @@ export class InstructorService {
}
}
private queuePlatformAccessPolicy(
organizationId: string | undefined,
organizationName: string,
) {
void this.syncPlatformAccessPolicy(organizationId, organizationName).catch((error) => {
const message = error instanceof Error ? error.message : String(error);
this.logger.warn(`Platform access sync failed in background: ${message}`);
});
}
private async syncPlatformAccessPolicy(
organizationId: string | undefined,
organizationName: string,
) {
const groups = await this.prisma.studentGroup.findMany({
where: {
...this.organizationWhere<Prisma.StudentGroupWhereInput>(
const [platforms, groups] = await Promise.all([
this.prisma.instructorPlatform.findMany({
where: this.organizationWhere<Prisma.InstructorPlatformWhereInput>(
organizationId,
organizationName,
),
instructorPlatformId: { not: null },
},
include: {
instructorPlatform: {
select: {
id: true,
displayName: true,
studioSettingsOverride: true,
},
select: {
id: true,
displayName: true,
studioSettingsOverride: true,
},
members: {
include: {
user: {
select: {
email: true,
}),
this.prisma.studentGroup.findMany({
where: {
...this.organizationWhere<Prisma.StudentGroupWhereInput>(
organizationId,
organizationName,
),
instructorPlatformId: { not: null },
},
select: {
instructorPlatformId: true,
members: {
select: {
user: {
select: {
email: true,
},
},
},
},
},
},
});
}),
]);
const assignments = groups
.map((group): GatewayAccessAssignment | null => {
const platform = group.instructorPlatform;
if (!platform) return null;
const agentHostname = this.readStringSetting(platform.studioSettingsOverride, 'agentHostname');
const gatewayPlatformId = this.readStringSetting(platform.studioSettingsOverride, 'gatewayPlatformId') ?? agentHostname ?? platform.id;
const studentEmails = group.members
.map((member) => member.user.email)
.filter(Boolean);
if (!studentEmails.length) return null;
return {
platformId: gatewayPlatformId,
platformName: platform.displayName,
agentHostname,
agentOwnerEmail: this.readStringSetting(platform.studioSettingsOverride, 'agentOwnerEmail'),
studentEmails,
};
})
.filter((assignment): assignment is GatewayAccessAssignment => Boolean(assignment));
const studentEmailsByPlatform = new Map<string, string[]>();
for (const group of groups) {
if (!group.instructorPlatformId) continue;
studentEmailsByPlatform.set(
group.instructorPlatformId,
group.members.map((member) => member.user.email).filter(Boolean),
);
}
// Include unassigned platforms with no students so the agent removes stale SSH keys.
const assignments: GatewayAccessAssignment[] = platforms.map((platform) => {
const agentHostname = this.readStringSetting(platform.studioSettingsOverride, 'agentHostname');
return {
platformId: this.readStringSetting(platform.studioSettingsOverride, 'gatewayPlatformId') ?? agentHostname ?? platform.id,
platformName: platform.displayName,
agentHostname,
agentOwnerEmail: this.readStringSetting(platform.studioSettingsOverride, 'agentOwnerEmail'),
studentEmails: studentEmailsByPlatform.get(platform.id) ?? [],
};
});
await this.platformGatewayClient.syncAccess(assignments);
}
@@ -2654,20 +2942,18 @@ export class InstructorService {
agents: GatewayAgent[],
binding: { nodeId?: string | null; hostname?: string | null; ownerEmail?: string | null },
) {
const bindingTokens = [binding.nodeId, binding.hostname, binding.ownerEmail]
// An owner can register multiple teaching devices. Bind an instructor
// platform to the saved node or hostname, never to the owner alone.
const bindingTokens = [binding.nodeId, binding.hostname]
.map((value) => this.normalizeAgentToken(value))
.filter(Boolean);
if (!bindingTokens.length) return null;
return agents.find((agent) => {
const agentTokens = [agent.id, agent.hostname, agent.name, agent.ip, agent.user]
const agentTokens = [agent.id, agent.hostname, agent.name, agent.ip]
.map((value) => this.normalizeAgentToken(value))
.filter(Boolean);
return bindingTokens.some((bindingToken) => agentTokens.some((agentToken) => (
agentToken === bindingToken ||
agentToken.includes(bindingToken) ||
bindingToken.includes(agentToken)
)));
return bindingTokens.some((bindingToken) => agentTokens.includes(bindingToken));
}) ?? null;
}
@@ -2714,6 +3000,10 @@ export class InstructorService {
platform: true,
},
},
environmentProfiles: {
where: { enabled: true },
orderBy: [{ platformKey: 'asc' }, { profileType: 'asc' }],
},
},
});
}
@@ -3323,9 +3613,7 @@ export class InstructorService {
}
private async grantLmsAppPermission(accessToken: string, authUserId: string) {
const launcherApiBaseUrl = this.configService
.get<string>('LAUNCHER_API_BASE_URL', 'http://localhost:4200/api')
.replace(/\/$/, '');
const launcherApiBaseUrl = this.launcherApiBaseUrl();
const response = await fetch(
`${launcherApiBaseUrl}/apps/${encodeURIComponent('LMS')}/users/${encodeURIComponent(authUserId)}/permission`,
{
@@ -3351,6 +3639,13 @@ export class InstructorService {
}
}
private launcherApiBaseUrl() {
return this.configService
.get<string>('LAUNCHER_API_BASE_URL', 'http://localhost:4200/api')
.replace(/\/$/, '');
}
private toSummary(students: Array<{ progress: number; online: boolean; issues: number }>) {
const total = students.length;
const averageProgress = total
@@ -42,6 +42,14 @@ export class PlatformControlController {
);
}
@Post('platforms/:platformId/extend')
extendControl(@Req() request: Request, @Param('platformId') platformId: string) {
return this.platformControlService.extendControl(
this.getBearerToken(request),
platformId,
);
}
@Post('platforms/:platformId/connect')
connectPlatform(@Req() request: Request, @Param('platformId') platformId: string) {
return this.platformControlService.connectPlatform(
@@ -84,6 +92,29 @@ export class PlatformControlController {
);
}
@Post('platforms/:platformId/visualization/status')
getRemoteVisualizationStatus(@Req() request: Request, @Param('platformId') platformId: string) {
return this.platformControlService.getRemoteVisualizationStatus(this.getBearerToken(request), platformId);
}
@Post('platforms/:platformId/visualization/start')
startRemoteVisualization(
@Req() request: Request,
@Param('platformId') platformId: string,
@Body() body: { stack?: string; rosDomainId?: string; profile?: unknown },
) {
return this.platformControlService.startRemoteVisualization(this.getBearerToken(request), platformId, body);
}
@Post('platforms/:platformId/visualization/stop')
stopRemoteVisualization(
@Req() request: Request,
@Param('platformId') platformId: string,
@Body() body: { stack?: string; rosDomainId?: string; profile?: unknown },
) {
return this.platformControlService.stopRemoteVisualization(this.getBearerToken(request), platformId, body);
}
private getBearerToken(request: Request): string | undefined {
const [type, token] = request.headers.authorization?.split(' ') ?? [];
return type === 'Bearer' ? token : undefined;
+172 -35
View File
@@ -5,6 +5,7 @@ import {
NotFoundException,
OnModuleDestroy,
OnModuleInit,
ServiceUnavailableException,
UnauthorizedException,
} from '@nestjs/common';
import { Prisma } from '@prisma/client';
@@ -205,7 +206,7 @@ export class PlatformControlService implements OnModuleInit, OnModuleDestroy {
? '사용 가능'
: this.toControlStatusLabel(control.status),
action: control.status === 'AVAILABLE'
? '제어권 요청'
? '제어요청'
: control.action,
state: control.state,
control,
@@ -233,7 +234,7 @@ export class PlatformControlService implements OnModuleInit, OnModuleDestroy {
const group = await this.findStudentGroup(user.id, context);
const resource = await this.resolveResource(platformOptionId, context, group?.instructorPlatformId ?? null);
return this.prisma.$transaction(async (tx) => {
const result = await this.prisma.$transaction(async (tx) => {
const active = await tx.platformControlSession.findFirst({
where: {
...this.resourceWhere(resource),
@@ -258,25 +259,100 @@ export class PlatformControlService implements OnModuleInit, OnModuleDestroy {
const next = await this.grantNextQueued(tx, resource, context);
if (next) this.notifyGranted(next);
const gatewayPlatformId = await this.resolveGatewayPlatformId(resource);
if (gatewayPlatformId) {
await this.platformGatewayClient.release({
platformId: gatewayPlatformId,
user: {
id: user.id,
email: user.email,
name: user.name,
role: user.authRole,
},
});
}
return {
result: 'RELEASED',
platformId: platformOptionId,
nextSession: next ? this.serializeSession(next) : null,
};
}, { isolationLevel: Prisma.TransactionIsolationLevel.Serializable });
// The gateway call can wait for a remote agent. Keep it outside the
// interactive transaction so its timeout cannot invalidate the DB commit.
const gatewayPlatformId = await this.resolveGatewayPlatformId(resource);
if (gatewayPlatformId) {
await this.platformGatewayClient.release({
platformId: gatewayPlatformId,
user: {
id: user.id,
email: user.email,
name: user.name,
role: user.authRole,
},
});
}
return result;
}
async extendControl(accessToken: string | undefined, platformOptionId: string) {
const { authUser, user } = await this.getStudent(accessToken);
const context = this.organizationContext(authUser);
const group = await this.findStudentGroup(user.id, context);
const resource = await this.resolveResource(platformOptionId, context, group?.instructorPlatformId ?? null);
await this.applyExpiredSessions(context);
const session = await this.prisma.$transaction(async (tx) => {
const active = await tx.platformControlSession.findFirst({
where: {
...this.resourceWhere(resource),
holderUserId: user.id,
status: 'ACTIVE',
},
});
if (!active) throw new NotFoundException('Active control session not found');
const now = new Date();
if (now < active.warningAt) {
throw new BadRequestException('제어 시간 경고 구간에서만 연장할 수 있습니다.');
}
if (active.extensionCount >= 1) {
throw new BadRequestException('제어 시간은 한 번만 연장할 수 있습니다.');
}
const waitingCount = await tx.platformControlQueueEntry.count({
where: {
...this.resourceWhere(resource),
status: 'WAITING',
},
});
if (waitingCount > 0) {
throw new BadRequestException('대기 중인 예약이 있어 제어 시간을 연장할 수 없습니다.');
}
const endsAt = new Date(active.endsAt.getTime() + resource.durationMinutes * 60_000);
const warningAt = new Date(endsAt.getTime() - resource.warningMinutes * 60_000);
return tx.platformControlSession.update({
where: { id: active.id },
data: {
endsAt,
warningAt,
warningSentAt: null,
extensionCount: { increment: 1 },
},
});
}, { isolationLevel: Prisma.TransactionIsolationLevel.Serializable });
const gatewayPlatformId = await this.resolveGatewayPlatformId(resource);
if (gatewayPlatformId) {
const ttlSeconds = Math.max(60, Math.floor((session.endsAt.getTime() - Date.now()) / 1000));
await this.platformGatewayClient.extendSession({
platformId: gatewayPlatformId,
ttlSeconds,
user: {
id: user.id,
email: user.email,
name: user.name,
role: user.authRole,
},
});
}
return {
result: 'EXTENDED',
platformId: platformOptionId,
session: this.serializeSession(session),
};
}
async connectPlatform(accessToken: string | undefined, platformOptionId: string) {
@@ -304,11 +380,15 @@ export class PlatformControlService implements OnModuleInit, OnModuleDestroy {
}
const agentOwnerEmail = await this.resolveAgentOwnerEmail(resource);
const assignedStudentEmails = group?.members
.map((member) => member.user.email)
.filter((email): email is string => Boolean(email))
?? (authUser.email ? [authUser.email] : []);
await this.platformGatewayClient.syncAccess([{
platformId: gatewayPlatformId,
platformName: resource.title,
agentOwnerEmail,
studentEmails: authUser.email ? [authUser.email] : [],
studentEmails: assignedStudentEmails,
}]);
const ttlSeconds = Math.max(60, Math.floor((active.endsAt.getTime() - Date.now()) / 1000));
@@ -323,30 +403,76 @@ export class PlatformControlService implements OnModuleInit, OnModuleDestroy {
role: user.authRole || undefined,
},
})
.catch(() => null);
.catch((error: unknown) => {
const detail = error instanceof Error ? error.message : '원격 플랫폼 Gateway에 연결하지 못했습니다.';
throw new ServiceUnavailableException(`원격 플랫폼 연결에 실패했습니다. ${detail}`);
});
if (!gateway?.connection) {
throw new ServiceUnavailableException('원격 플랫폼 연결 정보를 받지 못했습니다.');
}
return {
mode: gateway?.connection ? 'remote' : 'gateway-unavailable',
mode: 'remote',
gateway,
environmentConfigOverride: gateway?.connection
? {
target: 'remote-jetson',
agent: {
url: gateway.connection.agentUrl,
sshUser: gateway.connection.sshUser ?? null,
workspaceRoot: gateway.connection.workspaceRoot ?? null,
},
bridge: {
env: {
BRIDGE_HTTP_URL: gateway.connection.bridgeHttpUrl,
BRIDGE_WS_URL: gateway.connection.bridgeWsUrl,
},
},
}
: null,
environmentConfigOverride: {
target: 'remote-jetson',
agent: {
url: gateway.connection.agentUrl,
sshUser: gateway.connection.sshUser ?? null,
workspaceRoot: gateway.connection.workspaceRoot ?? null,
},
bridge: {
env: {
BRIDGE_HTTP_URL: gateway.connection.bridgeHttpUrl,
BRIDGE_WS_URL: gateway.connection.bridgeWsUrl,
},
},
},
};
}
async getRemoteVisualizationStatus(accessToken: string | undefined, platformOptionId: string) {
const { authUser, user } = await this.getStudent(accessToken);
const context = this.organizationContext(authUser);
const group = await this.findStudentGroup(user.id, context);
const resource = await this.resolveResource(platformOptionId, context, group?.instructorPlatformId ?? null);
await this.assertActiveControl(resource, user.id);
const gatewayPlatformId = await this.resolveGatewayPlatformId(resource);
if (!gatewayPlatformId) throw new BadRequestException('원격 플랫폼이 아닙니다.');
return this.platformGatewayClient.getVisualizationStatus(gatewayPlatformId);
}
async startRemoteVisualization(
accessToken: string | undefined,
platformOptionId: string,
input: { stack?: string; rosDomainId?: string; profile?: unknown },
) {
const { authUser, user } = await this.getStudent(accessToken);
const context = this.organizationContext(authUser);
const group = await this.findStudentGroup(user.id, context);
const resource = await this.resolveResource(platformOptionId, context, group?.instructorPlatformId ?? null);
await this.assertActiveControl(resource, user.id);
const gatewayPlatformId = await this.resolveGatewayPlatformId(resource);
if (!gatewayPlatformId) throw new BadRequestException('원격 플랫폼이 아닙니다.');
return this.platformGatewayClient.startVisualization(gatewayPlatformId, input);
}
async stopRemoteVisualization(
accessToken: string | undefined,
platformOptionId: string,
input: { stack?: string; rosDomainId?: string; profile?: unknown },
) {
const { authUser, user } = await this.getStudent(accessToken);
const context = this.organizationContext(authUser);
const group = await this.findStudentGroup(user.id, context);
const resource = await this.resolveResource(platformOptionId, context, group?.instructorPlatformId ?? null);
await this.assertActiveControl(resource, user.id);
const gatewayPlatformId = await this.resolveGatewayPlatformId(resource);
if (!gatewayPlatformId) throw new BadRequestException('원격 플랫폼이 아닙니다.');
return this.platformGatewayClient.stopVisualization(gatewayPlatformId, input);
}
async listRemoteModules(accessToken: string | undefined, platformOptionId: string) {
const { authUser, user } = await this.getStudent(accessToken);
const context = this.organizationContext(authUser);
@@ -782,6 +908,15 @@ export class PlatformControlService implements OnModuleInit, OnModuleDestroy {
some: { userId },
},
},
include: {
members: {
include: {
user: {
select: { email: true },
},
},
},
},
orderBy: { updatedAt: 'desc' },
});
}
@@ -832,7 +967,7 @@ export class PlatformControlService implements OnModuleInit, OnModuleDestroy {
private emptyState(status: 'AVAILABLE'): PlatformControlState {
return {
status,
action: '제어권 요청',
action: '제어요청',
state: 'ready',
session: null,
queue: null,
@@ -853,6 +988,7 @@ export class PlatformControlService implements OnModuleInit, OnModuleDestroy {
startedAt: Date;
endsAt: Date;
warningAt: Date;
extensionCount?: number;
holderUser?: { name: string | null; email: string } | null;
}) {
return {
@@ -862,6 +998,7 @@ export class PlatformControlService implements OnModuleInit, OnModuleDestroy {
startedAt: session.startedAt.toISOString(),
endsAt: session.endsAt.toISOString(),
warningAt: session.warningAt.toISOString(),
extensionCount: session.extensionCount ?? 0,
};
}
@@ -66,9 +66,27 @@ export class PlatformGatewayClient {
ttlSeconds: number;
}): Promise<GatewaySessionResponse | null> {
if (!this.enabled) return null;
const body = {
platformId: input.platformId,
user: input.user,
ttlSeconds: input.ttlSeconds,
...(input.agentOwnerEmail ? { agentOwnerEmail: input.agentOwnerEmail } : {}),
};
return this.request<GatewaySessionResponse>('/api/lms/platform-sessions', {
method: 'POST',
body: JSON.stringify(input),
body: JSON.stringify(body),
});
}
async extendSession(input: {
platformId: string;
user: { id: string; email?: string; name?: string | null; role?: string | null };
ttlSeconds: number;
}): Promise<void> {
if (!this.enabled) return;
await this.request(`/api/lms/platform-sessions/${encodeURIComponent(input.platformId)}/extend`, {
method: 'POST',
body: JSON.stringify({ user: input.user, ttlSeconds: input.ttlSeconds }),
});
}
@@ -135,6 +153,29 @@ export class PlatformGatewayClient {
});
}
async getVisualizationStatus(platformId: string): Promise<unknown> {
if (!this.enabled) return null;
return this.request(`/api/lms/platforms/${encodeURIComponent(platformId)}/visualization/status`, {
method: 'GET',
});
}
async startVisualization(platformId: string, input: { stack?: string | null; rosDomainId?: string | null; profile?: unknown }): Promise<unknown> {
if (!this.enabled) return null;
return this.request(`/api/lms/platforms/${encodeURIComponent(platformId)}/visualization/start`, {
method: 'POST',
body: JSON.stringify(input),
});
}
async stopVisualization(platformId: string, input: { stack?: string | null; rosDomainId?: string | null; profile?: unknown } = {}): Promise<unknown> {
if (!this.enabled) return null;
return this.request(`/api/lms/platforms/${encodeURIComponent(platformId)}/visualization/stop`, {
method: 'POST',
body: JSON.stringify(input),
});
}
async syncAccess(assignments: GatewayAccessAssignment[]): Promise<void> {
if (!this.enabled) return;
await this.request('/api/lms/access-sync', {
@@ -157,8 +198,10 @@ export class PlatformGatewayClient {
const payload = await response.json().catch(() => ({}));
if (!response.ok) {
const message = String(payload?.error ?? payload?.message ?? `platform_gateway_failed:${response.status}`);
this.logger.warn(`Platform gateway request failed. path=${path} status=${response.status} message=${message}`);
throw new Error(message);
const issues = payload?.issues ? ` issues=${JSON.stringify(payload.issues)}` : '';
const detail = payload?.result?.error ? ` detail=${String(payload.result.error)}` : '';
this.logger.warn(`Platform gateway request failed. path=${path} status=${response.status} message=${message}${issues}${detail}`);
throw new Error(`${message}${issues}${detail}`);
}
return payload as T;
}
+19 -1
View File
@@ -1,4 +1,4 @@
import { Body, Controller, Delete, Post, Req } from '@nestjs/common';
import { Body, Controller, Delete, Get, Param, Post, Query, Req } from '@nestjs/common';
import { Request } from 'express';
import { StudioService } from './studio.service';
@@ -65,6 +65,24 @@ export class StudioController {
return this.studioService.recordLearningTime(this.getBearerToken(request), body);
}
@Get('maps')
listSavedMaps(@Req() request: Request, @Query('courseId') courseId?: string, @Query('week') week?: string) {
return this.studioService.listSavedMaps(this.getBearerToken(request), courseId, Number(week));
}
@Post('maps')
recordSavedMap(
@Req() request: Request,
@Body() body: { courseId?: string; week?: number; name?: string; sourceMapKey?: string; worldFile?: string; viewerModel?: string; yamlPath?: string; pgmPath?: string; previewPgm?: string; databasePath?: string },
) {
return this.studioService.recordSavedMap(this.getBearerToken(request), body);
}
@Delete('maps/:id')
deleteSavedMap(@Req() request: Request, @Param('id') id: string) {
return this.studioService.deleteSavedMap(this.getBearerToken(request), id);
}
private getBearerToken(request: Request): string | undefined {
const [type, token] = request.headers.authorization?.split(' ') ?? [];
return type === 'Bearer' ? token : undefined;
+60
View File
@@ -412,6 +412,66 @@ export class StudioService {
};
}
async listSavedMaps(accessToken: string | undefined, courseId: string | undefined, week: number) {
const { user: authUser } = await this.authService.me(accessToken);
if (!authUser?.email) throw new UnauthorizedException('Invalid user');
if (!courseId?.trim()) throw new BadRequestException('courseId is required');
if (!Number.isInteger(week) || week < 1) throw new BadRequestException('week is required');
const lmsUser = await this.prisma.user.findUnique({ where: { email: authUser.email } });
if (!lmsUser) throw new NotFoundException('LMS user not found');
return this.prisma.studentSavedMap.findMany({
where: { userId: lmsUser.id, courseId: courseId.trim(), week },
orderBy: { createdAt: 'desc' },
});
}
async recordSavedMap(
accessToken: string | undefined,
input: { courseId?: string; week?: number; name?: string; sourceMapKey?: string; worldFile?: string; viewerModel?: string; yamlPath?: string; pgmPath?: string; previewPgm?: string; databasePath?: string },
) {
const { user: authUser } = await this.authService.me(accessToken);
if (!authUser?.email) throw new UnauthorizedException('Invalid user');
const courseId = input.courseId?.trim();
const yamlPath = input.yamlPath?.trim();
const pgmPath = input.pgmPath?.trim();
const previewPgm = input.previewPgm?.trim() || null;
const databasePath = input.databasePath?.trim() || null;
const sourceMapKey = input.sourceMapKey?.trim();
// Platform SLAM maps do not originate from a Gazebo world file.
const worldFile = input.worldFile?.trim() ?? '';
const viewerModel = input.viewerModel?.trim();
if (!courseId) throw new BadRequestException('courseId is required');
if (!Number.isInteger(input.week) || input.week! < 1) throw new BadRequestException('week is required');
if (!yamlPath?.startsWith('/workspace/maps/') || !yamlPath.endsWith('.yaml')) throw new BadRequestException('yamlPath is invalid');
if (!pgmPath?.startsWith('/workspace/maps/') || !pgmPath.endsWith('.pgm')) throw new BadRequestException('pgmPath is invalid');
if (previewPgm && (!/^[A-Za-z0-9+/=]+$/.test(previewPgm) || previewPgm.length > 70_000)) throw new BadRequestException('previewPgm is invalid');
if (databasePath && (!databasePath.startsWith('/workspace/maps/') || !databasePath.endsWith('.db'))) throw new BadRequestException('databasePath is invalid');
if (!sourceMapKey || !viewerModel) throw new BadRequestException('source map is required');
const lmsUser = await this.prisma.user.findUnique({ where: { email: authUser.email } });
if (!lmsUser) throw new NotFoundException('LMS user not found');
const enrollment = await this.prisma.studentEnrollment.findFirst({
where: { userId: lmsUser.id, courseId, status: 'ACTIVE' },
});
if (!enrollment) throw new NotFoundException('Student enrollment not found');
return this.prisma.studentSavedMap.upsert({
where: { userId_courseId_week_yamlPath: { userId: lmsUser.id, courseId, week: input.week!, yamlPath } },
update: { name: input.name?.trim() || 'SLAM 결과 맵', pgmPath, previewPgm, databasePath, sourceMapKey, worldFile, viewerModel },
create: { userId: lmsUser.id, courseId, week: input.week!, name: input.name?.trim() || 'SLAM 결과 맵', sourceMapKey, worldFile, viewerModel, yamlPath, pgmPath, previewPgm, databasePath },
});
}
async deleteSavedMap(accessToken: string | undefined, id: string) {
const { user: authUser } = await this.authService.me(accessToken);
if (!authUser?.email) throw new UnauthorizedException('Invalid user');
const lmsUser = await this.prisma.user.findUnique({ where: { email: authUser.email } });
if (!lmsUser) throw new NotFoundException('LMS user not found');
const result = await this.prisma.studentSavedMap.deleteMany({
where: { id, userId: lmsUser.id },
});
if (!result.count) throw new NotFoundException('Saved map not found');
return { ok: true };
}
private async ensureGiteaRepository(repoName: string, description: string) {
const existingResponse = await fetch(`${this.giteaBaseUrl}/api/v1/repos/${encodeURIComponent(this.studentOwner)}/${encodeURIComponent(repoName)}`, {
headers: this.giteaHeaders(),