Skip to content

Commit

Permalink
feat: compare previous email on customer updated
Browse files Browse the repository at this point in the history
Previously we were comparing two emails that were always identical
because the user tied to the Merchant Customer record had already been
updated. Because of this, an email was never getting sent to the new
user.
  • Loading branch information
jbranchaud authored and kodiakhq[bot] committed Mar 8, 2024
1 parent 8bdf2ba commit 1d3e9c2
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions packages/skill-api/src/core/services/process-stripe-webhook.ts
Expand Up @@ -285,25 +285,40 @@ export const processStripeWebhook = async (
},
})

const user = merchantCustomer?.user
const currentUser = merchantCustomer?.user

if (user) {
const currentEmail = user.email
const {email, name} = eventObject
if (currentUser) {
const {email: targetEmail, name} = z
.object({
email: z.string(),
name: z.string().optional(),
})
.parse(eventObject)

const {previous_attributes} = z
.object({
previous_attributes: z
.object({email: z.string().optional()})
.optional(),
})
.parse(event.data)

const previousEmail = previous_attributes?.email

const transferringToDifferentUser = previousEmail
? targetEmail.toLowerCase() !== previousEmail.toLowerCase()
: false

const {user: updateUser} = await findOrCreateUser(email, name)
const {user: updateUser} = await findOrCreateUser(targetEmail, name)

await transferPurchasesToNewUser({
merchantCustomerId: merchantCustomer.id,
userId: updateUser.id,
})

if (
currentEmail.toLowerCase() !== email.toLowerCase() &&
nextAuthOptions
) {
if (transferringToDifferentUser && nextAuthOptions) {
await sendServerEmail({
email,
email: targetEmail,
callbackUrl: `${process.env.NEXTAUTH_URL}`,
nextAuthOptions,
})
Expand Down

0 comments on commit 1d3e9c2

Please sign in to comment.