refactor(db): migrate app pages and APIs from Prisma facade to Drizzle (2)
Co-authored-by: Cursor <[email protected]>
This commit is contained in:
1 parent
7c39aef5d9
commit
2cd0863cb8
40 files changed
+1069
-726
No files matched your search
@@ -1,9 +1,10 @@
|
||||
import { asc, eq } from "drizzle-orm";
|
||||
import Link from "next/link";
|
||||
import { notFound, redirect } from "next/navigation";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import { CalendarCampaigns, CalendarRewards, db } from "@/lib/db";
|
||||
import { formatDate } from "@/lib/format-date";
|
||||
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
type Campaign = {
|
||||
id: number;
|
||||
@@ -48,18 +49,20 @@ export default async function AdminCalendarCampaign({
|
||||
|
||||
let campaign: Campaign | null = null;
|
||||
try {
|
||||
campaign = await prisma.calendarCampaigns.findUnique({
|
||||
where: { id: campaignId },
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
image: true,
|
||||
startTimestamp: true,
|
||||
totalDays: true,
|
||||
lockExpired: true,
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
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;
|
||||
}
|
||||
@@ -67,25 +70,25 @@ export default async function AdminCalendarCampaign({
|
||||
|
||||
let rewards: Reward[] = [];
|
||||
try {
|
||||
rewards = await prisma.calendarRewards.findMany({
|
||||
where: { campaignId },
|
||||
select: {
|
||||
id: true,
|
||||
campaignId: true,
|
||||
productName: true,
|
||||
customImage: true,
|
||||
credits: true,
|
||||
pixels: true,
|
||||
points: true,
|
||||
pointsType: true,
|
||||
badge: true,
|
||||
itemId: true,
|
||||
subscriptionType: true,
|
||||
subscriptionDays: true,
|
||||
},
|
||||
orderBy: { id: "asc" },
|
||||
take: 500,
|
||||
});
|
||||
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 = [];
|
||||
}
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { count, desc } from "drizzle-orm";
|
||||
import Link from "next/link";
|
||||
import { redirect } from "next/navigation";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import { StatusCard } from "@/components/admin/dashboard";
|
||||
import { CalendarCampaigns, CalendarRewards, db } from "@/lib/db";
|
||||
import { formatDate } from "@/lib/format-date";
|
||||
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
type Campaign = {
|
||||
id: number;
|
||||
@@ -27,19 +28,19 @@ export default async function AdminCalendar() {
|
||||
let campaigns: Campaign[] = [];
|
||||
let loadError = false;
|
||||
try {
|
||||
campaigns = await prisma.calendarCampaigns.findMany({
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
image: true,
|
||||
startTimestamp: true,
|
||||
totalDays: true,
|
||||
lockExpired: true,
|
||||
enabled: true,
|
||||
},
|
||||
orderBy: { id: "desc" },
|
||||
take: 200,
|
||||
});
|
||||
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;
|
||||
@@ -48,13 +49,16 @@ export default async function AdminCalendar() {
|
||||
const rewardCounts = new Map<number, number>();
|
||||
let totalRewards = 0;
|
||||
try {
|
||||
const grouped = await prisma.calendarRewards.groupBy({
|
||||
by: ["campaignId"],
|
||||
_count: { _all: true },
|
||||
});
|
||||
const grouped = await db
|
||||
.select({
|
||||
campaignId: CalendarRewards.campaignId,
|
||||
total: count(),
|
||||
})
|
||||
.from(CalendarRewards)
|
||||
.groupBy(CalendarRewards.campaignId);
|
||||
for (const g of grouped) {
|
||||
rewardCounts.set(g.campaignId, g._count._all);
|
||||
totalRewards += g._count._all;
|
||||
rewardCounts.set(g.campaignId, Number(g.total));
|
||||
totalRewards += Number(g.total);
|
||||
}
|
||||
} catch {
|
||||
loadError = true;
|
||||
|
||||
Reference in new issue
Block a user