mirror of
https://github.com/lukevella/rallly.git
synced 2025-05-19 11:56:21 +02:00
♻️ Abstract code for getting subscription status
This commit is contained in:
parent
06eca8f61c
commit
fe1933716d
3 changed files with 49 additions and 52 deletions
|
@ -1,6 +1,7 @@
|
||||||
import { prisma } from "@rallly/database";
|
import { prisma } from "@rallly/database";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|
||||||
|
import { getSubscriptionStatus } from "../../utils/auth";
|
||||||
import { possiblyPublicProcedure, privateProcedure, router } from "../trpc";
|
import { possiblyPublicProcedure, privateProcedure, router } from "../trpc";
|
||||||
|
|
||||||
export const user = router({
|
export const user = router({
|
||||||
|
@ -27,47 +28,7 @@ export const user = router({
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const user = await prisma.user.findUnique({
|
return await getSubscriptionStatus(ctx.user.id);
|
||||||
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,
|
|
||||||
};
|
|
||||||
}),
|
}),
|
||||||
changeName: privateProcedure
|
changeName: privateProcedure
|
||||||
.input(
|
.input(
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { prisma } from "@rallly/database";
|
|
||||||
import { initTRPC, TRPCError } from "@trpc/server";
|
import { initTRPC, TRPCError } from "@trpc/server";
|
||||||
import superjson from "superjson";
|
import superjson from "superjson";
|
||||||
|
|
||||||
|
import { getSubscriptionStatus } from "../utils/auth";
|
||||||
import { Context } from "./context";
|
import { Context } from "./context";
|
||||||
|
|
||||||
const t = initTRPC.context<Context>().create({
|
const t = initTRPC.context<Context>().create({
|
||||||
|
@ -38,16 +38,7 @@ export const proProcedure = t.procedure.use(
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const isPro = Boolean(
|
const { active: isPro } = await getSubscriptionStatus(ctx.user.id);
|
||||||
await prisma.userPaymentData.findFirst({
|
|
||||||
where: {
|
|
||||||
userId: ctx.user.id,
|
|
||||||
endDate: {
|
|
||||||
gt: new Date(),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!isPro) {
|
if (!isPro) {
|
||||||
throw new TRPCError({
|
throw new TRPCError({
|
||||||
|
|
45
packages/backend/utils/auth.ts
Normal file
45
packages/backend/utils/auth.ts
Normal file
|
@ -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,
|
||||||
|
};
|
||||||
|
};
|
Loading…
Add table
Add a link
Reference in a new issue