2025-10-25 11:39:24 +02:00

70 lines
1.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { NextRequest, NextResponse } from "next/server";
import { AuditColumns, AuditData, AuditSearchLabels } from "./mockData";
export async function GET(request: NextRequest) {
const { searchParams } = new URL(request.url);
const actionType = searchParams.get("actionType");
const affectedUserId = searchParams.get("affectedUserId");
const adminId = searchParams.get("adminId");
const adminUsername = searchParams.get("adminUsername");
const dateTimeStart = searchParams.get("dateTime_start");
const dateTimeEnd = searchParams.get("dateTime_end");
let filteredRows = [...AuditData];
if (actionType) {
filteredRows = filteredRows.filter(
tx => tx.actionType.toLocaleLowerCase() === actionType.toLocaleLowerCase()
);
}
if (affectedUserId) {
filteredRows = filteredRows.filter(
tx => tx.affectedUserId.toLowerCase() === affectedUserId.toLowerCase()
);
}
if (adminId) {
filteredRows = filteredRows.filter(tx => tx.adminId === adminId);
}
if (adminUsername) {
filteredRows = filteredRows.filter(
tx => tx.adminUsername === adminUsername
);
}
if (dateTimeStart && dateTimeEnd) {
const start = new Date(dateTimeStart);
const end = new Date(dateTimeEnd);
// Validate the date range to ensure its correct
if (isNaN(start.getTime()) || isNaN(end.getTime())) {
return NextResponse.json(
{
error: "Invalid date range",
},
{ status: 400 }
);
}
filteredRows = filteredRows.filter(tx => {
const txDate = new Date(tx.timeStampOfTheAction);
// Validate if the timestamp is a valid date
if (isNaN(txDate.getTime())) {
return false; // Skip invalid dates
}
return txDate >= start && txDate <= end;
});
}
return NextResponse.json({
tableRows: filteredRows,
tableColumns: AuditColumns,
tableSearchLabels: AuditSearchLabels,
});
}