diff --git a/src/core/baseClient.ts b/src/core/baseClient.ts index 1aab7b4..da97ba4 100644 --- a/src/core/baseClient.ts +++ b/src/core/baseClient.ts @@ -15,6 +15,7 @@ import type { GetStudentInfoResponse, HomeworksResponse, LessonsResponse, + PupilFieldsResponse, } from "../types.ts"; import { PING_INTERVAL } from "~/src/utils/consts.ts"; @@ -276,7 +277,7 @@ export class BaseClient { method: "GET", }, ) - ).data; + ); } /** * Gets the current student's attendance @@ -302,4 +303,18 @@ export class BaseClient { ) ); } + /** + * Gets the current student's pupil fields + * @returns Array of stats + */ + async getPupilFields(): Promise { + return ( + await this.makeAuthedRequest( + this.API_BASE + "/customfields/" + this.studentId, + { + method: "GET", + }, + ) + ); + } } diff --git a/src/core/studentClient.ts b/src/core/studentClient.ts index e21d0ba..7805a9a 100644 --- a/src/core/studentClient.ts +++ b/src/core/studentClient.ts @@ -1,6 +1,7 @@ import { API_BASE_STUDENT, BASE_URL } from "~/src/utils/consts.ts"; import { BaseClient } from "~/src/core/baseClient.ts"; import { parseCookies } from "~/src/utils/utils.ts"; +import { RewardPurchaseResponse, RewardsResponse } from "~/src/types.ts"; /** * Student Client @@ -58,4 +59,34 @@ export class StudentClient extends BaseClient { const user = await this.getStudentInfo(); this.studentId = user.data.user.id; } + /** + * Gets the current student's rewards shop + * @returns Array of purchasable items + */ + async getRewards(): Promise { + return ( + await this.makeAuthedRequest( + this.API_BASE + "/rewards/" + this.studentId, + { + method: "GET", + }, + ) + ); + } + /** + * Purchase a reward item from the current student's rewards shop + * @param itemId number + * @returns An object containg balence and id + */ + async purchaseReward(itemId: number): Promise { + return ( + await this.makeAuthedRequest( + this.API_BASE + "/purchase/" + itemId, + { + method: "POST", + body: `pupil_id=${this.studentId}`, + }, + ) + ); + } } diff --git a/src/types.ts b/src/types.ts index 14d1a71..bee8553 100644 --- a/src/types.ts +++ b/src/types.ts @@ -335,8 +335,17 @@ export interface Detention { name: string; }; } -// TODO: Update typings to include meta response. Currently not possible since I don't have access -export type DetentionsResponse = Array; + +export type DetentionsData = Array; + +export interface DetentionsMeta { + detention_alias_plural: string; +} + +export type DetentionsResponse = ClassChartsResponse< + DetentionsData, + DetentionsMeta +>; export interface Announcement { id: number; @@ -421,8 +430,8 @@ export interface AttendancePeriod { } export interface AttendanceMeta { - dates: string[]; - sessions: string[]; + dates: Array; + sessions: Array; start_date: string; end_date: string; percentage: string; @@ -435,3 +444,48 @@ export type AttendanceResponse = ClassChartsResponse< AttendanceData, AttendanceMeta >; + +export type RewardsData = { + id: number; + name: string; + description: string; + photo: string; + price: number; + stock_control: boolean; + stock: number; + can_purchase: boolean; + unable_to_purchase_reason: string; + once_per_pupil: boolean; + purchased: boolean; + purchased_count: string; + price_balance_difference: number; +}[]; + +export interface RewardsMeta { + pupil_score_balance: number; +} + +export type RewardsResponse = ClassChartsResponse; + +export interface RewardPurchaseData { + single_purchase: "yes" | "no"; + order_id: number; + balance: number; +} + +export type RewardPurchaseResponse = ClassChartsResponse< + RewardPurchaseData, + [] +>; + +export interface PupilFieldsData { + note: string; + fields: Array<{ + id: number; + name: string; + graphic: string; + value: string; + }>; +} + +export type PupilFieldsResponse = ClassChartsResponse;