mirror of
https://github.com/lukevella/rallly.git
synced 2025-04-30 02:36:30 +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 { 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;
|
||||||
|
|
|
@ -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 },
|
||||||
|
|
Loading…
Add table
Reference in a new issue