Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions src/app/api/root/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Attribute } from "@/app/general/interfaces";
import { Attribute, BotNullValues } from "@/app/general/interfaces";
import { getOperator } from "@/app/general/utils";
import { strOrNum } from "@/app/general/types";
import { NextRequest, NextResponse } from "next/server";
Expand All @@ -10,12 +10,17 @@ export async function POST(request: NextRequest) {
const req = await request.json();
const attributes = req.queryParams as Attribute[];
const fileName = req.filePath as string;
const nullValues = req.nullValues as BotNullValues;
const filePath = path.join(process.cwd(), "src/app/data", fileName);
const rows = await filterCSV(filePath, attributes);
const rows = await filterCSV(filePath, attributes, nullValues);
return NextResponse.json(rows);
}

async function filterCSV(filePath: string, attributes: Attribute[]) {
async function filterCSV(
filePath: string,
attributes: Attribute[],
nullValues: BotNullValues
) {
return new Promise<any[]>((resolve, reject) => {
const operators = attributes.map(
(query) => getOperator(query.operator) as any
Expand All @@ -24,7 +29,7 @@ async function filterCSV(filePath: string, attributes: Attribute[]) {
fs.createReadStream(filePath)
.pipe(csvParser())
.on("data", (row: any) => {
if (isRowRequired(attributes, operators, row)) {
if (isRowRequired(attributes, operators, row, nullValues)) {
rows.push(row);
}
})
Expand All @@ -40,26 +45,34 @@ async function filterCSV(filePath: string, attributes: Attribute[]) {
function isRowRequired(
attributes: Attribute[],
operators: any[],
row: any
row: any,
nullValues: BotNullValues
): boolean {
let isRequired: boolean = true;
const duplicates = findDuplicates(attributes, "name");
const { isFilterIncludesNull, nullValues: nullValuesArray } = nullValues;

if (duplicates.length > 0) {
const notDuplicatesAttributes = attributes.filter(
(attribute) => !duplicates.includes(attribute)
);
const isRequiredNotDuplicates = notDuplicatesAttributes.every(
(query, index) => operators[index](row[query.name], ...query.params)
(query, index) =>
operators[index](row[query.name], ...query.params) ||
(isFilterIncludesNull &&
nullValuesArray.includes(row[query.name]))
);
const namesOfDuplicates = duplicates.map((query) => query.name);
const duplicatesAttributes = [...new Set(namesOfDuplicates)];
const duplicatesRequiredArray = duplicatesAttributes.map((name) => {
const attributeOfTheName = attributes.filter(
(attribute) => attribute.name === name
);
const isRowRequired = attributeOfTheName.some((query, index) =>
operators[index](row[query.name], ...query.params)
const isRowRequired = attributeOfTheName.some(
(query, index) =>
operators[index](row[query.name], ...query.params) ||
(isFilterIncludesNull &&
nullValuesArray.includes(row[query.name]))
);
return isRowRequired;
});
Expand All @@ -69,8 +82,11 @@ function isRowRequired(

isRequired = isRequiredNotDuplicates && isRequiredDuplicates;
} else {
isRequired = attributes.every((query, index) =>
operators[index](row[query.name], ...query.params)
isRequired = attributes.every(
(query, index) =>
operators[index](row[query.name], ...query.params) ||
(isFilterIncludesNull &&
nullValuesArray.includes(row[query.name]))
);
}

Expand Down
2 changes: 2 additions & 0 deletions src/app/components/Chat/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@ export default function Chat({ bot }: ChatProps) {

const pathArray = bot?.filePath.split("/");
const path = pathArray.pop();
const nullValues = bot?.nullValues;

const response = await fetch("/api/root", {
method: "POST",
body: JSON.stringify({
queryParams,
filePath: path,
nullValues,
}),
});
if (!response.ok) {
Expand Down
6 changes: 6 additions & 0 deletions src/app/general/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ export interface Bot {
currentOperatorIndex: number;
operatorsFiles: BorOperatorsFiles;
colors: Colors;
nullValues: BotNullValues;
}

export interface BotNullValues {
nullValues: any[];
isFilterIncludesNull: boolean;
}

export interface Colors {
Expand Down
Loading