Files
libredesk/internal/csat/queries.sql
T
Stanislaw 7388ab9c65 fix: prevent duplicate CSAT submissions under concurrency
The pre-check on response_timestamp was a read-then-write race: concurrent
submits all passed it and all wrote. Guard the UPDATE with
response_timestamp IS NULL and treat a zero-row update as already submitted.
2026-09-14 15:03:17 +02:00

27 lines
538 B
SQL

-- name: insert
INSERT INTO csat_responses (conversation_id)
SELECT $1
WHERE NOT EXISTS (SELECT 1 FROM csat_responses WHERE conversation_id = $1)
RETURNING uuid;
-- name: get
SELECT id,
uuid,
created_at,
updated_at,
conversation_id,
rating,
feedback,
meta,
response_timestamp
FROM csat_responses
WHERE uuid = $1;
-- name: update
UPDATE csat_responses
SET rating = $2,
feedback = $3,
meta = COALESCE($4::jsonb, '{}'),
response_timestamp = NOW()
WHERE uuid = $1 AND response_timestamp IS NULL;