Files
harheimertc/server/api/cms/mytischtennis/import.post.js
Torsten Schulz (local) 63b5786e43
All checks were successful
Code Analysis and Production Deploy / analyze (push) Successful in 4m36s
Code Analysis and Production Deploy / deploy-production (push) Has been skipped
Code Analysis and Production Deploy / deploy-test (push) Successful in 2m33s
Füge myTischtennis-Verbindung hinzu: Ergänze Navigationslinks, verbessere Fehlerbehandlung und aktualisiere Import-Logik
2026-09-04 14:24:11 +02:00

22 lines
1.3 KiB
JavaScript

import { getUserFromToken, hasAnyRole } from '../../../utils/auth.js'
import { importQttrValues } from '../../../utils/qttr-import.js'
import { readMyTischtennisConnection, saveMyTischtennisConnection } from '../../../utils/mytischtennis-connection.js'
export default defineEventHandler(async (event) => {
const token = getCookie(event, 'auth_token') || getHeader(event, 'authorization')?.replace(/^Bearer\s+/i, '')
const user = token ? await getUserFromToken(token) : null
if (!user) throw createError({ statusCode: 401, statusMessage: 'Nicht authentifiziert' })
if (!hasAnyRole(user, 'admin', 'vorstand')) throw createError({ statusCode: 403, statusMessage: 'Keine Berechtigung' })
const connection = await readMyTischtennisConnection()
if (!connection) throw createError({ statusCode: 409, statusMessage: 'Keine myTischtennis-Verbindung eingerichtet.' })
try {
const result = await importQttrValues({ connection })
return { success: true, rowCount: result.rowCount, importedAt: result.importedAt }
} catch (error) {
const detail = String(error.message || error).slice(0, 500)
console.error('[mytischtennis] TTR-Abruf fehlgeschlagen:', detail)
await saveMyTischtennisConnection({ ...connection, lastImportError: detail })
throw createError({ statusCode: 502, statusMessage: `TTR-Abruf fehlgeschlagen: ${detail}` })
}
})