-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcheckForExerciseUpdates.ts
More file actions
52 lines (47 loc) · 1.74 KB
/
checkForExerciseUpdates.ts
File metadata and controls
52 lines (47 loc) · 1.74 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
import { Logger } from "../utilities";
import { ActionContext } from "./types";
import { flatten } from "lodash";
import { Err, Ok, Result } from "ts-results";
interface Options {
forceRefresh?: boolean;
}
interface OutdatedExercise {
courseId: number;
exerciseName: string;
exerciseId: number;
}
/**
* Checks all user's courses for exercise updates.
* @param courseId If given, check only updates for that course.
*/
export async function checkForExerciseUpdates(
actionContext: ActionContext,
options?: Options,
): Promise<Result<OutdatedExercise[], Error>> {
const { tmc, userData } = actionContext;
if (!(tmc.ok && userData.ok)) {
return new Err(new Error("Extension was not initialized properly"));
}
const forceRefresh = options?.forceRefresh ?? false;
Logger.info("Checking for exercise updates, forced update:", forceRefresh);
const checkUpdatesResult = await tmc.val.checkExerciseUpdates({ forceRefresh });
if (checkUpdatesResult.err) {
return checkUpdatesResult;
}
const updateableExerciseIds = new Set<number>(checkUpdatesResult.val.map((x) => x.id));
const outdatedExercisesByCourse = userData.val
.getCourses()
.map<OutdatedExercise[]>((course) => {
const outdatedExercises = course.exercises.filter((x) =>
updateableExerciseIds.has(x.id),
);
return outdatedExercises.map((x) => ({
courseId: course.id,
exerciseId: x.id,
exerciseName: x.name,
}));
});
const outdatedExercises = flatten(outdatedExercisesByCourse);
Logger.info(`Update check found ${outdatedExercises.length} outdated exercises`);
return Ok(outdatedExercises);
}