You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1.3 KiB
40 lines
1.3 KiB
import { dbGlobal } from "drizzle-pkg/lib/db";
|
|
import { users } from "drizzle-pkg/lib/schema/auth";
|
|
import { posts, timelineEvents } from "drizzle-pkg/lib/schema/content";
|
|
import { rssFeeds } from "drizzle-pkg/lib/schema/rss";
|
|
import { desc, sql } from "drizzle-orm";
|
|
import { requireAdmin } from "#server/utils/admin-guard";
|
|
|
|
export default defineWrappedResponseHandler(async (event) => {
|
|
await requireAdmin(event);
|
|
|
|
const q = getQuery(event);
|
|
const limit = Math.min(Number(q.limit ?? 50) || 50, 100);
|
|
const offset = Math.max(Number(q.offset ?? 0) || 0, 0);
|
|
|
|
const rows = await dbGlobal
|
|
.select({
|
|
id: users.id,
|
|
username: users.username,
|
|
email: users.email,
|
|
role: users.role,
|
|
status: users.status,
|
|
publicSlug: users.publicSlug,
|
|
createdAt: users.createdAt,
|
|
postCount: sql<number>`(select count(*) from ${posts} where ${posts.userId} = ${users.id})`.mapWith(
|
|
Number,
|
|
),
|
|
timelineEventCount: sql<number>`(select count(*) from ${timelineEvents} where ${timelineEvents.userId} = ${users.id})`.mapWith(
|
|
Number,
|
|
),
|
|
rssFeedCount: sql<number>`(select count(*) from ${rssFeeds} where ${rssFeeds.userId} = ${users.id})`.mapWith(
|
|
Number,
|
|
),
|
|
})
|
|
.from(users)
|
|
.orderBy(desc(users.id))
|
|
.limit(limit)
|
|
.offset(offset);
|
|
|
|
return R.success({ users: rows });
|
|
});
|
|
|