Fixes and closes #200, which fails if the customer already exists in both Supabase and Stripe.
The code in that PR looks a Stripe record and then tries to upsert a duplicate record to Supabase without checking if a Supabase record exists first, which raises an error if a Supabase record exists.
I refactored the code so it should perform reconciliation in all edge cases.
If both Supabase and Stripe records exist but the IDs don't match, it updates the Supabase record.
If a record exists in Stripe but not in Supabase, it inserts a new Supabase record with the existing Stripe ID.
If a record exists in Supabase but not in Stripe, it creates a new Stripe record and updates the Supabase record with the new Stripe ID.
If no record exists in Stripe, it creates a new customer in both Stripe and Supabase.
This is a fairly aggressive reconciliation process, but it should be robust and should help correct human errors caused by manually deleting database entries.
Fixes and closes #200, which fails if the customer already exists in both Supabase and Stripe.
The code in that PR looks a Stripe record and then tries to upsert a duplicate record to Supabase without checking if a Supabase record exists first, which raises an error if a Supabase record exists.
I refactored the code so it should perform reconciliation in all edge cases.
This is a fairly aggressive reconciliation process, but it should be robust and should help correct human errors caused by manually deleting database entries.