eny.space Landingpage
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix(dashboard): prevent duplicate subscription records

+33 -12
+14 -4
app/actions/subscription.ts
··· 186 186 customerId = customer.id; 187 187 188 188 // Store only customer ID in database (minimal) 189 - await supabase.from("subscriptions").upsert({ 190 - user_id: user.id, 191 - stripe_customer_id: customerId, 192 - }); 189 + const { data: existingSub } = await supabase 190 + .from("subscriptions") 191 + .select("id") 192 + .eq("user_id", user.id) 193 + .limit(1) 194 + .maybeSingle(); 195 + 196 + if (!existingSub) { 197 + const { error } = await supabase.from("subscriptions").insert({ 198 + user_id: user.id, 199 + stripe_customer_id: customerId, 200 + }); 201 + if (error) throw error; 202 + } 193 203 } 194 204 195 205 const headersList = await headers();
+19 -8
app/api/webhooks/route.ts
··· 191 191 : session.customer.id; 192 192 193 193 if (userId && customerId) { 194 - // Only store user_id -> stripe_customer_id mapping (minimal) 195 - const { error } = await supabase.from("subscriptions").upsert({ 196 - user_id: userId, 197 - stripe_customer_id: customerId, 198 - }); 194 + // Store user_id -> stripe_customer_id mapping (minimal) 195 + // Avoid creating duplicate rows if the webhook is delivered more than once. 196 + const { data: existingSub } = await supabase 197 + .from("subscriptions") 198 + .select("id") 199 + .eq("user_id", userId) 200 + .limit(1) 201 + .maybeSingle(); 199 202 200 - if (error) { 201 - console.error("Error storing customer ID:", error); 203 + if (!existingSub) { 204 + const { error } = await supabase 205 + .from("subscriptions") 206 + .insert({ user_id: userId, stripe_customer_id: customerId }); 207 + if (error) console.error("Error inserting customer ID:", error); 208 + else console.log(`✅ Customer ID stored for user ${userId}`); 202 209 } else { 203 - console.log(`✅ Customer ID stored for user ${userId}`); 210 + const { error } = await supabase 211 + .from("subscriptions") 212 + .update({ stripe_customer_id: customerId }) 213 + .eq("id", existingSub.id); 214 + if (error) console.error("Error updating customer ID:", error); 204 215 } 205 216 206 217 // Next step: provision the user's PDS