From 278e7c56e8147b920cb712d310d2eba116c8a97e Mon Sep 17 00:00:00 2001 From: raphckrman <41128238+raphckrman@users.noreply.github.com> Date: Mon, 4 Nov 2024 21:45:20 +0100 Subject: [PATCH] feat(history): add 3 endpoints to get histories --- src/api/history.ts | 58 ++++++++++++++++++++++++++++++++++++++++++ src/models/Client.ts | 13 ++++++++++ src/utils/endpoints.ts | 6 ++++- 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 src/api/history.ts diff --git a/src/api/history.ts b/src/api/history.ts new file mode 100644 index 0000000..89829f8 --- /dev/null +++ b/src/api/history.ts @@ -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 => { + 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 => { + 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 => { + 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; +}; diff --git a/src/models/Client.ts b/src/models/Client.ts index c67baff..f5fe985 100644 --- a/src/models/Client.ts +++ b/src/models/Client.ts @@ -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 ( @@ -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 { + return getFinancialHistory(uid, startTimestamp, endTimestamp, limit, this.cookies); + } + + public async getOrdersHistory(uid: number, startTimestamp: number = 1, endTimestamp: number = Date.now(), limit: number = 30): Promise { + return getOrdersHistory(uid, startTimestamp, endTimestamp, limit, this.cookies); + } + + public async getConsumptionsHistory(uid: number, startTimestamp: number = 1, endTimestamp: number = Date.now(), limit: number = 30): Promise { + 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, "").split("\""); diff --git a/src/utils/endpoints.ts b/src/utils/endpoints.ts index 58a4c74..4922523 100644 --- a/src/utils/endpoints.ts +++ b/src/utils/endpoints.ts @@ -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}`; +};