mirror of
https://github.com/JetSprow/J-Board-Lite.git
synced 2026-05-01 09:14:11 +05:30
Initial commit
This commit is contained in:
80
src/app/(admin)/admin/subscriptions/subscriptions-data.ts
Normal file
80
src/app/(admin)/admin/subscriptions/subscriptions-data.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
import type { Prisma } from "@prisma/client";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { parsePage } from "@/lib/utils";
|
||||
import type { StreamingServiceOption } from "./streaming-slot-dialog";
|
||||
|
||||
const adminSubscriptionInclude = {
|
||||
user: true,
|
||||
plan: true,
|
||||
nodeClient: {
|
||||
include: {
|
||||
inbound: {
|
||||
include: {
|
||||
server: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
streamingSlot: {
|
||||
include: {
|
||||
service: true,
|
||||
},
|
||||
},
|
||||
} satisfies Prisma.UserSubscriptionInclude;
|
||||
|
||||
export type AdminSubscriptionRow = Prisma.UserSubscriptionGetPayload<{
|
||||
include: typeof adminSubscriptionInclude;
|
||||
}>;
|
||||
|
||||
export async function getAdminSubscriptions(
|
||||
searchParams: Record<string, string | string[] | undefined>,
|
||||
) {
|
||||
const { page, skip, pageSize } = parsePage(searchParams);
|
||||
const q = typeof searchParams.q === "string" ? searchParams.q.trim() : "";
|
||||
const status = typeof searchParams.status === "string" ? searchParams.status : "";
|
||||
const type = typeof searchParams.type === "string" ? searchParams.type : "";
|
||||
|
||||
const where = {
|
||||
...(status ? { status: status as "ACTIVE" | "EXPIRED" | "CANCELLED" | "SUSPENDED" } : {}),
|
||||
...(type ? { plan: { type: type as "PROXY" | "STREAMING" } } : {}),
|
||||
...(q
|
||||
? {
|
||||
OR: [
|
||||
{ user: { email: { contains: q, mode: "insensitive" as const } } },
|
||||
{ user: { name: { contains: q, mode: "insensitive" as const } } },
|
||||
{ plan: { name: { contains: q, mode: "insensitive" as const } } },
|
||||
],
|
||||
}
|
||||
: {}),
|
||||
} satisfies Prisma.UserSubscriptionWhereInput;
|
||||
|
||||
const [subscriptions, total, streamingServices] = await Promise.all([
|
||||
prisma.userSubscription.findMany({
|
||||
where,
|
||||
include: adminSubscriptionInclude,
|
||||
orderBy: { createdAt: "desc" },
|
||||
skip,
|
||||
take: pageSize,
|
||||
}),
|
||||
prisma.userSubscription.count({ where }),
|
||||
prisma.streamingService.findMany({
|
||||
where: { isActive: true },
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
usedSlots: true,
|
||||
maxSlots: true,
|
||||
},
|
||||
orderBy: { name: "asc" },
|
||||
}),
|
||||
]);
|
||||
|
||||
return {
|
||||
subscriptions,
|
||||
total,
|
||||
page,
|
||||
pageSize,
|
||||
filters: { q, status, type },
|
||||
streamingServices: streamingServices satisfies StreamingServiceOption[],
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user