feat(admin): P1 — clearer tickets labels, pagination, min_staff_rank, analytics errors
Co-authored-by: Cursor <[email protected]>
This commit is contained in:
1 parent
75cdfdebe4
commit
f150aea8b9
13 files changed
+311
-101
No files matched your search
@@ -1,8 +1,10 @@
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import { StatusCard } from "@/components/admin/dashboard";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { redirect } from "next/navigation";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import { AdminSimplePager } from "@/components/admin/admin-simple-pager";
|
||||
import { StatusCard } from "@/components/admin/dashboard";
|
||||
import { calcPagination, parseListParams } from "@/lib/admin-helpers";
|
||||
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
type SubRow = {
|
||||
id: number;
|
||||
@@ -15,7 +17,11 @@ function fmt(ts: number): string {
|
||||
return new Date(ts * 1000).toISOString().slice(0, 16).replace("T", " ");
|
||||
}
|
||||
|
||||
export default async function AdminSubscriptions() {
|
||||
export default async function AdminSubscriptions({
|
||||
searchParams,
|
||||
}: {
|
||||
searchParams: Promise<Record<string, string>>;
|
||||
}) {
|
||||
const { session, permissions } = await getAdminContext();
|
||||
if (!canAccess(permissions, PERMS.SHOP_VIEW, session.user.rank)) {
|
||||
redirect("/admin");
|
||||
@@ -23,36 +29,86 @@ export default async function AdminSubscriptions() {
|
||||
|
||||
const t = await getTranslations("pages.admin.subscriptions");
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
const raw = await searchParams;
|
||||
const parsed = parseListParams(new URLSearchParams(raw));
|
||||
|
||||
let active: SubRow[] = [];
|
||||
let total = 0;
|
||||
let types = 0;
|
||||
let expiringSoon = 0;
|
||||
let loadError = false;
|
||||
try {
|
||||
const rows = await prisma.usersSubscriptions.findMany({
|
||||
where: { active: 1 },
|
||||
orderBy: { id: "desc" },
|
||||
take: 500,
|
||||
});
|
||||
const [countRows, typeRows, soonRows] = await Promise.all([
|
||||
prisma.$queryRaw<{ c: bigint }[]>`
|
||||
SELECT COUNT(*) AS c
|
||||
FROM users_subscriptions
|
||||
WHERE active = 1
|
||||
AND COALESCE(timestamp_start, 0) + COALESCE(duration, 0) > ${now}
|
||||
`,
|
||||
prisma.$queryRaw<{ c: bigint }[]>`
|
||||
SELECT COUNT(DISTINCT subscription_type) AS c
|
||||
FROM users_subscriptions
|
||||
WHERE active = 1
|
||||
AND COALESCE(timestamp_start, 0) + COALESCE(duration, 0) > ${now}
|
||||
`,
|
||||
prisma.$queryRaw<{ c: bigint }[]>`
|
||||
SELECT COUNT(*) AS c
|
||||
FROM users_subscriptions
|
||||
WHERE active = 1
|
||||
AND COALESCE(timestamp_start, 0) + COALESCE(duration, 0) > ${now}
|
||||
AND COALESCE(timestamp_start, 0) + COALESCE(duration, 0) <= ${now + 7 * 24 * 60 * 60}
|
||||
`,
|
||||
]);
|
||||
total = Number(countRows[0]?.c ?? 0);
|
||||
types = Number(typeRows[0]?.c ?? 0);
|
||||
expiringSoon = Number(soonRows[0]?.c ?? 0);
|
||||
const pagination = calcPagination(total, parsed.page, parsed.perPage);
|
||||
|
||||
active = rows
|
||||
.map((r) => ({
|
||||
id: r.id,
|
||||
userId: r.userId,
|
||||
subscriptionType: r.subscriptionType,
|
||||
expiresAt: (r.timestampStart ?? 0) + (r.duration ?? 0),
|
||||
}))
|
||||
.filter((r) => r.expiresAt > now)
|
||||
.sort((a, b) => a.expiresAt - b.expiresAt);
|
||||
const rows = await prisma.$queryRaw<
|
||||
{
|
||||
id: number;
|
||||
user_id: number | null;
|
||||
subscription_type: string | null;
|
||||
expires_at: number;
|
||||
}[]
|
||||
>`
|
||||
SELECT id,
|
||||
user_id,
|
||||
subscription_type,
|
||||
COALESCE(timestamp_start, 0) + COALESCE(duration, 0) AS expires_at
|
||||
FROM users_subscriptions
|
||||
WHERE active = 1
|
||||
AND COALESCE(timestamp_start, 0) + COALESCE(duration, 0) > ${now}
|
||||
ORDER BY expires_at ASC
|
||||
LIMIT ${pagination.perPage} OFFSET ${pagination.offset}
|
||||
`;
|
||||
|
||||
active = rows.map((r) => ({
|
||||
id: Number(r.id),
|
||||
userId: r.user_id == null ? null : Number(r.user_id),
|
||||
subscriptionType: r.subscription_type,
|
||||
expiresAt: Number(r.expires_at),
|
||||
}));
|
||||
} catch {
|
||||
active = [];
|
||||
total = 0;
|
||||
types = 0;
|
||||
expiringSoon = 0;
|
||||
loadError = true;
|
||||
}
|
||||
|
||||
const pagination = calcPagination(total, parsed.page, parsed.perPage);
|
||||
const soonCutoff = now + 7 * 24 * 60 * 60;
|
||||
const expiringSoon = active.filter((s) => s.expiresAt <= soonCutoff).length;
|
||||
const types = new Set(active.map((s) => s.subscriptionType ?? "—")).size;
|
||||
|
||||
return (
|
||||
<main>
|
||||
{loadError ? (
|
||||
<div className="mb-4 rounded-lg bg-[var(--admin-error-subtle)] p-3 text-xs text-[var(--admin-error)]">
|
||||
Could not load subscriptions (database unavailable).
|
||||
</div>
|
||||
) : null}
|
||||
<div className="grid grid-cols-[repeat(auto-fit,minmax(180px,1fr))] gap-3.5 mb-6">
|
||||
<StatusCard label={t("activeSubs")} value={active.length} icon="⭐" />
|
||||
<StatusCard label={t("activeSubs")} value={total} icon="⭐" />
|
||||
<StatusCard label={t("types")} value={types} icon="🏷️" />
|
||||
<StatusCard
|
||||
label={t("expiringSoon")}
|
||||
@@ -64,7 +120,7 @@ export default async function AdminSubscriptions() {
|
||||
|
||||
<section className="mt-6">
|
||||
<h2 className="admin-section-title">
|
||||
{t("activeSubscriptions")} ({active.length})
|
||||
{t("activeSubscriptions")} ({total})
|
||||
</h2>
|
||||
<div className="admin-card">
|
||||
<table>
|
||||
@@ -78,34 +134,34 @@ export default async function AdminSubscriptions() {
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{active.map((s) => {
|
||||
const soon = s.expiresAt <= soonCutoff;
|
||||
return (
|
||||
{active.length === 0 ? (
|
||||
<tr>
|
||||
<td colSpan={5} className="muted">
|
||||
—
|
||||
</td>
|
||||
</tr>
|
||||
) : (
|
||||
active.map((s) => (
|
||||
<tr key={s.id}>
|
||||
<td className="text-sm theme-text-muted dark:theme-text-muted whitespace-nowrap">
|
||||
{s.id}
|
||||
</td>
|
||||
<td>{s.id}</td>
|
||||
<td>{s.userId ?? "—"}</td>
|
||||
<td>{s.subscriptionType ?? "—"}</td>
|
||||
<td>
|
||||
<span
|
||||
className={`inline-block text-[0.72rem] font-bold px-2 py-0.5 rounded-full ${soon ? "bg-[var(--admin-accent)]/18 text-[var(--admin-text)]" : "bg-[var(--admin-success-subtle)] text-[var(--admin-text)]"}`}
|
||||
>
|
||||
{soon ? t("statusExpiring") : t("statusActive")}
|
||||
</span>
|
||||
</td>
|
||||
<td className="text-sm theme-text-muted dark:theme-text-muted whitespace-nowrap">
|
||||
{fmt(s.expiresAt)}
|
||||
{s.expiresAt <= soonCutoff ? t("expiringSoon") : "OK"}
|
||||
</td>
|
||||
<td>{fmt(s.expiresAt)}</td>
|
||||
</tr>
|
||||
);
|
||||
})}
|
||||
))
|
||||
)}
|
||||
</tbody>
|
||||
</table>
|
||||
{active.length === 0 ? (
|
||||
<div className="admin-empty">{t("noSubscriptions")}</div>
|
||||
) : null}
|
||||
</div>
|
||||
<AdminSimplePager
|
||||
page={pagination.page}
|
||||
lastPage={pagination.lastPage}
|
||||
total={total}
|
||||
basePath="/admin/subscriptions"
|
||||
/>
|
||||
</section>
|
||||
</main>
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user