Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 

56 строки
2.0 KiB

  1. import { NextResponse } from 'next/server';
  2. import { revalidatePath } from 'next/cache'; // ADD THIS
  3. import { getPortals, savePortals } from '@/lib/db';
  4. import { isValidHexColor, sanitizeWelcomeText } from '@/lib/sanitize';
  5. import { validatePortal } from '@/lib/validation';
  6. import { Portal } from '@/types';
  7. export const dynamic = 'force-dynamic';
  8. export async function GET() {
  9. const portals = await getPortals();
  10. return NextResponse.json(portals[0] || null);
  11. }
  12. export async function POST(request: Request) {
  13. try {
  14. const incomingPortal: Portal = await request.json();
  15. // Sanifica l'HTML del welcome text prima di validare la lunghezza, così il counter
  16. // riflette il contenuto effettivamente persistito.
  17. if (typeof incomingPortal.welcomeText === 'string') {
  18. incomingPortal.welcomeText = sanitizeWelcomeText(incomingPortal.welcomeText);
  19. }
  20. const { valid, errors } = validatePortal(incomingPortal);
  21. if (!valid) {
  22. return NextResponse.json({ error: 'Validation failed', errors }, { status: 400 });
  23. }
  24. // themeColor goes into a <style dangerouslySetInnerHTML> in PublicGrid,
  25. // so reject anything that is not a strict #RRGGBB.
  26. if (incomingPortal.themeColor !== undefined && !isValidHexColor(incomingPortal.themeColor)) {
  27. return NextResponse.json(
  28. { error: 'Validation failed', errors: [{ field: 'themeColor', message: 'Colore non valido (atteso #RRGGBB)' }] },
  29. { status: 400 }
  30. );
  31. }
  32. const portals = await getPortals();
  33. if (portals.length > 0) {
  34. portals[0] = { ...portals[0], ...incomingPortal };
  35. } else {
  36. portals.push({ ...incomingPortal, id: 'default-portal', tenantId: 'default' });
  37. }
  38. await savePortals(portals);
  39. // Rivalida sia la home che il layout (il font @font-face è applicato nel layout root)
  40. revalidatePath('/', 'layout');
  41. return NextResponse.json(portals[0], { status: 200 });
  42. } catch {
  43. return NextResponse.json({ error: 'Failed to save portal settings' }, { status: 500 });
  44. }
  45. }