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 { prisma } from "@rallly/database";
import * as Sentry from "@sentry/nextjs";
import { cookies } from "next/headers"; import { cookies } from "next/headers";
import type { NextRequest } from "next/server"; import type { NextRequest } from "next/server";
import { NextResponse } from "next/server"; import { NextResponse } from "next/server";
@ -33,11 +35,24 @@ const handleEmailChange = async (token: string) => {
return false; return false;
} }
await prisma.user.update({ const user = await prisma.user.update({
where: { email: payload.fromEmail }, where: { email: payload.fromEmail },
data: { email: payload.toEmail }, 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"); setEmailChangeCookie("success");
return true; return true;

View file

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