Files
harheimertc/server/api/homepage/settings.get.js
Torsten Schulz (local) b8bdbf0a8d feat: add homepage components and API for settings and spielplan options
- Introduced new Vue components for homepage teasers: HomeLinksTeaser, HomeSpielplanTeamWidget, HomeTrainingTeaser, and HomeVereinsmeisterschaftenTeaser.
- Created XML layout for tablet app window dump.
- Implemented API endpoints for fetching and updating homepage settings.
- Added API for retrieving spielplan options, including team extraction logic.
2026-05-29 15:37:45 +02:00

59 lines
1.9 KiB
JavaScript

import { getUserFromToken } from '../../utils/auth.js'
function normalizeConfig(config) {
if (!config || typeof config !== 'object') return undefined
const normalized = {
season: config.season ? String(config.season) : undefined,
teamName: config.teamName ? String(config.teamName) : undefined,
teamAgeGroup: config.teamAgeGroup ? String(config.teamAgeGroup) : undefined
}
if (!normalized.season && !normalized.teamName && !normalized.teamAgeGroup) {
return undefined
}
return normalized
}
function parseSections(value) {
if (!value || typeof value !== 'string') return []
try {
const parsed = JSON.parse(value)
if (!Array.isArray(parsed)) return []
return parsed
.filter(section => section?.id)
.map((section, index) => ({
key: section.key ? String(section.key) : `${String(section.id)}-${index}`,
id: String(section.id),
enabled: section.enabled !== false,
config: normalizeConfig(section.config)
}))
} catch {
return []
}
}
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
const rawCookieSections = getCookie(event, 'homepage_sections')
const cookieSections = parseSections(rawCookieSections)
const userSections = Array.isArray(user?.homepageSettings?.sections)
? user.homepageSettings.sections
.filter(section => section?.id)
.map((section, index) => ({
key: section.key ? String(section.key) : `${String(section.id)}-${index}`,
id: String(section.id),
enabled: section.enabled !== false,
config: normalizeConfig(section.config)
}))
: []
const isLoggedIn = !!user
return {
isLoggedIn,
storage: isLoggedIn ? 'user' : 'cookie',
sections: isLoggedIn ? userSections : cookieSections
}
})