feat(cms): add endpoint for manual spielplan import with league tables handling
This commit is contained in:
51
server/api/cms/import-spielplan.post.js
Normal file
51
server/api/cms/import-spielplan.post.js
Normal 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' })
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user