CV/src/education/education.service.ts

29 lines
1.0 KiB
TypeScript

import { Injectable } from "@nestjs/common";
import { EducationDto, EducationType } from "@/education/education.types";
import { education } from "@/education/education";
@Injectable()
export class EducationService {
private readonly education: readonly EducationDto[];
constructor() {
this.education = EducationDto.asDto(education).sort((educationA, educationB) =>
educationA.startDate < educationB.startDate ? 1 : -1
);
}
getMany(filter?: Partial<Omit<EducationType, "startDate" | "endDate">>) {
const filtersValues = Object.entries(filter ?? {}).map(([key, filterValue]) => [
key,
new RegExp(filterValue, "i"),
]) as Array<[keyof Omit<EducationType, "startDate" | "endDate">, RegExp]>;
if (!filter || filtersValues.length === 0) {
return this.education;
}
return this.education.filter((education) =>
filtersValues.some(([key, filterValue]) => filterValue.test(education[key]))
);
}
}