mirror of
https://github.com/lukevella/rallly.git
synced 2025-04-29 18:26:34 +02:00
✨ Sync stripe customer email (#1616)
This commit is contained in:
parent
bd21350a87
commit
f3ffe71df3
2 changed files with 17 additions and 2 deletions
|
@ -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;
|
||||
|
|
|
@ -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 },
|
||||
|
|
Loading…
Add table
Reference in a new issue