From f1179ddc09e46e24646c5b00a16e5a9112c3fd6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Kr=C4=8Dek?= Date: Wed, 3 Sep 2025 08:34:22 +0200 Subject: [PATCH] Fix when people order multiple times --- .../private/events/event/view/+page.svelte | 51 +++++++++++++++---- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/src/routes/private/events/event/view/+page.svelte b/src/routes/private/events/event/view/+page.svelte index e1578ac..5c4ba0d 100644 --- a/src/routes/private/events/event/view/+page.svelte +++ b/src/routes/private/events/event/view/+page.svelte @@ -138,35 +138,64 @@ if (rows.length === 0) throw new Error('No data found in sheet'); - // Extract participant data based on column mapping - const names: string[] = []; - const surnames: string[] = []; - const emails: string[] = []; + // --- Start of new logic to handle duplicates --- - // Skip header row (start from index 1) + // First, extract all potential participants from the sheet + const potentialParticipants = []; for (let i = 1; i < rows.length; i++) { const row = rows[i]; if (row.length > 0) { const name = row[event.name_column - 1] || ''; const surname = row[event.surname_column - 1] || ''; - const email = row[event.email_column - 1] || ''; + const email = (row[event.email_column - 1] || '').trim(); const confirmation = row[event.confirmation_column - 1] || ''; - // Only add if the row has meaningful data (not all empty) AND confirmation is TRUE const isConfirmed = confirmation.toString().toLowerCase() === 'true' || confirmation.toString().toLowerCase() === 'yes' || confirmation === '1' || confirmation === 'x'; - if ((name.trim() || surname.trim() || email.trim()) && isConfirmed) { - names.push(name.trim()); - surnames.push(surname.trim()); - emails.push(email.trim()); + if ((name.trim() || surname.trim() || email) && isConfirmed) { + potentialParticipants.push({ name: name.trim(), surname: surname.trim(), email }); } } } + // Create a map to count occurrences of each unique participant combination + const participantCounts = new Map(); + for (const p of potentialParticipants) { + const key = `${p.name}|${p.surname}|${p.email}`.toLowerCase(); // Create a unique key + participantCounts.set(key, (participantCounts.get(key) || 0) + 1); + } + + // Create final arrays, modifying duplicate surnames to be unique + const names: string[] = []; + const surnames: string[] = []; + const emails: string[] = []; + const processedParticipants = new Map(); + + for (const p of potentialParticipants) { + const key = `${p.name}|${p.surname}|${p.email}`.toLowerCase(); + let finalSurname = p.surname; + + // If this participant is a duplicate + if (participantCounts.get(key)! > 1) { + const count = (processedParticipants.get(key) || 0) + 1; + processedParticipants.set(key, count); + + // If it's not the first occurrence, append a counter to the surname + if (count > 1) { + finalSurname = `${p.surname} (${count})`; + } + } + + names.push(p.name); + surnames.push(finalSurname); + emails.push(p.email); // Keep the original email + } + // --- End of new logic --- + // Call database function to add participants const { error: syncError } = await data.supabase.rpc('participants_add_bulk', { p_event: eventId,