el.xwx.moe/lib/api/controllers/users/getUsers.ts

72 lines
1.4 KiB
TypeScript
Raw Normal View History

2024-04-22 17:00:59 -05:00
import { prisma } from "@/lib/api/db";
2024-10-29 17:08:47 -05:00
import { User } from "@prisma/client";
2024-04-22 17:00:59 -05:00
2024-10-29 17:08:47 -05:00
export default async function getUsers(user: User) {
if (user.id === Number(process.env.NEXT_PUBLIC_ADMIN || 1)) {
const users = await prisma.user.findMany({
select: {
id: true,
username: true,
email: true,
emailVerified: true,
subscriptions: {
select: {
active: true,
},
},
createdAt: true,
},
});
return {
response: users.sort((a: any, b: any) => a.id - b.id),
status: 200,
};
} else {
let subscriptionId = (
await prisma.subscription.findFirst({
where: {
userId: user.id,
},
2024-04-22 17:00:59 -05:00
select: {
2024-10-29 17:08:47 -05:00
id: true,
2024-04-22 17:00:59 -05:00
},
2024-10-29 17:08:47 -05:00
})
)?.id;
if (!subscriptionId)
return {
response: "Subscription not found.",
status: 404,
};
const users = await prisma.user.findMany({
where: {
OR: [
{
parentSubscriptionId: subscriptionId,
},
{
subscriptions: {
id: subscriptionId,
},
},
],
},
select: {
id: true,
name: true,
username: true,
email: true,
emailVerified: true,
createdAt: true,
2024-04-22 17:00:59 -05:00
},
2024-10-29 17:08:47 -05:00
});
2024-04-22 17:00:59 -05:00
2024-10-29 17:08:47 -05:00
return {
response: users.sort((a: any, b: any) => a.id - b.id),
status: 200,
};
}
2024-04-22 17:00:59 -05:00
}