diff --git a/src/baseClient.ts b/src/baseClient.ts index b8fc134..a928ea7 100644 --- a/src/baseClient.ts +++ b/src/baseClient.ts @@ -8,6 +8,7 @@ import type { DetentionsResponse, GetActivityOptions, GetBehaviourOptions, + GetFullActivityOptions, GetHomeworkOptions, GetLessonsOptions, Homework, @@ -68,7 +69,7 @@ export class ClasschartsClient { return data?.user; } /** - * Get's the logged in student's general activity + * This function is only used for pagination, you likely want client.getFullActivity * @param options GetActivityOptions * @returns Activity data */ @@ -84,7 +85,35 @@ export class ClasschartsClient { } ); } - + /** + * Helper function for getActivity, returns all the data from the selected dates + * @param options GetFullActivityOptions + * @returns Activity Data + */ + async getFullActivity( + options: GetFullActivityOptions + ): Promise { + let data: ActivityResponse = []; + let prevLast: number | undefined; + let gotData = true; + while (gotData) { + const params: GetActivityOptions = { + from: options.from, + to: options.to, + }; + if (prevLast) { + params.last_id = String(prevLast); + } + const fragment = await this.getActivity(params); + if (!fragment || !fragment.length) { + gotData = false; + } else { + data = data.concat(fragment); + prevLast = fragment[fragment.length - 1].id; + } + } + return data; + } /** * Gets the logged in students behaviour points * @param options GetBehaviourOptions diff --git a/src/types.ts b/src/types.ts index dcd0ba0..bb1a4f7 100644 --- a/src/types.ts +++ b/src/types.ts @@ -299,4 +299,15 @@ export interface Pupil extends Student { announcements_count: number; messages_count: number; } -export type GetPupilsResponse = Array; \ No newline at end of file +export type GetPupilsResponse = Array; + +export interface GetFullActivityOptions { + /** + * From date, in format YYYY-MM-DD + */ + from: string; + /** + * To date, in format YYYY-MM-DD + */ + to: string; +} \ No newline at end of file