refactor(db): finish Drizzle migration for remaining actions and services
Co-authored-by: Cursor <[email protected]>
This commit is contained in:
1 parent
22234fe102
commit
65b2fbee6a
19 files changed
+386
-306
No files matched your search
@@ -1,9 +1,10 @@
|
|||||||
"use server";
|
"use server";
|
||||||
|
|
||||||
|
import { asc } from "drizzle-orm";
|
||||||
import { requirePermission } from "@/lib/admin/guard";
|
import { requirePermission } from "@/lib/admin/guard";
|
||||||
|
import { db, WebsiteHousekeepingPermissions } from "@/lib/db";
|
||||||
import { redirectSafe } from "@/lib/foundation/security";
|
import { redirectSafe } from "@/lib/foundation/security";
|
||||||
import { PERMS } from "@/lib/permissions";
|
import { PERMS } from "@/lib/permissions";
|
||||||
import { prisma } from "@/lib/prisma";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Housekeeping table writes are retired. Runtime access uses ACL only.
|
* Housekeeping table writes are retired. Runtime access uses ACL only.
|
||||||
@@ -34,16 +35,19 @@ export async function bulkImportPermissions(
|
|||||||
export async function exportPermissions(): Promise<string> {
|
export async function exportPermissions(): Promise<string> {
|
||||||
await requirePermission(PERMS.SETTINGS_VIEW);
|
await requirePermission(PERMS.SETTINGS_VIEW);
|
||||||
|
|
||||||
const perms = await prisma.websiteHousekeepingPermissions.findMany({
|
const perms = await db
|
||||||
orderBy: [{ groupName: "asc" }, { permission: "asc" }],
|
.select({
|
||||||
select: {
|
permission: WebsiteHousekeepingPermissions.permission,
|
||||||
permission: true,
|
minRank: WebsiteHousekeepingPermissions.minRank,
|
||||||
minRank: true,
|
description: WebsiteHousekeepingPermissions.description,
|
||||||
description: true,
|
groupName: WebsiteHousekeepingPermissions.groupName,
|
||||||
groupName: true,
|
dependsOn: WebsiteHousekeepingPermissions.dependsOn,
|
||||||
dependsOn: true,
|
})
|
||||||
},
|
.from(WebsiteHousekeepingPermissions)
|
||||||
});
|
.orderBy(
|
||||||
|
asc(WebsiteHousekeepingPermissions.groupName),
|
||||||
|
asc(WebsiteHousekeepingPermissions.permission),
|
||||||
|
);
|
||||||
|
|
||||||
return JSON.stringify(perms, null, 2);
|
return JSON.stringify(perms, null, 2);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
"use server";
|
"use server";
|
||||||
|
|
||||||
import { requirePermission } from "@/lib/admin/guard";
|
import { requirePermission } from "@/lib/admin/guard";
|
||||||
|
import { db, WebsiteBadges } from "@/lib/db";
|
||||||
import { PERMS } from "@/lib/permissions";
|
import { PERMS } from "@/lib/permissions";
|
||||||
import { prisma } from "@/lib/prisma";
|
|
||||||
|
|
||||||
export async function importBadgeFromRemote({
|
export async function importBadgeFromRemote({
|
||||||
code,
|
code,
|
||||||
@@ -15,19 +15,21 @@ export async function importBadgeFromRemote({
|
|||||||
}) {
|
}) {
|
||||||
await requirePermission(PERMS.ASSETS_IMPORT);
|
await requirePermission(PERMS.ASSETS_IMPORT);
|
||||||
try {
|
try {
|
||||||
await prisma.websiteBadges.upsert({
|
const now = new Date();
|
||||||
where: { badgeKey: code },
|
await db
|
||||||
update: {
|
.insert(WebsiteBadges)
|
||||||
badgeName: name,
|
.values({
|
||||||
badgeDescription: description,
|
|
||||||
updatedAt: new Date(),
|
|
||||||
},
|
|
||||||
create: {
|
|
||||||
badgeKey: code,
|
badgeKey: code,
|
||||||
badgeName: name,
|
badgeName: name,
|
||||||
badgeDescription: description,
|
badgeDescription: description,
|
||||||
createdAt: new Date(),
|
createdAt: now,
|
||||||
updatedAt: new Date(),
|
updatedAt: now,
|
||||||
|
})
|
||||||
|
.onDuplicateKeyUpdate({
|
||||||
|
set: {
|
||||||
|
badgeName: name,
|
||||||
|
badgeDescription: description,
|
||||||
|
updatedAt: now,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
return { ok: true as const };
|
return { ok: true as const };
|
||||||
|
|||||||
@@ -3,10 +3,10 @@
|
|||||||
import { revalidatePath } from "next/cache";
|
import { revalidatePath } from "next/cache";
|
||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
import { auth } from "@/lib/auth";
|
import { auth } from "@/lib/auth";
|
||||||
import { prisma } from "@/lib/prisma";
|
import { db, RadioApplications } from "@/lib/db";
|
||||||
import { clientIp, rateLimit } from "@/lib/rate-limit";
|
import { clientIp, rateLimit } from "@/lib/rate-limit";
|
||||||
|
|
||||||
// Column bounds from prisma/schema.prisma (radio_applications):
|
// Column bounds from schema (radio_applications):
|
||||||
// real_name VARCHAR(255); the rest are TEXT. age is an INT.
|
// real_name VARCHAR(255); the rest are TEXT. age is an INT.
|
||||||
const NAME_MAX = 255;
|
const NAME_MAX = 255;
|
||||||
const TEXT_MAX = 5000;
|
const TEXT_MAX = 5000;
|
||||||
@@ -66,8 +66,7 @@ export async function applyDj(formData: FormData): Promise<void> {
|
|||||||
outcome = "invalid";
|
outcome = "invalid";
|
||||||
} else {
|
} else {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
await prisma.radioApplications.create({
|
await db.insert(RadioApplications).values({
|
||||||
data: {
|
|
||||||
userId: BigInt(userId),
|
userId: BigInt(userId),
|
||||||
realName,
|
realName,
|
||||||
age,
|
age,
|
||||||
@@ -78,7 +77,6 @@ export async function applyDj(formData: FormData): Promise<void> {
|
|||||||
status: "pending",
|
status: "pending",
|
||||||
createdAt: now,
|
createdAt: now,
|
||||||
updatedAt: now,
|
updatedAt: now,
|
||||||
},
|
|
||||||
});
|
});
|
||||||
outcome = "submitted";
|
outcome = "submitted";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
import { revalidatePath } from "next/cache";
|
import { revalidatePath } from "next/cache";
|
||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
import { auth } from "@/lib/auth";
|
import { auth } from "@/lib/auth";
|
||||||
import { prisma } from "@/lib/prisma";
|
import { db, RadioSongRequests } from "@/lib/db";
|
||||||
import { clientIp, rateLimit } from "@/lib/rate-limit";
|
import { clientIp, rateLimit } from "@/lib/rate-limit";
|
||||||
|
|
||||||
const SONG_MAX = 255;
|
const SONG_MAX = 255;
|
||||||
@@ -52,15 +52,13 @@ export async function submitRequest(formData: FormData): Promise<void> {
|
|||||||
outcome = "empty";
|
outcome = "empty";
|
||||||
} else {
|
} else {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
await prisma.radioSongRequests.create({
|
await db.insert(RadioSongRequests).values({
|
||||||
data: {
|
|
||||||
userId: BigInt(userId),
|
userId: BigInt(userId),
|
||||||
songTitle: songTitle || null,
|
songTitle: songTitle || null,
|
||||||
artist: artist || null,
|
artist: artist || null,
|
||||||
submittedAt: now,
|
submittedAt: now,
|
||||||
createdAt: now,
|
createdAt: now,
|
||||||
updatedAt: now,
|
updatedAt: now,
|
||||||
},
|
|
||||||
});
|
});
|
||||||
outcome = "posted";
|
outcome = "posted";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { revalidatePath } from "next/cache";
|
|||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { auth } from "@/lib/auth";
|
import { auth } from "@/lib/auth";
|
||||||
import { prisma } from "@/lib/prisma";
|
import { db, RadioShouts } from "@/lib/db";
|
||||||
import { clientIp, rateLimit } from "@/lib/rate-limit";
|
import { clientIp, rateLimit } from "@/lib/rate-limit";
|
||||||
import { moderateOrThrow } from "@/lib/services/moderation";
|
import { moderateOrThrow } from "@/lib/services/moderation";
|
||||||
|
|
||||||
@@ -77,13 +77,11 @@ export async function postShout(formData: FormData): Promise<void> {
|
|||||||
|
|
||||||
if (!moderated) {
|
if (!moderated) {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
await prisma.radioShouts.create({
|
await db.insert(RadioShouts).values({
|
||||||
data: {
|
|
||||||
userId: BigInt(userId),
|
userId: BigInt(userId),
|
||||||
message,
|
message,
|
||||||
createdAt: now,
|
createdAt: now,
|
||||||
updatedAt: now,
|
updatedAt: now,
|
||||||
},
|
|
||||||
});
|
});
|
||||||
outcome = "posted";
|
outcome = "posted";
|
||||||
}
|
}
|
||||||
|
|||||||
+78
-57
File diff suppressed because it is too large.
Load diff
@@ -1,7 +1,8 @@
|
|||||||
import "server-only";
|
import "server-only";
|
||||||
|
|
||||||
|
import { count, eq, sql } from "drizzle-orm";
|
||||||
import { env } from "@/env";
|
import { env } from "@/env";
|
||||||
import { prisma } from "@/lib/prisma";
|
import { db, User } from "@/lib/db";
|
||||||
import { redis } from "@/lib/redis";
|
import { redis } from "@/lib/redis";
|
||||||
import { rcon } from "@/lib/services/rcon";
|
import { rcon } from "@/lib/services/rcon";
|
||||||
|
|
||||||
@@ -19,11 +20,11 @@ export type OpsHealth = {
|
|||||||
* Semantics match `/api/health` for Redis (null when not configured).
|
* Semantics match `/api/health` for Redis (null when not configured).
|
||||||
*/
|
*/
|
||||||
export async function fetchOpsHealth(): Promise<OpsHealth> {
|
export async function fetchOpsHealth(): Promise<OpsHealth> {
|
||||||
const [db, redisOk, emulatorOk, onlineUsers] = await Promise.all([
|
const [dbProbe, redisOk, emulatorOk, onlineUsers] = await Promise.all([
|
||||||
(async () => {
|
(async () => {
|
||||||
try {
|
try {
|
||||||
const start = Date.now();
|
const start = Date.now();
|
||||||
await prisma.$queryRaw`SELECT 1`;
|
await db.execute(sql`SELECT 1`);
|
||||||
return { dbOk: true, dbLatencyMs: Date.now() - start };
|
return { dbOk: true, dbLatencyMs: Date.now() - start };
|
||||||
} catch {
|
} catch {
|
||||||
return { dbOk: false, dbLatencyMs: 0 };
|
return { dbOk: false, dbLatencyMs: 0 };
|
||||||
@@ -40,12 +41,16 @@ export async function fetchOpsHealth(): Promise<OpsHealth> {
|
|||||||
}
|
}
|
||||||
})(),
|
})(),
|
||||||
rcon.send("ping", null).catch(() => false),
|
rcon.send("ping", null).catch(() => false),
|
||||||
prisma.user.count({ where: { online: "1" } }).catch(() => 0),
|
db
|
||||||
|
.select({ total: count() })
|
||||||
|
.from(User)
|
||||||
|
.where(eq(User.online, "1"))
|
||||||
|
.then((rows) => rows[0]?.total ?? 0)
|
||||||
|
.catch(() => 0),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
dbOk: db.dbOk,
|
dbOk: dbProbe.dbOk,
|
||||||
dbLatencyMs: db.dbLatencyMs,
|
dbLatencyMs: dbProbe.dbLatencyMs,
|
||||||
redisOk,
|
redisOk,
|
||||||
emulatorOk: Boolean(emulatorOk),
|
emulatorOk: Boolean(emulatorOk),
|
||||||
onlineUsers,
|
onlineUsers,
|
||||||
|
|||||||
@@ -1,25 +1,39 @@
|
|||||||
|
import { count, desc, eq } from "drizzle-orm";
|
||||||
import type { OnlineUser } from "@/components/admin/dashboard";
|
import type { OnlineUser } from "@/components/admin/dashboard";
|
||||||
import { prisma } from "@/lib/prisma";
|
import { db, User } from "@/lib/db";
|
||||||
|
|
||||||
/** Shared online roster used by CommandoCentrum (and linked from other ops hubs). */
|
/** Shared online roster used by CommandoCentrum (and linked from other ops hubs). */
|
||||||
export async function fetchOpsOnlineUsers(
|
export async function fetchOpsOnlineUsers(
|
||||||
limit = 40,
|
limit = 40,
|
||||||
): Promise<{ count: number; users: OnlineUser[] }> {
|
): Promise<{ count: number; users: OnlineUser[] }> {
|
||||||
const [count, onlineUsersRaw] = await Promise.all([
|
const [countRow, onlineUsersRaw] = await Promise.all([
|
||||||
prisma.user.count({ where: { online: "1" } }).catch(() => 0),
|
db
|
||||||
prisma.user
|
.select({ total: count() })
|
||||||
.findMany({
|
.from(User)
|
||||||
where: { online: "1" },
|
.where(eq(User.online, "1"))
|
||||||
select: {
|
.then((rows) => rows[0]?.total ?? 0)
|
||||||
id: true,
|
.catch(() => 0),
|
||||||
username: true,
|
db
|
||||||
look: true,
|
.select({
|
||||||
lastOnline: true,
|
id: User.id,
|
||||||
},
|
username: User.username,
|
||||||
orderBy: { lastOnline: "desc" },
|
look: User.look,
|
||||||
take: limit,
|
lastOnline: User.lastOnline,
|
||||||
})
|
})
|
||||||
.catch(() => []),
|
.from(User)
|
||||||
|
.where(eq(User.online, "1"))
|
||||||
|
.orderBy(desc(User.lastOnline))
|
||||||
|
.limit(limit)
|
||||||
|
.then((rows) => rows)
|
||||||
|
.catch(
|
||||||
|
() =>
|
||||||
|
[] as Array<{
|
||||||
|
id: number;
|
||||||
|
username: string;
|
||||||
|
look: string;
|
||||||
|
lastOnline: number;
|
||||||
|
}>,
|
||||||
|
),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const users: OnlineUser[] = onlineUsersRaw.map((u) => ({
|
const users: OnlineUser[] = onlineUsersRaw.map((u) => ({
|
||||||
@@ -31,5 +45,5 @@ export async function fetchOpsOnlineUsers(
|
|||||||
lastActivity: u.lastOnline > 0 ? new Date(u.lastOnline * 1000) : new Date(),
|
lastActivity: u.lastOnline > 0 ? new Date(u.lastOnline * 1000) : new Date(),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return { count, users };
|
return { count: countRow, users };
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import "server-only";
|
import "server-only";
|
||||||
|
|
||||||
import { prisma } from "@/lib/prisma";
|
import { count, eq, ne } from "drizzle-orm";
|
||||||
|
import { db, WebsiteHelpCenterTickets, WebsiteTicket } from "@/lib/db";
|
||||||
|
|
||||||
/** Open-queue sizes for dual ticket products (CMS desk vs help-center). */
|
/** Open-queue sizes for dual ticket products (CMS desk vs help-center). */
|
||||||
export async function fetchTicketQueueOpenCounts(): Promise<{
|
export async function fetchTicketQueueOpenCounts(): Promise<{
|
||||||
@@ -8,11 +9,17 @@ export async function fetchTicketQueueOpenCounts(): Promise<{
|
|||||||
helpOpen: number;
|
helpOpen: number;
|
||||||
}> {
|
}> {
|
||||||
const [cmsOpen, helpOpen] = await Promise.all([
|
const [cmsOpen, helpOpen] = await Promise.all([
|
||||||
prisma.websiteTicket
|
db
|
||||||
.count({ where: { status: { not: "closed" } } })
|
.select({ total: count() })
|
||||||
|
.from(WebsiteTicket)
|
||||||
|
.where(ne(WebsiteTicket.status, "closed"))
|
||||||
|
.then((rows) => rows[0]?.total ?? 0)
|
||||||
.catch(() => 0),
|
.catch(() => 0),
|
||||||
prisma.websiteHelpCenterTickets
|
db
|
||||||
.count({ where: { open: true } })
|
.select({ total: count() })
|
||||||
|
.from(WebsiteHelpCenterTickets)
|
||||||
|
.where(eq(WebsiteHelpCenterTickets.open, true))
|
||||||
|
.then((rows) => rows[0]?.total ?? 0)
|
||||||
.catch(() => 0),
|
.catch(() => 0),
|
||||||
]);
|
]);
|
||||||
return { cmsOpen, helpOpen };
|
return { cmsOpen, helpOpen };
|
||||||
|
|||||||
@@ -1,14 +1,16 @@
|
|||||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
const createFn = vi.hoisted(() => vi.fn());
|
const insertValues = vi.hoisted(() => vi.fn());
|
||||||
const sendMailFn = vi.hoisted(() => vi.fn().mockResolvedValue(true));
|
|
||||||
|
|
||||||
vi.mock("@/lib/prisma", () => ({
|
vi.mock("@/lib/db", () => ({
|
||||||
prisma: { alertLogs: { create: createFn } },
|
db: {
|
||||||
|
insert: vi.fn(() => ({ values: insertValues })),
|
||||||
|
},
|
||||||
|
AlertLogs: {},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
vi.mock("@/lib/services/email", () => ({
|
vi.mock("@/lib/services/email", () => ({
|
||||||
sendMail: sendMailFn,
|
sendMail: vi.fn().mockResolvedValue(true),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
vi.mock("@/env", () => ({
|
vi.mock("@/env", () => ({
|
||||||
@@ -21,12 +23,14 @@ vi.mock("@/env", () => ({
|
|||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
import { sendMail } from "@/lib/services/email";
|
||||||
import { sendAlert } from "./alert";
|
import { sendAlert } from "./alert";
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
createFn.mockReset();
|
insertValues.mockReset();
|
||||||
sendMailFn.mockReset();
|
vi.mocked(sendMail).mockReset();
|
||||||
createFn.mockResolvedValue({ id: 1 });
|
vi.mocked(sendMail).mockResolvedValue(true);
|
||||||
|
insertValues.mockResolvedValue([{ insertId: 1 }]);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("sendAlert", () => {
|
describe("sendAlert", () => {
|
||||||
@@ -36,14 +40,14 @@ describe("sendAlert", () => {
|
|||||||
severity: "warning",
|
severity: "warning",
|
||||||
message: "test alert",
|
message: "test alert",
|
||||||
});
|
});
|
||||||
expect(createFn).toHaveBeenCalledWith({
|
expect(insertValues).toHaveBeenCalledWith(
|
||||||
data: expect.objectContaining({
|
expect.objectContaining({
|
||||||
type: "test",
|
type: "test",
|
||||||
severity: "warning",
|
severity: "warning",
|
||||||
message: "test alert",
|
message: "test alert",
|
||||||
isRead: false,
|
isRead: false,
|
||||||
}),
|
}),
|
||||||
});
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("returns logged=true when DB write succeeds", async () => {
|
it("returns logged=true when DB write succeeds", async () => {
|
||||||
@@ -57,6 +61,6 @@ describe("sendAlert", () => {
|
|||||||
|
|
||||||
it("does not call sendMail when ALERT_EMAIL is unset", async () => {
|
it("does not call sendMail when ALERT_EMAIL is unset", async () => {
|
||||||
await sendAlert({ type: "test", severity: "error", message: "m" });
|
await sendAlert({ type: "test", severity: "error", message: "m" });
|
||||||
expect(sendMailFn).not.toHaveBeenCalled();
|
expect(sendMail).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -1,14 +1,14 @@
|
|||||||
import { env } from "@/env";
|
import { env } from "@/env";
|
||||||
|
import { AlertLogs, db } from "@/lib/db";
|
||||||
import { logger } from "@/lib/logger";
|
import { logger } from "@/lib/logger";
|
||||||
import { prisma } from "@/lib/prisma";
|
|
||||||
import { sendMail } from "@/lib/services/email";
|
import { sendMail } from "@/lib/services/email";
|
||||||
|
|
||||||
// === Alert service (AtomCMS → Next.js) ===========================================
|
// === Alert service (AtomCMS → Next.js) ===========================================
|
||||||
//
|
//
|
||||||
// A pure, dependency-free server module for raising operational alerts. A single
|
// A pure, dependency-free server module for raising operational alerts. A single
|
||||||
// sendAlert() call (a) persists a row in alert_logs (prisma.alertLogs delegate),
|
// sendAlert() call (a) persists a row in alert_logs, (b) pushes a Discord embed
|
||||||
// (b) pushes a Discord embed to DISCORD_WEBHOOK_URL when set, and (c) optionally
|
// to DISCORD_WEBHOOK_URL when set, and (c) optionally emails staff via sendMail()
|
||||||
// emails staff via sendMail() when ALERT_EMAIL is set.
|
// when ALERT_EMAIL is set.
|
||||||
//
|
//
|
||||||
// Every external side-effect is wrapped in try/catch so a failing webhook, dead
|
// Every external side-effect is wrapped in try/catch so a failing webhook, dead
|
||||||
// SMTP host, or unreachable DB never throws into the caller (which is usually a
|
// SMTP host, or unreachable DB never throws into the caller (which is usually a
|
||||||
@@ -186,18 +186,17 @@ export async function sendAlert(
|
|||||||
|
|
||||||
let logged = false;
|
let logged = false;
|
||||||
try {
|
try {
|
||||||
await prisma.alertLogs.create({
|
const now = new Date();
|
||||||
data: {
|
await db.insert(AlertLogs).values({
|
||||||
type: input.type.slice(0, 255),
|
type: input.type.slice(0, 255),
|
||||||
severity: String(input.severity).slice(0, 255),
|
severity: String(input.severity).slice(0, 255),
|
||||||
message: input.message,
|
message: input.message,
|
||||||
context: input.context ? (input.context as object) : undefined,
|
context: input.context ? JSON.stringify(input.context) : null,
|
||||||
sentViaDiscord,
|
sentViaDiscord,
|
||||||
sentViaEmail,
|
sentViaEmail,
|
||||||
isRead: false,
|
isRead: false,
|
||||||
createdAt: new Date(),
|
createdAt: now,
|
||||||
updatedAt: new Date(),
|
updatedAt: now,
|
||||||
},
|
|
||||||
});
|
});
|
||||||
logged = true;
|
logged = true;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { existsSync } from "node:fs";
|
import { existsSync } from "node:fs";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { prisma } from "@/lib/prisma";
|
import { sql } from "drizzle-orm";
|
||||||
|
import { db } from "@/lib/db";
|
||||||
import { fetchSourceFurnidata } from "@/lib/services/clone-import";
|
import { fetchSourceFurnidata } from "@/lib/services/clone-import";
|
||||||
import { listSources } from "@/lib/services/clone-sources";
|
import { listSources } from "@/lib/services/clone-sources";
|
||||||
import { getFurniAssetDirs } from "@/lib/services/furni-asset-dirs";
|
import { getFurniAssetDirs } from "@/lib/services/furni-asset-dirs";
|
||||||
@@ -90,14 +91,18 @@ export async function runCatalogAudit(
|
|||||||
type: string;
|
type: string;
|
||||||
}>;
|
}>;
|
||||||
try {
|
try {
|
||||||
items = await prisma.$queryRaw<
|
const [rows] = (await db.execute(sql`
|
||||||
|
SELECT id, item_name, public_name, type FROM items_base ORDER BY id
|
||||||
|
`)) as unknown as [
|
||||||
Array<{
|
Array<{
|
||||||
id: number;
|
id: number;
|
||||||
item_name: string;
|
item_name: string;
|
||||||
public_name: string;
|
public_name: string;
|
||||||
type: string;
|
type: string;
|
||||||
}>
|
}>,
|
||||||
>`SELECT id, item_name, public_name, type FROM items_base ORDER BY id`;
|
unknown,
|
||||||
|
];
|
||||||
|
items = rows;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
onEvent?.({
|
onEvent?.({
|
||||||
type: "error",
|
type: "error",
|
||||||
@@ -116,9 +121,13 @@ export async function runCatalogAudit(
|
|||||||
catalog_name: string;
|
catalog_name: string;
|
||||||
}>;
|
}>;
|
||||||
try {
|
try {
|
||||||
catalogItems = await prisma.$queryRaw<
|
const [rows] = (await db.execute(sql`
|
||||||
Array<{ id: number; item_ids: string; catalog_name: string }>
|
SELECT id, item_ids, catalog_name FROM catalog_items ORDER BY id
|
||||||
>`SELECT id, item_ids, catalog_name FROM catalog_items ORDER BY id`;
|
`)) as unknown as [
|
||||||
|
Array<{ id: number; item_ids: string; catalog_name: string }>,
|
||||||
|
unknown,
|
||||||
|
];
|
||||||
|
catalogItems = rows;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
onEvent?.({
|
onEvent?.({
|
||||||
type: "error",
|
type: "error",
|
||||||
|
|||||||
@@ -8,16 +8,16 @@ const {
|
|||||||
downloadFile,
|
downloadFile,
|
||||||
appendFurniEntry,
|
appendFurniEntry,
|
||||||
parseNitroBundle,
|
parseNitroBundle,
|
||||||
execRaw,
|
executeFn,
|
||||||
queryRaw,
|
selectLimit,
|
||||||
fsUnlink,
|
fsUnlink,
|
||||||
fsReadFile,
|
fsReadFile,
|
||||||
} = vi.hoisted(() => ({
|
} = vi.hoisted(() => ({
|
||||||
downloadFile: vi.fn<AnyFn>(async () => ({ ok: true, size: 200 })),
|
downloadFile: vi.fn<AnyFn>(async () => ({ ok: true, size: 200 })),
|
||||||
appendFurniEntry: vi.fn<AnyFn>(async () => {}),
|
appendFurniEntry: vi.fn<AnyFn>(async () => {}),
|
||||||
parseNitroBundle: vi.fn<AnyFn>(() => ({ json: {}, png: Buffer.alloc(0) })),
|
parseNitroBundle: vi.fn<AnyFn>(() => ({ json: {}, png: Buffer.alloc(0) })),
|
||||||
execRaw: vi.fn<AnyFn>(async () => 1),
|
executeFn: vi.fn<AnyFn>(async () => [[{ next: 1 }], []]),
|
||||||
queryRaw: vi.fn<AnyFn>(async () => [] as unknown[]),
|
selectLimit: vi.fn<AnyFn>(async () => []),
|
||||||
fsUnlink: vi.fn<AnyFn>(async () => {}),
|
fsUnlink: vi.fn<AnyFn>(async () => {}),
|
||||||
fsReadFile: vi.fn<AnyFn>(async () => Buffer.from("NITRO")),
|
fsReadFile: vi.fn<AnyFn>(async () => Buffer.from("NITRO")),
|
||||||
}));
|
}));
|
||||||
@@ -55,13 +55,18 @@ vi.mock("node:fs", async (orig) => {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
vi.mock("@/lib/prisma", () => ({
|
vi.mock("@/lib/db", () => ({
|
||||||
prisma: {
|
db: {
|
||||||
$executeRaw: execRaw as AnyFn,
|
execute: executeFn,
|
||||||
$queryRaw: queryRaw as AnyFn,
|
select: vi.fn(() => ({
|
||||||
$executeRawUnsafe: execRaw as AnyFn,
|
from: vi.fn(() => ({
|
||||||
$queryRawUnsafe: queryRaw as AnyFn,
|
where: vi.fn(() => ({
|
||||||
|
limit: selectLimit,
|
||||||
|
})),
|
||||||
|
})),
|
||||||
|
})),
|
||||||
},
|
},
|
||||||
|
ItemsBase: { id: "id", itemName: "itemName" },
|
||||||
}));
|
}));
|
||||||
|
|
||||||
import { cloneSingleFurni, parseFurnidata } from "./clone-import";
|
import { cloneSingleFurni, parseFurnidata } from "./clone-import";
|
||||||
@@ -100,11 +105,14 @@ describe("clone-import", () => {
|
|||||||
// Reset mocks to clear state from other tests.
|
// Reset mocks to clear state from other tests.
|
||||||
downloadFile.mockReset();
|
downloadFile.mockReset();
|
||||||
downloadFile.mockResolvedValue({ ok: true, size: 200 });
|
downloadFile.mockResolvedValue({ ok: true, size: 200 });
|
||||||
execRaw.mockReset();
|
executeFn.mockReset();
|
||||||
execRaw.mockResolvedValue(1);
|
// allocateItemsBaseId MAX(id)+1, then items_base INSERT, then catalog INSERT.
|
||||||
queryRaw.mockReset();
|
executeFn
|
||||||
// First call: dedup check (no existing row); second call: allocateItemsBaseId MAX(id)+1.
|
.mockResolvedValueOnce([[{ next: 42 }], []])
|
||||||
queryRaw.mockResolvedValueOnce([]).mockResolvedValueOnce([{ next: 42 }]);
|
.mockResolvedValueOnce([[], []])
|
||||||
|
.mockResolvedValueOnce([[], []]);
|
||||||
|
selectLimit.mockReset();
|
||||||
|
selectLimit.mockResolvedValue([]);
|
||||||
appendFurniEntry.mockReset();
|
appendFurniEntry.mockReset();
|
||||||
appendFurniEntry.mockResolvedValue(undefined);
|
appendFurniEntry.mockResolvedValue(undefined);
|
||||||
|
|
||||||
@@ -124,12 +132,12 @@ describe("clone-import", () => {
|
|||||||
expect.objectContaining({ validate: "png" }),
|
expect.objectContaining({ validate: "png" }),
|
||||||
);
|
);
|
||||||
expect(appendFurniEntry).toHaveBeenCalled();
|
expect(appendFurniEntry).toHaveBeenCalled();
|
||||||
expect(execRaw).toHaveBeenCalled(); // items_base insert + catalog
|
expect(executeFn).toHaveBeenCalled(); // items_base insert + catalog
|
||||||
});
|
});
|
||||||
|
|
||||||
it("skips when classname already exists (dedup)", async () => {
|
it("skips when classname already exists (dedup)", async () => {
|
||||||
queryRaw.mockReset();
|
selectLimit.mockReset();
|
||||||
queryRaw.mockResolvedValueOnce([{ id: 5 }]); // existing items_base row
|
selectLimit.mockResolvedValueOnce([{ id: 5 }]); // existing items_base row
|
||||||
const r = await cloneSingleFurni({
|
const r = await cloneSingleFurni({
|
||||||
source: SOURCE,
|
source: SOURCE,
|
||||||
entry: { ...ENTRY, itemType: "s" },
|
entry: { ...ENTRY, itemType: "s" },
|
||||||
@@ -145,14 +153,14 @@ describe("clone-import", () => {
|
|||||||
fsUnlink.mockReset();
|
fsUnlink.mockReset();
|
||||||
fsUnlink.mockResolvedValue(undefined);
|
fsUnlink.mockResolvedValue(undefined);
|
||||||
appendFurniEntry.mockReset();
|
appendFurniEntry.mockReset();
|
||||||
queryRaw.mockReset();
|
selectLimit.mockReset();
|
||||||
execRaw.mockReset();
|
executeFn.mockReset();
|
||||||
// Dedup query: no existing row.
|
// Dedup query: no existing row.
|
||||||
queryRaw.mockResolvedValueOnce([]);
|
selectLimit.mockResolvedValueOnce([]);
|
||||||
// allocateItemsBaseId: MAX(id)+1 query returns next id.
|
// allocateItemsBaseId: MAX(id)+1 query returns next id.
|
||||||
queryRaw.mockResolvedValueOnce([{ next: 7 }]);
|
executeFn.mockResolvedValueOnce([[{ next: 7 }], []]);
|
||||||
// items_base INSERT rejects.
|
// items_base INSERT rejects.
|
||||||
execRaw.mockRejectedValueOnce(new Error("Duplicate entry"));
|
executeFn.mockRejectedValueOnce(new Error("Duplicate entry"));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const r = await cloneSingleFurni({
|
const r = await cloneSingleFurni({
|
||||||
|
|||||||
File diff suppressed because it is too large.
Load diff
@@ -1,8 +1,10 @@
|
|||||||
// @vitest-environment node
|
// @vitest-environment node
|
||||||
import { describe, expect, it, vi } from "vitest";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
vi.mock("@/lib/prisma", () => ({
|
vi.mock("@/lib/db", () => ({
|
||||||
prisma: {},
|
db: {},
|
||||||
|
CatalogPages: {},
|
||||||
|
ItemsBase: {},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
vi.mock("@/lib/services/furni-data", () => ({
|
vi.mock("@/lib/services/furni-data", () => ({
|
||||||
@@ -40,17 +42,11 @@ describe("buildItemsBaseEnrichmentUpdate", () => {
|
|||||||
interactionType: "default",
|
interactionType: "default",
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(update.sql).not.toContain("specialtype");
|
const serialized = JSON.stringify(update);
|
||||||
expect(update.values).toEqual([
|
expect(serialized).not.toContain("specialtype");
|
||||||
2,
|
expect(serialized).toContain("UPDATE items_base");
|
||||||
3,
|
expect(serialized).toContain("123");
|
||||||
1.5,
|
expect(serialized).toContain("1.5");
|
||||||
"1",
|
expect(serialized).toContain("default");
|
||||||
"0",
|
|
||||||
"1",
|
|
||||||
"1",
|
|
||||||
"default",
|
|
||||||
123,
|
|
||||||
]);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -1,6 +1,7 @@
|
|||||||
import { existsSync, promises as fs } from "node:fs";
|
import { existsSync, promises as fs } from "node:fs";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { prisma } from "@/lib/prisma";
|
import { sql } from "drizzle-orm";
|
||||||
|
import { db } from "@/lib/db";
|
||||||
import { extractFurniIconPng } from "@/lib/services/clone-icon";
|
import { extractFurniIconPng } from "@/lib/services/clone-icon";
|
||||||
import { listSources } from "@/lib/services/clone-sources";
|
import { listSources } from "@/lib/services/clone-sources";
|
||||||
import { getFurniAssetDirs } from "@/lib/services/furni-asset-dirs";
|
import { getFurniAssetDirs } from "@/lib/services/furni-asset-dirs";
|
||||||
@@ -54,9 +55,10 @@ export async function repairMissingIcons(
|
|||||||
|
|
||||||
let items: Array<{ id: number; item_name: string }>;
|
let items: Array<{ id: number; item_name: string }>;
|
||||||
try {
|
try {
|
||||||
items = await prisma.$queryRaw<
|
const [rows] = (await db.execute(sql`
|
||||||
Array<{ id: number; item_name: string }>
|
SELECT id, item_name FROM items_base ORDER BY id
|
||||||
>`SELECT id, item_name FROM items_base ORDER BY id`;
|
`)) as unknown as [Array<{ id: number; item_name: string }>, unknown];
|
||||||
|
items = rows;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
onEvent?.({
|
onEvent?.({
|
||||||
type: "error",
|
type: "error",
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { existsSync, promises as fs } from "node:fs";
|
import { existsSync, promises as fs } from "node:fs";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { prisma } from "@/lib/prisma";
|
import { eq, sql } from "drizzle-orm";
|
||||||
|
import { db, ItemsBase } from "@/lib/db";
|
||||||
import { getFurniAssetDirs } from "@/lib/services/furni-asset-dirs";
|
import { getFurniAssetDirs } from "@/lib/services/furni-asset-dirs";
|
||||||
import { appendFurniEntry, buildFurniEntry } from "@/lib/services/furni-data";
|
import { appendFurniEntry, buildFurniEntry } from "@/lib/services/furni-data";
|
||||||
import {
|
import {
|
||||||
@@ -52,9 +53,10 @@ async function allocateItemsBaseId<T>(
|
|||||||
});
|
});
|
||||||
await prev.catch(() => {});
|
await prev.catch(() => {});
|
||||||
try {
|
try {
|
||||||
const idRow = await prisma.$queryRaw<Array<{ next: number }>>`
|
const [idRows] = (await db.execute(sql`
|
||||||
SELECT COALESCE(MAX(id), 0) + 1 AS next FROM items_base`;
|
SELECT COALESCE(MAX(id), 0) + 1 AS next FROM items_base
|
||||||
const nextId = Number(idRow[0]?.next ?? 1);
|
`)) as unknown as [Array<{ next: number }>, unknown];
|
||||||
|
const nextId = Number(idRows[0]?.next ?? 1);
|
||||||
return await insertFn(nextId);
|
return await insertFn(nextId);
|
||||||
} finally {
|
} finally {
|
||||||
settle();
|
settle();
|
||||||
@@ -175,8 +177,11 @@ export async function uploadSingleFurni(params: {
|
|||||||
return { ok: false, classname, warnings, error: "invalid classname" };
|
return { ok: false, classname, warnings, error: "invalid classname" };
|
||||||
}
|
}
|
||||||
|
|
||||||
const existing = await prisma.$queryRaw<Array<{ id: number }>>`
|
const existing = await db
|
||||||
SELECT id FROM items_base WHERE item_name = ${classname} LIMIT 1`;
|
.select({ id: ItemsBase.id })
|
||||||
|
.from(ItemsBase)
|
||||||
|
.where(eq(ItemsBase.itemName, classname))
|
||||||
|
.limit(1);
|
||||||
if (existing.length > 0) {
|
if (existing.length > 0) {
|
||||||
return {
|
return {
|
||||||
ok: false,
|
ok: false,
|
||||||
@@ -231,13 +236,13 @@ export async function uploadSingleFurni(params: {
|
|||||||
let newId: number;
|
let newId: number;
|
||||||
try {
|
try {
|
||||||
newId = await allocateItemsBaseId(async (nextId) => {
|
newId = await allocateItemsBaseId(async (nextId) => {
|
||||||
await prisma.$executeRaw`
|
await db.execute(sql`
|
||||||
INSERT INTO items_base
|
INSERT INTO items_base
|
||||||
(id, sprite_id, public_name, item_name, type, width, length, stack_height,
|
(id, sprite_id, public_name, item_name, type, width, length, stack_height,
|
||||||
allow_stack, allow_sit, allow_lay, allow_walk, interaction_type, customparams)
|
allow_stack, allow_sit, allow_lay, allow_walk, interaction_type, customparams)
|
||||||
VALUES
|
VALUES
|
||||||
(${nextId}, ${nextId}, ${name}, ${classname}, ${itemType}, ${xdim}, ${ydim}, ${stackHeight},
|
(${nextId}, ${nextId}, ${name}, ${classname}, ${itemType}, ${xdim}, ${ydim}, ${stackHeight},
|
||||||
${allowStack}, ${cansiton ? "1" : "0"}, ${canlayon ? "1" : "0"}, ${canstandon ? "1" : "0"}, ${userInteractionType}, ${customparams})`;
|
${allowStack}, ${cansiton ? "1" : "0"}, ${canlayon ? "1" : "0"}, ${canstandon ? "1" : "0"}, ${userInteractionType}, ${customparams})`);
|
||||||
return nextId;
|
return nextId;
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -272,9 +277,9 @@ export async function uploadSingleFurni(params: {
|
|||||||
try {
|
try {
|
||||||
const pageId = await getOrCreateCategoryPage(classname, itemType);
|
const pageId = await getOrCreateCategoryPage(classname, itemType);
|
||||||
catalogItemId = await allocateCatalogItemId(async (nextCatalogId) => {
|
catalogItemId = await allocateCatalogItemId(async (nextCatalogId) => {
|
||||||
await prisma.$executeRaw`
|
await db.execute(sql`
|
||||||
INSERT INTO catalog_items (id, page_id, item_ids, catalog_name, cost_credits, cost_points, points_type, amount, order_number, offer_id, extradata)
|
INSERT INTO catalog_items (id, page_id, item_ids, catalog_name, cost_credits, cost_points, points_type, amount, order_number, offer_id, extradata)
|
||||||
VALUES (${nextCatalogId}, ${String(pageId)}, ${String(newId)}, ${classname}, ${price.credits}, ${price.points}, ${price.pointsType}, 1, 1, '-1', '')`;
|
VALUES (${nextCatalogId}, ${String(pageId)}, ${String(newId)}, ${classname}, ${price.credits}, ${price.points}, ${price.pointsType}, 1, 1, '-1', '')`);
|
||||||
return nextCatalogId;
|
return nextCatalogId;
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
|
import { eq } from "drizzle-orm";
|
||||||
import { unstable_cache } from "next/cache";
|
import { unstable_cache } from "next/cache";
|
||||||
import { prisma } from "@/lib/prisma";
|
import { db, UserWatch } from "@/lib/db";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the set of user IDs that the given staff member has marked as
|
* Returns the set of user IDs that the given staff member has marked as
|
||||||
@@ -12,10 +13,10 @@ export async function getWatchedUserIds(staffId: number): Promise<Set<number>> {
|
|||||||
|
|
||||||
const getCachedWatchedIds = unstable_cache(
|
const getCachedWatchedIds = unstable_cache(
|
||||||
async (staffId: number): Promise<number[]> => {
|
async (staffId: number): Promise<number[]> => {
|
||||||
const rows = await prisma.userWatch.findMany({
|
const rows = await db
|
||||||
where: { staffId },
|
.select({ targetUserId: UserWatch.targetUserId })
|
||||||
select: { targetUserId: true },
|
.from(UserWatch)
|
||||||
});
|
.where(eq(UserWatch.staffId, staffId));
|
||||||
return rows.map((r) => r.targetUserId);
|
return rows.map((r) => r.targetUserId);
|
||||||
},
|
},
|
||||||
["user-watch-list"],
|
["user-watch-list"],
|
||||||
|
|||||||
Reference in new issue
Block a user