1
0
Fork 0
mirror of https://github.com/classchartsapi/classcharts-api-js.git synced 2026-05-14 11:58:13 +00:00

feat: getFullActivity

Co-Authored-By: Tim <65774333+timthedev07@users.noreply.github.com>
This commit is contained in:
James Cook 2022-04-02 12:11:04 +01:00
parent 0f21e7d744
commit eea157566c
2 changed files with 43 additions and 3 deletions

View file

@ -8,6 +8,7 @@ import type {
DetentionsResponse, DetentionsResponse,
GetActivityOptions, GetActivityOptions,
GetBehaviourOptions, GetBehaviourOptions,
GetFullActivityOptions,
GetHomeworkOptions, GetHomeworkOptions,
GetLessonsOptions, GetLessonsOptions,
Homework, Homework,
@ -68,7 +69,7 @@ export class ClasschartsClient {
return data?.user; 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 * @param options GetActivityOptions
* @returns Activity data * @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<ActivityResponse> {
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 * Gets the logged in students behaviour points
* @param options GetBehaviourOptions * @param options GetBehaviourOptions

View file

@ -300,3 +300,14 @@ export interface Pupil extends Student {
messages_count: number; messages_count: number;
} }
export type GetPupilsResponse = Array<Pupil>; export type GetPupilsResponse = Array<Pupil>;
export interface GetFullActivityOptions {
/**
* From date, in format YYYY-MM-DD
*/
from: string;
/**
* To date, in format YYYY-MM-DD
*/
to: string;
}