61 lines
3.0 KiB
TypeScript
61 lines
3.0 KiB
TypeScript
import type { ActionFunctionArgs } from "react-router";
|
|
|
|
import { validatePortalCsrfToken } from "~/services/customer-portal/portal-csrf.server";
|
|
import { PortalComplaintError, submitPortalComplaint } from "~/services/customer-portal/portal-complaint.server";
|
|
import { portalNotFound, requirePortalSessionContext } from "~/services/customer-portal/portal-data.server";
|
|
|
|
const MAX_COMPLAINT_BYTES = 256 * 1024;
|
|
|
|
export async function action({ request, params }: ActionFunctionArgs): Promise<Response> {
|
|
if (request.method !== "POST" || !params.projectId || !params.formId) throw portalNotFound();
|
|
const contentLength = Number(request.headers.get("content-length") ?? "0");
|
|
if (Number.isFinite(contentLength) && contentLength > MAX_COMPLAINT_BYTES) return failure("customerPortal.complaint.errors.payloadTooLarge", 413);
|
|
const session = await requirePortalSessionContext(request, params.tenant);
|
|
const form = await request.formData();
|
|
await validatePortalCsrfToken(request, form);
|
|
let values: Record<string, string | number | boolean | readonly string[] | null>;
|
|
const rawPayload = form.get("payload");
|
|
try {
|
|
values = typeof rawPayload === "string" ? (JSON.parse(rawPayload) as typeof values) : complaintValuesFromForm(form);
|
|
} catch {
|
|
return failure("customerPortal.complaint.errors.invalidPayload", 400, session.responseHeaders);
|
|
}
|
|
try {
|
|
const created = await submitPortalComplaint({
|
|
identity: session.identity,
|
|
projectId: params.projectId,
|
|
formId: params.formId,
|
|
payload: {
|
|
values,
|
|
gdprAccepted: form.get("gdprAccepted") === "true" || form.get("gdprAccepted") === "on",
|
|
idempotencyKey: form.get("idempotencyKey")?.toString(),
|
|
},
|
|
});
|
|
return Response.json({ ok: true, ...created, messageKey: "customerPortal.complaint.success" }, { status: 201, headers: session.responseHeaders });
|
|
} catch (error) {
|
|
if (error instanceof PortalComplaintError) {
|
|
if (error.code === "PORTAL_FROZEN" || error.code === "PROJECT_ACCESS_DENIED" || error.code === "FORM_NOT_FOUND") throw portalNotFound();
|
|
return failure(`customerPortal.complaint.errors.${error.code}`, error.code === "SUBMISSION_FAILED" ? 503 : 422, session.responseHeaders);
|
|
}
|
|
return failure("customerPortal.complaint.errors.unavailable", 503, session.responseHeaders);
|
|
}
|
|
}
|
|
|
|
export async function loader(): Promise<never> {
|
|
throw portalNotFound();
|
|
}
|
|
|
|
function complaintValuesFromForm(form: FormData): Record<string, string | number | boolean | readonly string[] | null> {
|
|
const values: Record<string, string | number | boolean | readonly string[] | null> = {};
|
|
for (const [key, value] of form.entries()) {
|
|
if (["csrf", "gdprAccepted", "idempotencyKey", "payload"].includes(key) || value instanceof File) continue;
|
|
const all = form.getAll(key).filter((entry): entry is string => typeof entry === "string");
|
|
values[key] = all.length > 1 ? all : value;
|
|
}
|
|
return values;
|
|
}
|
|
|
|
function failure(errorKey: string, status: number, headers?: Headers): Response {
|
|
return Response.json({ ok: false, errorKey }, { status, headers });
|
|
}
|