POST /api/organisations et POST /api/avis : limite de débit, seuls les
champs que pages/ajouter.vue envoie, contrôlés en type et longueur (bornes
de /api/submit pour nom), statut imposé pending (moderation_status,
ai_processed false), toute autre clé ignorée, réponse réduite à { Id }.
GET /api/comment/[orgId] et /api/avis/[orgId] : numéro strict, lignes
publiées seulement, champs affichés seulement (jamais auteur_email).
url d'une fiche publique : rendue seulement en http(s).
Test : scripts/test-fiche-publique.mjs, 189 cas (CRLF toléré).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
94 lines
4.9 KiB
TypeScript
94 lines
4.9 KiB
TypeScript
/**
|
|
* Ce que le public peut écrire, et ce qu'on lui relit, hors des fiches (lot 15a, checkpoint du 21/11/2026).
|
|
*
|
|
* - `POST /api/organisations` (pages/ajouter.vue) : seuls les champs que la page envoie, contrôlés ;
|
|
* la ligne part toujours `pending`, jamais approuvée par le client.
|
|
* - `POST /api/avis` (pages/ajouter.vue) : idem, statut forcé `pending`.
|
|
* - `GET /api/comment/[orgId]` et `GET /api/avis/[orgId]` : lignes publiées seulement, champs affichés seulement.
|
|
*
|
|
* Fonctions pures, sans réseau : testées par scripts/test-fiche-publique.mjs.
|
|
*/
|
|
import { z } from 'zod'
|
|
|
|
// ── Proposer une organisation (pages/ajouter.vue) ─────────────────────────
|
|
|
|
/** Les options du <select> « Type d'organisation » de pages/ajouter.vue. */
|
|
export const TYPES_ORG_AJOUTER = ['association', 'syndicat', 'institution', 'reseau', 'collectif', 'ecole', 'media'] as const
|
|
|
|
const lienHttp = z.string().trim().max(500, 'Maximum 500 caractères')
|
|
.refine((v) => { try { const u = new URL(v); return u.protocol === 'http:' || u.protocol === 'https:' } catch { return false } }, 'URL invalide')
|
|
|
|
/** Corps accepté. `.strip()` (défaut zod) : toute autre clé du client est ignorée. */
|
|
export const OrganisationProposeeSchema = z.object({
|
|
// Bornes de /api/submit (SubmitSchema.nom)
|
|
nom: z.string().trim().min(3, 'Minimum 3 caractères').max(150, 'Maximum 150 caractères'),
|
|
type_org: z.enum(TYPES_ORG_AJOUTER),
|
|
// Absent de /api/submit ; la page ne borne pas : 2 000 caractères
|
|
description: z.string().trim().min(1, 'Champ requis').max(2000, 'Maximum 2000 caractères'),
|
|
lien: z.union([z.literal(''), z.null(), lienHttp]).optional().transform((v) => v || null),
|
|
tags: z.union([z.null(), z.string().trim().max(200, 'Maximum 200 caractères')]).optional().transform((v) => v || null),
|
|
})
|
|
|
|
/** La ligne à insérer : champs contrôlés + statut imposé par le serveur. */
|
|
export function ligneOrganisationProposee(corps: unknown):
|
|
{ ok: true, ligne: Record<string, unknown> } | { ok: false, erreurs: Record<string, string[] | undefined> } {
|
|
const r = OrganisationProposeeSchema.safeParse(corps)
|
|
if (!r.success) return { ok: false, erreurs: r.error.flatten().fieldErrors }
|
|
return {
|
|
ok: true,
|
|
ligne: { ...r.data, status: 'pending', moderation_status: 'pending', ai_processed: false },
|
|
}
|
|
}
|
|
|
|
// ── Proposer un avis (pages/ajouter.vue, après l'organisation) ────────────
|
|
|
|
export const AvisProposeSchema = z.object({
|
|
organisation_id: z.number().int().positive().max(999999999),
|
|
// « Ton prénom » : 100 caractères (comme localisation_ville dans /api/submit)
|
|
auteur: z.string().trim().min(1, 'Champ requis').max(100, 'Maximum 100 caractères'),
|
|
texte: z.string().trim().min(1, 'Champ requis').max(2000, 'Maximum 2000 caractères'),
|
|
})
|
|
|
|
export function ligneAvisPropose(corps: unknown):
|
|
{ ok: true, ligne: Record<string, unknown> } | { ok: false, erreurs: Record<string, string[] | undefined> } {
|
|
const r = AvisProposeSchema.safeParse(corps)
|
|
if (!r.success) return { ok: false, erreurs: r.error.flatten().fieldErrors }
|
|
return { ok: true, ligne: { ...r.data, status: 'pending' } }
|
|
}
|
|
|
|
/** Ce que la page lit de la réponse d'insertion : le numéro, rien d'autre. */
|
|
export function reponseInsertion(data: unknown): { Id?: number } {
|
|
const id = (data as { Id?: unknown } | null)?.Id
|
|
return Number.isInteger(id) ? { Id: id as number } : {}
|
|
}
|
|
|
|
// ── Relire les commentaires et les avis ───────────────────────────────────
|
|
|
|
/** Champs d'un commentaire lus par components/CommentSection.vue. */
|
|
export const CHAMPS_PUBLICS_COMMENTAIRE = ['Id', 'orga_id', 'auteur_pseudo', 'contenu', 'submitted_at'] as const
|
|
|
|
/** Champs d'un avis : aucun composant n'affiche les avis au 21/11 ; le texte seulement (pas le prénom). */
|
|
export const CHAMPS_PUBLICS_AVIS = ['Id', 'organisation_id', 'texte'] as const
|
|
|
|
function projeter(ligne: Record<string, unknown>, champs: readonly string[]) {
|
|
const out: Record<string, unknown> = {}
|
|
for (const k of champs) if (Object.prototype.hasOwnProperty.call(ligne, k)) out[k] = ligne[k]
|
|
return out
|
|
}
|
|
|
|
const objet = (l: unknown): l is Record<string, unknown> => !!l && typeof l === 'object'
|
|
|
|
/** Commentaires publiés (`published === true`) d'une fiche, réduits à leurs champs affichés. */
|
|
export function commentairesPublics(lignes: unknown, orgaId: number) {
|
|
return Array.isArray(lignes)
|
|
? lignes.filter((l) => objet(l) && l.published === true && Number(l.orga_id) === orgaId).map((l) => projeter(l, CHAMPS_PUBLICS_COMMENTAIRE))
|
|
: []
|
|
}
|
|
|
|
/** Avis approuvés d'une fiche, réduits à leurs champs publics. */
|
|
export function avisPublics(lignes: unknown, orgaId: number) {
|
|
return Array.isArray(lignes)
|
|
? lignes.filter((l) => objet(l) && l.status === 'approved' && Number(l.organisation_id) === orgaId).map((l) => projeter(l, CHAMPS_PUBLICS_AVIS))
|
|
: []
|
|
}
|