Skip to content

Commit

Permalink
Major Update v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
raphckrman committed Mar 1, 2024
1 parent 2c4b92a commit 295f40c
Show file tree
Hide file tree
Showing 19 changed files with 9,169 additions and 0 deletions.
36 changes: 36 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module.exports = [
{ // Apply to `cjs`, `.mjs` and `.js` files.
files: ["**/*.?([cm])js"]
},
{ // Apply to `cts`, `.mts` and `.ts` files.
files: ["**/*.?([cm])ts"],
languageOptions: {
parser: require("@typescript-eslint/parser"),
parserOptions: {
sourceType: "module"
}
}
},
{
ignores: ["dist/**", "docs/.astro/**", "docs/dist/**"],
plugins: {
stylistic: require("@stylistic/eslint-plugin")
},
rules: {
"stylistic/indent": ["error", 2],
"stylistic/semi": ["error", "always"],
"stylistic/eol-last": ["error", "always"],
"stylistic/quotes": ["error", "double"],
"stylistic/dot-location": ["error", "property"],
"stylistic/array-bracket-spacing": ["error", "never"],
"stylistic/arrow-parens": ["error", "always"],
"stylistic/arrow-spacing": "error",
"stylistic/block-spacing": ["error", "always"],
"stylistic/brace-style": ["error", "stroustrup"],
"stylistic/comma-dangle": ["error", "never"],
"stylistic/comma-spacing": ["error", { before: false, after: true }],
"stylistic/function-call-spacing": ["error", "never"],
"stylistic/no-trailing-spaces": "error"
}
}
];
32 changes: 32 additions & 0 deletions lib/auth/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { RESTManager } from "../rest/RESTManager";
import * as endpoints from "../rest/endpoints";
import { Turboself } from "../structures/Client";
import { TurboselfLoginBody, TurboselfLoginResponse } from "../types/Login";

const manager = new RESTManager();

/** This method is used to authenticate into the Turboself account.
* @param email Email of the user.
* @param password Password of the user.
* @param refreshToken If the token should be refreshed.
*/
export const authTurboselfWithCredentials = async (email: string, password: string, refreshToken: boolean | false): Promise<Turboself> => {
return manager.makeAuthRequest<TurboselfLoginResponse>({
method: "POST",
url: endpoints.LOGIN(),
data: {
username: email,
password: password
} as TurboselfLoginBody
}).then((data: unknown) => {
let typedData = data as TurboselfLoginResponse;
if (refreshToken) {
return new Turboself(email, password, typedData.access_token, typedData.hoteId, typedData.userId);
}
else {
return new Turboself(null, null, typedData.access_token, typedData.hoteId, typedData.userId);
}
}).catch((error: unknown) => {
throw error;
});
};
3 changes: 3 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./auth/index";
export * from "./routes/User";
export * from "./structures/Client";
16 changes: 16 additions & 0 deletions lib/rest/RESTManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/** @module RESTManager */

import { AxiosRequestConfig } from "axios";
import { RequestHandler } from "./RequestHandler";

export class RESTManager {
private requestHandler: RequestHandler;

constructor() {
this.requestHandler = new RequestHandler();
}

public async makeAuthRequest<T = unknown>(options?: AxiosRequestConfig) {
return this.requestHandler.makeRequest(options);
}
}
25 changes: 25 additions & 0 deletions lib/rest/RequestHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/** @module RequestHandler */

import axios, { AxiosRequestConfig } from "axios";

export class RequestHandler {
public async makeRequest<T = unknown>(options: AxiosRequestConfig = {}) {
if (!options.method) {
throw new Error("Invalid method " + options.method);
}

options.baseURL = "https://api-rest-prod.incb.fr";
options.headers = {
...options.headers,
"User-Agent": "MyTurboself/66 CFNetwork/1492.0.1 Darwin/23.3.0"
};

try {
const response = await axios.request<T>(options);
return response.data;
}
catch (error) {
throw new Error(String(error));
}
}
}
12 changes: 12 additions & 0 deletions lib/rest/endpoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/** REST/Endpoints */

export const LOGIN = () => "/api/v1/auth/login";

export const USER = (hoteId: number) => `/api/v1/hotes/${hoteId}`;
export const USER_BALANCES = (hoteId: number) => `/api/v1/comptes/hotes/${hoteId}/3`;
export const USER_HOME = (hoteId: number) => `/api/v2/hotes/${hoteId}/accueil`;

export const SCHOOL = (etabId: number) => `/api/v1/etablissements/etabId/${etabId}`;

export const BOOKINGS = (hoteId: number) => `/api/v1/reservations/hotes/${hoteId}/semaines`;
export const BOOK_DAY = (hoteId: number) => `/api/v2/hotes/${hoteId}/reservations-jours`;
41 changes: 41 additions & 0 deletions lib/routes/School.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { RESTManager } from "../rest/RESTManager";
import * as endpoints from "../rest/endpoints";
import { SchoolResponse } from "../types/School";
import { userResponse } from "../types/User";

export class School {
#manager: RESTManager;

constructor() {
this.#manager = new RESTManager();
}

/** This method is used to get the user school informations.
* @param hoteId The ID of the hote.
*/
async getUserSchool(hoteId: number, token: string): Promise<SchoolResponse> {
return this.#manager.makeAuthRequest<userResponse>({
method: "GET",
url: endpoints.USER(hoteId),
headers: {
"Authorization": "Bearer " + token
}
}).then((data: unknown) => {
let typedData = data as userResponse;
return typedData.etab;
});
}

/** This method is used to get the school informations.
* @param etabId The ID of the school.
*/
async getSchool(etabId: number): Promise<SchoolResponse> {
return this.#manager.makeAuthRequest<SchoolResponse>({
method: "GET",
url: endpoints.SCHOOL(etabId)
}).then((data: unknown) => {
let typedData = data as SchoolResponse;
return typedData;
});
}
}
215 changes: 215 additions & 0 deletions lib/routes/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
import { RESTManager } from "../rest/RESTManager";
import * as endpoints from "../rest/endpoints";
import { balanceResponse } from "../types/Balance";
import { bookMealResponse, bookingResponse, bookings } from "../types/Booking";
import { historyElement, homeResponse, latestPayment } from "../types/Home";
import { userInfo, userResponse } from "../types/User";

export class User {
#manager: RESTManager;

constructor() {
this.#manager = new RESTManager();
}

/** This method is used to get the user information.
* @param hoteId The ID of the hote to get.
* @param token The token of the user.
*/
async getUser(hoteId: number, token: string): Promise<userInfo> {
return this.#manager.makeAuthRequest<userResponse>({
method: "GET",
url: endpoints.USER(hoteId),
headers: {
"Authorization": "Bearer " + token
}
}).then((data: unknown) => {
let typedData = data as userResponse;
return {
nom: typedData.nom,
prenom: typedData.prenom,
mode: typedData.mode,
qualite: typedData.qualite,
division: typedData.division,
prixDej: typedData.prixDej,
type: typedData.type,
nbMulti: typedData.nbMulti,
droitPaiement: typedData.droitPaiement,
droitReservation: typedData.droitReservation,
droitCafeteria: typedData.droitCafeteria,
dateDernSynchro: typedData.dateDernSynchro,
desactive: typedData.desactive,
mdpPrive: typedData.mdpPrive,
autoriseReservSoldeIns: typedData.autoriseReservSoldeIns,
profilForfaitModule: typedData.profilForfaitModule,
carteCodee: typedData.carteCodee
} as userInfo;
});
}

/** This method is used to get the user history.
* @param hoteId The ID of the hote to get.
* @param token The token of the user.
*/
async getHistory(hoteId: number, token: string): Promise<Array<historyElement>> {
return this.#manager.makeAuthRequest<homeResponse>({
method: "GET",
url: endpoints.USER_HOME(hoteId),
headers: {
"Authorization": "Bearer " + token
}
}).then((data: unknown) => {
let typedData = data as homeResponse;
return typedData.historiques;
});
}

/** This method is used to get the last payment of the user.
* @param hoteId The ID of the hote to get.
* @param token The token of the user.
*/
async getLastPayment(hoteId: number, token: string): Promise<latestPayment> {
return this.#manager.makeAuthRequest<homeResponse>({
method: "GET",
url: endpoints.USER_HOME(hoteId),
headers: {
"Authorization": "Bearer " + token
}
}).then((data: unknown) => {
let typedData = data as homeResponse;
return typedData.latestPaiement;
});
}

/** This method is used to get the balances of his accounts.
* @param hoteId The ID of the hote to get.
* @param token The token of the user.
*/
async getSolds(hoteId: number, token: string): Promise<balanceResponse[]> {
return this.#manager.makeAuthRequest<balanceResponse[]>({
method: "GET",
url: endpoints.USER_BALANCES(hoteId),
headers: {
"Authorization": "Bearer " + token
}
}).then((data: unknown) => {
return data as balanceResponse[];
});
}

/** This method is used to get the current week of booking.
* @param hoteId The ID of the hote to get.
* @param token The token of the user.
*/
async getCurrentBookingWeek(hoteId: number, token: string): Promise<string> {
return this.#manager.makeAuthRequest<bookingResponse>({
method: "GET",
url: endpoints.BOOKINGS(hoteId),
headers: {
"Authorization": "Bearer " + token
}
}).then((data: unknown) => {
let typedData = data as bookingResponse;
return typedData.dateSemaine;
});
}

/** This method is used to get the current week of booking.
* @param hoteId The ID of the hote to get.
* @param token The token of the user.
*/
async getCurrentBookingWeekNumber(hoteId: number, token: string): Promise<number> {
return this.#manager.makeAuthRequest<bookingResponse>({
method: "GET",
url: endpoints.BOOKINGS(hoteId),
headers: {
"Authorization": "Bearer " + token
}
}).then((data: unknown) => {
let typedData = data as bookingResponse;
return typedData.rsvWebDto[0].semaine ?? 0;
});
}

/** This method is used to get the bookings of the user for a week.
* @param hoteId The ID of the hote to get.
* @param token The token of the user.
* @param week The week number to get.
*/
async getBookings(hoteId: number, token: string, week?: number): Promise<bookings[]> {
return this.#manager.makeAuthRequest<bookingResponse>({
method: "GET",
url: endpoints.BOOKINGS(hoteId) + (week ? `?num=${week}` : ""),
headers: {
"Authorization": "Bearer " + token
}
}).then((data: unknown) => {
let typedData = data as bookingResponse;
return typedData.rsvWebDto;
});
}

/** This method is used to get the weeks available for booking.
* @param hoteId The ID of the hote to get.
* @param token The token of the user.
*/
async getBooksWeeksAvailable(hoteId: number, token: string): Promise<number[]> {
return this.#manager.makeAuthRequest<bookingResponse>({
method: "GET",
url: endpoints.BOOKINGS(hoteId),
headers: {
"Authorization": "Bearer " + token
}
}).then((data: unknown) => {
let typedData = data as bookingResponse;
return typedData.numSemaines;
});
}

/** This method is used to get the state of evening reservation.
* @param hoteId The ID of the hote to get.
* @param token The token of the user.
*/
async getEveningBookingState(hoteId: number, token: string): Promise<boolean> {
return this.#manager.makeAuthRequest<bookingResponse>({
method: "GET",
url: endpoints.BOOKINGS(hoteId),
headers: {
"Authorization": "Bearer " + token
}
}).then((data: unknown) => {
let typedData = data as bookingResponse;
return typedData.isResaSoirActive;
});
}

/** This method is used to get the bookings of the user for a week.
* @param hoteId The ID of the hote to get.
* @param token The token of the user.
* @param state The state of the booking (1==true, 0==false).
* @param weekId The week number to get.
* @param dayOfWeek The ID of the borne to get.
* @param bookEvening If the evening should be booked.
*/
async bookDay(hoteId: number, token: string, state: number, weekNumber?: number, dayOfWeek?: number, bookEvening?: boolean ): Promise<bookMealResponse> {
const weekId: string = await this.getBookings(hoteId, token, weekNumber).then((data) => data[0]?.id) ?? await this.getBookings(hoteId, token).then((data) => data[0]?.id);
return this.#manager.makeAuthRequest<bookMealResponse>({
method: "PUT",
url: endpoints.BOOK_DAY(hoteId),
headers: {
"Authorization": "Bearer " + token
},
data: {
dayOfWeek: dayOfWeek ?? new Date().getDay(),
dayReserv: state,
web: {
id: weekId
},
hasHoteResaSoirActive: bookEvening ?? false
}
}).then((data: unknown) => {
let typedData = data as bookMealResponse;
return typedData;
});
}
}
Loading

0 comments on commit 295f40c

Please sign in to comment.