Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

♻️ Abstract code for getting subscription status

Luke Vellafe1933716dec
diff --git a/packages/backend/trpc/routers/user.ts b/packages/backend/trpc/routers/user.ts
index b153b95b8..f868bd896 100644
--- a/packages/backend/trpc/routers/user.ts
+++ b/packages/backend/trpc/routers/user.ts
@@ -1,6 +1,7 @@
import { prisma } from "@rallly/database";
import { z } from "zod";
+import { getSubscriptionStatus } from "../../utils/auth";
import { possiblyPublicProcedure, privateProcedure, router } from "../trpc";
export const user = router({
@@ -27,47 +28,7 @@ export const user = router({
};
}
- const user = await prisma.user.findUnique({
- where: {
- id: ctx.user.id,
- },
- select: {
- subscription: {
- select: {
- active: true,
- },
- },
- },
- });
-
- if (user?.subscription?.active === true) {
- return {
- active: true,
- };
- }
-
- const userPaymentData = await prisma.userPaymentData.findUnique({
- where: {
- userId: ctx.user.id,
- },
- select: {
- endDate: true,
- },
- });
-
- if (
- userPaymentData?.endDate &&
- userPaymentData.endDate.getTime() > Date.now()
- ) {
- return {
- active: true,
- legacy: true,
- };
- }
-
- return {
- active: false,
- };
+ return await getSubscriptionStatus(ctx.user.id);
}),
changeName: privateProcedure
.input(
diff --git a/packages/backend/trpc/trpc.ts b/packages/backend/trpc/trpc.ts
index 213862eef..61cb15c35 100644
--- a/packages/backend/trpc/trpc.ts
+++ b/packages/backend/trpc/trpc.ts
@@ -1,7 +1,7 @@
-import { prisma } from "@rallly/database";
import { initTRPC, TRPCError } from "@trpc/server";
import superjson from "superjson";
+import { getSubscriptionStatus } from "../utils/auth";
import { Context } from "./context";
const t = initTRPC.context<Context>().create({
@@ -38,16 +38,7 @@ export const proProcedure = t.procedure.use(
});
}
- const isPro = Boolean(
- await prisma.userPaymentData.findFirst({
- where: {
- userId: ctx.user.id,
- endDate: {
- gt: new Date(),
- },
- },
- }),
- );
+ const { active: isPro } = await getSubscriptionStatus(ctx.user.id);
if (!isPro) {
throw new TRPCError({
diff --git a/packages/backend/utils/auth.ts b/packages/backend/utils/auth.ts
new file mode 100644
index 000000000..0327460e0
--- /dev/null
+++ b/packages/backend/utils/auth.ts
@@ -0,0 +1,45 @@
+import { prisma } from "@rallly/database";
+
+export const getSubscriptionStatus = async (userId: string) => {
+ const user = await prisma.user.findUnique({
+ where: {
+ id: userId,
+ },
+ select: {
+ subscription: {
+ select: {
+ active: true,
+ },
+ },
+ },
+ });
+
+ if (user?.subscription?.active === true) {
+ return {
+ active: true,
+ };
+ }
+
+ const userPaymentData = await prisma.userPaymentData.findFirst({
+ where: {
+ userId,
+ endDate: {
+ gt: new Date(),
+ },
+ },
+ });
+
+ if (
+ userPaymentData?.endDate &&
+ userPaymentData.endDate.getTime() > Date.now()
+ ) {
+ return {
+ active: true,
+ legacy: true,
+ };
+ }
+
+ return {
+ active: false,
+ };
+};