146 lines
3.6 KiB
TypeScript
146 lines
3.6 KiB
TypeScript
import { supabase } from '../lib/supabase.js'
|
|
import { correctEE as deepseekCorrectEE, correctEO as deepseekCorrectEO } from '../lib/deepseek.js'
|
|
import type { EERapport, EORapport } from '../lib/deepseek.js'
|
|
import type { AuthProfile } from '../middleware/auth.js'
|
|
|
|
type CorrectionError = {
|
|
error: true
|
|
code: string
|
|
message: string
|
|
status: number
|
|
}
|
|
|
|
export async function correctEE(
|
|
simulationId: string,
|
|
contenu: string,
|
|
tache: string,
|
|
profile: AuthProfile
|
|
): Promise<{ data: EERapport } | CorrectionError> {
|
|
// 1. Vérifier que la production existe et appartient à l'utilisateur
|
|
const { data: production, error: fetchError } = await supabase
|
|
.from('productions')
|
|
.select('id, user_id, tache')
|
|
.eq('id', simulationId)
|
|
.single()
|
|
|
|
if (fetchError || !production) {
|
|
return {
|
|
error: true,
|
|
code: 'SIMULATION_NOT_FOUND',
|
|
message: 'Simulation introuvable.',
|
|
status: 404,
|
|
}
|
|
}
|
|
|
|
if (production.user_id !== profile.id) {
|
|
return {
|
|
error: true,
|
|
code: 'AUTH_REQUIRED',
|
|
message: 'Cette simulation ne vous appartient pas.',
|
|
status: 401,
|
|
}
|
|
}
|
|
|
|
// 2. Appeler DeepSeek pour la correction
|
|
let rapport: EERapport
|
|
try {
|
|
rapport = await deepseekCorrectEE(contenu, tache)
|
|
} catch {
|
|
return {
|
|
error: true,
|
|
code: 'INTERNAL_ERROR',
|
|
message: 'Erreur lors de la correction. Veuillez réessayer dans quelques instants.',
|
|
status: 500,
|
|
}
|
|
}
|
|
|
|
// 3. Mettre à jour la production dans Supabase
|
|
const { error: updateError } = await supabase
|
|
.from('productions')
|
|
.update({
|
|
score: rapport.score,
|
|
nclc: rapport.nclc,
|
|
rapport: JSON.stringify(rapport),
|
|
})
|
|
.eq('id', simulationId)
|
|
|
|
if (updateError) {
|
|
return {
|
|
error: true,
|
|
code: 'INTERNAL_ERROR',
|
|
message: 'Erreur lors de la sauvegarde du rapport. Veuillez réessayer.',
|
|
status: 500,
|
|
}
|
|
}
|
|
|
|
// 4. Retourner le rapport complet
|
|
return { data: rapport }
|
|
}
|
|
|
|
export async function correctEO(
|
|
simulationId: string,
|
|
transcript: string,
|
|
tache: string,
|
|
profile: AuthProfile
|
|
): Promise<{ data: EORapport } | CorrectionError> {
|
|
// 1. Vérifier que la production existe et appartient à l'utilisateur
|
|
const { data: production, error: fetchError } = await supabase
|
|
.from('productions')
|
|
.select('id, user_id, tache')
|
|
.eq('id', simulationId)
|
|
.single()
|
|
|
|
if (fetchError || !production) {
|
|
return {
|
|
error: true,
|
|
code: 'SIMULATION_NOT_FOUND',
|
|
message: 'Simulation introuvable.',
|
|
status: 404,
|
|
}
|
|
}
|
|
|
|
if (production.user_id !== profile.id) {
|
|
return {
|
|
error: true,
|
|
code: 'AUTH_REQUIRED',
|
|
message: 'Cette simulation ne vous appartient pas.',
|
|
status: 401,
|
|
}
|
|
}
|
|
|
|
// 2. Appeler DeepSeek pour la correction EO
|
|
let rapport: EORapport
|
|
try {
|
|
rapport = await deepseekCorrectEO(transcript, tache)
|
|
} catch {
|
|
return {
|
|
error: true,
|
|
code: 'INTERNAL_ERROR',
|
|
message: 'Erreur lors de la correction. Veuillez réessayer dans quelques instants.',
|
|
status: 500,
|
|
}
|
|
}
|
|
|
|
// 3. Mettre à jour la production dans Supabase
|
|
const { error: updateError } = await supabase
|
|
.from('productions')
|
|
.update({
|
|
contenu: transcript,
|
|
score: rapport.score,
|
|
nclc: rapport.nclc,
|
|
rapport: JSON.stringify(rapport),
|
|
})
|
|
.eq('id', simulationId)
|
|
|
|
if (updateError) {
|
|
return {
|
|
error: true,
|
|
code: 'INTERNAL_ERROR',
|
|
message: 'Erreur lors de la sauvegarde du rapport. Veuillez réessayer.',
|
|
status: 500,
|
|
}
|
|
}
|
|
|
|
// 4. Retourner le rapport complet
|
|
return { data: rapport }
|
|
}
|