julong-lib/utils/findLastEducation.ts

59 lines
2.0 KiB
TypeScript

export const findLastEducation = (searchTerm: string, userEducation: any[]) => {
if (!Array.isArray(userEducation) || userEducation.length === 0) {
return null; // Jika array kosong, kembalikan null
}
const educationMapping: Record<string, string | string[]> = {
SD: "10 - Junior High School",
SLTP: "9 - Senior High School",
SLTA: "3 - Bachelor",
"D 1": "4 - Diploma 1",
"D 2": "5 - Diploma 2",
"D 3": "6 - Diploma 3",
"D 1/2/3": ["4 - Diploma 1", "5 - Diploma 2", "6 - Diploma 3"], // Cari D1/D2/D3 tertinggi & terbaru
S1: ["3 - Bachelor", "7 - Diploma 4"], // S1 setara dengan D4
S2: "2 - Master Degree",
S3: "1 - Doctoral / Professor",
};
const mappedValue = educationMapping[searchTerm];
if (!mappedValue) return null;
let filteredEducations;
if (Array.isArray(mappedValue)) {
// Jika "D 1/2/3" atau "S1" (D4)
filteredEducations = userEducation.filter((edu) =>
mappedValue.includes(edu.education_level)
);
if (filteredEducations.length === 0) return null;
// Pilih jenjang tertinggi, jika sama maka pilih graduate_year terbaru
return filteredEducations.reduce((highest, current) => {
const highestIndex = mappedValue.indexOf(highest.education_level);
const currentIndex = mappedValue.indexOf(current.education_level);
if (currentIndex > highestIndex) {
return current;
}
if (currentIndex === highestIndex) {
return current.graduate_year > highest.graduate_year
? current
: highest;
}
return highest;
});
} else {
// Jika hanya satu jenjang (misalnya SD, SLTP, SLTA, S2, S3)
filteredEducations = userEducation.filter(
(edu) => edu.education_level === mappedValue
);
if (filteredEducations.length === 0) return null;
// Pilih graduate_year terbaru
return filteredEducations.reduce((latest, current) =>
current.graduate_year > latest.graduate_year ? current : latest
);
}
};