mirror of
https://github.com/lukevella/rallly.git
synced 2025-06-09 22:21:49 +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
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