refactor(api): extract audit log input schemas
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const auditLogListInputSchema = z.object({
|
||||
entityType: z.string().optional(),
|
||||
entityId: z.string().optional(),
|
||||
userId: z.string().optional(),
|
||||
action: z.string().optional(),
|
||||
source: z.string().optional(),
|
||||
startDate: z.date().optional(),
|
||||
endDate: z.date().optional(),
|
||||
search: z.string().optional(),
|
||||
limit: z.number().min(1).max(100).default(50),
|
||||
cursor: z.string().optional(),
|
||||
});
|
||||
|
||||
export const auditLogByEntityInputSchema = z.object({
|
||||
entityType: z.string(),
|
||||
entityId: z.string(),
|
||||
limit: z.number().min(1).max(200).default(50),
|
||||
});
|
||||
|
||||
export const auditLogTimelineInputSchema = z.object({
|
||||
startDate: z.date().optional(),
|
||||
endDate: z.date().optional(),
|
||||
limit: z.number().min(1).max(500).default(200),
|
||||
});
|
||||
@@ -1,5 +1,10 @@
|
||||
import { z } from "zod";
|
||||
import { createTRPCRouter, controllerProcedure } from "../trpc.js";
|
||||
import {
|
||||
auditLogByEntityInputSchema,
|
||||
auditLogListInputSchema,
|
||||
auditLogTimelineInputSchema,
|
||||
} from "./audit-log-inputs.js";
|
||||
import {
|
||||
formatAuditDetailEntry,
|
||||
formatAuditListEntry,
|
||||
@@ -20,20 +25,7 @@ export const auditLogRouter = createTRPCRouter({
|
||||
* Cursor-based pagination using createdAt + id.
|
||||
*/
|
||||
list: controllerProcedure
|
||||
.input(
|
||||
z.object({
|
||||
entityType: z.string().optional(),
|
||||
entityId: z.string().optional(),
|
||||
userId: z.string().optional(),
|
||||
action: z.string().optional(),
|
||||
source: z.string().optional(),
|
||||
startDate: z.date().optional(),
|
||||
endDate: z.date().optional(),
|
||||
search: z.string().optional(),
|
||||
limit: z.number().min(1).max(100).default(50),
|
||||
cursor: z.string().optional(), // id of the last item
|
||||
}),
|
||||
)
|
||||
.input(auditLogListInputSchema)
|
||||
.query(async ({ ctx, input }) => {
|
||||
return listAuditEntries(ctx.db, toAuditListInput({
|
||||
entityType: input.entityType,
|
||||
@@ -50,20 +42,7 @@ export const auditLogRouter = createTRPCRouter({
|
||||
}),
|
||||
|
||||
listDetail: controllerProcedure
|
||||
.input(
|
||||
z.object({
|
||||
entityType: z.string().optional(),
|
||||
entityId: z.string().optional(),
|
||||
userId: z.string().optional(),
|
||||
action: z.string().optional(),
|
||||
source: z.string().optional(),
|
||||
startDate: z.date().optional(),
|
||||
endDate: z.date().optional(),
|
||||
search: z.string().optional(),
|
||||
limit: z.number().min(1).max(100).default(50),
|
||||
cursor: z.string().optional(),
|
||||
}),
|
||||
)
|
||||
.input(auditLogListInputSchema)
|
||||
.query(async ({ ctx, input }) => {
|
||||
const result = await listAuditEntries(ctx.db, toAuditListInput({
|
||||
entityType: input.entityType,
|
||||
@@ -103,25 +82,13 @@ export const auditLogRouter = createTRPCRouter({
|
||||
* Get all audit entries for a specific entity (e.g. a project or resource).
|
||||
*/
|
||||
getByEntity: controllerProcedure
|
||||
.input(
|
||||
z.object({
|
||||
entityType: z.string(),
|
||||
entityId: z.string(),
|
||||
limit: z.number().min(1).max(200).default(50),
|
||||
}),
|
||||
)
|
||||
.input(auditLogByEntityInputSchema)
|
||||
.query(async ({ ctx, input }) => {
|
||||
return getAuditEntriesByEntity(ctx.db, input);
|
||||
}),
|
||||
|
||||
getByEntityDetail: controllerProcedure
|
||||
.input(
|
||||
z.object({
|
||||
entityType: z.string(),
|
||||
entityId: z.string(),
|
||||
limit: z.number().min(1).max(200).default(50),
|
||||
}),
|
||||
)
|
||||
.input(auditLogByEntityInputSchema)
|
||||
.query(async ({ ctx, input }) => {
|
||||
const entries = await getAuditEntriesByEntity(ctx.db, input);
|
||||
return {
|
||||
@@ -137,13 +104,7 @@ export const auditLogRouter = createTRPCRouter({
|
||||
* Timeline view: entries grouped by date (YYYY-MM-DD).
|
||||
*/
|
||||
getTimeline: controllerProcedure
|
||||
.input(
|
||||
z.object({
|
||||
startDate: z.date().optional(),
|
||||
endDate: z.date().optional(),
|
||||
limit: z.number().min(1).max(500).default(200),
|
||||
}),
|
||||
)
|
||||
.input(auditLogTimelineInputSchema)
|
||||
.query(async ({ ctx, input }) => {
|
||||
return getAuditTimeline(ctx.db, toAuditTimelineInput({
|
||||
startDate: input.startDate,
|
||||
@@ -153,13 +114,7 @@ export const auditLogRouter = createTRPCRouter({
|
||||
}),
|
||||
|
||||
getTimelineDetail: controllerProcedure
|
||||
.input(
|
||||
z.object({
|
||||
startDate: z.date().optional(),
|
||||
endDate: z.date().optional(),
|
||||
limit: z.number().min(1).max(500).default(200),
|
||||
}),
|
||||
)
|
||||
.input(auditLogTimelineInputSchema)
|
||||
.query(async ({ ctx, input }) => {
|
||||
const timeline = await getAuditTimeline(ctx.db, toAuditTimelineInput({
|
||||
startDate: input.startDate,
|
||||
|
||||
Reference in New Issue
Block a user