import { checkAdminAuth } from '@/lib/auth'; import fs from 'fs'; import { NextRequest, NextResponse } from 'next/server'; import path from 'path'; // Path to the editable text file const TEXT_FILE = path.join(process.cwd(), 'data', 'editable_text.json'); export type EditableText = { id: string; content: string }; // Ensure the data directory exists function ensureDataDirectory() { const dataDir = path.join(process.cwd(), 'data'); if (!fs.existsSync(dataDir)) { fs.mkdirSync(dataDir, { recursive: true }); } } // Get all editable texts function getEditableTexts() { ensureDataDirectory(); if (!fs.existsSync(TEXT_FILE)) { // Default texts const defaultTexts = [ { id: 'welcome-text', content: '

Herzlich willkommen bei der Online-Abstimmungsplattform des SCHAFWASCHENER SEGELVEREIN CHIEMSEE E.V. RIMSTING.

Diese Plattform ermöglicht es Mitgliedern, sicher und bequem über Vereinsangelegenheiten abzustimmen.

' }, { id: 'current-vote-text', content: '

Derzeit läuft eine Abstimmung zur Änderung der Vereinssatzung.

Bitte nutzen Sie den Ihnen zugesandten Link, um an der Abstimmung teilzunehmen.

Bei Fragen wenden Sie sich bitte an den Vorstand.

' }, { id: 'vote-question', content: '

Stimmen Sie der vorgeschlagenen Änderung der Vereinssatzung zu?

' }, { id: 'vote-options', content: [ { id: 'yes', label: 'Ja, ich stimme zu' }, { id: 'no', label: 'Nein, ich stimme nicht zu' }, { id: 'abstain', label: 'Ich enthalte mich' } ] } ]; fs.writeFileSync(TEXT_FILE, JSON.stringify(defaultTexts, null, 2)); return defaultTexts; } const data = fs.readFileSync(TEXT_FILE, 'utf-8'); return JSON.parse(data); } // Save editable texts function saveEditableTexts(texts: EditableText[]) { ensureDataDirectory(); fs.writeFileSync(TEXT_FILE, JSON.stringify(texts, null, 2)); } // GET handler to retrieve all editable texts export async function GET() { try { const texts = getEditableTexts(); return NextResponse.json({ texts }); } catch (error) { console.error('Error getting editable texts:', error); return NextResponse.json( { error: 'Failed to get editable texts' }, { status: 500 } ); } } // POST handler to update an editable text (requires admin authentication) export async function POST(request: NextRequest) { try { const body = await request.json(); // Check for admin auth const { password, } = body; const isAuthenticated = await checkAdminAuth(password); if (!isAuthenticated) { return NextResponse.json( { error: 'Unauthorized' }, { status: 401 } ); } const { id, content } = body; if (!id || !content) { return NextResponse.json( { error: 'ID and content are required' }, { status: 400 } ); } // Get current texts const texts = getEditableTexts(); // Find and update the text with the given ID const textIndex = texts.findIndex((text: EditableText ) => text.id === id); if (textIndex === -1) { // Text not found, add new texts.push({ id, content }); } else { // Update existing text texts[textIndex].content = content; } // Save updated texts saveEditableTexts(texts); return NextResponse.json({ success: true }); } catch (error) { console.error('Error updating editable text:', error); return NextResponse.json( { error: 'Failed to update editable text' }, { status: 500 } ); } }