Sync stripe customer email (#1616)

This commit is contained in:
Luke Vella 2025-03-06 10:07:36 +00:00 committed by GitHub
parent bd21350a87
commit f3ffe71df3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 17 additions and 2 deletions

View file

@ -1,4 +1,6 @@
import { stripe } from "@rallly/billing";
import { prisma } from "@rallly/database";
import * as Sentry from "@sentry/nextjs";
import { cookies } from "next/headers";
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
@ -33,11 +35,24 @@ const handleEmailChange = async (token: string) => {
return false;
}
await prisma.user.update({
const user = await prisma.user.update({
where: { email: payload.fromEmail },
data: { email: payload.toEmail },
select: {
customerId: true,
},
});
try {
if (user.customerId) {
await stripe.customers.update(user.customerId, {
email: payload.toEmail,
});
}
} catch (error) {
Sentry.captureException(error);
}
setEmailChangeCookie("success");
return true;

View file

@ -107,8 +107,8 @@ export const user = router({
return { success: true };
}),
requestEmailChange: privateProcedure
.use(createRateLimitMiddleware("request_email_change", 10, "1 h"))
.input(z.object({ email: z.string().email() }))
.use(createRateLimitMiddleware("request_email_change", 5, "1 h"))
.mutation(async ({ input, ctx }) => {
const currentUser = await prisma.user.findUnique({
where: { id: ctx.user.id },