Skip to content

Commit

Permalink
Merge pull request #9 from raphckrman/dev
Browse files Browse the repository at this point in the history
feat(Account): Add requestNewPassword for password reset request
  • Loading branch information
raphckrman authored May 10, 2024
2 parents b755d26 + 0ea84fa commit d468eec
Show file tree
Hide file tree
Showing 7 changed files with 7,769 additions and 26 deletions.
8 changes: 8 additions & 0 deletions examples/requestNewPassword.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const TurboSelf = require('turboself-api')

async function main() {
let newPasswordRequest = await TurboSelf.requestNewPassword("your_email");
console.log(newPasswordRequest);
}

main()
14 changes: 14 additions & 0 deletions lib/api/requestNewPassword.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { RequestNewPasswordResult } from "../interfaces/Account";
import { SEND_PASSWORD_RESET_REQUEST } from "../utils/endpoints";
import { TurboselfFetcher } from "../utils/fetcher";

export const requestNewPassword = async (email: string): Promise<RequestNewPasswordResult> => {
const response = await TurboselfFetcher("https://api-rest-prod.incb.fr" + SEND_PASSWORD_RESET_REQUEST(email), {
method: "GET",
headers: {
"Content-Type": "application/json"
}
});

return await response.json() as RequestNewPasswordResult;
};
72 changes: 46 additions & 26 deletions lib/client/Turboself.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import { getHistory } from "../api/historyGet";
import { getHost } from "../api/hostGet";
import { getLatestPayment } from "../api/lastPaymentGet";
import { getSiblings } from "../api/siblingsGet";
import { requestNewPassword } from "../api/requestNewPassword";
import { authenticateWithCredentials } from "../authenticate";
import { AuthFlowData } from "../interfaces/AuthFlow";
import { RequestNewPasswordResult } from "../interfaces/Account";
import { Balance } from "../parser/Balance";
import { BookedMeal } from "../parser/BookedMeal";
import { BookingWeek } from "../parser/BookingWeek";
Expand All @@ -19,10 +21,7 @@ import { LatestPayment } from "../parser/LatestPayment";

export default class Turboself {
tokenExpires: number = 0;
constructor(
public token: string,
private loginData: AuthFlowData
) {
constructor(public token: string, private loginData: AuthFlowData) {
this.token = token;
this.tokenExpires = Date.now() + 55 * 60 * 1000;
}
Expand All @@ -39,24 +38,26 @@ export default class Turboself {
return true;
}


/** This method is used to search schools from all schools using Turboself.
* @param query The query to search for.
* @param limit The maximum number of results to return.
* @param query The query to search for.
* @param limit The maximum number of results to return.
*/
public async searchEstablishments(query: string, limit: number): Promise<Establishment[]> {
public async searchEstablishments(
query: string,
limit: number
): Promise<Establishment[]> {
return await searchEstablishment(query, limit);
}

/** This method is used to get an Establishment from Turboself.
* @param code The code of the establishment.
*/
* @param code The code of the establishment.
*/
public async getEstablishment(code: string): Promise<Establishment> {
return await this.getEstablishment(code);
}

/** This method is used to get the host of a Turboself user.
*/
*/
public async getHost(): Promise<Host> {
if (Date.now() > this.tokenExpires) {
await this.refreshToken();
Expand All @@ -65,7 +66,7 @@ export default class Turboself {
}

/** This method is used to get the balance of a Turboself user.
*/
*/
public async getBalance(): Promise<Balance> {
if (Date.now() > this.tokenExpires) {
await this.refreshToken();
Expand All @@ -74,7 +75,7 @@ export default class Turboself {
}

/** This method is used to get the siblings of a Turboself user.
*/
*/
public async getSiblings(): Promise<Host[]> {
if (Date.now() > this.tokenExpires) {
await this.refreshToken();
Expand All @@ -83,7 +84,7 @@ export default class Turboself {
}

/** This method is used to get the latest payment of a Turboself user.
*/
*/
public async getHistory(): Promise<History[]> {
if (Date.now() > this.tokenExpires) {
await this.refreshToken();
Expand All @@ -92,7 +93,7 @@ export default class Turboself {
}

/** This method is used to get the latest payment of a Turboself user.
*/
*/
public async getLatestPayment(): Promise<LatestPayment> {
if (Date.now() > this.tokenExpires) {
await this.refreshToken();
Expand All @@ -101,7 +102,7 @@ export default class Turboself {
}

/** This method is used to get if the user can book evening.
*/
*/
public async canBookEvening(): Promise<boolean> {
if (Date.now() > this.tokenExpires) {
await this.refreshToken();
Expand All @@ -110,27 +111,46 @@ export default class Turboself {
}

/** This method is used to get a booking week.
* @param weekNumber The number of the week.
*/
* @param weekNumber The number of the week.
*/
public async getBookingWeek(weekNumber?: number): Promise<BookingWeek> {
if (Date.now() > this.tokenExpires) {
await this.refreshToken();
}
return await getBookingWeek(this.token, this.loginData.hoteId, weekNumber);
}

/** This method is used to request a new password.
* @param email The email to request a new password for.
*/
public async requestNewPassword(email: string): Promise<RequestNewPasswordResult> {
return await requestNewPassword(email);
}

/** This method is used to book a meal.
* @param id The id of the associated to the terminal.
* @param book If the meal should be booked.
* @param bookEvening If the meal should be booked evening.
* @param dayOfWeek The day of the week the meal should be booked.
*/
public async bookDay(id: string, book: boolean, bookEvening?: boolean, dayOfWeek?: number): Promise<BookedMeal> {
* @param id The id of the associated to the terminal.
* @param book If the meal should be booked.
* @param bookEvening If the meal should be booked evening.
* @param dayOfWeek The day of the week the meal should be booked.
*/
public async bookDay(
id: string,
book: boolean,
bookEvening?: boolean,
dayOfWeek?: number
): Promise<BookedMeal> {
if (Date.now() > this.tokenExpires) {
await this.refreshToken();
}
if (!dayOfWeek) dayOfWeek = ((new Date()).getDay()) - 1;
if (!dayOfWeek) dayOfWeek = new Date().getDay() - 1;
if (!bookEvening) bookEvening = false;
return await bookMeal(this.token, this.loginData.hoteId, id, book === false ? 0 : 1, bookEvening === false ? 0 : 1, dayOfWeek);
return await bookMeal(
this.token,
this.loginData.hoteId,
id,
book === false ? 0 : 1,
bookEvening === false ? 0 : 1,
dayOfWeek
);
}
}
1 change: 1 addition & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./authenticate";

export * from "./api/requestNewPassword";
export * from "./api/balanceGet";
export * from "./api/establishmentSearch";
export * from "./api/establishmentGet";
Expand Down
21 changes: 21 additions & 0 deletions lib/interfaces/Account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export interface RequestNewPasswordResult {
/** List of email addresses that were accepted */
accepted: string[];
/** List of email addresses that were rejected */
rejected: string[];
/** Time it took to send the envelope */
envelopeTime: number;
/** Time it took to send the message */
messageTime: number;
/** Size of the message in bytes */
messageSize: number;
/** Response from the server */
response: string;
/** Envelope information */
envelope: {
from: string;
to: string[];
};
/** Message identifier */
messageId: string;
}
1 change: 1 addition & 0 deletions lib/utils/endpoints.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const LOGIN = () => "/api/v1/auth/login";
export const SEND_PASSWORD_RESET_REQUEST = (email: string) => `/api/v1/utilisateurs/password?email=${email}`;

export const SEARCH_ETABLISHMENT = (city: string, limit: number) => `/api/v1/etablissements?q=${city}&limit=${limit}`;
export const GET_ETABLISHMENT = (id: number) => `/api/v1/etablissements/etabId/${id}`;
Expand Down
Loading

0 comments on commit d468eec

Please sign in to comment.