Compare commits
7 Commits
759c725793
...
8acf1d2e12
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8acf1d2e12 | ||
|
|
1698912a27 | ||
|
|
94006be778 | ||
|
|
77a8170a6e | ||
|
|
20bc51b86c | ||
|
|
3ffba9ef4f | ||
|
|
c78a7b687e |
@@ -18,6 +18,7 @@ import {
|
||||
fetchEnrollments,
|
||||
deleteCourse,
|
||||
submitQuiz,
|
||||
updateLesson,
|
||||
generateVideos,
|
||||
getVideoStatus
|
||||
} from '@/lib/sdk/academy/api'
|
||||
@@ -39,6 +40,11 @@ export default function CourseDetailPage() {
|
||||
const [isSubmittingQuiz, setIsSubmittingQuiz] = useState(false)
|
||||
const [videoStatus, setVideoStatus] = useState<any>(null)
|
||||
const [isGeneratingVideos, setIsGeneratingVideos] = useState(false)
|
||||
const [isEditing, setIsEditing] = useState(false)
|
||||
const [editContent, setEditContent] = useState('')
|
||||
const [editTitle, setEditTitle] = useState('')
|
||||
const [isSaving, setIsSaving] = useState(false)
|
||||
const [saveMessage, setSaveMessage] = useState<{ type: 'success' | 'error', text: string } | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
const loadData = async () => {
|
||||
@@ -89,6 +95,65 @@ export default function CourseDetailPage() {
|
||||
}
|
||||
}
|
||||
|
||||
const handleStartEdit = () => {
|
||||
if (!selectedLesson) return
|
||||
setEditContent(selectedLesson.contentMarkdown || '')
|
||||
setEditTitle(selectedLesson.title || '')
|
||||
setIsEditing(true)
|
||||
setSaveMessage(null)
|
||||
}
|
||||
|
||||
const handleCancelEdit = () => {
|
||||
setIsEditing(false)
|
||||
setSaveMessage(null)
|
||||
}
|
||||
|
||||
const handleSaveLesson = async () => {
|
||||
if (!selectedLesson) return
|
||||
setIsSaving(true)
|
||||
setSaveMessage(null)
|
||||
try {
|
||||
await updateLesson(selectedLesson.id, {
|
||||
title: editTitle,
|
||||
content_url: editContent,
|
||||
})
|
||||
const updatedLesson = { ...selectedLesson, title: editTitle, contentMarkdown: editContent }
|
||||
setSelectedLesson(updatedLesson)
|
||||
if (course) {
|
||||
const updatedLessons = course.lessons.map(l => l.id === updatedLesson.id ? updatedLesson : l)
|
||||
setCourse({ ...course, lessons: updatedLessons })
|
||||
}
|
||||
setIsEditing(false)
|
||||
setSaveMessage({ type: 'success', text: 'Lektion gespeichert.' })
|
||||
} catch (error: any) {
|
||||
setSaveMessage({ type: 'error', text: error.message || 'Fehler beim Speichern.' })
|
||||
} finally {
|
||||
setIsSaving(false)
|
||||
}
|
||||
}
|
||||
|
||||
const handleApproveLesson = async () => {
|
||||
if (!selectedLesson) return
|
||||
if (!confirm('Lektion fuer Video-Verarbeitung freigeben? Der Text wird als final markiert.')) return
|
||||
setIsSaving(true)
|
||||
setSaveMessage(null)
|
||||
try {
|
||||
await updateLesson(selectedLesson.id, {
|
||||
description: 'approved_for_video',
|
||||
})
|
||||
const updatedLesson = { ...selectedLesson }
|
||||
if (course) {
|
||||
const updatedLessons = course.lessons.map(l => l.id === updatedLesson.id ? updatedLesson : l)
|
||||
setCourse({ ...course, lessons: updatedLessons })
|
||||
}
|
||||
setSaveMessage({ type: 'success', text: 'Lektion fuer Video-Verarbeitung freigegeben.' })
|
||||
} catch (error: any) {
|
||||
setSaveMessage({ type: 'error', text: error.message || 'Fehler bei der Freigabe.' })
|
||||
} finally {
|
||||
setIsSaving(false)
|
||||
}
|
||||
}
|
||||
|
||||
const handleGenerateVideos = async () => {
|
||||
setIsGeneratingVideos(true)
|
||||
try {
|
||||
@@ -285,16 +350,70 @@ export default function CourseDetailPage() {
|
||||
{selectedLesson ? (
|
||||
<div className="space-y-6">
|
||||
<div className="flex items-center justify-between">
|
||||
<h2 className="text-xl font-semibold text-gray-900">{selectedLesson.title}</h2>
|
||||
<span className={`px-3 py-1 text-xs rounded-full ${
|
||||
selectedLesson.type === 'quiz' ? 'bg-yellow-100 text-yellow-700' :
|
||||
selectedLesson.type === 'video' ? 'bg-blue-100 text-blue-700' :
|
||||
'bg-gray-100 text-gray-600'
|
||||
}`}>
|
||||
{selectedLesson.type === 'quiz' ? 'Quiz' : selectedLesson.type === 'video' ? 'Video' : 'Text'}
|
||||
</span>
|
||||
{isEditing ? (
|
||||
<input
|
||||
type="text"
|
||||
value={editTitle}
|
||||
onChange={e => setEditTitle(e.target.value)}
|
||||
className="text-xl font-semibold text-gray-900 border border-gray-300 rounded-lg px-3 py-1 flex-1 mr-3"
|
||||
/>
|
||||
) : (
|
||||
<h2 className="text-xl font-semibold text-gray-900">{selectedLesson.title}</h2>
|
||||
)}
|
||||
<div className="flex items-center gap-2">
|
||||
<span className={`px-3 py-1 text-xs rounded-full ${
|
||||
selectedLesson.type === 'quiz' ? 'bg-yellow-100 text-yellow-700' :
|
||||
selectedLesson.type === 'video' ? 'bg-blue-100 text-blue-700' :
|
||||
'bg-gray-100 text-gray-600'
|
||||
}`}>
|
||||
{selectedLesson.type === 'quiz' ? 'Quiz' : selectedLesson.type === 'video' ? 'Video' : 'Text'}
|
||||
</span>
|
||||
{selectedLesson.type !== 'quiz' && !isEditing && (
|
||||
<>
|
||||
<button
|
||||
onClick={handleStartEdit}
|
||||
className="px-3 py-1 text-sm bg-gray-100 text-gray-700 rounded-lg hover:bg-gray-200 transition-colors"
|
||||
>
|
||||
Bearbeiten
|
||||
</button>
|
||||
<button
|
||||
onClick={handleApproveLesson}
|
||||
disabled={isSaving}
|
||||
className="px-3 py-1 text-sm bg-green-600 text-white rounded-lg hover:bg-green-700 transition-colors disabled:opacity-50"
|
||||
>
|
||||
Freigeben
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
{isEditing && (
|
||||
<>
|
||||
<button
|
||||
onClick={handleCancelEdit}
|
||||
className="px-3 py-1 text-sm bg-gray-100 text-gray-700 rounded-lg hover:bg-gray-200 transition-colors"
|
||||
>
|
||||
Abbrechen
|
||||
</button>
|
||||
<button
|
||||
onClick={handleSaveLesson}
|
||||
disabled={isSaving}
|
||||
className="px-3 py-1 text-sm bg-purple-600 text-white rounded-lg hover:bg-purple-700 transition-colors disabled:opacity-50"
|
||||
>
|
||||
{isSaving ? 'Speichert...' : 'Speichern'}
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Save/Approve Message */}
|
||||
{saveMessage && (
|
||||
<div className={`p-3 rounded-lg text-sm ${
|
||||
saveMessage.type === 'success' ? 'bg-green-50 text-green-700 border border-green-200' : 'bg-red-50 text-red-700 border border-red-200'
|
||||
}`}>
|
||||
{saveMessage.text}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Video Player */}
|
||||
{selectedLesson.type === 'video' && selectedLesson.videoUrl && (
|
||||
<div className="aspect-video bg-gray-900 rounded-xl overflow-hidden">
|
||||
@@ -306,8 +425,23 @@ export default function CourseDetailPage() {
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Text Content */}
|
||||
{(selectedLesson.type === 'text' || selectedLesson.type === 'video') && selectedLesson.contentMarkdown && (
|
||||
{/* Text Content - Edit Mode */}
|
||||
{isEditing && (selectedLesson.type === 'text' || selectedLesson.type === 'video') && (
|
||||
<div className="space-y-2">
|
||||
<label className="text-sm text-gray-500">Inhalt (Markdown)</label>
|
||||
<textarea
|
||||
value={editContent}
|
||||
onChange={e => setEditContent(e.target.value)}
|
||||
rows={20}
|
||||
className="w-full border border-gray-300 rounded-xl p-4 text-sm font-mono text-gray-800 leading-relaxed resize-y focus:ring-2 focus:ring-purple-500 focus:border-transparent"
|
||||
placeholder="Markdown-Inhalt der Lektion..."
|
||||
/>
|
||||
<p className="text-xs text-gray-400">Unterstuetzt: # Ueberschrift, ## Unterueberschrift, - Aufzaehlung, **fett**</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Text Content - View Mode */}
|
||||
{!isEditing && (selectedLesson.type === 'text' || selectedLesson.type === 'video') && selectedLesson.contentMarkdown && (
|
||||
<div className="prose prose-sm max-w-none">
|
||||
<div className="whitespace-pre-wrap text-gray-700 leading-relaxed">
|
||||
{selectedLesson.contentMarkdown.split('\n').map((line, i) => {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
'use client'
|
||||
|
||||
import React, { useState } from 'react'
|
||||
import React, { useState, useEffect } from 'react'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import Link from 'next/link'
|
||||
import { useSDK } from '@/lib/sdk'
|
||||
@@ -11,6 +11,8 @@ import {
|
||||
GenerateCourseRequest
|
||||
} from '@/lib/sdk/academy/types'
|
||||
import { createCourse, generateCourse } from '@/lib/sdk/academy/api'
|
||||
import { getModules } from '@/lib/sdk/training/api'
|
||||
import type { TrainingModule } from '@/lib/sdk/training/types'
|
||||
|
||||
type CreationMode = 'manual' | 'ai'
|
||||
|
||||
@@ -27,10 +29,26 @@ export default function NewCoursePage() {
|
||||
const [duration, setDuration] = useState(60)
|
||||
const [passingScore, setPassingScore] = useState(70)
|
||||
|
||||
// AI generation state
|
||||
const [topic, setTopic] = useState('')
|
||||
const [targetGroup, setTargetGroup] = useState('Alle Mitarbeiter')
|
||||
const [useRag, setUseRag] = useState(true)
|
||||
// AI generation state - module selection
|
||||
const [trainingModules, setTrainingModules] = useState<TrainingModule[]>([])
|
||||
const [selectedModuleId, setSelectedModuleId] = useState('')
|
||||
const [modulesLoading, setModulesLoading] = useState(true)
|
||||
|
||||
// Load training modules on mount
|
||||
useEffect(() => {
|
||||
async function loadModules() {
|
||||
setModulesLoading(true)
|
||||
try {
|
||||
const res = await getModules()
|
||||
setTrainingModules(res.modules || [])
|
||||
} catch {
|
||||
setError('Training-Module konnten nicht geladen werden.')
|
||||
} finally {
|
||||
setModulesLoading(false)
|
||||
}
|
||||
}
|
||||
loadModules()
|
||||
}, [])
|
||||
|
||||
const handleManualCreate = async () => {
|
||||
if (!title.trim()) {
|
||||
@@ -69,25 +87,16 @@ export default function NewCoursePage() {
|
||||
}
|
||||
|
||||
const handleAIGenerate = async () => {
|
||||
if (!topic.trim()) {
|
||||
setError('Bitte geben Sie ein Thema fuer den Kurs ein.')
|
||||
if (!selectedModuleId) {
|
||||
setError('Bitte waehlen Sie ein Training-Modul aus.')
|
||||
return
|
||||
}
|
||||
setIsLoading(true)
|
||||
setError(null)
|
||||
|
||||
try {
|
||||
const tenantId = typeof window !== 'undefined'
|
||||
? localStorage.getItem('bp_tenant_id') || 'default-tenant'
|
||||
: 'default-tenant'
|
||||
|
||||
const result = await generateCourse({
|
||||
tenantId,
|
||||
topic: topic.trim(),
|
||||
category,
|
||||
targetGroup: targetGroup.trim(),
|
||||
language: 'de',
|
||||
useRag
|
||||
moduleId: selectedModuleId,
|
||||
})
|
||||
|
||||
if (result && result.course && result.course.id) {
|
||||
@@ -174,82 +183,69 @@ export default function NewCoursePage() {
|
||||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M13 10V3L4 14h7v7l9-11h-7z" />
|
||||
</svg>
|
||||
<div>
|
||||
<h3 className="font-medium text-purple-800">KI-generierter Kurs</h3>
|
||||
<h3 className="font-medium text-purple-800">Kurs aus Training-Modul generieren</h3>
|
||||
<p className="text-sm text-purple-600 mt-1">
|
||||
Die KI erstellt automatisch Lektionen, Inhalte und Quizfragen basierend auf dem gewaehlten Thema.
|
||||
Optionaler RAG-Kontext aus relevanten Gesetzestexten wird einbezogen.
|
||||
Waehlen Sie ein bestehendes Training-Modul aus. Der Academy-Kurs wird automatisch mit den generierten Inhalten und Quizfragen erstellt.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Topic */}
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-2">Schulungsthema *</label>
|
||||
<input
|
||||
type="text"
|
||||
value={topic}
|
||||
onChange={(e) => setTopic(e.target.value)}
|
||||
placeholder="z.B. DSGVO-Grundlagen fuer neue Mitarbeiter"
|
||||
className="w-full px-4 py-3 border border-gray-300 rounded-xl focus:ring-2 focus:ring-purple-500 focus:border-purple-500 text-base"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Category */}
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-2">Kategorie *</label>
|
||||
<div className="grid grid-cols-2 md:grid-cols-3 gap-3">
|
||||
{Object.entries(COURSE_CATEGORY_INFO).map(([cat, info]) => (
|
||||
<button
|
||||
key={cat}
|
||||
type="button"
|
||||
onClick={() => setCategory(cat as CourseCategory)}
|
||||
className={`p-4 rounded-xl border-2 text-left transition-all ${
|
||||
category === cat
|
||||
? 'border-purple-500 bg-purple-50'
|
||||
: 'border-gray-200 hover:border-gray-300'
|
||||
}`}
|
||||
>
|
||||
<div className={`text-sm font-medium ${category === cat ? 'text-purple-700' : 'text-gray-700'}`}>
|
||||
{info.label}
|
||||
</div>
|
||||
<div className="text-xs text-gray-500 mt-1 line-clamp-2">{info.description}</div>
|
||||
</button>
|
||||
))}
|
||||
{/* Module Selection */}
|
||||
{modulesLoading ? (
|
||||
<div className="flex items-center gap-3 py-8 justify-center text-gray-500">
|
||||
<svg className="animate-spin w-5 h-5" fill="none" viewBox="0 0 24 24">
|
||||
<circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4" />
|
||||
<path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z" />
|
||||
</svg>
|
||||
Training-Module werden geladen...
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Target Group */}
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-2">Zielgruppe</label>
|
||||
<input
|
||||
type="text"
|
||||
value={targetGroup}
|
||||
onChange={(e) => setTargetGroup(e.target.value)}
|
||||
placeholder="z.B. Alle Mitarbeiter, IT-Abteilung, Fuehrungskraefte"
|
||||
className="w-full px-4 py-3 border border-gray-300 rounded-xl focus:ring-2 focus:ring-purple-500 focus:border-purple-500"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* RAG Toggle */}
|
||||
<div className="flex items-center gap-3">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setUseRag(!useRag)}
|
||||
className={`relative inline-flex h-6 w-11 items-center rounded-full transition-colors ${
|
||||
useRag ? 'bg-purple-600' : 'bg-gray-300'
|
||||
}`}
|
||||
>
|
||||
<span
|
||||
className={`inline-block h-4 w-4 transform rounded-full bg-white transition-transform ${
|
||||
useRag ? 'translate-x-6' : 'translate-x-1'
|
||||
}`}
|
||||
/>
|
||||
</button>
|
||||
) : trainingModules.length === 0 ? (
|
||||
<div className="text-center py-8">
|
||||
<p className="text-gray-500">Keine Training-Module gefunden.</p>
|
||||
<p className="text-sm text-gray-400 mt-1">Bitte erstellen Sie zuerst Module unter Schulung > Module.</p>
|
||||
</div>
|
||||
) : (
|
||||
<div>
|
||||
<span className="text-sm font-medium text-gray-700">RAG-Kontext verwenden</span>
|
||||
<p className="text-xs text-gray-500">Relevante Gesetzestexte (DSGVO, AI Act, NIS2) einbeziehen</p>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-2">Training-Modul auswaehlen *</label>
|
||||
<p className="text-xs text-gray-400 mb-3">Module mit bestehendem Kurs werden beim Generieren uebersprungen oder neu verknuepft.</p>
|
||||
<div className="grid gap-3 max-h-[400px] overflow-y-auto pr-2">
|
||||
{trainingModules.map((mod) => (
|
||||
<button
|
||||
key={mod.id}
|
||||
type="button"
|
||||
onClick={() => setSelectedModuleId(mod.id)}
|
||||
className={`p-4 rounded-xl border-2 text-left transition-all ${
|
||||
selectedModuleId === mod.id
|
||||
? 'border-purple-500 bg-purple-50'
|
||||
: 'border-gray-200 hover:border-gray-300'
|
||||
}`}
|
||||
>
|
||||
<div className="flex items-center justify-between">
|
||||
<div className={`text-sm font-medium ${selectedModuleId === mod.id ? 'text-purple-700' : 'text-gray-700'}`}>
|
||||
{mod.title}
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
{mod.academy_course_id && (
|
||||
<span className="text-xs px-2 py-0.5 rounded-full bg-green-100 text-green-700">Kurs vorhanden</span>
|
||||
)}
|
||||
<span className="text-xs px-2 py-1 rounded-full bg-gray-100 text-gray-600">
|
||||
{mod.regulation_area}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
{mod.description && (
|
||||
<div className="text-xs text-gray-500 mt-1 line-clamp-2">{mod.description}</div>
|
||||
)}
|
||||
<div className="flex gap-3 mt-2 text-xs text-gray-400">
|
||||
<span>{mod.duration_minutes} Min.</span>
|
||||
<span>{mod.frequency_type}</span>
|
||||
<span>{mod.module_code}</span>
|
||||
</div>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Submit */}
|
||||
<div className="flex justify-end gap-3 pt-4 border-t border-gray-200">
|
||||
@@ -261,7 +257,7 @@ export default function NewCoursePage() {
|
||||
</Link>
|
||||
<button
|
||||
onClick={handleAIGenerate}
|
||||
disabled={isLoading || !topic.trim()}
|
||||
disabled={isLoading || !selectedModuleId}
|
||||
className="px-6 py-2.5 bg-purple-600 text-white rounded-xl hover:bg-purple-700 transition-colors disabled:opacity-50 disabled:cursor-not-allowed flex items-center gap-2"
|
||||
>
|
||||
{isLoading ? (
|
||||
@@ -270,7 +266,7 @@ export default function NewCoursePage() {
|
||||
<circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4" />
|
||||
<path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z" />
|
||||
</svg>
|
||||
KI generiert Kurs...
|
||||
Kurs wird generiert...
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
|
||||
@@ -125,7 +125,7 @@ function StatCard({
|
||||
}
|
||||
|
||||
function CourseCard({ course, enrollmentCount }: { course: Course; enrollmentCount: number }) {
|
||||
const categoryInfo = COURSE_CATEGORY_INFO[course.category]
|
||||
const categoryInfo = COURSE_CATEGORY_INFO[course.category] || COURSE_CATEGORY_INFO['custom']
|
||||
|
||||
return (
|
||||
<Link href={`/sdk/academy/${course.id}`}>
|
||||
@@ -589,7 +589,7 @@ export default function AcademyPage() {
|
||||
/>
|
||||
<StatCard
|
||||
label="Aktive Teilnehmer"
|
||||
value={statistics.byStatus.in_progress + statistics.byStatus.not_started}
|
||||
value={(statistics.byStatus?.in_progress || 0) + (statistics.byStatus?.not_started || 0)}
|
||||
color="blue"
|
||||
/>
|
||||
<StatCard
|
||||
|
||||
@@ -30,11 +30,13 @@ async function proxyRequest(
|
||||
}
|
||||
|
||||
// Forward identity headers for RBAC context
|
||||
// Only use client-provided values if they look like UUIDs
|
||||
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
|
||||
const userHeader = request.headers.get('x-user-id')
|
||||
headers['X-User-ID'] = userHeader || '00000000-0000-0000-0000-000000000001'
|
||||
headers['X-User-ID'] = (userHeader && uuidRegex.test(userHeader)) ? userHeader : '00000000-0000-0000-0000-000000000001'
|
||||
|
||||
const tenantHeader = request.headers.get('x-tenant-id')
|
||||
headers['X-Tenant-ID'] = tenantHeader || (process.env.DEFAULT_TENANT_ID || '9282a473-5c95-4b3a-bf78-0ecc0ec71d3e')
|
||||
headers['X-Tenant-ID'] = (tenantHeader && uuidRegex.test(tenantHeader)) ? tenantHeader : (process.env.DEFAULT_TENANT_ID || '9282a473-5c95-4b3a-bf78-0ecc0ec71d3e')
|
||||
|
||||
const fetchOptions: RequestInit = {
|
||||
method,
|
||||
|
||||
@@ -33,8 +33,12 @@ async function proxyRequest(
|
||||
}
|
||||
|
||||
// Forward identity headers with defaults for RBAC context
|
||||
headers['X-User-ID'] = request.headers.get('x-user-id') || '00000000-0000-0000-0000-000000000001'
|
||||
headers['X-Tenant-ID'] = request.headers.get('x-tenant-id') || (process.env.DEFAULT_TENANT_ID || '9282a473-5c95-4b3a-bf78-0ecc0ec71d3e')
|
||||
// Only use client-provided values if they look like UUIDs
|
||||
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
|
||||
const clientUserId = request.headers.get('x-user-id')
|
||||
const clientTenantId = request.headers.get('x-tenant-id')
|
||||
headers['X-User-ID'] = (clientUserId && uuidRegex.test(clientUserId)) ? clientUserId : '00000000-0000-0000-0000-000000000001'
|
||||
headers['X-Tenant-ID'] = (clientTenantId && uuidRegex.test(clientTenantId)) ? clientTenantId : (process.env.DEFAULT_TENANT_ID || '9282a473-5c95-4b3a-bf78-0ecc0ec71d3e')
|
||||
|
||||
const fetchOptions: RequestInit = {
|
||||
method,
|
||||
|
||||
@@ -142,6 +142,14 @@ interface BackendCourse {
|
||||
updated_at: string
|
||||
}
|
||||
|
||||
interface BackendQuizQuestion {
|
||||
id: string
|
||||
question: string
|
||||
options: string[]
|
||||
correct_index: number
|
||||
explanation: string
|
||||
}
|
||||
|
||||
interface BackendLesson {
|
||||
id: string
|
||||
course_id: string
|
||||
@@ -151,7 +159,7 @@ interface BackendLesson {
|
||||
content_url?: string
|
||||
duration_minutes: number
|
||||
order_index: number
|
||||
quiz_questions?: Array<{ question: string; options: string[]; correct_index: number; explanation: string }>
|
||||
quiz_questions?: BackendQuizQuestion[]
|
||||
}
|
||||
|
||||
function mapCourseFromBackend(bc: BackendCourse): Course {
|
||||
@@ -170,6 +178,14 @@ function mapCourseFromBackend(bc: BackendCourse): Course {
|
||||
contentMarkdown: l.content_url || '',
|
||||
durationMinutes: l.duration_minutes || 0,
|
||||
order: l.order_index,
|
||||
quizQuestions: (l.quiz_questions || []).map(q => ({
|
||||
id: q.id || `q-${Math.random().toString(36).slice(2)}`,
|
||||
lessonId: l.id,
|
||||
question: q.question,
|
||||
options: q.options,
|
||||
correctOptionIndex: q.correct_index,
|
||||
explanation: q.explanation,
|
||||
})),
|
||||
})),
|
||||
createdAt: bc.created_at,
|
||||
updatedAt: bc.updated_at,
|
||||
@@ -305,11 +321,11 @@ export async function generateCertificate(enrollmentId: string): Promise<Certifi
|
||||
// =============================================================================
|
||||
|
||||
/**
|
||||
* Quiz-Antworten einreichen und auswerten
|
||||
* Quiz-Antworten einreichen und auswerten (ohne Enrollment)
|
||||
*/
|
||||
export async function submitQuiz(lessonId: string, answers: SubmitQuizRequest): Promise<SubmitQuizResponse> {
|
||||
return fetchWithTimeout<SubmitQuizResponse>(
|
||||
`${ACADEMY_API_BASE}/lessons/${lessonId}/quiz`,
|
||||
`${ACADEMY_API_BASE}/lessons/${lessonId}/quiz-test`,
|
||||
{
|
||||
method: 'POST',
|
||||
body: JSON.stringify(answers)
|
||||
@@ -317,6 +333,25 @@ export async function submitQuiz(lessonId: string, answers: SubmitQuizRequest):
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Lektion aktualisieren (Content, Titel, Quiz-Fragen)
|
||||
*/
|
||||
export async function updateLesson(lessonId: string, update: {
|
||||
title?: string
|
||||
description?: string
|
||||
content_url?: string
|
||||
duration_minutes?: number
|
||||
quiz_questions?: Array<{ question: string; options: string[]; correct_index: number; explanation: string }>
|
||||
}): Promise<{ lesson: any }> {
|
||||
return fetchWithTimeout(
|
||||
`${ACADEMY_API_BASE}/lessons/${lessonId}`,
|
||||
{
|
||||
method: 'PUT',
|
||||
body: JSON.stringify(update)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// STATISTICS
|
||||
// =============================================================================
|
||||
|
||||
@@ -131,6 +131,7 @@ export interface Lesson {
|
||||
videoUrl?: string
|
||||
order: number
|
||||
durationMinutes: number
|
||||
quizQuestions?: QuizQuestion[]
|
||||
}
|
||||
|
||||
export interface QuizQuestion {
|
||||
|
||||
@@ -481,6 +481,10 @@ func main() {
|
||||
// Quiz
|
||||
academyRoutes.POST("/courses/:id/quiz", academyHandlers.SubmitQuiz)
|
||||
|
||||
// Lessons
|
||||
academyRoutes.PUT("/lessons/:id", academyHandlers.UpdateLesson)
|
||||
academyRoutes.POST("/lessons/:id/quiz-test", academyHandlers.TestQuiz)
|
||||
|
||||
// Statistics
|
||||
academyRoutes.GET("/stats", academyHandlers.GetStatistics)
|
||||
|
||||
|
||||
@@ -331,6 +331,23 @@ func (s *Store) GetLesson(ctx context.Context, id uuid.UUID) (*Lesson, error) {
|
||||
return &lesson, nil
|
||||
}
|
||||
|
||||
// UpdateLesson updates a lesson's content, title, and quiz questions
|
||||
func (s *Store) UpdateLesson(ctx context.Context, lesson *Lesson) error {
|
||||
quizQuestions, _ := json.Marshal(lesson.QuizQuestions)
|
||||
|
||||
_, err := s.pool.Exec(ctx, `
|
||||
UPDATE academy_lessons SET
|
||||
title = $2, description = $3, content_url = $4,
|
||||
duration_minutes = $5, quiz_questions = $6
|
||||
WHERE id = $1
|
||||
`,
|
||||
lesson.ID, lesson.Title, lesson.Description,
|
||||
lesson.ContentURL, lesson.DurationMinutes, quizQuestions,
|
||||
)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Enrollment Operations
|
||||
// ============================================================================
|
||||
|
||||
@@ -572,6 +572,131 @@ func (h *AcademyHandlers) SubmitQuiz(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Lesson Update
|
||||
// ============================================================================
|
||||
|
||||
// UpdateLesson updates a lesson's content, title, or quiz questions
|
||||
// PUT /sdk/v1/academy/lessons/:id
|
||||
func (h *AcademyHandlers) UpdateLesson(c *gin.Context) {
|
||||
lessonID, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid lesson ID"})
|
||||
return
|
||||
}
|
||||
|
||||
lesson, err := h.store.GetLesson(c.Request.Context(), lessonID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if lesson == nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "lesson not found"})
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
Title *string `json:"title"`
|
||||
Description *string `json:"description"`
|
||||
ContentURL *string `json:"content_url"`
|
||||
DurationMinutes *int `json:"duration_minutes"`
|
||||
QuizQuestions *[]academy.QuizQuestion `json:"quiz_questions"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if req.Title != nil {
|
||||
lesson.Title = *req.Title
|
||||
}
|
||||
if req.Description != nil {
|
||||
lesson.Description = *req.Description
|
||||
}
|
||||
if req.ContentURL != nil {
|
||||
lesson.ContentURL = *req.ContentURL
|
||||
}
|
||||
if req.DurationMinutes != nil {
|
||||
lesson.DurationMinutes = *req.DurationMinutes
|
||||
}
|
||||
if req.QuizQuestions != nil {
|
||||
lesson.QuizQuestions = *req.QuizQuestions
|
||||
}
|
||||
|
||||
if err := h.store.UpdateLesson(c.Request.Context(), lesson); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"lesson": lesson})
|
||||
}
|
||||
|
||||
// TestQuiz evaluates quiz answers without requiring an enrollment
|
||||
// POST /sdk/v1/academy/lessons/:id/quiz-test
|
||||
func (h *AcademyHandlers) TestQuiz(c *gin.Context) {
|
||||
lessonID, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid lesson ID"})
|
||||
return
|
||||
}
|
||||
|
||||
lesson, err := h.store.GetLesson(c.Request.Context(), lessonID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if lesson == nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "lesson not found"})
|
||||
return
|
||||
}
|
||||
|
||||
if len(lesson.QuizQuestions) == 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "lesson has no quiz questions"})
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
Answers []int `json:"answers"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if len(req.Answers) != len(lesson.QuizQuestions) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "number of answers must match number of questions"})
|
||||
return
|
||||
}
|
||||
|
||||
correctCount := 0
|
||||
var results []academy.QuizResult
|
||||
for i, question := range lesson.QuizQuestions {
|
||||
correct := req.Answers[i] == question.CorrectIndex
|
||||
if correct {
|
||||
correctCount++
|
||||
}
|
||||
results = append(results, academy.QuizResult{
|
||||
Question: question.Question,
|
||||
Correct: correct,
|
||||
Explanation: question.Explanation,
|
||||
})
|
||||
}
|
||||
|
||||
totalQuestions := len(lesson.QuizQuestions)
|
||||
score := 0
|
||||
if totalQuestions > 0 {
|
||||
score = (correctCount * 100) / totalQuestions
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, academy.SubmitQuizResponse{
|
||||
Score: score,
|
||||
Passed: score >= 70,
|
||||
CorrectAnswers: correctCount,
|
||||
TotalQuestions: totalQuestions,
|
||||
Results: results,
|
||||
})
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Statistics
|
||||
// ============================================================================
|
||||
|
||||
Reference in New Issue
Block a user