mirror of
https://github.com/outbackdingo/pangolin.git
synced 2026-01-27 18:20:04 +00:00
69 lines
2.1 KiB
TypeScript
69 lines
2.1 KiB
TypeScript
import { registry } from "@server/openApi";
|
|
import { NextFunction } from "express";
|
|
import { Request, Response } from "express";
|
|
import { OpenAPITags } from "@server/openApi";
|
|
import createHttpError from "http-errors";
|
|
import HttpCode from "@server/types/HttpCode";
|
|
import { fromError } from "zod-validation-error";
|
|
import logger from "@server/logger";
|
|
import { queryAccessAuditLogsQuery, queryRequestAuditLogsParams, queryRequest } from "./queryRequstAuditLog";
|
|
import { generateCSV } from "./generateCSV";
|
|
|
|
registry.registerPath({
|
|
method: "get",
|
|
path: "/org/{orgId}/logs/request",
|
|
description: "Query the request audit log for an organization",
|
|
tags: [OpenAPITags.Org],
|
|
request: {
|
|
query: queryAccessAuditLogsQuery,
|
|
params: queryRequestAuditLogsParams
|
|
},
|
|
responses: {}
|
|
});
|
|
|
|
export async function exportRequestAuditLogs(
|
|
req: Request,
|
|
res: Response,
|
|
next: NextFunction
|
|
): Promise<any> {
|
|
try {
|
|
const parsedQuery = queryAccessAuditLogsQuery.safeParse(req.query);
|
|
if (!parsedQuery.success) {
|
|
return next(
|
|
createHttpError(
|
|
HttpCode.BAD_REQUEST,
|
|
fromError(parsedQuery.error)
|
|
)
|
|
);
|
|
}
|
|
|
|
const parsedParams = queryRequestAuditLogsParams.safeParse(req.params);
|
|
if (!parsedParams.success) {
|
|
return next(
|
|
createHttpError(
|
|
HttpCode.BAD_REQUEST,
|
|
fromError(parsedParams.error)
|
|
)
|
|
);
|
|
}
|
|
|
|
const data = { ...parsedQuery.data, ...parsedParams.data };
|
|
|
|
const baseQuery = queryRequest(data);
|
|
|
|
const log = await baseQuery.limit(data.limit).offset(data.offset);
|
|
|
|
const csvData = generateCSV(log);
|
|
|
|
res.setHeader('Content-Type', 'text/csv');
|
|
res.setHeader('Content-Disposition', `attachment; filename="request-audit-logs-${data.orgId}-${Date.now()}.csv"`);
|
|
|
|
return res.send(csvData);
|
|
} catch (error) {
|
|
logger.error(error);
|
|
return next(
|
|
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
|
);
|
|
}
|
|
}
|