52 lines
2.0 KiB
JavaScript
52 lines
2.0 KiB
JavaScript
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' })
|
|
}
|
|
})
|