mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 06:11:11 -05:00
Connect to Lovable Cloud
This commit is contained in:
@@ -249,14 +249,36 @@ const handler = async (req: Request) => {
|
||||
);
|
||||
}
|
||||
|
||||
// STEP 6: Register idempotency key as processing
|
||||
// STEP 6: Register idempotency key as processing (atomic upsert)
|
||||
// ✅ CRITICAL FIX: Use ON CONFLICT to prevent race conditions
|
||||
if (!existingKey) {
|
||||
await supabase.from('submission_idempotency_keys').insert({
|
||||
idempotency_key: idempotencyKey,
|
||||
submission_id: submissionId,
|
||||
moderator_id: user.id,
|
||||
status: 'processing'
|
||||
});
|
||||
const { data: insertedKey, error: idempotencyError } = await supabase
|
||||
.from('submission_idempotency_keys')
|
||||
.insert({
|
||||
idempotency_key: idempotencyKey,
|
||||
submission_id: submissionId,
|
||||
moderator_id: user.id,
|
||||
status: 'processing'
|
||||
})
|
||||
.select()
|
||||
.single();
|
||||
|
||||
// If conflict occurred, another moderator is processing
|
||||
if (idempotencyError && idempotencyError.code === '23505') {
|
||||
edgeLogger.warn('Idempotency key conflict - another request processing', {
|
||||
requestId,
|
||||
idempotencyKey,
|
||||
moderatorId: user.id
|
||||
});
|
||||
return new Response(
|
||||
JSON.stringify({ error: 'Another moderator is processing this submission' }),
|
||||
{ status: 409, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
|
||||
);
|
||||
}
|
||||
|
||||
if (idempotencyError) {
|
||||
throw idempotencyError;
|
||||
}
|
||||
}
|
||||
|
||||
// Create child span for RPC transaction
|
||||
|
||||
Reference in New Issue
Block a user