Skip to content

Commit

Permalink
Merge pull request #5 from raphckrman/dev
Browse files Browse the repository at this point in the history
dev
  • Loading branch information
raphckrman authored Apr 7, 2024
2 parents c078cc2 + 89889fb commit e330d9d
Show file tree
Hide file tree
Showing 64 changed files with 1,660 additions and 1,004 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: ESLint
on: [push, pull_request, workflow_call]


jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Dependencies
run: npm i
- name: Run ESLint
run: npm run lint
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
dist
scripts/tests
.git
.idea
42 changes: 28 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,40 @@
# 🔧 Utilisation
### Connexion par identifiants
```javascript
const TurboSelf = require('turboself-api')
# turboself-api

async function main() {
let client = await TurboSelf.authTurboselfWithCredentials("your_email", "your_password")
console.log(client)
}
[![npm](https://img.shields.io/npm/l/turboself-api)](https://www.npmjs.com/package/turboself-api)
[![npm version](https://badge.fury.io/js/turboself-api.svg)](https://badge.fury.io/js/turboself-api)
[![npm](https://img.shields.io/npm/dw/turboself-api)](https://www.npmjs.com/package/turboself-api)
[![npm](https://img.shields.io/npm/dt/turboself-api)](https://www.npmjs.com/package/turboself-api)

main()
A simple wrapper to interact with the TurboSelf API.

> [!warning]
> This project is not affiliated with TurboSelf or INCB in any way.
## 📦 Installation

```bash
npm install turboself-api
```
### Réserver un déjeuner pour lundi prochain

## 🔧 Usage
### Authentification with credentials
```javascript
const TurboSelf = require('turboself-api')

async function main() {
let client = await TurboSelf.authTurboselfWithCredentials("your_email", "your_password")
client.bookDay(1, await client.getCurrentBookingWeekNumber()+1, 1)
let client = await TurboSelf.authenticateWithCredentials({ username: "your_email", password: "your_password" })
console.log(client)
}

main()
```

### Autre exemples
Pour voir d'autre exemples d'utilisation, vous pouvez regarder le dossier ``examples``
### Other exemples
To see more examples, check the [examples](https://github.com/raphckrman/turboself-api/tree/dev/examples) folder.

## ௷ Credits

Thanks to the following people for their contributions to this project:

- [Remy Godet](https://github.com/godetremy)
- [Tom Theret](https://github.com/tom-theret)
11 changes: 11 additions & 0 deletions examples/book.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const TurboSelf = require('turboself-api')

async function main() {
let client = await TurboSelf.authenticateWithCredentials({ username: "your_email", password: "your_password" })
let currentWeek = await client.getBookingWeek();

await week.terminals[0].days[0].book()
console.log("Booked the day " + week.terminals[0].days[0].date)
}

main()
11 changes: 11 additions & 0 deletions examples/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const TurboSelf = require('turboself-api')

async function main() {
let client = await TurboSelf.authenticateWithCredentials({ username: "your_email", password: "your_password" })
let siblings = await client.getSiblings()
let evening = await client.canBookEvening()
console.log("Number of siblings\x1b[34m » \x1b[1;32m" + siblings.length + "\x1b[0m")
console.log("Can book evening\x1b[34m » \x1b[1;32m" + evening)
}

main()
9 changes: 9 additions & 0 deletions examples/getBalance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const TurboSelf = require('turboself-api')

async function main() {
let client = await TurboSelf.authenticateWithCredentials({ username: "your_email", password: "your_password" })
let balance = await client.getBalance()
console.log(balance)
}

main()
13 changes: 13 additions & 0 deletions examples/getBookingWeek.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const TurboSelf = require('turboself-api')

async function main() {
let client = await TurboSelf.authenticateWithCredentials({ username: "your_email", password: "your_password" })
let weeksAvailable = await client.getBookingWeek()

for (let week of weeksAvailable.weeksNumber) {
const bookings = await client.getBookingWeek(week)
console.log({ bookings })
}
}

main()
13 changes: 0 additions & 13 deletions examples/getBookings.js

This file was deleted.

21 changes: 21 additions & 0 deletions examples/getEtablishment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const TurboSelf = require('turboself-api')

async function main() {
console.log("\x1b[0mFetching etablishments ID 287\x1b[0m")
const search = await TurboSelf.getEtablishment(287)
console.log("Name\x1b[34m » \x1b[1;32m" + search.name + " \x1b[0m\x1b[0m")
console.log("Addresse\x1b[34m » \x1b[1;32m" + search.geolocation?.address1 + " " + search.geolocation?.zipCode + " " + search.geolocation?.city + " \x1b[0m\x1b[0m")
console.log("Version\x1b[34m » \x1b[1;32m" + search.version + " \x1b[0m\x1b[0m")
console.log("Code UAI\x1b[34m » \x1b[1;32m" + search.uai + " \x1b[0m\x1b[0m")
console.log("Code 2P5\x1b[34m » \x1b[1;32m" + search.code + " \x1b[0m\x1b[0m")
console.log("Monnaie\x1b[34m » \x1b[1;32m" + search.currencySymbol + " \x1b[0m\x1b[0m")
console.log("Crédit minimum\x1b[34m » \x1b[1;32m" + search.minimumCredit + " \x1b[0m\x1b[0m")
console.log("Repas minimum\x1b[34m » \x1b[1;32m" + search.minimumMeal + " \x1b[0m\x1b[0m")
console.log("MAC Address\x1b[34m » \x1b[1;32m" + search.macServerAddress + "\x1b[0m\x1b[0m")
console.log(" ")
console.log("Élève peuvent utiliser les QRCode ?\x1b[34m » \x1b[1;32m" + search.permissions?.canStudentUseQrCode + "\x1b[0m\x1b[0m")
console.log("Élève peuvent virer l'historique? \x1b[34m » \x1b[1;32m" + search.permissions?.canStudentSeeHistory + "\x1b[0m\x1b[0m")
console.log(search)
}

main()
9 changes: 9 additions & 0 deletions examples/getHistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const TurboSelf = require('turboself-api')

async function main() {
let client = await TurboSelf.authenticateWithCredentials({ username: "your_email", password: "your_password" })
let history = await client.getHistory()
console.log(history)
}

main()
9 changes: 9 additions & 0 deletions examples/getHost.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const TurboSelf = require('turboself-api')

async function main() {
let client = await TurboSelf.authenticateWithCredentials({ username: "your_email", password: "your_password" })
let host = await client.getHost()
console.log(host)
}

main()
9 changes: 9 additions & 0 deletions examples/getLatestPayment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const TurboSelf = require('turboself-api')

async function main() {
let client = await TurboSelf.authenticateWithCredentials({ username: "your_email", password: "your_password" })
let latestPayment = await client.getLatestPayment()
console.log(latestPayment)
}

main()
12 changes: 12 additions & 0 deletions examples/searchEstablishment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const TurboSelf = require('turboself-api')

async function main() {
console.log("\x1b[0mFetching etablishments in Paris (search results are limited to 20)\x1b[0m")
const search = await TurboSelf.searchEstablishment("Paris", 20)
for (const etab of search) {
console.log("\x1b[34m» \x1b[1;32m" + etab.name + " \x1b[0mis running at version \x1b[1;32m" + etab.version + "\x1b[0m")
}
console.log(search)
}

main()
26 changes: 26 additions & 0 deletions lib/api/balanceGet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { BalanceGetResult } from "../interfaces/Balance";
import { Balance } from "../parser/Balance";
import { GET_HOST_BALANCE } from "../utils/endpoints";
import { TurboselfFetcher } from "../utils/fetcher";

export const getBalance = async (token: string, id: number): Promise<Balance> => {
const response = await TurboselfFetcher("https://api-rest-prod.incb.fr" + GET_HOST_BALANCE(id), {
method: "GET",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + token
}
});

const raw = await response.json() as Array<BalanceGetResult>;
const estimatedAt: Date = new Date();
estimatedAt.setFullYear(parseInt(raw[0].montantEstimeMsg.replace("Montant estimé au ", "").split("/")[2]));
estimatedAt.setMonth(parseInt(raw[0].montantEstimeMsg.replace("Montant estimé au ", "").split("/")[1])-1);
estimatedAt.setDate(parseInt(raw[0].montantEstimeMsg.replace("Montant estimé au ", "").split("/")[0]));
estimatedAt.setHours(12);
estimatedAt.setMinutes(0);
estimatedAt.setSeconds(0);
estimatedAt.setMilliseconds(0);

return new Balance(token, id, raw[0].montant, raw[0].montantEstime, estimatedAt);
};
101 changes: 101 additions & 0 deletions lib/api/bookDay.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { BookMealPut } from "../interfaces/Bookings";
import { BookedMeal } from "../parser/BookedMeal";
import { Establishment } from "../parser/Establishment";
import { Host } from "../parser/Host";
import { GET_BOOKING_WEEK, PUT_BOOK_MEAL } from "../utils/endpoints";
import { TurboselfFetcher } from "../utils/fetcher";
import { getWeekNumber } from "../utils/weekNumber";

export const bookMeal = async (token: string, id: number, bookId: string, book: number, bookEvening: number, day: number): Promise<BookedMeal> => {
const response = await TurboselfFetcher("https://api-rest-prod.incb.fr" + PUT_BOOK_MEAL(id), {
method: "PUT",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + token
},
body: JSON.stringify({
dayOfWeek: day,
dayReserv: book,
web: {
id: bookId
},
hasHoteResaSoirActive: bookEvening
})
});


const raw = await response.json() as BookMealPut;
const establishment = new Establishment(
raw.web.hote.etab.code2p5,
raw.web.hote.etab.nom,
raw.web.hote.etab.versionTS,
raw.web.hote.etab.id,
raw.web.hote.etab.idTurboself,

raw.web.hote.etab.currencySymbol,
raw.web.hote.etab.configuration?.nbRepasMini ?? 0,
raw.web.hote.etab.configuration?.creanceMini?? 0,
raw.web.hote.etab.configuration?.montantCreditMini?? 0,

raw.web.hote.etab.configuration?.msgAccueil ?? "",

raw.web.hote.etab.desactive,
raw.web.hote.etab.numEtab,
raw.web.hote.etab.pcServeur,
raw.web.hote.etab.configurationsReservation?.map((data) => {
return {
id: data.id,
usage: data.usage,
elecom: data.elecom,
endReservation: data.finReserv
};
}),
{
address1: raw.web.hote.etab.adr1 ?? "",
address2: raw.web.hote.etab.adr2 ?? "",
zipCode: raw.web.hote.etab.cp ?? "",
city: raw.web.hote.etab.ville ?? ""
},
{
phoneNumber: raw.web.hote.etab.tel ?? "",
website: raw.web.hote.etab.configuration?.url ?? "",
email: raw.web.hote.etab.configuration?.email ?? ""
},
{
canStudentUseQrCode: raw.web.hote.etab.configuration?.autoriseQrCodeEleve?? false,
canCompanionUseQrCode: raw.web.hote.etab.configuration?.autoriseQrCodeCommensal?? false,
canInternUseQrCode: raw.web.hote.etab.configuration?.autoriseQrCodeStagiaire?? false,
canStudentSeeHistory: raw.web.hote.etab.configuration?.cacherHistorique?? false
},
{
firstSync: raw.web.hote.etab.datePremSynchro ?? "",
lastSync: raw.web.hote.etab.dateDernSynchro ?? "",
lastSelfSync: raw.web.hote.etab.configurationSelf?.dateDernSynchro ?? ""
}
);
const host = new Host(
raw.web.hote.id,
raw.web.hote.prenom,
raw.web.hote.nom,
raw.web.hote.qualite,
raw.web.hote.division,
raw.web.hote.prixDej,
establishment,
undefined,
{
lastSync: raw.web.hote.dateDernSynchro ?? ""
},
raw.web.hote.carteCodee ?? -1,
token
);

return new BookedMeal(
raw.id,
raw.dayReserv === 0 ? false : true,
raw.reservHorsKiosk === 0 ? false : true,
raw.reservDernSynchro === 0? false : true,
raw.rsvwebid,
raw.msg,
host
);
};
Loading

0 comments on commit e330d9d

Please sign in to comment.