forked from Hackthletes-APU/cron-apu-timetable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetchTimetable.js
More file actions
71 lines (57 loc) · 2.06 KB
/
fetchTimetable.js
File metadata and controls
71 lines (57 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { config } from "dotenv";
config();
const s3Endpoint = process.env.APU_TIMETABLE_S3;
const intakeCode = process.env.INTAKE_CODE;
const nonElectives = ["CT065-3-3-ALG",]; // Add MODID that is NOT your elective
const grouping = "G1"; // G1 is default, grouping to filter by
export async function fetchTimetable() {
if (!s3Endpoint) {
console.error("❌ Error: S3 endpoint not configured");
return []; // Return empty array instead of Response
}
if (!intakeCode) {
console.error("❌ Error: Intake Code not configured");
return [];
}
try {
const response = await fetch(s3Endpoint);
if (!response.ok) {
console.error("❌ Error: Unable to fetch data from S3");
return [];
}
const data = await response.json();
// Filter the entire S3 dataset by intake code
const filteredData = filterByIntake(data, intakeCode);
const filteredByGrouping = filterByGrouping(filteredData, grouping);
// Filter the courses by the user's elective choices
const result = filterByElective(filteredByGrouping, nonElectives);
return result; // ✅ Return data directly as an array
} catch (error) {
console.error("❌ Error fetching data:", error.message);
return []; // ✅ Return empty array on error
}
}
const filterByIntake = (data, intakeCode) => {
return data.filter((entry) => {
const intakeValue = entry.INTAKE?.trim();
const searchValue = intakeCode.trim();
return intakeValue === searchValue;
});
};
const filterByElective = (filteredData, nonElectives) => {
if (nonElectives.length < 1) {
return filteredData;
}
return filteredData.filter((entry) => {
const modid = entry.MODID?.trim();
const isNonElective = nonElectives.some((mod) => modid?.includes(mod));
return !isNonElective; // Keep only those that are NOT nonElectives
});
};
const filterByGrouping = (filteredData, grouping) => {
return filteredData.filter((entry) => {
const groupingValue = entry.GROUPING?.trim();
const searchValue = grouping.trim();
return groupingValue === searchValue;
});
};