Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+219
-266
@@ -19,7 +19,7 @@
|
||||
* 6. Validate payload against the stored form definition.
|
||||
* 7. Persist a {@link FormSubmission} row (status=received) AND publish a
|
||||
* `form.submission.process` outbox event so the worker can route the
|
||||
* payload to CRM-contact / entity-row / webhook targets.
|
||||
* payload to CRM-contact / webhook targets.
|
||||
*
|
||||
* Response shape: `{ ok: true }` on success, `{ ok: false, error }` for the
|
||||
* client renderer. Honeypot-triggered + origin-blocked requests do NOT echo
|
||||
@@ -34,6 +34,7 @@ import {
|
||||
isHoneypotTriggered,
|
||||
matchAllowedOrigin,
|
||||
resolveSubmissionTarget,
|
||||
validateFormValues,
|
||||
type FormDefinition,
|
||||
} from "@hivegrid/form-widget";
|
||||
import { publishOutboxEvent, type OutboxPublisherTransaction } from "@hivegrid/db/outbox";
|
||||
@@ -46,6 +47,9 @@ import { createScopedLogger } from "~/utils/logger.server";
|
||||
import { evaluateFlagForContext } from "~/utils/services/.server/featureFlagEvaluator.server";
|
||||
import { getSecret, resolveCaptchaConfig, verifyCaptchaToken } from "@hivegrid/captcha";
|
||||
import { getTrustedClientIp } from "~/utils/server/IpUtils";
|
||||
import { getPseudonymousAuthBucketKey } from "~/services/auth/auth-rate-limit-key.server";
|
||||
import { runWithRlsContext } from "@hivegrid/db/rls-context.server";
|
||||
import { resolvePublicFormKeyRlsIdentity } from "@hivegrid/db/rls-resolvers.server";
|
||||
|
||||
const log = createScopedLogger("form-widget.submit");
|
||||
|
||||
@@ -53,12 +57,6 @@ const RATE_LIMIT_WINDOW_SECONDS = 60;
|
||||
const RATE_LIMIT_PER_IP = 10;
|
||||
const RATE_LIMIT_PER_ORIGIN = 100;
|
||||
|
||||
// On a rate-limit store (Redis) outage, production fails closed so an attacker
|
||||
// cannot flood this unauthenticated endpoint by knocking the store offline.
|
||||
// Non-production fails open so a local Redis blip doesn't drop submissions.
|
||||
function shouldFailClosedOnRateLimitInfraFailure(): boolean {
|
||||
return process.env.NODE_ENV === "production";
|
||||
}
|
||||
const PAYLOAD_BYTE_LIMIT = 256 * 1024; // 256 KiB hard cap on the JSON payload
|
||||
const REQUEST_BYTE_LIMIT = 30 * 1024 * 1024;
|
||||
|
||||
@@ -171,53 +169,6 @@ function clientIp(request: Request): string | null {
|
||||
return ip === "unknown" ? null : ip;
|
||||
}
|
||||
|
||||
function validateAgainstDefinition(
|
||||
values: Record<string, unknown>,
|
||||
def: FormDefinition
|
||||
): { code: EmbedSubmitErrorCode; params: Record<string, string | number | boolean> } | null {
|
||||
for (const field of def.fields) {
|
||||
const raw = values[field.key];
|
||||
if (field.type === "honeypot") continue;
|
||||
const params = { fieldKey: field.key, fieldLabel: field.label };
|
||||
const filled = Array.isArray(raw) ? raw.length > 0 : raw !== undefined && raw !== null && raw !== "" && raw !== false;
|
||||
if (field.required && !filled) {
|
||||
return { code: "FIELD_REQUIRED", params };
|
||||
}
|
||||
if (!filled) continue;
|
||||
if (typeof raw === "string") {
|
||||
const maxLength = field.validationRules?.maxLength ?? field.maxLength;
|
||||
const minLength = field.validationRules?.minLength ?? field.minLength;
|
||||
if (maxLength !== undefined && raw.length > maxLength) {
|
||||
return { code: "FIELD_TOO_LONG", params: { ...params, maxLength } };
|
||||
}
|
||||
if (minLength !== undefined && raw.length < minLength) {
|
||||
return { code: "FIELD_TOO_SHORT", params: { ...params, minLength } };
|
||||
}
|
||||
if (field.type === "email" && filled && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(raw)) {
|
||||
return { code: "FIELD_INVALID_EMAIL", params };
|
||||
}
|
||||
if (field.type === "phone" && !/^[+()\d\s./-]{3,40}$/u.test(raw)) {
|
||||
return { code: "INVALID_PAYLOAD_SHAPE", params };
|
||||
}
|
||||
}
|
||||
if (field.type === "number") {
|
||||
const numberValue = typeof raw === "number" ? raw : typeof raw === "string" ? Number(raw) : Number.NaN;
|
||||
if (!Number.isFinite(numberValue)) return { code: "INVALID_PAYLOAD_SHAPE", params };
|
||||
if (field.validationRules?.min !== undefined && numberValue < field.validationRules.min) return { code: "INVALID_PAYLOAD_SHAPE", params };
|
||||
if (field.validationRules?.max !== undefined && numberValue > field.validationRules.max) return { code: "INVALID_PAYLOAD_SHAPE", params };
|
||||
}
|
||||
if (field.type === "multiselect") {
|
||||
if (!Array.isArray(raw)) return { code: "INVALID_PAYLOAD_SHAPE", params };
|
||||
if (field.validationRules?.minSelected !== undefined && raw.length < field.validationRules.minSelected) return { code: "FIELD_REQUIRED", params };
|
||||
if (field.validationRules?.maxSelected !== undefined && raw.length > field.validationRules.maxSelected) return { code: "INVALID_PAYLOAD_SHAPE", params };
|
||||
}
|
||||
if (field.type === "boolean" && typeof raw !== "boolean") {
|
||||
return { code: "INVALID_PAYLOAD_SHAPE", params };
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export async function action({ params, request }: { params: { formKey?: string }; request: Request }): Promise<Response> {
|
||||
const { t } = await getTranslations(request);
|
||||
if (request.method !== "POST") {
|
||||
@@ -231,243 +182,245 @@ export async function action({ params, request }: { params: { formKey?: string }
|
||||
return errorResponse(t, "PAYLOAD_TOO_LARGE", 413);
|
||||
}
|
||||
|
||||
const form = await db.form.findUnique({
|
||||
where: { key: formKey },
|
||||
select: {
|
||||
id: true,
|
||||
tenantId: true,
|
||||
isEnabled: true,
|
||||
definition: true,
|
||||
allowedOrigins: true,
|
||||
targetType: true,
|
||||
targetEntityId: true,
|
||||
fieldMapping: true,
|
||||
config: true,
|
||||
webhookUrl: true,
|
||||
notifyEmails: true,
|
||||
},
|
||||
});
|
||||
if (!form || !form.isEnabled) {
|
||||
return errorResponse(t, "FORM_NOT_FOUND", 404);
|
||||
}
|
||||
const identity = await resolvePublicFormKeyRlsIdentity(formKey);
|
||||
if (!identity) return errorResponse(t, "FORM_NOT_FOUND", 404);
|
||||
|
||||
// 2. Origin allowlist
|
||||
const origin = request.headers.get("origin");
|
||||
if (!matchAllowedOrigin(origin, form.allowedOrigins)) {
|
||||
log.info({ op: "blocked.origin", formId: form.id, origin }, "Submission origin not allowlisted");
|
||||
return errorResponse(t, "ORIGIN_NOT_ALLOWED", 403);
|
||||
}
|
||||
|
||||
// 1. Parse body
|
||||
let payloadJson: unknown;
|
||||
let formBody: FormData;
|
||||
try {
|
||||
formBody = await request.formData();
|
||||
const raw = formBody.get("__payload");
|
||||
if (typeof raw !== "string") throw new Error("payload missing");
|
||||
if (raw.length > PAYLOAD_BYTE_LIMIT) throw new Error("payload too large");
|
||||
payloadJson = JSON.parse(raw);
|
||||
} catch (err) {
|
||||
return errorResponse(t, "INVALID_PAYLOAD", 400);
|
||||
}
|
||||
|
||||
const parsed = FormSubmissionPayloadSchema.safeParse(payloadJson);
|
||||
if (!parsed.success) {
|
||||
return errorResponse(t, "INVALID_PAYLOAD_SHAPE", 400);
|
||||
}
|
||||
const { values, turnstileToken, captchaToken, gdprAccepted, idempotencyKey } = parsed.data;
|
||||
|
||||
// 3. Honeypot — silent 204 to starve bot signal
|
||||
if (isHoneypotTriggered(values)) {
|
||||
log.info({ op: "blocked.honeypot", formId: form.id }, "Honeypot triggered — silent drop");
|
||||
return new Response(null, { status: 204 });
|
||||
}
|
||||
|
||||
// 4. Rate-limit (per-IP and per-origin)
|
||||
const ip = clientIp(request);
|
||||
try {
|
||||
const store = getSharedStore();
|
||||
const limiter = createSlidingWindowRateLimiter({
|
||||
store,
|
||||
max: RATE_LIMIT_PER_IP,
|
||||
windowSeconds: RATE_LIMIT_WINDOW_SECONDS,
|
||||
prefix: "form-widget:rl",
|
||||
return runWithRlsContext({ tenantId: identity.tenantId }, async () => {
|
||||
const form = await db.form.findFirst({
|
||||
where: { id: identity.resourceId, tenantId: identity.tenantId, key: formKey, isEnabled: true, subtype: { not: "portal_form" } },
|
||||
select: {
|
||||
id: true,
|
||||
tenantId: true,
|
||||
isEnabled: true,
|
||||
subtype: true,
|
||||
definition: true,
|
||||
allowedOrigins: true,
|
||||
targetType: true,
|
||||
targetEntityId: true,
|
||||
fieldMapping: true,
|
||||
config: true,
|
||||
webhookUrl: true,
|
||||
notifyEmails: true,
|
||||
},
|
||||
});
|
||||
if (ip) {
|
||||
const decision = await limiter.consume(`ip:${ip}`);
|
||||
if (!decision.allowed) {
|
||||
return errorResponse(t, "RATE_LIMIT_IP", 429);
|
||||
}
|
||||
if (!form || form.subtype === "portal_form") {
|
||||
return errorResponse(t, "FORM_NOT_FOUND", 404);
|
||||
}
|
||||
if (origin) {
|
||||
const originLimiter = createSlidingWindowRateLimiter({
|
||||
|
||||
// 2. Origin allowlist
|
||||
const origin = request.headers.get("origin");
|
||||
if (!matchAllowedOrigin(origin, form.allowedOrigins)) {
|
||||
log.info({ op: "blocked.origin", formId: form.id, origin }, "Submission origin not allowlisted");
|
||||
return errorResponse(t, "ORIGIN_NOT_ALLOWED", 403);
|
||||
}
|
||||
|
||||
// Reserve both public buckets before parsing a potentially multipart 30 MiB
|
||||
// request. Bucket identifiers are HMAC-pseudonymized before reaching Redis.
|
||||
const ip = clientIp(request);
|
||||
try {
|
||||
const store = getSharedStore();
|
||||
const limiter = createSlidingWindowRateLimiter({
|
||||
store,
|
||||
max: RATE_LIMIT_PER_ORIGIN,
|
||||
max: RATE_LIMIT_PER_IP,
|
||||
windowSeconds: RATE_LIMIT_WINDOW_SECONDS,
|
||||
prefix: "form-widget:rl-origin",
|
||||
prefix: "form-widget:rl",
|
||||
});
|
||||
const decision = await originLimiter.consume(`origin:${origin}`);
|
||||
if (!decision.allowed) {
|
||||
return errorResponse(t, "RATE_LIMIT_ORIGIN", 429);
|
||||
if (ip) {
|
||||
const decision = await limiter.consume(getPseudonymousAuthBucketKey({ request, action: "form-submit", scope: "client" }));
|
||||
if (!decision.allowed) {
|
||||
return errorResponse(t, "RATE_LIMIT_IP", 429);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
log.warn({ op: "rate-limit.error", err: err instanceof Error ? err.message : String(err) }, "Rate-limit check failed");
|
||||
if (shouldFailClosedOnRateLimitInfraFailure()) {
|
||||
// Production: refuse rather than allow unbounded submissions on this
|
||||
// unauthenticated endpoint while the rate-limit store is unavailable.
|
||||
if (origin) {
|
||||
const originLimiter = createSlidingWindowRateLimiter({
|
||||
store,
|
||||
max: RATE_LIMIT_PER_ORIGIN,
|
||||
windowSeconds: RATE_LIMIT_WINDOW_SECONDS,
|
||||
prefix: "form-widget:rl-origin",
|
||||
});
|
||||
const decision = await originLimiter.consume(getPseudonymousAuthBucketKey({ request, action: "form-submit", identifier: origin, scope: "identifier" }));
|
||||
if (!decision.allowed) {
|
||||
return errorResponse(t, "RATE_LIMIT_ORIGIN", 429);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
log.warn({ op: "rate-limit.error", errorName: error instanceof Error ? error.name : "UnknownError" }, "Rate-limit check failed");
|
||||
return errorResponse(t, "RATE_LIMIT_IP", 503);
|
||||
}
|
||||
// Non-production: fail open so a local Redis blip doesn't drop submissions.
|
||||
}
|
||||
|
||||
// 5. Validate against form definition
|
||||
const def = FormDefinitionSchema.safeParse(form.definition);
|
||||
if (!def.success) {
|
||||
log.error({ op: "form.misconfigured", formId: form.id }, "Stored definition failed schema");
|
||||
return errorResponse(t, "FORM_MISCONFIGURED", 500);
|
||||
}
|
||||
if (gdprAccepted !== true) {
|
||||
return errorResponse(t, "FIELD_REQUIRED", 400, { fieldKey: "__gdprAccepted", fieldLabel: def.data.gdprConsentLabel });
|
||||
}
|
||||
const visibleFields = filterVisibleFields(def.data.fields, values);
|
||||
const visibleDef: FormDefinition = { ...def.data, fields: visibleFields };
|
||||
const valueError = validateAgainstDefinition(values, visibleDef);
|
||||
if (valueError) {
|
||||
return errorResponse(t, valueError.code, 400, valueError.params);
|
||||
}
|
||||
|
||||
// 6. Optional captcha gate (self-hosted provider)
|
||||
const flag = await evaluateFlagForContext("form-widget-turnstile", {
|
||||
flagName: "form-widget-turnstile",
|
||||
tenantId: form.tenantId,
|
||||
userId: null,
|
||||
});
|
||||
const cfg = (form.config ?? {}) as { turnstileEnabled?: boolean; captchaEnabled?: boolean };
|
||||
const captchaEnabled = cfg.captchaEnabled === true || cfg.turnstileEnabled === true || def.data.captchaEnabled === true;
|
||||
if (flag.enabled && captchaEnabled) {
|
||||
const captcha = resolveCaptchaConfig(process.env);
|
||||
const secret = getSecret(captcha, "forms");
|
||||
const ok = await verifyCaptchaToken({
|
||||
token: captchaToken ?? turnstileToken,
|
||||
sitekey: captcha.sitekeys.forms,
|
||||
secret,
|
||||
instanceUrl: captcha.instanceUrl,
|
||||
provider: captcha.provider,
|
||||
failOpen: captcha.failOpen,
|
||||
logger: log,
|
||||
});
|
||||
if (!ok) {
|
||||
return errorResponse(t, "TURNSTILE_FAILED", 403);
|
||||
// 1. Parse body
|
||||
let payloadJson: unknown;
|
||||
let formBody: FormData;
|
||||
try {
|
||||
formBody = await request.formData();
|
||||
const raw = formBody.get("__payload");
|
||||
if (typeof raw !== "string") throw new Error("payload missing");
|
||||
if (raw.length > PAYLOAD_BYTE_LIMIT) throw new Error("payload too large");
|
||||
payloadJson = JSON.parse(raw);
|
||||
} catch {
|
||||
return errorResponse(t, "INVALID_PAYLOAD", 400);
|
||||
}
|
||||
}
|
||||
|
||||
// 7. Persist submission + publish outbox event atomically.
|
||||
//
|
||||
// The outbox row is committed in the same transaction as the FormSubmission
|
||||
// insert, so the worker (apps/worker/src/jobs/form-submission-route.ts) is
|
||||
// guaranteed to see a routable row when it claims the event. For
|
||||
// `targetType=generic` the worker no-ops; for `crm-contact` / `entity-row`
|
||||
// it materializes the actual entity write via the existing CRM / Rows APIs.
|
||||
const target = resolveSubmissionTarget({
|
||||
targetType: form.targetType,
|
||||
targetEntityId: form.targetEntityId,
|
||||
fieldMapping: (form.fieldMapping ?? {}) as Record<string, string>,
|
||||
});
|
||||
|
||||
if (idempotencyKey) {
|
||||
const existing = await db.formSubmission.findUnique({
|
||||
where: { formId_idempotencyKey: { formId: form.id, idempotencyKey } },
|
||||
select: { id: true },
|
||||
});
|
||||
if (existing) {
|
||||
log.info({ op: "submission.duplicate", formId: form.id, idempotencyKey }, "Duplicate form submission ignored");
|
||||
return Response.json({ ok: true });
|
||||
const parsed = FormSubmissionPayloadSchema.safeParse(payloadJson);
|
||||
if (!parsed.success) {
|
||||
return errorResponse(t, "INVALID_PAYLOAD_SHAPE", 400);
|
||||
}
|
||||
}
|
||||
const { values, turnstileToken, captchaToken, gdprAccepted, idempotencyKey } = parsed.data;
|
||||
|
||||
let preparedFiles: PreparedFormFile[];
|
||||
try {
|
||||
preparedFiles = await prepareUploadedFiles({ tenantId: form.tenantId, formBody, fields: visibleFields });
|
||||
} catch (error) {
|
||||
log.warn({ op: "submission.file-rejected", formId: form.id, error: error instanceof Error ? error.message : String(error) }, "Submitted file rejected");
|
||||
return errorResponse(t, "INVALID_PAYLOAD_SHAPE", 400);
|
||||
}
|
||||
// 3. Honeypot — silent 204 to starve bot signal
|
||||
if (isHoneypotTriggered(values)) {
|
||||
log.info({ op: "blocked.honeypot", formId: form.id }, "Honeypot triggered — silent drop");
|
||||
return new Response(null, { status: 204 });
|
||||
}
|
||||
|
||||
let submission: { id: string };
|
||||
try {
|
||||
submission = await db.$transaction(async (tx) => {
|
||||
const created = await tx.formSubmission.create({
|
||||
data: {
|
||||
formId: form.id,
|
||||
tenantId: form.tenantId,
|
||||
payload: { values } as object,
|
||||
sourceOrigin: origin ?? null,
|
||||
ipHash: hashIp(ip),
|
||||
userAgent: request.headers.get("user-agent")?.slice(0, 500) ?? null,
|
||||
status: "received",
|
||||
result: { target: target.type } as object,
|
||||
gdprAccepted: true,
|
||||
idempotencyKey: idempotencyKey ?? null,
|
||||
},
|
||||
// 5. Validate against form definition
|
||||
const def = FormDefinitionSchema.safeParse(form.definition);
|
||||
if (!def.success) {
|
||||
log.error({ op: "form.misconfigured", formId: form.id }, "Stored definition failed schema");
|
||||
return errorResponse(t, "FORM_MISCONFIGURED", 500);
|
||||
}
|
||||
if (gdprAccepted !== true) {
|
||||
return errorResponse(t, "FIELD_REQUIRED", 400, { fieldKey: "__gdprAccepted", fieldLabel: def.data.gdprConsentLabel });
|
||||
}
|
||||
const visibleFields = filterVisibleFields(def.data.fields, values);
|
||||
const visibleDef: FormDefinition = { ...def.data, fields: visibleFields };
|
||||
const valueError = validateFormValues(values, visibleDef);
|
||||
if (valueError) {
|
||||
return errorResponse(t, valueError.code, 400, valueError.params);
|
||||
}
|
||||
|
||||
// 6. Optional captcha gate (self-hosted provider)
|
||||
const flag = await evaluateFlagForContext("form-widget-turnstile", {
|
||||
flagName: "form-widget-turnstile",
|
||||
tenantId: form.tenantId,
|
||||
userId: null,
|
||||
});
|
||||
const cfg = (form.config ?? {}) as { turnstileEnabled?: boolean; captchaEnabled?: boolean };
|
||||
const captchaEnabled = cfg.captchaEnabled === true || cfg.turnstileEnabled === true || def.data.captchaEnabled === true;
|
||||
if (flag.enabled && captchaEnabled) {
|
||||
const captcha = resolveCaptchaConfig(process.env);
|
||||
const secret = getSecret(captcha, "forms");
|
||||
const ok = await verifyCaptchaToken({
|
||||
token: captchaToken ?? turnstileToken,
|
||||
sitekey: captcha.sitekeys.forms,
|
||||
secret,
|
||||
instanceUrl: captcha.instanceUrl,
|
||||
provider: captcha.provider,
|
||||
failOpen: captcha.failOpen,
|
||||
logger: log,
|
||||
});
|
||||
if (!ok) {
|
||||
return errorResponse(t, "TURNSTILE_FAILED", 403);
|
||||
}
|
||||
}
|
||||
|
||||
// 7. Persist submission + publish outbox event atomically.
|
||||
//
|
||||
// The outbox row is committed in the same transaction as the FormSubmission
|
||||
// insert, so the worker (apps/worker/src/jobs/form-submission-route.ts) is
|
||||
// guaranteed to see a routable row when it claims the event. For
|
||||
// `targetType=generic` the worker no-ops; for `crm-contact`
|
||||
// it materializes the typed CRM write in the worker.
|
||||
const target = resolveSubmissionTarget({
|
||||
targetType: form.targetType,
|
||||
targetEntityId: form.targetEntityId,
|
||||
fieldMapping: (form.fieldMapping ?? {}) as Record<string, string>,
|
||||
});
|
||||
|
||||
if (idempotencyKey) {
|
||||
const existing = await db.formSubmission.findUnique({
|
||||
where: { formId_idempotencyKey: { formId: form.id, idempotencyKey } },
|
||||
select: { id: true },
|
||||
});
|
||||
if (existing) {
|
||||
log.info({ op: "submission.duplicate", formId: form.id, idempotencyKey }, "Duplicate form submission ignored");
|
||||
return Response.json({ ok: true });
|
||||
}
|
||||
}
|
||||
|
||||
if (preparedFiles.length > 0) {
|
||||
await tx.formFile.createMany({
|
||||
data: preparedFiles.map((file) => ({
|
||||
let preparedFiles: PreparedFormFile[];
|
||||
try {
|
||||
preparedFiles = await prepareUploadedFiles({ tenantId: form.tenantId, formBody, fields: visibleFields });
|
||||
} catch (error) {
|
||||
log.warn({ op: "submission.file-rejected", formId: form.id, error: error instanceof Error ? error.message : String(error) }, "Submitted file rejected");
|
||||
return errorResponse(t, "INVALID_PAYLOAD_SHAPE", 400);
|
||||
}
|
||||
|
||||
let submission: { id: string };
|
||||
try {
|
||||
submission = await db.$transaction(async (tx) => {
|
||||
const created = await tx.formSubmission.create({
|
||||
data: {
|
||||
formId: form.id,
|
||||
tenantId: form.tenantId,
|
||||
submissionId: created.id,
|
||||
fieldKey: file.fieldKey,
|
||||
fileId: file.fileId,
|
||||
kind: file.kind,
|
||||
})),
|
||||
payload: { values } as object,
|
||||
sourceOrigin: origin ?? null,
|
||||
ipHash: hashIp(ip),
|
||||
userAgent: request.headers.get("user-agent")?.slice(0, 500) ?? null,
|
||||
status: "received",
|
||||
result: { target: target.type } as object,
|
||||
gdprAccepted: true,
|
||||
idempotencyKey: idempotencyKey ?? null,
|
||||
},
|
||||
select: { id: true },
|
||||
});
|
||||
await tx.fileEntityLink.createMany({
|
||||
data: preparedFiles.map((file) => ({
|
||||
tenantId: form.tenantId,
|
||||
createdByUserId: "system:form",
|
||||
fileId: file.fileId,
|
||||
entityType: "form_submission",
|
||||
entityId: created.id,
|
||||
linkRole: file.fieldKey,
|
||||
})),
|
||||
skipDuplicates: true,
|
||||
|
||||
if (preparedFiles.length > 0) {
|
||||
await tx.formFile.createMany({
|
||||
data: preparedFiles.map((file) => ({
|
||||
formId: form.id,
|
||||
tenantId: form.tenantId,
|
||||
submissionId: created.id,
|
||||
fieldKey: file.fieldKey,
|
||||
fileId: file.fileId,
|
||||
kind: file.kind,
|
||||
})),
|
||||
});
|
||||
await tx.fileEntityLink.createMany({
|
||||
data: preparedFiles.map((file) => ({
|
||||
tenantId: form.tenantId,
|
||||
createdByUserId: "system:form",
|
||||
fileId: file.fileId,
|
||||
entityType: "form_submission",
|
||||
entityId: created.id,
|
||||
linkRole: file.fieldKey,
|
||||
})),
|
||||
skipDuplicates: true,
|
||||
});
|
||||
}
|
||||
|
||||
const outboxPayload = {
|
||||
submissionId: created.id,
|
||||
formId: form.id,
|
||||
tenantId: form.tenantId,
|
||||
targetType: target.type,
|
||||
targetEntityId: target.entityId,
|
||||
fieldMapping: { ...target.fieldMapping },
|
||||
webhookUrl: form.webhookUrl ?? null,
|
||||
notifyEmails: form.notifyEmails,
|
||||
values,
|
||||
files: preparedFiles,
|
||||
} as unknown as Parameters<typeof publishOutboxEvent>[1]["payload"];
|
||||
|
||||
await publishOutboxEvent(tx as unknown as OutboxPublisherTransaction, {
|
||||
tenantId: form.tenantId,
|
||||
type: "form.submission.process",
|
||||
payload: outboxPayload,
|
||||
dispatchKey: `form-submission:${created.id}`,
|
||||
});
|
||||
}
|
||||
|
||||
const outboxPayload = {
|
||||
submissionId: created.id,
|
||||
formId: form.id,
|
||||
tenantId: form.tenantId,
|
||||
targetType: target.type,
|
||||
targetEntityId: target.entityId,
|
||||
fieldMapping: { ...target.fieldMapping },
|
||||
webhookUrl: form.webhookUrl ?? null,
|
||||
notifyEmails: form.notifyEmails,
|
||||
values,
|
||||
files: preparedFiles,
|
||||
} as unknown as Parameters<typeof publishOutboxEvent>[1]["payload"];
|
||||
|
||||
await publishOutboxEvent(tx as unknown as OutboxPublisherTransaction, {
|
||||
tenantId: form.tenantId,
|
||||
type: "form.submission.process",
|
||||
payload: outboxPayload,
|
||||
dispatchKey: `form-submission:${created.id}`,
|
||||
return created;
|
||||
});
|
||||
|
||||
return created;
|
||||
});
|
||||
} catch (error) {
|
||||
if (idempotencyKey && isUniqueConstraintError(error)) {
|
||||
log.info({ op: "submission.duplicate", formId: form.id, idempotencyKey }, "Duplicate form submission ignored");
|
||||
return Response.json({ ok: true });
|
||||
} catch (error) {
|
||||
if (idempotencyKey && isUniqueConstraintError(error)) {
|
||||
log.info({ op: "submission.duplicate", formId: form.id, idempotencyKey }, "Duplicate form submission ignored");
|
||||
return Response.json({ ok: true });
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
|
||||
log.info({ op: "submission.received", formId: form.id, submissionId: submission.id, target: target.type }, "Submission accepted");
|
||||
log.info({ op: "submission.received", formId: form.id, submissionId: submission.id, target: target.type }, "Submission accepted");
|
||||
|
||||
return Response.json({ ok: true });
|
||||
return Response.json({ ok: true });
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user