Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 

50 lignes
1.7 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 } 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. const { valid, errors } = validatePortal(incomingPortal);
  16. if (!valid) {
  17. return NextResponse.json({ error: 'Validation failed', errors }, { status: 400 });
  18. }
  19. // themeColor goes into a <style dangerouslySetInnerHTML> in PublicGrid,
  20. // so reject anything that is not a strict #RRGGBB.
  21. if (incomingPortal.themeColor !== undefined && !isValidHexColor(incomingPortal.themeColor)) {
  22. return NextResponse.json(
  23. { error: 'Validation failed', errors: [{ field: 'themeColor', message: 'Colore non valido (atteso #RRGGBB)' }] },
  24. { status: 400 }
  25. );
  26. }
  27. const portals = await getPortals();
  28. if (portals.length > 0) {
  29. portals[0] = { ...portals[0], ...incomingPortal };
  30. } else {
  31. portals.push({ ...incomingPortal, id: 'default-portal', tenantId: 'default' });
  32. }
  33. await savePortals(portals);
  34. // Rivalida sia la home che il layout (il font @font-face è applicato nel layout root)
  35. revalidatePath('/', 'layout');
  36. return NextResponse.json(portals[0], { status: 200 });
  37. } catch {
  38. return NextResponse.json({ error: 'Failed to save portal settings' }, { status: 500 });
  39. }
  40. }