feat: WhatsApp dinâmico do CMS - Criada API /api/contact-info que busca número do CMS - Header e botão flutuante agora puxam número dinamicamente - Número padrão: (35) 9882-9445

This commit is contained in:
Erik
2025-11-27 20:01:11 -03:00
parent 1fa574881c
commit d5183e0a0d
3 changed files with 104 additions and 7 deletions

View File

@@ -0,0 +1,68 @@
import { NextResponse } from 'next/server';
import prisma from '@/lib/prisma';
// Cache em memória para evitar muitas consultas ao banco
let cachedContactInfo: { whatsapp: string; whatsappLink: string } | null = null;
let cacheTime = 0;
const CACHE_TTL = 60 * 1000; // 1 minuto
export async function GET() {
try {
// Verifica se cache é válido
if (cachedContactInfo && Date.now() - cacheTime < CACHE_TTL) {
return NextResponse.json(cachedContactInfo);
}
// Busca dados da página de contato
const pageContent = await prisma.pageContent.findUnique({
where: {
slug_locale: {
slug: 'contato',
locale: 'pt'
}
}
});
if (!pageContent || !pageContent.content) {
// Retorna valores padrão
return NextResponse.json({
whatsapp: '(35) 9882-9445',
whatsappLink: 'https://wa.me/5535988229445'
});
}
const content = pageContent.content as {
info?: {
items?: Array<{
icon?: string;
link?: string;
linkText?: string;
}>;
};
};
// Encontra o item de WhatsApp
const whatsappItem = content.info?.items?.find(
item => item.icon === 'ri-whatsapp-line' || item.link?.includes('wa.me')
);
const result = {
whatsapp: whatsappItem?.linkText || '(35) 9882-9445',
whatsappLink: whatsappItem?.link || 'https://wa.me/5535988229445'
};
// Atualiza cache
cachedContactInfo = result;
cacheTime = Date.now();
return NextResponse.json(result);
} catch (error) {
console.error('Erro ao buscar informações de contato:', error);
// Retorna valores padrão em caso de erro
return NextResponse.json({
whatsapp: '(35) 9882-9445',
whatsappLink: 'https://wa.me/5535988229445'
});
}
}