34 lines
1.1 KiB
TypeScript
34 lines
1.1 KiB
TypeScript
import type { FastifyInstance } from 'fastify';
|
|||
|
|
import { z } from 'zod';
|
||
|
|
import { getDb, contactMessages } from '@lawdesk/db';
|
||
|
|
import { sendEmail, contactAckEmail } from '../lib/email';
|
||
|
|
|
||
|
|
const contactBody = z.object({
|
||
|
|
fullName: z.string().min(1).max(120).trim(),
|
||
|
|
email: z.string().email().max(254).toLowerCase().trim(),
|
||
|
|
message: z.string().min(1).max(5000).trim(),
|
||
|
|
});
|
||
|
|
|
||
|
|
export async function contactRoutes(app: FastifyInstance) {
|
||
|
|
app.post(
|
||
|
|
'/api/contact',
|
||
|
|
{ config: { rateLimit: { max: 5, timeWindow: '10 minutes' } } },
|
||
|
|
async (req, reply) => {
|
||
|
|
const parsed = contactBody.safeParse(req.body);
|
||
|
|
if (!parsed.success) return reply.code(400).send({ error: 'invalid_input' });
|
||
|
|
const body = parsed.data;
|
||
|
|
await getDb().insert(contactMessages).values({
|
||
|
|
fullName: body.fullName,
|
||
|
|
email: body.email,
|
||
|
|
message: body.message,
|
||
|
|
ip: req.ip ?? null,
|
||
|
|
});
|
||
|
|
const tpl = contactAckEmail(body.fullName);
|
||
|
|
sendEmail({ to: body.email, ...tpl }).catch((err) =>
|
||
|
|
app.log.warn({ err }, 'contact ack email failed'),
|
||
|
|
);
|
||
|
|
return reply.code(201).send({ ok: true });
|
||
|
|
},
|
||
|
|
);
|
||
|
|
}
|