mirror of
https://github.com/classchartsapi/classcharts-api-js.git
synced 2026-05-11 18:59:05 +00:00
feat: reward shop methods, get pupil fields and add meta on detentions (#35)
This commit is contained in:
parent
936057bbd4
commit
41f23e01d0
3 changed files with 105 additions and 5 deletions
|
|
@ -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<PupilFieldsResponse> {
|
||||
return (
|
||||
await this.makeAuthedRequest(
|
||||
this.API_BASE + "/customfields/" + this.studentId,
|
||||
{
|
||||
method: "GET",
|
||||
},
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<RewardsResponse> {
|
||||
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<RewardPurchaseResponse> {
|
||||
return (
|
||||
await this.makeAuthedRequest(
|
||||
this.API_BASE + "/purchase/" + itemId,
|
||||
{
|
||||
method: "POST",
|
||||
body: `pupil_id=${this.studentId}`,
|
||||
},
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
62
src/types.ts
62
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<Detention>;
|
||||
|
||||
export type DetentionsData = Array<Detention>;
|
||||
|
||||
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<string>;
|
||||
sessions: Array<string>;
|
||||
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<RewardsData, RewardsMeta>;
|
||||
|
||||
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<PupilFieldsData, []>;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue