Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: compare previous email on customer updated #1432

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
Comment on lines +298 to +304
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can pull the previous email out of previous_attributes that stripe includes in its webhook payload.


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