Answer a retried feedback submission with the one already saved
Test and deploy / test-and-deploy (pull_request) Successful in 46s

When a submission saved but its response never reached the phone, the
form showed an error, the visitor tapped "Feedback absenden" again, and a
second identical row was stored (seen twice in production: 82/83, 87/88).

The form now sends a clientToken, one UUID per visit, on every attempt.
A token that is already stored returns the original submissionId and
saves nothing; the lookup runs before the eligibility check, so a retry
still succeeds if the feedback window closed in between. Two concurrent
retries are settled by a UNIQUE index on submissions.client_token: the
loser gets ER_DUP_ENTRY, rolls back and returns the winner's id. A missing
or non-UUID token behaves as before, so cached copies of the old form
still submit.

Migration 003 must be applied before this is deployed - the INSERT now
writes client_token.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
PaddyandClaude Opus 5.5 committed 2026-11-16 17:48:33 +01:00
1 parent c9c40af382
commit f3f4ca2c9f
5 files changed
+202 -11

No files matched your search

@@ -56,6 +56,10 @@
* website:
* type: string
* description: Honeypot field. Must stay empty; a real visitor never fills it in.
* clientToken:
* type: string
* format: uuid
* description: Generated once per form and resent on every retry. A retry whose token is already stored returns the original submissionId instead of saving a duplicate. Optional; a value that isn't a UUID is ignored.
* SubmissionResponse:
* type: object
* properties:
@@ -95,4 +99,5 @@ export interface SubmissionRequestBody {
guestBook?: GuestBookInput;
newsletter?: NewsletterInput;
website?: string;
clientToken?: string;
}
@@ -11,6 +11,8 @@ const CONSENT_TEXT_VERSION = '2026-08-02';
const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const CLIENT_TOKEN_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
// A real setlist tops out around a few dozen songs and a handful of
// questions, so a legitimate submission never comes close to this. Caps
// total generated rows regardless of how large the client's answers/ratings
@@ -136,6 +138,27 @@ export const validateNewsletter = (input?: NewsletterInput): ValidatedNewsletter
return {firstName, lastName, email};
};
/**
* The form generates one UUID per visit and resends it on every retry, so a
* request whose response was lost (the row saved, but the phone never heard
* back) can be told apart from a genuinely new submission. Anything that
* isn't a UUID is treated as "no token" rather than rejected - an old cached
* copy of the form must still be able to submit.
*/
export const normalizeClientToken = (token: unknown): string | null => {
return typeof token === 'string' && CLIENT_TOKEN_RE.test(token) ? token.toLowerCase() : null;
};
const findSubmissionIdByClientToken = async (clientToken: string): Promise<number | null> => {
const conn = await NachklangFeedbackDB.getConnection();
try {
const rows = await conn.query('SELECT submission_id FROM submissions WHERE client_token = ?', [clientToken]);
return rows.length > 0 ? rows[0].submission_id : null;
} finally {
await conn.end();
}
};
export type SubmitResult =
| { status: 'OK'; submissionId: number; newsletterDropped: boolean }
| { status: 'NOT_FOUND' }
@@ -146,9 +169,29 @@ export type SubmitResult =
* Validates and persists one feedback submission. Re-checks event
* eligibility (the window may have closed between page load and submit),
* validates every answer against the event's live questions/songs, then
* inserts everything in a single transaction.
* inserts everything in a single transaction. A retry carrying an already
* stored client token gets the original submission back instead of a copy.
*/
export const submitFeedback = async (slug: string, body: SubmissionRequestBody, ipHash: string | null): Promise<SubmitResult> => {
const clientToken = normalizeClientToken(body.clientToken);
const newsletter = validateNewsletter(body.newsletter);
// body.newsletter is only sent at all when the visitor had the opt-in
// checkbox on (see FeedbackForm.tsx), so a present-but-invalid block
// (e.g. a mistyped email) is distinguishable from "didn't opt in" - the
// rest of the submission still saves, but the client can tell the
// visitor their newsletter signup specifically didn't go through.
const newsletterDropped = !!body.newsletter && !newsletter;
// Checked before eligibility, so a retry still succeeds if the feedback
// window closed in the seconds after the original saved.
if (clientToken) {
const existingId = await findSubmissionIdByClientToken(clientToken);
if (existingId !== null) {
logger.info('Feedback submission retry answered with the stored submission', {submissionId: existingId});
return {status: 'OK', submissionId: existingId, newsletterDropped};
}
}
const lookup = await getEventConfigBySlug(slug);
if (lookup.status === 'NOT_FOUND') return {status: 'NOT_FOUND'};
if (lookup.status === 'CLOSED') return {status: 'CLOSED'};
@@ -159,13 +202,6 @@ export const submitFeedback = async (slug: string, body: SubmissionRequestBody,
const answerRows = validateAnswers(body.answers || [], questionsById, songTitleById);
const guestBook = validateGuestBook(body.guestBook);
const newsletter = validateNewsletter(body.newsletter);
// body.newsletter is only sent at all when the visitor had the opt-in
// checkbox on (see FeedbackForm.tsx), so a present-but-invalid block
// (e.g. a mistyped email) is distinguishable from "didn't opt in" - the
// rest of the submission still saves, but the client can tell the
// visitor their newsletter signup specifically didn't go through.
const newsletterDropped = !!body.newsletter && !newsletter;
if (answerRows.length === 0 && !guestBook && !newsletter) {
return {status: 'EMPTY'};
@@ -175,8 +211,8 @@ export const submitFeedback = async (slug: string, body: SubmissionRequestBody,
try {
await conn.beginTransaction();
const subQuery = 'INSERT INTO submissions (event_id, ip_hash, has_guestbook, has_newsletter) VALUES (?,?,?,?) RETURNING submission_id';
const subRes = await conn.query(subQuery, [eventId, ipHash, guestBook ? 1 : 0, newsletter ? 1 : 0]);
const subQuery = 'INSERT INTO submissions (event_id, ip_hash, has_guestbook, has_newsletter, client_token) VALUES (?,?,?,?,?) RETURNING submission_id';
const subRes = await conn.query(subQuery, [eventId, ipHash, guestBook ? 1 : 0, newsletter ? 1 : 0, clientToken]);
const submissionId = subRes[0].submission_id;
for (const row of answerRows) {
@@ -220,8 +256,16 @@ export const submitFeedback = async (slug: string, body: SubmissionRequestBody,
}
return {status: 'OK', submissionId, newsletterDropped};
} catch (err) {
} catch (err: any) {
await conn.rollback();
// Two copies of the same retry both got past the lookup above; the
// unique index on client_token let only one of them commit.
if (clientToken && err?.code === 'ER_DUP_ENTRY') {
const existingId = await findSubmissionIdByClientToken(clientToken);
if (existingId !== null) {
return {status: 'OK', submissionId: existingId, newsletterDropped};
}
}
throw err;
} finally {
await conn.end();