tianji/src/server/trpc/routers/auditLog.ts

61 lines
1.4 KiB
TypeScript
Raw Normal View History

2024-01-28 15:15:16 +00:00
import { z } from 'zod';
2024-02-17 16:47:22 +00:00
import { OpenApiMetaInfo, router, workspaceProcedure } from '../trpc';
2024-01-28 15:15:16 +00:00
import { OPENAPI_TAG } from '../../utils/const';
import { WorkspaceAuditLogModelSchema } from '../../prisma/zod';
import { prisma } from '../../model/_client';
import { fetchDataByCursor } from '../../utils/prisma';
2024-02-17 16:47:22 +00:00
import { OpenApiMeta } from 'trpc-openapi';
2024-01-28 15:15:16 +00:00
export const auditLogRouter = router({
fetchByCursor: workspaceProcedure
2024-02-17 16:47:22 +00:00
.meta(
buildAuditLogOpenapi({
2024-01-28 15:15:16 +00:00
method: 'GET',
path: '/fetchByCursor',
description: 'Fetch workspace audit log',
2024-02-17 16:47:22 +00:00
})
)
2024-01-28 15:15:16 +00:00
.input(
z.object({
limit: z.number().min(1).max(100).default(50),
cursor: z.string().optional(),
})
)
.output(
z.object({
items: z.array(WorkspaceAuditLogModelSchema),
nextCursor: z.string().optional(),
})
)
.query(async ({ input }) => {
const { workspaceId, cursor, limit } = input;
const { items, nextCursor } = await fetchDataByCursor(
prisma.workspaceAuditLog,
{
where: {
workspaceId,
},
limit,
cursor,
}
);
return {
items,
nextCursor,
};
}),
});
2024-02-17 16:47:22 +00:00
function buildAuditLogOpenapi(meta: OpenApiMetaInfo): OpenApiMeta {
return {
openapi: {
tags: [OPENAPI_TAG.AUDIT_LOG],
protect: true,
...meta,
path: `/audit${meta.path}`,
},
};
}