17 lines
1.4 KiB
TypeScript
17 lines
1.4 KiB
TypeScript
import { z } from "zod"
|
|
import { and, eq, inArray, asc } from "drizzle-orm"
|
|
import { db } from "../../db/index.js"
|
|
import { servers } from "../../db/schema.js"
|
|
import type { ActionContext } from "./action-types.js"
|
|
|
|
export const catalogPaging = z.object({ serverId: z.number().int().positive().safe().optional(), serverOffset: z.number().int().min(0).max(10000).default(0), serverLimit: z.number().int().min(1).max(10).default(5), offset: z.number().int().min(0).max(100000).default(0), limit: z.number().int().min(1).max(200).default(100) }).strict()
|
|
|
|
/** Resolve only the policy already intersected with the credential's grant,
|
|
* before constructing clients or polling a device. */
|
|
export async function catalogDevices(input: Record<string, unknown>, context: ActionContext, options: { includeDisabled?: boolean } = {}) {
|
|
const ids = input.serverId ? [Number(input.serverId)] : context.connection.policy.scope.serverIds
|
|
const eligible = ids.length ? await db.select().from(servers).where(and(inArray(servers.id, ids), ...(options.includeDisabled ? [] : [eq(servers.enabled, true)]))).orderBy(asc(servers.id)) : []
|
|
const start = Number(input.serverOffset), selected = eligible.slice(start, start + Number(input.serverLimit))
|
|
return { selected, serverIds: selected.map(row => row.id), totalServers: eligible.length, nextServerOffset: start + selected.length < eligible.length ? start + selected.length : null, incomplete: selected.length < eligible.length }
|
|
}
|