Skip to content

Commit 41f23e0

Browse files
authored
feat: reward shop methods, get pupil fields and add meta on detentions (#35)
1 parent 936057b commit 41f23e0

3 files changed

Lines changed: 105 additions & 5 deletions

File tree

src/core/baseClient.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {
1515
GetStudentInfoResponse,
1616
HomeworksResponse,
1717
LessonsResponse,
18+
PupilFieldsResponse,
1819
} from "../types.ts";
1920
import { PING_INTERVAL } from "~/src/utils/consts.ts";
2021

@@ -276,7 +277,7 @@ export class BaseClient {
276277
method: "GET",
277278
},
278279
)
279-
).data;
280+
);
280281
}
281282
/**
282283
* Gets the current student's attendance
@@ -302,4 +303,18 @@ export class BaseClient {
302303
)
303304
);
304305
}
306+
/**
307+
* Gets the current student's pupil fields
308+
* @returns Array of stats
309+
*/
310+
async getPupilFields(): Promise<PupilFieldsResponse> {
311+
return (
312+
await this.makeAuthedRequest(
313+
this.API_BASE + "/customfields/" + this.studentId,
314+
{
315+
method: "GET",
316+
},
317+
)
318+
);
319+
}
305320
}

src/core/studentClient.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { API_BASE_STUDENT, BASE_URL } from "~/src/utils/consts.ts";
22
import { BaseClient } from "~/src/core/baseClient.ts";
33
import { parseCookies } from "~/src/utils/utils.ts";
4+
import { RewardPurchaseResponse, RewardsResponse } from "~/src/types.ts";
45

56
/**
67
* Student Client
@@ -58,4 +59,34 @@ export class StudentClient extends BaseClient {
5859
const user = await this.getStudentInfo();
5960
this.studentId = user.data.user.id;
6061
}
62+
/**
63+
* Gets the current student's rewards shop
64+
* @returns Array of purchasable items
65+
*/
66+
async getRewards(): Promise<RewardsResponse> {
67+
return (
68+
await this.makeAuthedRequest(
69+
this.API_BASE + "/rewards/" + this.studentId,
70+
{
71+
method: "GET",
72+
},
73+
)
74+
);
75+
}
76+
/**
77+
* Purchase a reward item from the current student's rewards shop
78+
* @param itemId number
79+
* @returns An object containg balence and id
80+
*/
81+
async purchaseReward(itemId: number): Promise<RewardPurchaseResponse> {
82+
return (
83+
await this.makeAuthedRequest(
84+
this.API_BASE + "/purchase/" + itemId,
85+
{
86+
method: "POST",
87+
body: `pupil_id=${this.studentId}`,
88+
},
89+
)
90+
);
91+
}
6192
}

src/types.ts

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,17 @@ export interface Detention {
335335
name: string;
336336
};
337337
}
338-
// TODO: Update typings to include meta response. Currently not possible since I don't have access
339-
export type DetentionsResponse = Array<Detention>;
338+
339+
export type DetentionsData = Array<Detention>;
340+
341+
export interface DetentionsMeta {
342+
detention_alias_plural: string;
343+
}
344+
345+
export type DetentionsResponse = ClassChartsResponse<
346+
DetentionsData,
347+
DetentionsMeta
348+
>;
340349

341350
export interface Announcement {
342351
id: number;
@@ -421,8 +430,8 @@ export interface AttendancePeriod {
421430
}
422431

423432
export interface AttendanceMeta {
424-
dates: string[];
425-
sessions: string[];
433+
dates: Array<string>;
434+
sessions: Array<string>;
426435
start_date: string;
427436
end_date: string;
428437
percentage: string;
@@ -435,3 +444,48 @@ export type AttendanceResponse = ClassChartsResponse<
435444
AttendanceData,
436445
AttendanceMeta
437446
>;
447+
448+
export type RewardsData = {
449+
id: number;
450+
name: string;
451+
description: string;
452+
photo: string;
453+
price: number;
454+
stock_control: boolean;
455+
stock: number;
456+
can_purchase: boolean;
457+
unable_to_purchase_reason: string;
458+
once_per_pupil: boolean;
459+
purchased: boolean;
460+
purchased_count: string;
461+
price_balance_difference: number;
462+
}[];
463+
464+
export interface RewardsMeta {
465+
pupil_score_balance: number;
466+
}
467+
468+
export type RewardsResponse = ClassChartsResponse<RewardsData, RewardsMeta>;
469+
470+
export interface RewardPurchaseData {
471+
single_purchase: "yes" | "no";
472+
order_id: number;
473+
balance: number;
474+
}
475+
476+
export type RewardPurchaseResponse = ClassChartsResponse<
477+
RewardPurchaseData,
478+
[]
479+
>;
480+
481+
export interface PupilFieldsData {
482+
note: string;
483+
fields: Array<{
484+
id: number;
485+
name: string;
486+
graphic: string;
487+
value: string;
488+
}>;
489+
}
490+
491+
export type PupilFieldsResponse = ClassChartsResponse<PupilFieldsData, []>;

0 commit comments

Comments
 (0)