Files
harheimertc/server/api/homepage/settings.put.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

81 lines
2.2 KiB
JavaScript

import { getUserFromToken, readUsers, writeUsers } 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 normalizeSections(sections) {
if (!Array.isArray(sections)) return []
const seenKeys = new Set()
return 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)
}))
.filter(section => {
if (seenKeys.has(section.key)) return false
seenKeys.add(section.key)
return true
})
}
export default defineEventHandler(async (event) => {
const body = await readBody(event)
const sections = normalizeSections(body?.sections)
const token = getCookie(event, 'auth_token') || getHeader(event, 'authorization')?.replace(/^Bearer\s+/i, '')
const authUser = token ? await getUserFromToken(token) : null
if (!authUser) {
setCookie(event, 'homepage_sections', JSON.stringify(sections), {
path: '/',
sameSite: 'lax',
secure: process.env.NODE_ENV === 'production',
httpOnly: false,
maxAge: 60 * 60 * 24 * 180
})
return {
success: true,
storage: 'cookie',
sections
}
}
const users = await readUsers()
const userIndex = users.findIndex(user => user.id === authUser.id)
if (userIndex < 0) {
throw createError({
statusCode: 404,
message: 'Benutzer nicht gefunden.'
})
}
const current = users[userIndex]
users[userIndex] = {
...current,
homepageSettings: {
sections,
updatedAt: new Date().toISOString()
}
}
await writeUsers(users)
return {
success: true,
storage: 'user',
sections
}
})