Files
CoHaiSe/backend/crm/src/routes/leadRoutes.ts
T
Esiah 09cf07d79b feat: migrate showcase to Next.js and overhaul CRM UI
- rebuild the public site on the Next.js App Router with static export,
  per-section SEO pages, metadata, sitemap, robots and a generated OG image
- add full-screen scroll-snap regions, staged scroll reveals, a hash-free
  tab navigation with a sliding indicator and region-by-region menu scrolling
- replace the theme text toggle with animated sun/moon icons
- restructure the CRM API into config, middleware, models, routes, services
  and utils layers
- redesign the CRM shell with a single surface, tab rail, full-width blurred
  DataTable, record dialogs and mask-based icons
- update Docker/Nginx publishing, Makefile targets, shared UI polish and docs
2026-11-16 00:52:29 +01:00

52 lines
1.8 KiB
TypeScript

import { z } from "zod";
import { AppError, type App, type Db } from "@cohaise/server";
import { PUBLIC_LEAD_RATE_LIMIT } from "../config/app.js";
import { createLead } from "../services/leadService.js";
import { createRateLimiter } from "../utils/rateLimit.js";
/**
* Point d'entrée public du formulaire de contact de la vitrine.
*
* Route volontairement sans `requireAuth` : n'importe quel visiteur de
* cohaise.dev peut créer un prospect. Le honeypot et le rate-limit sont
* décrits dans `services/leadService.ts`.
*/
const leadBody = z.object({
firstName: z.string().trim().min(1).max(100),
lastName: z.string().trim().min(1).max(100),
email: z.string().trim().min(1).max(255),
organization: z.string().trim().min(1).max(255).nullish(),
phone: z.string().trim().max(50).nullish(),
message: z.string().trim().max(4000).nullish(),
// Honeypot : vide chez un humain, rempli par un robot.
website: z.string().max(255).nullish(),
});
// Une instance unique par processus : suffisant pour un CRM à un seul nœud.
const limiter = createRateLimiter(PUBLIC_LEAD_RATE_LIMIT);
export const registerLeadRoutes = (app: App, db: Db): void => {
app.post("/api/crm/leads", async (context) => {
const forwarded = context.req.header("x-forwarded-for");
const ip =
forwarded?.split(",")[0]?.trim() ??
context.req.header("x-real-ip") ??
"unknown";
if (!limiter.isAllowed(ip)) throw AppError.tooManyRequests();
const parsed = leadBody.safeParse(await context.req.json().catch(() => null));
if (!parsed.success) {
const issue = parsed.error.issues[0];
throw AppError.badRequest(
issue
? `${issue.path.join(".") || "corps"} : ${issue.message}`
: "Corps invalide.",
);
}
const result = await createLead(db, parsed.data);
return context.json(result, 201);
});
};