Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | 1x 1x 1x 13x 13x 13x 13x 1x 12x 12x 1x 11x 11x 11x 9x 9x 2x 7x 9x 9x 2x 2x 1x 1x 8x 1x 7x 1x 6x 1x 5x 1x 4x 1x 3x 2x 2x 11x | // Public Landing-Page Lead Routes (API Key Protected)
//
// Leads from standalone campaign landing pages (e.g. /get-matched on the
// marketplace). Unlike /inquiries these carry no proId — the support team is
// emailed the details and matches a designer manually.
import { Hono } from "hono";
import type { Dal } from "../../dal";
import { success, handleError } from "../../lib/response";
import { ValidationError } from "../../lib/errors";
import { rateLimit } from "../../middleware/rate-limit.middleware";
import { fireInternalNotification } from "../../lib/internal-notifications";
import { parseIndianPhone } from "@interioring/utils/validation/phone";
import {
validateCustomerName,
customerNameErrorMessage,
normalizeCustomerName,
} from "@interioring/utils/validation/customer-name";
import { isValidEmail } from "@interioring/utils/validation/email";
type Env = {
Bindings: CloudflareBindings;
Variables: {
dal: Dal;
};
};
const landingLeads = new Hono<Env>();
// Same budget as the inquiry limiter: 5 per 10 minutes per IP in production.
const landingLeadRateLimit = rateLimit({
windowMs: 10 * 60 * 1000,
max: 5,
devMax: 1000,
message: "Too many submissions. Please try again in 10 minutes.",
});
// Submit a landing-page lead
landingLeads.post("/", landingLeadRateLimit, async (c) => {
try {
const dal = c.get("dal");
const body = await c.req.json();
if (!body.name) {
throw new ValidationError("name is required");
}
const nameError = validateCustomerName(String(body.name));
if (nameError) {
throw new ValidationError(customerNameErrorMessage(nameError));
}
const name = normalizeCustomerName(String(body.name));
// Contact rule: at least one of phone/email must be present and valid.
// A provided-but-invalid value is a hard error (never silently dropped) —
// otherwise a typo'd phone could produce an unreachable lead.
let phone: string | null = null;
if (body.phone) {
const parsed = parseIndianPhone(String(body.phone));
if (!parsed || parsed.type !== "mobile") {
throw new ValidationError(
"phone must be a valid 10-digit Indian mobile number",
);
}
phone = parsed.e164;
}
let email: string | null = null;
if (body.email) {
const trimmed = String(body.email).trim();
if (trimmed.length > 254 || !isValidEmail(trimmed)) {
throw new ValidationError("email must be a valid email address");
}
email = trimmed;
}
if (!phone && !email) {
throw new ValidationError(
"Provide at least one contact: a valid 10-digit mobile number or an email address",
);
}
// Length-cap free-text fields before persisting (same rationale as the
// inquiry route — unbounded payloads slow every subsequent read).
if (body.locality && String(body.locality).length > 200) {
throw new ValidationError("locality must be 200 characters or fewer");
}
if (body.notes && String(body.notes).length > 1000) {
throw new ValidationError("notes must be 1000 characters or fewer");
}
if (body.source && String(body.source).length > 100) {
throw new ValidationError("source must be 100 characters or fewer");
}
if (body.pagePath && String(body.pagePath).length > 500) {
throw new ValidationError("pagePath must be 500 characters or fewer");
}
const lead = await dal.landingLeads.create({
name,
phone,
email,
locality: body.locality ? String(body.locality).trim() : null,
notes: body.notes ? String(body.notes).trim() : null,
source: body.source ? String(body.source) : "landing",
pagePath: body.pagePath ? String(body.pagePath) : null,
});
// Best-effort email to support — never blocks or fails the response.
fireInternalNotification(c, dal, {
event: "landing_lead_internal",
leadId: lead.id,
name: lead.name,
phone: lead.phone,
email: lead.email,
locality: lead.locality,
notes: lead.notes,
source: lead.source,
});
return success(c, { id: lead.id }, 201);
} catch (error) {
return handleError(c, error);
}
});
export default landingLeads;
|