feat(housekeeping): cut over administration to ase
This commit is contained in:
1 parent
c9e35cf602
commit
2b8f73a91d
457 files changed
+589
-55936
No files matched your search
@@ -1,211 +0,0 @@
|
||||
import { asc, eq } from "drizzle-orm";
|
||||
import { notFound, redirect } from "next/navigation";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import Link from "@/components/link";
|
||||
import { CalendarCampaigns, CalendarRewards, db } from "@/lib/db";
|
||||
import { formatDate } from "@/lib/format-date";
|
||||
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
|
||||
|
||||
type Campaign = {
|
||||
id: number;
|
||||
name: string;
|
||||
image: string;
|
||||
startTimestamp: number;
|
||||
totalDays: number;
|
||||
lockExpired: string;
|
||||
enabled: string;
|
||||
};
|
||||
|
||||
type Reward = {
|
||||
id: number;
|
||||
campaignId: number;
|
||||
productName: string;
|
||||
customImage: string;
|
||||
credits: number;
|
||||
pixels: number;
|
||||
points: number;
|
||||
pointsType: number;
|
||||
badge: string;
|
||||
itemId: number;
|
||||
subscriptionType: string | null;
|
||||
subscriptionDays: number;
|
||||
};
|
||||
|
||||
export default async function AdminCalendarCampaign({
|
||||
params,
|
||||
}: {
|
||||
params: Promise<{ id: string }>;
|
||||
}) {
|
||||
const { session, permissions } = await getAdminContext();
|
||||
if (!canAccess(permissions, PERMS.SHOP_VIEW, session.user.rank)) {
|
||||
redirect("/admin");
|
||||
}
|
||||
|
||||
const t = await getTranslations("pages.admin.calendar");
|
||||
|
||||
const { id } = await params;
|
||||
const campaignId = Number(id);
|
||||
if (!Number.isInteger(campaignId)) notFound();
|
||||
|
||||
let campaign: Campaign | null = null;
|
||||
try {
|
||||
const [row] = await db
|
||||
.select({
|
||||
id: CalendarCampaigns.id,
|
||||
name: CalendarCampaigns.name,
|
||||
image: CalendarCampaigns.image,
|
||||
startTimestamp: CalendarCampaigns.startTimestamp,
|
||||
totalDays: CalendarCampaigns.totalDays,
|
||||
lockExpired: CalendarCampaigns.lockExpired,
|
||||
enabled: CalendarCampaigns.enabled,
|
||||
})
|
||||
.from(CalendarCampaigns)
|
||||
.where(eq(CalendarCampaigns.id, campaignId))
|
||||
.limit(1);
|
||||
campaign = row ?? null;
|
||||
} catch {
|
||||
campaign = null;
|
||||
}
|
||||
if (!campaign) notFound();
|
||||
|
||||
let rewards: Reward[] = [];
|
||||
try {
|
||||
rewards = await db
|
||||
.select({
|
||||
id: CalendarRewards.id,
|
||||
campaignId: CalendarRewards.campaignId,
|
||||
productName: CalendarRewards.productName,
|
||||
customImage: CalendarRewards.customImage,
|
||||
credits: CalendarRewards.credits,
|
||||
pixels: CalendarRewards.pixels,
|
||||
points: CalendarRewards.points,
|
||||
pointsType: CalendarRewards.pointsType,
|
||||
badge: CalendarRewards.badge,
|
||||
itemId: CalendarRewards.itemId,
|
||||
subscriptionType: CalendarRewards.subscriptionType,
|
||||
subscriptionDays: CalendarRewards.subscriptionDays,
|
||||
})
|
||||
.from(CalendarRewards)
|
||||
.where(eq(CalendarRewards.campaignId, campaignId))
|
||||
.orderBy(asc(CalendarRewards.id))
|
||||
.limit(500);
|
||||
} catch {
|
||||
rewards = [];
|
||||
}
|
||||
|
||||
const disabled = campaign.enabled !== "1";
|
||||
|
||||
return (
|
||||
<main>
|
||||
<p className="text-xs theme-text-muted dark:theme-text-muted mb-4">
|
||||
<Link href="/admin/calendar">← {t("title")}</Link>
|
||||
</p>
|
||||
|
||||
<div className="admin-card flex items-center gap-5 mb-6">
|
||||
{campaign.image ? (
|
||||
// eslint-disable-next-line @next/next/no-img-element
|
||||
<img
|
||||
className="w-24 h-24 object-cover"
|
||||
src={campaign.image}
|
||||
alt={campaign.name}
|
||||
/>
|
||||
) : null}
|
||||
<div>
|
||||
<h1 className="m-0 mb-1">{campaign.name || `#${campaign.id}`}</h1>
|
||||
<p className="text-xs theme-text-muted dark:theme-text-muted m-0">
|
||||
{t("detail.campaignMeta", {
|
||||
id: campaign.id,
|
||||
days: campaign.totalDays,
|
||||
date: formatDate(
|
||||
new Date(campaign.startTimestamp * 1000),
|
||||
"date",
|
||||
),
|
||||
})}
|
||||
{disabled
|
||||
? ` · ${t("detail.disabled")}`
|
||||
: ` · ${t("detail.enabled")}`}
|
||||
{campaign.lockExpired === "1"
|
||||
? ` · ${t("detail.locksExpired")}`
|
||||
: ""}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<h2 className="admin-section-title">{t("detail.rewards")}</h2>
|
||||
{rewards.length === 0 ? (
|
||||
<div className="admin-empty">{t("detail.noRewards")}</div>
|
||||
) : (
|
||||
<table className="w-full">
|
||||
<thead>
|
||||
<tr className="text-left text-[var(--muted)]">
|
||||
<th>{t("detail.colId")}</th>
|
||||
<th>{t("detail.colProduct")}</th>
|
||||
<th>{t("detail.colItem")}</th>
|
||||
<th>{t("detail.colCredits")}</th>
|
||||
<th>{t("detail.colPixels")}</th>
|
||||
<th>{t("detail.colPoints")}</th>
|
||||
<th>{t("detail.colBadge")}</th>
|
||||
<th>{t("detail.colSubscription")}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{rewards.map((r) => (
|
||||
<tr key={r.id} className="border-t border-[var(--admin-border)]">
|
||||
<td>{r.id}</td>
|
||||
<td>
|
||||
{r.productName || (
|
||||
<span className="text-sm theme-text-muted dark:theme-text-muted">
|
||||
—
|
||||
</span>
|
||||
)}
|
||||
</td>
|
||||
<td className="text-sm theme-text-muted dark:theme-text-muted whitespace-nowrap">
|
||||
{r.itemId > 0 ? r.itemId : "—"}
|
||||
</td>
|
||||
<td>
|
||||
{r.credits > 0 ? (
|
||||
r.credits
|
||||
) : (
|
||||
<span className="text-sm theme-text-muted dark:theme-text-muted">
|
||||
—
|
||||
</span>
|
||||
)}
|
||||
</td>
|
||||
<td>
|
||||
{r.pixels > 0 ? (
|
||||
r.pixels
|
||||
) : (
|
||||
<span className="text-sm theme-text-muted dark:theme-text-muted">
|
||||
—
|
||||
</span>
|
||||
)}
|
||||
</td>
|
||||
<td>
|
||||
{r.points > 0 ? (
|
||||
`${r.points} (${t("detail.typeLabel", { type: r.pointsType })})`
|
||||
) : (
|
||||
<span className="text-sm theme-text-muted dark:theme-text-muted">
|
||||
—
|
||||
</span>
|
||||
)}
|
||||
</td>
|
||||
<td className="text-sm theme-text-muted dark:theme-text-muted whitespace-nowrap">
|
||||
{r.badge || "—"}
|
||||
</td>
|
||||
<td className="text-sm theme-text-muted dark:theme-text-muted whitespace-nowrap">
|
||||
{r.subscriptionType
|
||||
? `${r.subscriptionType} · ${r.subscriptionDays}d`
|
||||
: "—"}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
)}
|
||||
|
||||
<p className="text-xs theme-text-muted dark:theme-text-muted mt-4">
|
||||
{t("detail.rewardCount", { count: rewards.length })}
|
||||
</p>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
@@ -1,139 +0,0 @@
|
||||
import { count, desc } from "drizzle-orm";
|
||||
import { redirect } from "next/navigation";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import { StatusCard } from "@/components/admin/dashboard";
|
||||
import Link from "@/components/link";
|
||||
import { CalendarCampaigns, CalendarRewards, db } from "@/lib/db";
|
||||
import { formatDate } from "@/lib/format-date";
|
||||
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
|
||||
|
||||
type Campaign = {
|
||||
id: number;
|
||||
name: string;
|
||||
image: string;
|
||||
startTimestamp: number;
|
||||
totalDays: number;
|
||||
lockExpired: string;
|
||||
enabled: string;
|
||||
};
|
||||
|
||||
export default async function AdminCalendar() {
|
||||
const { session, permissions } = await getAdminContext();
|
||||
if (!canAccess(permissions, PERMS.SHOP_VIEW, session.user.rank)) {
|
||||
redirect("/admin");
|
||||
}
|
||||
|
||||
const t = await getTranslations("pages.admin.calendar");
|
||||
|
||||
let campaigns: Campaign[] = [];
|
||||
let loadError = false;
|
||||
try {
|
||||
campaigns = await db
|
||||
.select({
|
||||
id: CalendarCampaigns.id,
|
||||
name: CalendarCampaigns.name,
|
||||
image: CalendarCampaigns.image,
|
||||
startTimestamp: CalendarCampaigns.startTimestamp,
|
||||
totalDays: CalendarCampaigns.totalDays,
|
||||
lockExpired: CalendarCampaigns.lockExpired,
|
||||
enabled: CalendarCampaigns.enabled,
|
||||
})
|
||||
.from(CalendarCampaigns)
|
||||
.orderBy(desc(CalendarCampaigns.id))
|
||||
.limit(200);
|
||||
} catch {
|
||||
campaigns = [];
|
||||
loadError = true;
|
||||
}
|
||||
|
||||
const rewardCounts = new Map<number, number>();
|
||||
let totalRewards = 0;
|
||||
try {
|
||||
const grouped = await db
|
||||
.select({
|
||||
campaignId: CalendarRewards.campaignId,
|
||||
total: count(),
|
||||
})
|
||||
.from(CalendarRewards)
|
||||
.groupBy(CalendarRewards.campaignId);
|
||||
for (const g of grouped) {
|
||||
rewardCounts.set(g.campaignId, Number(g.total));
|
||||
totalRewards += Number(g.total);
|
||||
}
|
||||
} catch {
|
||||
loadError = true;
|
||||
}
|
||||
|
||||
const enabledCount = campaigns.filter((c) => c.enabled === "1").length;
|
||||
|
||||
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 some calendar data (database unavailable).
|
||||
</div>
|
||||
) : null}
|
||||
<div className="grid grid-cols-[repeat(auto-fit,minmax(180px,1fr))] gap-3.5 mb-6">
|
||||
<StatusCard label={t("campaigns")} value={campaigns.length} icon="📅" />
|
||||
<StatusCard
|
||||
label={t("enabled")}
|
||||
value={enabledCount}
|
||||
state={enabledCount > 0 ? "ok" : "neutral"}
|
||||
icon="✅"
|
||||
/>
|
||||
<StatusCard label={t("totalRewards")} value={totalRewards} icon="🎁" />
|
||||
</div>
|
||||
|
||||
{campaigns.length === 0 ? (
|
||||
<div className="admin-empty">{t("noCampaigns")}</div>
|
||||
) : (
|
||||
<div className="grid grid-cols-1 gap-4 md:grid-cols-3">
|
||||
{campaigns.map((c) => {
|
||||
const enabled = c.enabled === "1";
|
||||
const count = rewardCounts.get(c.id) ?? 0;
|
||||
return (
|
||||
<Link
|
||||
key={c.id}
|
||||
href={`/admin/calendar/${c.id}`}
|
||||
className="admin-card hover article"
|
||||
>
|
||||
{c.image ? (
|
||||
// eslint-disable-next-line @next/next/no-img-element
|
||||
<img className="article-img" src={c.image} alt={c.name} />
|
||||
) : (
|
||||
<div className="article-img" />
|
||||
)}
|
||||
<div className="body">
|
||||
<div className="flex items-center justify-between gap-2 mb-1">
|
||||
<h3 className="m-0">{c.name || `#${c.id}`}</h3>
|
||||
<span
|
||||
className={`inline-block text-[0.72rem] font-bold px-2 py-0.5 rounded-full ${enabled ? "bg-[var(--admin-success-subtle)] text-[var(--admin-text)]" : "bg-[var(--admin-error-subtle)] text-[var(--admin-text)]"}`}
|
||||
>
|
||||
{enabled ? t("statusEnabled") : t("statusDisabled")}
|
||||
</span>
|
||||
</div>
|
||||
<p className="text-xs theme-text-muted dark:theme-text-muted m-0">
|
||||
{t("campaignMeta", {
|
||||
id: c.id,
|
||||
days: c.totalDays,
|
||||
rewards: count,
|
||||
})}
|
||||
</p>
|
||||
<p className="text-xs theme-text-muted dark:theme-text-muted mt-1">
|
||||
{t("startsAt", {
|
||||
date: formatDate(
|
||||
new Date(c.startTimestamp * 1000),
|
||||
"date",
|
||||
),
|
||||
})}
|
||||
{c.lockExpired === "1" ? ` · ${t("locksExpired")}` : ""}
|
||||
</p>
|
||||
</div>
|
||||
</Link>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
</main>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user