feat(cms): add endpoint for manual spielplan import with league tables handling
Some checks failed
Code Analysis and Production Deploy / analyze (push) Failing after 3m47s
Code Analysis and Production Deploy / deploy-production (push) Has been skipped
Code Analysis and Production Deploy / deploy-test (push) Has been skipped

This commit is contained in:
Torsten Schulz (local)
2026-07-17 08:41:11 +02:00
parent f060c9771f
commit bd45beadd5
6 changed files with 1498 additions and 5937 deletions

View File

@@ -0,0 +1,51 @@
import { getUserFromToken, hasAnyRole } from '../../utils/auth.js'
import { importSpielplan } from '../../utils/spielplan-import.js'
import { importLeagueTables } from '../../utils/spielklassen-tables-import.js'
import { publishImportedSpielplan } from '../../utils/spielplan-publish.js'
import { error as loggerError, info as loggerInfo } from '../../utils/logger.js'
export default defineEventHandler(async (event) => {
let token = getCookie(event, 'auth_token')
if (!token) {
const authorization = getHeader(event, 'authorization')
if (authorization?.startsWith('Bearer ')) token = authorization.substring(7).trim()
}
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' })
}
try {
const imported = await importSpielplan()
const published = await publishImportedSpielplan({ inputPath: imported.jsonFile })
let tableMessage = ''
try {
const tables = await importLeagueTables()
tableMessage = ` Tabellen: ${tables.importedCount}/${tables.teamCount}.`
} catch (error) {
loggerError('[cms] Tabellen-Import fehlgeschlagen:', { error })
tableMessage = ' Spielplan wurde aktualisiert; Tabellen konnten nicht aktualisiert werden.'
}
loggerInfo('[cms] Spielplan manuell importiert', {
userId: user.id,
season: published.seasonSlug,
matchCount: imported.matchCount
})
return {
success: true,
message: `Spielplan ${published.seasonSlug} mit ${imported.matchCount} Spielen aktualisiert.${tableMessage}`,
season: published.seasonSlug,
matchCount: imported.matchCount
}
} catch (error) {
loggerError('[cms] Manueller Spielplan-Import fehlgeschlagen:', { error })
throw createError({ statusCode: 502, statusMessage: 'Spielplan konnte nicht von myTischtennis importiert werden' })
}
})