P1-P3 admin cleanup: rank guard, SSRF guard, count caching, dead code removal, pagination rewrite
CI / check (push) Canceled after 0s
CI / tests-unit (push) Canceled after 0s
CI / tests-integration (push) Canceled after 0s
CI / tests-ui (push) Canceled after 0s
CI / preflight (push) Canceled after 0s
CI / deploy (push) Canceled after 0s
Gitea Runner Clean Test / test-job (push) Canceled after 0s
Gitea Actions Runner Test / test-job (push) Canceled after 0s
CI / check (push) Canceled after 0s
CI / tests-unit (push) Canceled after 0s
CI / tests-integration (push) Canceled after 0s
CI / tests-ui (push) Canceled after 0s
CI / preflight (push) Canceled after 0s
CI / deploy (push) Canceled after 0s
Gitea Runner Clean Test / test-job (push) Canceled after 0s
Gitea Actions Runner Test / test-job (push) Canceled after 0s
- P1: new src/lib/admin/rank-guard.ts with tryGuardRank/guardRank + RankGuardTarget/Result; extracted from users.ts; applied to admin-bans (create/lift + rank redirect) and admin-badges (caps + rank guard + error surfacing); import-url-safety.ts SSRF guard wired into clone save+icon fetch; logAudit await in help-tickets; antiddos mutations SETTINGS_EDIT; markAllAlertsRead NOTIFICATIONS_EDIT; rankError/saveError in all 25 message files; tests rew/new for bans/badges/rank-guard/import-url-safety - P2: ticket-inbox merged pagination reworked to single UNION ALL SQL with LIMIT/OFFSET (avoids O(offset) materialization); getCachedAdminCount with cacheFiltered option applied to devops/errors/photos/items/guilds/help-tickets/moderation pages; admin-theme catches→?error=1+banner+25 locales; logger in silent catches (rare-values/writeable-boxes); catalog-items-loader CAST→plain equality; count caching helper extended with opt-in cacheFiltered - P3: removed dead modules is-staff/notice/recent-furni-resync/log-filters plus load-user-sanctions/user-sanction-timeline; removed orphaned CATALOG_PAGE_COLUMNS re-export; removed getFurniIconUrl from studio-furni-icons Build: lint 0, typecheck 0, 3480 tests pass, i18n:check exit 0, colors exit 0. Circular chunk warning (9452/10535/66463) is webpack code-splitting artifact; build succeeds.
This commit is contained in:
1 parent
57529fd1ea
commit
7b249a7222
71 files changed
+1031
-1078
No files matched your search
@@ -1,14 +0,0 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { isStaff } from "./is-staff";
|
||||
|
||||
describe("isStaff", () => {
|
||||
it("is true at or above the min staff rank", () => {
|
||||
expect(isStaff(7, 7)).toBe(true);
|
||||
expect(isStaff(10, 7)).toBe(true);
|
||||
});
|
||||
|
||||
it("is false below the min staff rank", () => {
|
||||
expect(isStaff(6, 7)).toBe(false);
|
||||
expect(isStaff(1, 7)).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +0,0 @@
|
||||
/** AtomCMS housekeeping gate: staff are users with rank >= min_staff_rank. */
|
||||
export function isStaff(rank: number, minStaffRank: number): boolean {
|
||||
return rank >= minStaffRank;
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { buildStaffActivityWhere } from "./log-filters";
|
||||
|
||||
describe("buildStaffActivityWhere", () => {
|
||||
it("returns an empty where when no filters are set", () => {
|
||||
expect(buildStaffActivityWhere({})).toEqual({});
|
||||
});
|
||||
|
||||
it("builds an OR search across action, description and IP", () => {
|
||||
const where = buildStaffActivityWhere({ q: "ban " });
|
||||
expect(where.OR).toEqual([
|
||||
{ action: { contains: "ban" } },
|
||||
{ description: { contains: "ban" } },
|
||||
{ ipAddress: { contains: "ban" } },
|
||||
]);
|
||||
expect(where.OR?.[0]).toEqual({ action: { contains: "ban" } });
|
||||
});
|
||||
|
||||
it("sets userId when staffId is provided", () => {
|
||||
const where = buildStaffActivityWhere({ staffId: 42 });
|
||||
expect(where.userId).toBe(BigInt(42));
|
||||
});
|
||||
|
||||
it("uses a prefix match for authorization-only", () => {
|
||||
expect(buildStaffActivityWhere({ authorizationOnly: true })).toEqual({
|
||||
action: { startsWith: "permission." },
|
||||
});
|
||||
});
|
||||
|
||||
it("uses a contains match for a specific action", () => {
|
||||
expect(buildStaffActivityWhere({ action: "ban" })).toEqual({
|
||||
action: { contains: "ban" },
|
||||
});
|
||||
});
|
||||
|
||||
it("ignores whitespace-only search terms", () => {
|
||||
expect(buildStaffActivityWhere({ q: " " })).toEqual({});
|
||||
});
|
||||
});
|
||||
@@ -1,28 +0,0 @@
|
||||
export interface StaffActivityFilters {
|
||||
q?: string;
|
||||
staffId?: number | null;
|
||||
action?: string | null;
|
||||
authorizationOnly?: boolean;
|
||||
}
|
||||
|
||||
interface StaffActivityWhere {
|
||||
OR?: Array<Record<string, { contains?: string }>>;
|
||||
userId?: bigint;
|
||||
action?: { startsWith: string } | { contains: string };
|
||||
}
|
||||
|
||||
export function buildStaffActivityWhere(
|
||||
filters: StaffActivityFilters,
|
||||
): StaffActivityWhere {
|
||||
const where: StaffActivityWhere = {};
|
||||
if (filters.q?.trim())
|
||||
where.OR = [
|
||||
{ action: { contains: filters.q.trim() } },
|
||||
{ description: { contains: filters.q.trim() } },
|
||||
{ ipAddress: { contains: filters.q.trim() } },
|
||||
];
|
||||
if (filters.staffId) where.userId = BigInt(filters.staffId);
|
||||
if (filters.authorizationOnly) where.action = { startsWith: "permission." };
|
||||
else if (filters.action) where.action = { contains: filters.action };
|
||||
return where;
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { adminMutationNotice } from "./notice";
|
||||
|
||||
describe("adminMutationNotice", () => {
|
||||
it("returns a danger notice when there is an error", () => {
|
||||
expect(adminMutationNotice({ error: "boom" })).toEqual({
|
||||
tone: "danger",
|
||||
label: "Error",
|
||||
message: "The operation could not be completed.",
|
||||
});
|
||||
});
|
||||
it("returns a success notice when saved is 1", () => {
|
||||
expect(adminMutationNotice({ saved: "1" })).toEqual({
|
||||
tone: "ok",
|
||||
label: "Saved",
|
||||
message: "Changes were saved successfully.",
|
||||
});
|
||||
});
|
||||
it("returns null when nothing is set", () => {
|
||||
expect(adminMutationNotice({})).toBeNull();
|
||||
});
|
||||
it("prioritises errors over saved flags", () => {
|
||||
const notice = adminMutationNotice({ saved: "1", error: "x" });
|
||||
expect(notice?.tone).toBe("danger");
|
||||
});
|
||||
});
|
||||
@@ -1,26 +0,0 @@
|
||||
export interface AdminMutationNotice {
|
||||
tone: "ok" | "danger";
|
||||
label: "Saved" | "Error";
|
||||
message: string;
|
||||
}
|
||||
|
||||
export function adminMutationNotice(params: {
|
||||
saved?: string;
|
||||
error?: string;
|
||||
}): AdminMutationNotice | null {
|
||||
if (params.error) {
|
||||
return {
|
||||
tone: "danger",
|
||||
label: "Error",
|
||||
message: "The operation could not be completed.",
|
||||
};
|
||||
}
|
||||
if (params.saved === "1") {
|
||||
return {
|
||||
tone: "ok",
|
||||
label: "Saved",
|
||||
message: "Changes were saved successfully.",
|
||||
};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
// @ts-nocheck
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const state = vi.hoisted(() => ({
|
||||
users: [] as Array<{ username: string; rank: number; mail: string | null }>,
|
||||
highestRank: 8,
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/db", async () => {
|
||||
const schema = await import("@/db/schema");
|
||||
const { createFakeDb } = await import("@/test/fake-db");
|
||||
const fake = createFakeDb((_table, projection) =>
|
||||
"username" in projection ? state.users : [],
|
||||
);
|
||||
return { ...schema, db: fake };
|
||||
});
|
||||
|
||||
vi.mock("@/lib/permissions", () => ({
|
||||
getHighestRank: vi.fn(async () => state.highestRank),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/safe-action-shared", () => ({
|
||||
ActionError: class ActionError extends Error {},
|
||||
}));
|
||||
|
||||
import { getHighestRank } from "@/lib/permissions";
|
||||
import { guardRank, tryGuardRank } from "./rank-guard";
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
state.users = [{ username: "victim", rank: 5, mail: "[email protected]" }];
|
||||
state.highestRank = 8;
|
||||
});
|
||||
|
||||
describe("tryGuardRank", () => {
|
||||
it("allows an actor at a strictly higher rank", async () => {
|
||||
const result = await tryGuardRank(1, 7);
|
||||
expect(result).toEqual({ ok: true, target: state.users[0] });
|
||||
});
|
||||
|
||||
it("blocks an actor at the same rank", async () => {
|
||||
state.users = [{ username: "victim", rank: 7, mail: null }];
|
||||
const result = await tryGuardRank(1, 7);
|
||||
expect(result).toEqual({ ok: false, reason: "rank" });
|
||||
});
|
||||
|
||||
it("blocks an actor at a lower rank", async () => {
|
||||
const result = await tryGuardRank(1, 3);
|
||||
expect(result).toEqual({ ok: false, reason: "rank" });
|
||||
});
|
||||
|
||||
it("lets the dynamic super admin act on anyone", async () => {
|
||||
state.users = [{ username: "victim", rank: 8, mail: "[email protected]" }];
|
||||
state.highestRank = 8;
|
||||
const result = await tryGuardRank(1, 8);
|
||||
expect(result).toEqual({
|
||||
ok: true,
|
||||
target: { username: "victim", rank: 8, mail: "[email protected]" },
|
||||
});
|
||||
expect(await getHighestRank()).toBe(8);
|
||||
});
|
||||
|
||||
it("reports a missing target as not_found", async () => {
|
||||
state.users = [];
|
||||
const result = await tryGuardRank(999, 7);
|
||||
expect(result).toEqual({ ok: false, reason: "not_found" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("guardRank", () => {
|
||||
it("throws a readable ActionError for a target at equal or higher rank", async () => {
|
||||
state.users = [{ username: "victim", rank: 7, mail: null }];
|
||||
await expect(guardRank(1, 7)).rejects.toThrow(
|
||||
"Cannot modify user with equal or higher rank",
|
||||
);
|
||||
});
|
||||
|
||||
it("throws a readable ActionError for a missing target", async () => {
|
||||
state.users = [];
|
||||
await expect(guardRank(999, 7)).rejects.toThrow("User not found");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,65 @@
|
||||
import { eq } from "drizzle-orm";
|
||||
import { isDynamicSuperAdmin } from "@/lib/admin/authorization-policy";
|
||||
import { db, User } from "@/lib/db";
|
||||
import { getHighestRank } from "@/lib/permissions";
|
||||
import { ActionError } from "@/lib/safe-action-shared";
|
||||
|
||||
export interface RankGuardTarget {
|
||||
username: string;
|
||||
rank: number;
|
||||
mail: string | null;
|
||||
}
|
||||
|
||||
export type RankGuardResult =
|
||||
| { ok: true; target: RankGuardTarget }
|
||||
| { ok: false; reason: "not_found" | "rank" };
|
||||
|
||||
/**
|
||||
* Refuse to act on a target whose rank is equal to or higher than the actor's,
|
||||
* unless the actor is the dynamic super admin (the account holding the hotel's
|
||||
* current highest rank). This is the single source of truth for the
|
||||
* "cannot modify equal/higher rank" invariant; every admin action that mutates
|
||||
* a specific user must go through it.
|
||||
*
|
||||
* Returns a discriminated result instead of throwing so callers can choose
|
||||
* between "block the request" and "tolerate a deleted user" (e.g. lifting a
|
||||
* ban for an account that no longer exists).
|
||||
*/
|
||||
export async function tryGuardRank(
|
||||
targetUserId: number,
|
||||
sessionRank: number,
|
||||
): Promise<RankGuardResult> {
|
||||
const [target] = await db
|
||||
.select({
|
||||
username: User.username,
|
||||
rank: User.rank,
|
||||
mail: User.mail,
|
||||
})
|
||||
.from(User)
|
||||
.where(eq(User.id, targetUserId))
|
||||
.limit(1);
|
||||
if (!target) return { ok: false, reason: "not_found" };
|
||||
const highestRank = await getHighestRank();
|
||||
if (
|
||||
target.rank >= sessionRank &&
|
||||
!isDynamicSuperAdmin(sessionRank, highestRank)
|
||||
) {
|
||||
return { ok: false, reason: "rank" };
|
||||
}
|
||||
return { ok: true, target };
|
||||
}
|
||||
|
||||
export async function guardRank(
|
||||
targetUserId: number,
|
||||
sessionRank: number,
|
||||
): Promise<RankGuardTarget> {
|
||||
const result = await tryGuardRank(targetUserId, sessionRank);
|
||||
if (!result.ok) {
|
||||
throw new ActionError(
|
||||
result.reason === "not_found"
|
||||
? "User not found"
|
||||
: "Cannot modify user with equal or higher rank",
|
||||
);
|
||||
}
|
||||
return result.target;
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
RECENT_FURNI_RESYNC_URL,
|
||||
requestRecentFurniResync,
|
||||
} from "./recent-furni-resync";
|
||||
|
||||
describe("requestRecentFurniResync", () => {
|
||||
it("POSTs to the resync endpoint and maps the payload", async () => {
|
||||
const fetcher = vi.fn().mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
examined: 10,
|
||||
resynced: 4,
|
||||
failed: 1,
|
||||
rconOk: true,
|
||||
errors: [{ classname: "sofa", message: "boom" }],
|
||||
}),
|
||||
});
|
||||
const result = await requestRecentFurniResync(fetcher);
|
||||
expect(fetcher).toHaveBeenCalledWith(RECENT_FURNI_RESYNC_URL, {
|
||||
method: "POST",
|
||||
});
|
||||
expect(result).toEqual({
|
||||
examined: 10,
|
||||
resynced: 4,
|
||||
failed: 1,
|
||||
rconOk: true,
|
||||
errors: [{ classname: "sofa", message: "boom" }],
|
||||
});
|
||||
});
|
||||
|
||||
it("throws with the server error message on failure", async () => {
|
||||
const fetcher = vi.fn().mockResolvedValue({
|
||||
ok: false,
|
||||
json: async () => ({ error: "no permission" }),
|
||||
});
|
||||
await expect(requestRecentFurniResync(fetcher)).rejects.toThrow(
|
||||
"no permission",
|
||||
);
|
||||
});
|
||||
|
||||
it("falls back to a generic error message", async () => {
|
||||
const fetcher = vi.fn().mockResolvedValue({
|
||||
ok: false,
|
||||
json: async () => ({}),
|
||||
});
|
||||
await expect(requestRecentFurniResync(fetcher)).rejects.toThrow(
|
||||
"Furni resync failed",
|
||||
);
|
||||
});
|
||||
|
||||
it("filters malformed error entries and coerces counts", async () => {
|
||||
const fetcher = vi.fn().mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
examined: "10",
|
||||
resynced: undefined,
|
||||
failed: null,
|
||||
rconOk: "yes",
|
||||
errors: [{ classname: "a", message: "b" }, { nope: 1 }, "x"],
|
||||
}),
|
||||
});
|
||||
const result = await requestRecentFurniResync(fetcher);
|
||||
expect(result.examined).toBe(10);
|
||||
expect(result.resynced).toBe(0);
|
||||
expect(result.failed).toBe(0);
|
||||
expect(result.rconOk).toBe(false);
|
||||
expect(result.errors).toEqual([{ classname: "a", message: "b" }]);
|
||||
});
|
||||
});
|
||||
@@ -1,49 +0,0 @@
|
||||
export const RECENT_FURNI_RESYNC_URL = "/api/admin/import/furni/resync?days=7";
|
||||
|
||||
export interface RecentFurniResyncError {
|
||||
classname: string;
|
||||
message: string;
|
||||
}
|
||||
|
||||
export interface RecentFurniResyncResult {
|
||||
examined: number;
|
||||
resynced: number;
|
||||
failed: number;
|
||||
rconOk: boolean;
|
||||
errors: RecentFurniResyncError[];
|
||||
}
|
||||
|
||||
type AdminFetcher = (
|
||||
input: RequestInfo | URL,
|
||||
init?: RequestInit,
|
||||
) => Promise<Response>;
|
||||
|
||||
export async function requestRecentFurniResync(
|
||||
fetcher: AdminFetcher,
|
||||
): Promise<RecentFurniResyncResult> {
|
||||
const response = await fetcher(RECENT_FURNI_RESYNC_URL, { method: "POST" });
|
||||
const payload = (await response.json()) as Record<string, unknown>;
|
||||
if (!response.ok) {
|
||||
throw new Error(
|
||||
typeof payload.error === "string" ? payload.error : "Furni resync failed",
|
||||
);
|
||||
}
|
||||
|
||||
const errors = Array.isArray(payload.errors)
|
||||
? payload.errors.filter(
|
||||
(value): value is RecentFurniResyncError =>
|
||||
typeof value === "object" &&
|
||||
value !== null &&
|
||||
typeof (value as RecentFurniResyncError).classname === "string" &&
|
||||
typeof (value as RecentFurniResyncError).message === "string",
|
||||
)
|
||||
: [];
|
||||
|
||||
return {
|
||||
examined: Number(payload.examined) || 0,
|
||||
resynced: Number(payload.resynced) || 0,
|
||||
failed: Number(payload.failed) || 0,
|
||||
rconOk: payload.rconOk === true,
|
||||
errors,
|
||||
};
|
||||
}
|
||||
+166
-229
@@ -1,20 +1,17 @@
|
||||
import "server-only";
|
||||
|
||||
import {
|
||||
and,
|
||||
count,
|
||||
desc,
|
||||
eq,
|
||||
inArray,
|
||||
like,
|
||||
ne,
|
||||
or,
|
||||
type SQL,
|
||||
} from "drizzle-orm";
|
||||
import { and, count, eq, like, ne, or, type SQL, sql } from "drizzle-orm";
|
||||
import { alias } from "drizzle-orm/mysql-core";
|
||||
import { calcPagination } from "@/lib/admin-helpers";
|
||||
import { db, User, WebsiteHelpCenterTickets, WebsiteTicket } from "@/lib/db";
|
||||
import {
|
||||
db,
|
||||
queryRows,
|
||||
User,
|
||||
WebsiteHelpCenterTickets,
|
||||
WebsiteTicket,
|
||||
} from "@/lib/db";
|
||||
import { formatDate } from "@/lib/format-date";
|
||||
import { logger } from "@/lib/logger";
|
||||
|
||||
export type TicketInboxKind = "cms" | "help";
|
||||
export type TicketInboxTypeFilter = "all" | TicketInboxKind;
|
||||
@@ -43,6 +40,17 @@ export interface FetchTicketInboxOptions {
|
||||
base?: "admin" | "mod";
|
||||
}
|
||||
|
||||
interface InboxRawRow {
|
||||
kind: TicketInboxKind;
|
||||
id: string;
|
||||
title: string;
|
||||
username: string | null;
|
||||
user_id: number | null;
|
||||
status: string;
|
||||
is_open: number | boolean;
|
||||
sort_at: Date | null;
|
||||
}
|
||||
|
||||
function toSortMs(value: Date | string | null | undefined): number {
|
||||
if (!value) return 0;
|
||||
const ms =
|
||||
@@ -50,160 +58,160 @@ function toSortMs(value: Date | string | null | undefined): number {
|
||||
return Number.isFinite(ms) ? ms : 0;
|
||||
}
|
||||
|
||||
async function fetchCmsCandidates(
|
||||
limit: number,
|
||||
/**
|
||||
* Builds a single UNION ALL over both queues, normalized to the same column
|
||||
* shape so the database can apply ORDER BY + LIMIT/OFFSET. This keeps deep
|
||||
* pages O(perPage) instead of materializing every row up to the offset (the
|
||||
* previous app-side merge fetched `offset + perPage` rows from each source).
|
||||
*
|
||||
* Ordering matches the old merge: newest first, ties broken by descending
|
||||
* `kind` then descending string `id` (equivalent to the old `${kind}-${id}`
|
||||
* key comparison).
|
||||
*/
|
||||
function buildInboxUnion(
|
||||
includeCms: boolean,
|
||||
includeHelp: boolean,
|
||||
search: string,
|
||||
openOnly: boolean,
|
||||
base: "admin" | "mod",
|
||||
): Promise<TicketInboxRow[]> {
|
||||
const Creator = alias(User, "inbox_ticket_creator");
|
||||
const conditions: SQL[] = [];
|
||||
): SQL {
|
||||
const branches: SQL[] = [];
|
||||
|
||||
if (openOnly) {
|
||||
conditions.push(ne(WebsiteTicket.status, "closed"));
|
||||
if (includeCms) {
|
||||
const conditions: SQL[] = [];
|
||||
if (openOnly) conditions.push(sql`t.status <> 'closed'`);
|
||||
if (search) {
|
||||
const pattern = `%${search}%`;
|
||||
const parts: SQL[] = [
|
||||
sql`t.subject LIKE ${pattern}`,
|
||||
sql`t.category LIKE ${pattern}`,
|
||||
sql`EXISTS (SELECT 1 FROM users cu WHERE cu.id = t.creator_id AND cu.username LIKE ${pattern})`,
|
||||
];
|
||||
const asId = Number(search);
|
||||
if (Number.isFinite(asId) && asId > 0) parts.push(sql`t.id = ${asId}`);
|
||||
conditions.push(sql`(${sql.join(parts, sql` OR `)})`);
|
||||
}
|
||||
const where =
|
||||
conditions.length > 0
|
||||
? sql` WHERE ${sql.join(conditions, sql` AND `)}`
|
||||
: sql``;
|
||||
branches.push(sql`
|
||||
SELECT 'cms' AS kind, CAST(t.id AS CHAR) AS id, t.subject AS title,
|
||||
cu.username AS username, t.creator_id AS user_id, t.status AS status,
|
||||
(t.status <> 'closed') AS is_open,
|
||||
COALESCE(t.updated_at, t.created_at) AS sort_at
|
||||
FROM website_tickets t
|
||||
LEFT JOIN users cu ON cu.id = t.creator_id${where}
|
||||
`);
|
||||
}
|
||||
|
||||
if (search) {
|
||||
const asId = Number(search);
|
||||
const searchOr = [
|
||||
like(WebsiteTicket.subject, `%${search}%`),
|
||||
like(WebsiteTicket.category, `%${search}%`),
|
||||
like(Creator.username, `%${search}%`),
|
||||
...(Number.isFinite(asId) && asId > 0
|
||||
? [eq(WebsiteTicket.id, asId)]
|
||||
: []),
|
||||
];
|
||||
const searchFilter = or(...searchOr);
|
||||
if (searchFilter) conditions.push(searchFilter);
|
||||
if (includeHelp) {
|
||||
const conditions: SQL[] = [];
|
||||
if (openOnly) conditions.push(sql`h.open = 1`);
|
||||
if (search) {
|
||||
const pattern = `%${search}%`;
|
||||
const parts: SQL[] = [
|
||||
sql`h.title LIKE ${pattern}`,
|
||||
sql`h.content LIKE ${pattern}`,
|
||||
sql`EXISTS (SELECT 1 FROM users hu WHERE hu.id = h.user_id AND hu.username LIKE ${pattern})`,
|
||||
];
|
||||
if (/^\d+$/.test(search)) parts.push(sql`h.id = ${search}`);
|
||||
conditions.push(sql`(${sql.join(parts, sql` OR `)})`);
|
||||
}
|
||||
const where =
|
||||
conditions.length > 0
|
||||
? sql` WHERE ${sql.join(conditions, sql` AND `)}`
|
||||
: sql``;
|
||||
branches.push(sql`
|
||||
SELECT 'help' AS kind, CAST(h.id AS CHAR) AS id, h.title AS title,
|
||||
hu.username AS username, h.user_id AS user_id,
|
||||
IF(h.open = 1, 'open', 'closed') AS status, h.open AS is_open,
|
||||
COALESCE(h.updated_at, h.created_at) AS sort_at
|
||||
FROM website_help_center_tickets h
|
||||
LEFT JOIN users hu ON hu.id = h.user_id${where}
|
||||
`);
|
||||
}
|
||||
|
||||
const where = conditions.length > 0 ? and(...conditions) : undefined;
|
||||
|
||||
const rows = await db
|
||||
.select({
|
||||
id: WebsiteTicket.id,
|
||||
subject: WebsiteTicket.subject,
|
||||
status: WebsiteTicket.status,
|
||||
creatorId: WebsiteTicket.creatorId,
|
||||
updatedAt: WebsiteTicket.updatedAt,
|
||||
createdAt: WebsiteTicket.createdAt,
|
||||
creatorUsername: Creator.username,
|
||||
})
|
||||
.from(WebsiteTicket)
|
||||
.leftJoin(Creator, eq(WebsiteTicket.creatorId, Creator.id))
|
||||
.where(where)
|
||||
.orderBy(desc(WebsiteTicket.updatedAt), desc(WebsiteTicket.id))
|
||||
.limit(limit)
|
||||
.catch(() => []);
|
||||
|
||||
const prefix = base === "mod" ? "/mod/tickets" : "/admin/tickets";
|
||||
|
||||
return rows.map((row) => ({
|
||||
key: `cms-${row.id}`,
|
||||
kind: "cms" as const,
|
||||
id: String(row.id),
|
||||
title: row.subject,
|
||||
user: row.creatorUsername ?? "—",
|
||||
userId: row.creatorId,
|
||||
status: row.status,
|
||||
open: row.status !== "closed",
|
||||
sortAt: toSortMs(row.updatedAt ?? row.createdAt),
|
||||
date: formatDate(row.updatedAt ?? row.createdAt, "date"),
|
||||
href: `${prefix}/${row.id}`,
|
||||
}));
|
||||
return sql.join(branches, sql` UNION ALL `);
|
||||
}
|
||||
|
||||
async function fetchHelpCandidates(
|
||||
limit: number,
|
||||
search: string,
|
||||
openOnly: boolean,
|
||||
base: "admin" | "mod",
|
||||
): Promise<TicketInboxRow[]> {
|
||||
const conditions: SQL[] = [];
|
||||
/** Merged read-model over CMS desk + help-center queues (single DB query). */
|
||||
export async function fetchUnifiedTicketInbox(
|
||||
options: FetchTicketInboxOptions,
|
||||
): Promise<{
|
||||
rows: TicketInboxRow[];
|
||||
total: number;
|
||||
page: number;
|
||||
perPage: number;
|
||||
lastPage: number;
|
||||
cmsTotal: number;
|
||||
helpTotal: number;
|
||||
}> {
|
||||
const type = options.type ?? "all";
|
||||
const openOnly = options.openOnly !== false;
|
||||
const search = options.search?.trim() ?? "";
|
||||
const base = options.base ?? "admin";
|
||||
|
||||
if (openOnly) {
|
||||
conditions.push(eq(WebsiteHelpCenterTickets.open, true));
|
||||
const includeCms = type === "all" || type === "cms";
|
||||
const includeHelp = type === "all" || type === "help";
|
||||
|
||||
const [cmsTotal, helpTotal] = await Promise.all([
|
||||
includeCms ? countCms(search, openOnly) : Promise.resolve(0),
|
||||
includeHelp ? countHelp(search, openOnly) : Promise.resolve(0),
|
||||
]);
|
||||
|
||||
const total = cmsTotal + helpTotal;
|
||||
const pagination = calcPagination(total, options.page, options.perPage);
|
||||
|
||||
const cmsPrefix = base === "mod" ? "/mod/tickets" : "/admin/tickets";
|
||||
const helpPrefix =
|
||||
base === "mod" ? "/mod/help-tickets" : "/admin/help-tickets";
|
||||
|
||||
let rows: TicketInboxRow[] = [];
|
||||
if (includeCms || includeHelp) {
|
||||
const union = buildInboxUnion(includeCms, includeHelp, search, openOnly);
|
||||
try {
|
||||
const raw = await queryRows<InboxRawRow>(sql`
|
||||
SELECT * FROM (${union}) AS merged
|
||||
ORDER BY sort_at DESC, kind DESC, id DESC
|
||||
LIMIT ${pagination.perPage} OFFSET ${pagination.offset}
|
||||
`);
|
||||
rows = raw.map((row) => {
|
||||
const open = Boolean(row.is_open);
|
||||
return {
|
||||
key: `${row.kind}-${row.id}`,
|
||||
kind: row.kind,
|
||||
id: String(row.id),
|
||||
title: row.title,
|
||||
user:
|
||||
row.username ??
|
||||
(row.kind === "help" && row.user_id != null
|
||||
? `#${row.user_id}`
|
||||
: "—"),
|
||||
userId: row.user_id,
|
||||
status: row.kind === "cms" ? row.status : open ? "open" : "closed",
|
||||
open,
|
||||
sortAt: toSortMs(row.sort_at),
|
||||
date: formatDate(row.sort_at, "date"),
|
||||
href:
|
||||
row.kind === "cms"
|
||||
? `${cmsPrefix}/${row.id}`
|
||||
: `${helpPrefix}/${row.id}`,
|
||||
};
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error("Failed to load unified ticket inbox", { error });
|
||||
}
|
||||
}
|
||||
|
||||
if (search) {
|
||||
const asId = /^\d+$/.test(search) ? BigInt(search) : null;
|
||||
const matchingUsers = await db
|
||||
.select({ id: User.id })
|
||||
.from(User)
|
||||
.where(like(User.username, `%${search}%`))
|
||||
.limit(50)
|
||||
.catch(() => []);
|
||||
|
||||
const searchOr = [
|
||||
like(WebsiteHelpCenterTickets.title, `%${search}%`),
|
||||
like(WebsiteHelpCenterTickets.content, `%${search}%`),
|
||||
...(matchingUsers.length > 0
|
||||
? [
|
||||
inArray(
|
||||
WebsiteHelpCenterTickets.userId,
|
||||
matchingUsers.map((u) => u.id),
|
||||
),
|
||||
]
|
||||
: []),
|
||||
...(asId !== null ? [eq(WebsiteHelpCenterTickets.id, asId)] : []),
|
||||
];
|
||||
const searchFilter = or(...searchOr);
|
||||
if (searchFilter) conditions.push(searchFilter);
|
||||
}
|
||||
|
||||
const where = conditions.length > 0 ? and(...conditions) : undefined;
|
||||
|
||||
const rows = await db
|
||||
.select({
|
||||
id: WebsiteHelpCenterTickets.id,
|
||||
title: WebsiteHelpCenterTickets.title,
|
||||
open: WebsiteHelpCenterTickets.open,
|
||||
userId: WebsiteHelpCenterTickets.userId,
|
||||
updatedAt: WebsiteHelpCenterTickets.updatedAt,
|
||||
createdAt: WebsiteHelpCenterTickets.createdAt,
|
||||
})
|
||||
.from(WebsiteHelpCenterTickets)
|
||||
.where(where)
|
||||
.orderBy(
|
||||
desc(WebsiteHelpCenterTickets.updatedAt),
|
||||
desc(WebsiteHelpCenterTickets.id),
|
||||
)
|
||||
.limit(limit)
|
||||
.catch(() => []);
|
||||
|
||||
const userIds = [
|
||||
...new Set(
|
||||
rows.map((row) => row.userId).filter((id): id is number => id != null),
|
||||
),
|
||||
];
|
||||
const users =
|
||||
userIds.length > 0
|
||||
? await db
|
||||
.select({ id: User.id, username: User.username })
|
||||
.from(User)
|
||||
.where(inArray(User.id, userIds))
|
||||
.catch(() => [])
|
||||
: [];
|
||||
const usernameById = new Map(users.map((u) => [u.id, u.username]));
|
||||
|
||||
const prefix = base === "mod" ? "/mod/help-tickets" : "/admin/help-tickets";
|
||||
|
||||
return rows.map((row) => ({
|
||||
key: `help-${row.id}`,
|
||||
kind: "help" as const,
|
||||
id: String(row.id),
|
||||
title: row.title,
|
||||
user:
|
||||
row.userId != null
|
||||
? (usernameById.get(row.userId) ?? `#${row.userId}`)
|
||||
: "—",
|
||||
userId: row.userId,
|
||||
status: row.open ? "open" : "closed",
|
||||
open: row.open,
|
||||
sortAt: toSortMs(row.updatedAt ?? row.createdAt),
|
||||
date: formatDate(row.updatedAt ?? row.createdAt, "date"),
|
||||
href: `${prefix}/${row.id}`,
|
||||
}));
|
||||
return {
|
||||
rows,
|
||||
total,
|
||||
page: pagination.page,
|
||||
perPage: pagination.perPage,
|
||||
lastPage: pagination.lastPage,
|
||||
cmsTotal,
|
||||
helpTotal,
|
||||
};
|
||||
}
|
||||
|
||||
async function countCms(search: string, openOnly: boolean): Promise<number> {
|
||||
@@ -236,24 +244,12 @@ async function countHelp(search: string, openOnly: boolean): Promise<number> {
|
||||
const conditions: SQL[] = [];
|
||||
if (openOnly) conditions.push(eq(WebsiteHelpCenterTickets.open, true));
|
||||
if (search) {
|
||||
const pattern = `%${search}%`;
|
||||
const asId = /^\d+$/.test(search) ? BigInt(search) : null;
|
||||
const matchingUsers = await db
|
||||
.select({ id: User.id })
|
||||
.from(User)
|
||||
.where(like(User.username, `%${search}%`))
|
||||
.limit(50)
|
||||
.catch(() => []);
|
||||
const searchFilter = or(
|
||||
like(WebsiteHelpCenterTickets.title, `%${search}%`),
|
||||
like(WebsiteHelpCenterTickets.content, `%${search}%`),
|
||||
...(matchingUsers.length > 0
|
||||
? [
|
||||
inArray(
|
||||
WebsiteHelpCenterTickets.userId,
|
||||
matchingUsers.map((u) => u.id),
|
||||
),
|
||||
]
|
||||
: []),
|
||||
like(WebsiteHelpCenterTickets.title, pattern),
|
||||
like(WebsiteHelpCenterTickets.content, pattern),
|
||||
sql`EXISTS (SELECT 1 FROM users hu WHERE hu.id = ${WebsiteHelpCenterTickets.userId} AND hu.username LIKE ${pattern})`,
|
||||
...(asId !== null ? [eq(WebsiteHelpCenterTickets.id, asId)] : []),
|
||||
);
|
||||
if (searchFilter) conditions.push(searchFilter);
|
||||
@@ -266,62 +262,3 @@ async function countHelp(search: string, openOnly: boolean): Promise<number> {
|
||||
.catch(() => [{ total: 0 }]);
|
||||
return Number(rows[0]?.total ?? 0);
|
||||
}
|
||||
|
||||
/** Merged read-model over CMS desk + help-center queues (no DB merge). */
|
||||
export async function fetchUnifiedTicketInbox(
|
||||
options: FetchTicketInboxOptions,
|
||||
): Promise<{
|
||||
rows: TicketInboxRow[];
|
||||
total: number;
|
||||
page: number;
|
||||
perPage: number;
|
||||
lastPage: number;
|
||||
cmsTotal: number;
|
||||
helpTotal: number;
|
||||
}> {
|
||||
const type = options.type ?? "all";
|
||||
const openOnly = options.openOnly !== false;
|
||||
const search = options.search?.trim() ?? "";
|
||||
const base = options.base ?? "admin";
|
||||
|
||||
const includeCms = type === "all" || type === "cms";
|
||||
const includeHelp = type === "all" || type === "help";
|
||||
|
||||
const [cmsTotal, helpTotal] = await Promise.all([
|
||||
includeCms ? countCms(search, openOnly) : Promise.resolve(0),
|
||||
includeHelp ? countHelp(search, openOnly) : Promise.resolve(0),
|
||||
]);
|
||||
|
||||
const total = cmsTotal + helpTotal;
|
||||
const pagination = calcPagination(total, options.page, options.perPage);
|
||||
const window = pagination.offset + pagination.perPage;
|
||||
|
||||
const [cmsRows, helpRows] = await Promise.all([
|
||||
includeCms
|
||||
? fetchCmsCandidates(window, search, openOnly, base)
|
||||
: Promise.resolve([]),
|
||||
includeHelp
|
||||
? fetchHelpCandidates(window, search, openOnly, base)
|
||||
: Promise.resolve([]),
|
||||
]);
|
||||
|
||||
const merged = [...cmsRows, ...helpRows].sort((a, b) => {
|
||||
if (b.sortAt !== a.sortAt) return b.sortAt - a.sortAt;
|
||||
return a.key < b.key ? 1 : -1;
|
||||
});
|
||||
|
||||
const rows = merged.slice(
|
||||
pagination.offset,
|
||||
pagination.offset + pagination.perPage,
|
||||
);
|
||||
|
||||
return {
|
||||
rows,
|
||||
total,
|
||||
page: pagination.page,
|
||||
perPage: pagination.perPage,
|
||||
lastPage: pagination.lastPage,
|
||||
cmsTotal,
|
||||
helpTotal,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user