71 lines
1.9 KiB
TypeScript
71 lines
1.9 KiB
TypeScript
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 it’s 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,
|
||
});
|
||
}
|