Merge remote-tracking branch 'refs/remotes/origin/main'
This commit is contained in:
@@ -2,6 +2,7 @@ import api, { activiteService, athleteService, coachService, sessionService } fr
|
||||
import { Activite, Admin, Athlete, Coach, Session, User } from "./classes";
|
||||
import Keycloak from 'keycloak-js'
|
||||
import { AdminDTO, AthleteDTO, CoachDTO } from "./classesDTO";
|
||||
import { useLocalData } from "./context/useLocalData";
|
||||
|
||||
//debug:
|
||||
export function delay(ms: number): Promise<void> {
|
||||
@@ -15,28 +16,46 @@ export function delay(ms: number): Promise<void> {
|
||||
/*
|
||||
retourne l'utilisateur lié à l'identifiant keyloack
|
||||
*/
|
||||
export async function getUser(keycloak:Keycloak): Promise<User|null>{
|
||||
export async function loginOrRegister(keycloak:Keycloak): Promise<User|null>{
|
||||
try {
|
||||
if(keycloak.tokenParsed!=null){
|
||||
const roles = keycloak.tokenParsed?.realm_access?.roles
|
||||
if(roles!=null){
|
||||
if(roles.includes("admin")){
|
||||
const response = await athleteService.create(keycloak.tokenParsed);
|
||||
const id = keycloak.tokenParsed?.sub;
|
||||
if(id!=null){
|
||||
const response = await athleteService.getByKeycloakId(id);
|
||||
const admin = new Admin(response.data);
|
||||
return admin;
|
||||
}
|
||||
}
|
||||
else if(roles.includes("coach")){
|
||||
const response = await coachService.create(keycloak.tokenParsed);
|
||||
const id = keycloak.tokenParsed?.sub;
|
||||
if(id!=null){
|
||||
const response = await coachService.getByKeycloakId(id);
|
||||
const coach = new Coach(response.data);
|
||||
return coach;
|
||||
}
|
||||
else if(roles.includes("athletes")){
|
||||
const response = await athleteService.create(keycloak.tokenParsed);
|
||||
}
|
||||
else if(roles.includes("athlete")){
|
||||
console.error("role = Athlete");
|
||||
const newAthlete: Athlete = new Athlete();
|
||||
newAthlete.keycloakId = keycloak.tokenParsed.sub || "";
|
||||
newAthlete.email = keycloak.tokenParsed.email || "";
|
||||
newAthlete.nom = keycloak.tokenParsed.family_name || "";
|
||||
newAthlete.prenom = keycloak.tokenParsed.given_name || "";
|
||||
const response = await athleteService.create(newAthlete.toDTO());
|
||||
console.log(response);
|
||||
const athlete = new Athlete(response.data);
|
||||
console.log(athlete);
|
||||
return athlete;
|
||||
}
|
||||
console.error("Error roles inconnu");
|
||||
else console.error("Error : role inconnu");
|
||||
}
|
||||
else console.error("Error : role null");
|
||||
}
|
||||
|
||||
else console.error("Error : token Keylcoak null");
|
||||
console.error("Error : pendant la récupération de l'User");
|
||||
return null;
|
||||
}
|
||||
catch (error) {
|
||||
@@ -45,27 +64,21 @@ export async function getUser(keycloak:Keycloak): Promise<User|null>{
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
retourne toutes les Session dont l'user est inscrit
|
||||
*/
|
||||
export async function updateSessionsOfUserAPI(user:Coach|Athlete, min: Date|null, max: Date|null){
|
||||
export async function updateActivitiesOfSessionAPI(session:Session,activities:Activite[]){
|
||||
try {
|
||||
const response = await api.get<Session[]>(`/users/${user.id}/sessions`, {
|
||||
params: {
|
||||
minDate: min ? min.toISOString() : undefined,
|
||||
maxDate: max ? max.toISOString() : undefined
|
||||
}
|
||||
});
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error("Error fetching sessions for user:", error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export async function updateActivitiesOfSessionAPI(session:Session){
|
||||
try {
|
||||
const response = await api.get<Activite[]>(`/sessions/${session.id}/activities`);
|
||||
const session_id = session.id
|
||||
for (const activity of activities) {
|
||||
const response = await activiteService.update(activity.id!, {
|
||||
name: activity.nom,
|
||||
duree: activity.duree,
|
||||
date: activity.data,
|
||||
theme: activity.theme,
|
||||
sessionId: session_id,
|
||||
},
|
||||
);
|
||||
}
|
||||
// To refresh the activities in the session object
|
||||
const response = await sessionService.getActivities(session_id!);
|
||||
session.activites = response.data;
|
||||
} catch (error) {
|
||||
console.error("Error fetching activities for session:", error);
|
||||
@@ -75,7 +88,9 @@ export async function updateActivitiesOfSessionAPI(session:Session){
|
||||
|
||||
export async function subscribeSessionAPI(user:User, session:Session):Promise<boolean>{
|
||||
try {
|
||||
await api.post(`/sessions/${session.id}/subscribe`);
|
||||
const session_id =session.id
|
||||
const user_id = user.id
|
||||
const response = await sessionService.subscribe(session_id!, user_id!);
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error("Error subscribing to session:", error);
|
||||
@@ -85,7 +100,9 @@ export async function subscribeSessionAPI(user:User, session:Session):Promise<bo
|
||||
|
||||
export async function unsubscribeSessionAPI(user:User, session:Session):Promise<boolean>{
|
||||
try {
|
||||
await api.post(`/session/${session.id}/unsubscribe`);
|
||||
const session_id =session.id
|
||||
const user_id = user.id
|
||||
const response = await sessionService.unsubscribe(session_id!, user_id!);
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error("Error unsubscribing from session:", error);
|
||||
@@ -95,10 +112,6 @@ export async function unsubscribeSessionAPI(user:User, session:Session):Promise<
|
||||
|
||||
// ADMIN :
|
||||
|
||||
export async function updateAllSessionAPI(min: Date, max: Date){
|
||||
//TODO
|
||||
}
|
||||
|
||||
export async function updateAllUserAPI(){
|
||||
|
||||
}
|
||||
@@ -192,26 +205,37 @@ export async function setSessionCreneauAPI(session: Session, date:Date){
|
||||
|
||||
}
|
||||
|
||||
export async function getSessionsAPI(): Promise<Session[]> {
|
||||
//GET /////////////////////////////////////////////////////////
|
||||
|
||||
//SESSION
|
||||
|
||||
export async function getSessionsOfUserAPI(user:Coach|Athlete){
|
||||
try {
|
||||
const response = await api.get<Session[]>("/session");
|
||||
if (user instanceof Coach) {
|
||||
const response = await coachService.getSessionsForCoach(user.id); //TODO
|
||||
return response.data;
|
||||
}else if (user instanceof Athlete) {
|
||||
const response = await athleteService.getSessionsForAthlete(user.id); //TODO
|
||||
return response.data;
|
||||
}
|
||||
}catch (error) {
|
||||
console.error("Error fetching sessions for user:", error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export async function getAllSessionsAPI():Promise<Session[]>{
|
||||
try {
|
||||
const response = await sessionService.getAll();
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error("Error fetching sessions:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
export async function getUsersAPI(): Promise<User[]> {
|
||||
try {
|
||||
const response = await api.get<User[]>("/coach/all");
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error("Error fetching users:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getCoachsAPI(): Promise<Coach[]> {
|
||||
//COACH
|
||||
export async function getAllCoach(): Promise<Coach[]> {
|
||||
try {
|
||||
const response = await api.get<Coach[]>("/coach/all");
|
||||
console.log(response);
|
||||
@@ -220,4 +244,16 @@ export async function getCoachsAPI(): Promise<Coach[]> {
|
||||
console.error("Error fetching coachs:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//ATHLETE
|
||||
export async function getAllAthlete(): Promise<Athlete[]> {
|
||||
try {
|
||||
const response = await athleteService.getAll();
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error("Error fetching users:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user