Skip to content

Commit

Permalink
feat(history): add 3 endpoints to get histories
Browse files Browse the repository at this point in the history
  • Loading branch information
raphckrman committed Nov 4, 2024
1 parent ed711e7 commit 278e7c5
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
58 changes: 58 additions & 0 deletions src/api/history.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { USER_AGENT } from "~/utils/constants";
import { createAjaxEndpoint, createEndpointURL } from "~/utils/endpoints";

export interface FinancialHistoryEvent {
operation_uid: number;
operationDate: number
operationName: string;
eWalletName: string;
beneficiary_uid: number;
paymentMethod: string;
credit: number | null;
debit: number | null;
}

export interface OrderHistoryEvent {
order_uid: number;
orderDate: number;
orderReference: number;
sponsorName: number;
state: number;
amount: number;
}

export interface ConsumptionHistoryEvent {
consumption_uid: number;
consumptionDate: number;
consumptionDescription: string;
amount: number;
eWalletName: string;
}

export const getFinancialHistory = async (uid: number, startTimestamp: number, endTimestamp: number, limit: number = 30, cookies: string[]): Promise<FinancialHistoryEvent[]> => {
const response = await fetch(createAjaxEndpoint(`?eID=tx_afereload_ajax_financialhistory&fe_uid=${uid}&startTimestamp=${startTimestamp}&endTimestamp=${endTimestamp}&page=1&start=0&limit=${limit}`), {
headers: { "Cookie": cookies.join("; "), "User-Agent": USER_AGENT }
});

const data = await response.json() as unknown as { total: number, operations: FinancialHistoryEvent[] };

return data.operations;
};

export const getOrdersHistory = async (uid: number, startTimestamp: number, endTimestamp: number, limit: number = 30, cookies: string[]): Promise<OrderHistoryEvent[]> => {
const response = await fetch(createAjaxEndpoint(`?eID=tx_afereload_ajax_ordershistory&fe_uid=${uid}&startTimestamp=${startTimestamp}&endTimestamp=${endTimestamp}&page=1&start=0&limit=${limit}`), {
headers: { "Cookie": cookies.join("; "), "User-Agent": USER_AGENT }
});

const data = await response.json() as unknown as { total: number, orders: OrderHistoryEvent[] };
return data.orders;
};

export const getConsumptionsHistory = async (uid: number, startTimestamp: number, endTimestamp: number, limit: number = 30, cookies: string[]): Promise<ConsumptionHistoryEvent[]> => {
const response = await fetch(createAjaxEndpoint(`?eID=tx_afereload_ajax_consumptionhistory&fe_uid=${uid}&startTimestamp=${startTimestamp}&endTimestamp=${endTimestamp}&page=1&start=0&limit=${limit}`), {
headers: { "Cookie": cookies.join("; "), "User-Agent": USER_AGENT }
});

const data = await response.json() as unknown as { total: number, consumptions: ConsumptionHistoryEvent[] };
return data.consumptions;
};
13 changes: 13 additions & 0 deletions src/models/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ARD_BASE_ENDPOINT, ARD_HOST } from "~/utils/constants";

import { type OnlinePayments, getOnlinePayments } from "~/api/online-payments";
import { findValueBetween } from "@literate.ink/utilities";
import { FinancialHistoryEvent, getFinancialHistory, getOrdersHistory, OrderHistoryEvent, getConsumptionsHistory, ConsumptionHistoryEvent } from "~/api/history";

export class Client {
public constructor (
Expand All @@ -16,6 +17,18 @@ export class Client {
return getOnlinePayments(this.schoolID, this.cookies);
}

public async getFinancialHistory(uid: number, startTimestamp: number = 1, endTimestamp: number = Date.now(), limit: number = 30): Promise<FinancialHistoryEvent[]> {
return getFinancialHistory(uid, startTimestamp, endTimestamp, limit, this.cookies);
}

public async getOrdersHistory(uid: number, startTimestamp: number = 1, endTimestamp: number = Date.now(), limit: number = 30): Promise<OrderHistoryEvent[]> {
return getOrdersHistory(uid, startTimestamp, endTimestamp, limit, this.cookies);
}

public async getConsumptionsHistory(uid: number, startTimestamp: number = 1, endTimestamp: number = Date.now(), limit: number = 30): Promise<ConsumptionHistoryEvent[]> {
return getConsumptionsHistory(uid, startTimestamp, endTimestamp, limit, this.cookies);
}

public static fromAPI (html: string, cookies: string[] = [], pid = "") {
const schoolID = findValueBetween(html, ARD_BASE_ENDPOINT.slice(1) + "/", "/accueil.html");
const [schoolImagePath, , schoolName] = findValueBetween(html, "<img src=\"", "\" />").split("\"");
Expand Down
6 changes: 5 additions & 1 deletion src/utils/endpoints.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { ARD_BASE_ENDPOINT_WITH_HOST } from "~/utils/constants";
import { ARD_BASE_ENDPOINT_WITH_HOST, ARD_HOST } from "~/utils/constants";

export const createEndpointURL = (schoolID: string, path: string): string => {
return `${ARD_BASE_ENDPOINT_WITH_HOST}/${schoolID}/${path}`;
};

export const createAjaxEndpoint = (path: string): string => {
return `${ARD_HOST}/${path}`;
};

0 comments on commit 278e7c5

Please sign in to comment.