npm run test:routes bâtit l'application, la démarre sur un port local avec un environnement vide, et joue des requêtes HTTP. NocoDB, Bifrost, Mistral, Resend et LightRAG sont remplacés par un faux serveur qui enregistre ce qu'il reçoit. Adresses Resend et Mistral et dossier des limites de débit réglables par l'environnement (RESEND_API_URL, MISTRAL_API_URL, NAV_RATELIMIT_DIR), valeur d'aujourd'hui par défaut. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
97 lines
3.3 KiB
TypeScript
97 lines
3.3 KiB
TypeScript
/**
|
|
* POST /api/report-general
|
|
*
|
|
* Signalement général (bug, contenu inapproprié, suggestion)
|
|
*
|
|
* Body : { category: string, description: string, email?: string }
|
|
* Rate limit : 5/IP/jour
|
|
* Envoi vers jules@trans-former.fr via Resend API
|
|
*/
|
|
|
|
import { checkRateLimitJson } from '~/server/utils/rateLimitJson'
|
|
|
|
const EMAIL_JULES = process.env.EMAIL_JULES || 'jules@trans-former.fr'
|
|
// Adresse de l'API Resend, réglable pour le banc de routes (scripts/banc-routes.mjs).
|
|
const RESEND_API_URL = process.env.RESEND_API_URL || 'https://api.resend.com'
|
|
|
|
const VALID_CATEGORIES = ['Une fiche', 'Le chatbot', 'La carte', 'Autre'] as const
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
// 1. IP
|
|
const ip =
|
|
getHeader(event, 'x-forwarded-for')?.split(',')[0].trim() ||
|
|
event.node.req.socket?.remoteAddress ||
|
|
'0.0.0.0'
|
|
|
|
// 2. Rate limit 5/IP/jour
|
|
const allowed = checkRateLimitJson(ip, 'report-general', 5)
|
|
if (!allowed) {
|
|
throw createError({
|
|
statusCode: 429,
|
|
statusMessage: 'Limite de 5 signalements par jour atteinte.',
|
|
})
|
|
}
|
|
|
|
// 3. Lire le body
|
|
const body = await readBody(event)
|
|
const category: string = (body?.category ?? '').trim()
|
|
const description: string = (body?.description ?? '').trim()
|
|
const email: string = (body?.email ?? '').trim()
|
|
|
|
// 4. Validation
|
|
if (!VALID_CATEGORIES.includes(category as any)) {
|
|
throw createError({ statusCode: 400, statusMessage: 'Catégorie invalide.' })
|
|
}
|
|
if (!description || description.length < 5 || description.length > 500) {
|
|
throw createError({ statusCode: 400, statusMessage: 'Description requise (5-500 caractères).' })
|
|
}
|
|
if (email) {
|
|
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
|
|
if (!emailRegex.test(email)) {
|
|
throw createError({ statusCode: 400, statusMessage: 'Email invalide.' })
|
|
}
|
|
}
|
|
|
|
// 5. Envoi via Resend
|
|
const resendApiKey = process.env.RESEND_API_KEY
|
|
if (!resendApiKey) {
|
|
console.error('[report-general] RESEND_API_KEY manquante')
|
|
throw createError({ statusCode: 500, statusMessage: 'Configuration email manquante.' })
|
|
}
|
|
|
|
const submittedAt = new Date().toLocaleString('fr-FR', { timeZone: 'Europe/Paris' })
|
|
|
|
try {
|
|
await $fetch(`${RESEND_API_URL}/emails`, {
|
|
method: 'POST',
|
|
headers: {
|
|
Authorization: `Bearer ${resendApiKey}`,
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({
|
|
from: 'AEP Signalement <noreply@trans-former.fr>',
|
|
to: EMAIL_JULES,
|
|
subject: `[AEP] Signalement — ${category}`,
|
|
html: `
|
|
<h2>Signalement AEP — ${category}</h2>
|
|
<p><strong>Date :</strong> ${submittedAt}</p>
|
|
<p><strong>Catégorie :</strong> ${category}</p>
|
|
${email ? `<p><strong>Email expéditeur :</strong> ${email}</p>` : '<p><em>Pas d\'email fourni</em></p>'}
|
|
<p><strong>Description :</strong></p>
|
|
<blockquote style="border-left:3px solid #ccc;padding-left:12px;color:#555;">
|
|
${description.replace(/\n/g, '<br/>')}
|
|
</blockquote>
|
|
`,
|
|
}),
|
|
})
|
|
} catch (e: any) {
|
|
console.error('[report-general] Erreur Resend:', e?.message ?? e)
|
|
throw createError({
|
|
statusCode: 502,
|
|
statusMessage: 'Erreur envoi email — réessaie dans quelques instants.',
|
|
})
|
|
}
|
|
|
|
return { ok: true, message: 'Signalement envoyé, merci !' }
|
|
})
|