diff --git a/src/app/api/idnot/UserController.ts b/src/app/api/idnot/UserController.ts index ac4db9e9..54e95067 100644 --- a/src/app/api/idnot/UserController.ts +++ b/src/app/api/idnot/UserController.ts @@ -32,19 +32,24 @@ export default class UserController extends ApiController { protected async getUserInfosFromIdnot(req: Request, response: Response) { try { const code = req.params["code"]; + console.log("code", code); if (!code) throw new Error("code is required"); const idNotToken = await this.idNotService.getIdNotToken(code); + console.log("idNotToken", idNotToken); if (!idNotToken) { + console.error("IdNot token undefined"); this.httpValidationError(response, "IdNot token undefined"); return; } const user = await this.idNotService.getOrCreateUser(idNotToken); + console.log("user", user); if (!user) { + console.error("User not found"); this.httpUnauthorized(response, "User not found"); return; } @@ -56,6 +61,7 @@ export default class UserController extends ApiController { const prismaUser = await this.userService.getByUid(user.uid, { contact: true, role: true, office_membership: true }); if (!prismaUser) { + console.error("Prisma user not found"); this.httpNotFoundRequest(response, "user not found"); return; } @@ -65,11 +71,11 @@ export default class UserController extends ApiController { if (!userHydrated.contact?.email || userHydrated.contact?.email === "") { + console.error("Email not found"); this.httpUnauthorized(response, "Email not found"); return; } let isSubscribed = await this.subscriptionsService.isUserSubscribed(user.uid, userHydrated.office_membership?.uid!); - //Check if user is whitelisted // const isWhitelisted = await this.whitelistService.getByEmail(userHydrated.contact!.email); @@ -86,8 +92,13 @@ export default class UserController extends ApiController { await this.idNotService.updateOffice(user.office_uid); const payload = await this.authService.getUserJwtPayload(user.idNot); + console.log("payload", payload); - if (!payload) return; + + if (!payload) { + console.error("No payload"); + return; + } if (!isSubscribed && (userHydrated.role?.name === "admin" || userHydrated.role?.name === "super-admin")) { @@ -106,7 +117,8 @@ export default class UserController extends ApiController { isSubscribed = true; } - if (!isSubscribed) { + if (!isSubscribed) { + console.error("User not subscribed"); this.httpUnauthorized(response, "User not subscribed"); return; } diff --git a/src/services/admin/UsersService/UsersService.ts b/src/services/admin/UsersService/UsersService.ts index 934e1341..d13c6429 100644 --- a/src/services/admin/UsersService/UsersService.ts +++ b/src/services/admin/UsersService/UsersService.ts @@ -24,7 +24,7 @@ export default class UsersService extends BaseService { * @throws {Error} If user modification failed */ public async update(uid: string, userEntity: User): Promise { - return this.userRepository.updateOfficeRole(uid, userEntity); + return this.userRepository.update(uid, userEntity); } /** diff --git a/src/services/common/IdNotService/IdNotService.ts b/src/services/common/IdNotService/IdNotService.ts index 74b9a233..6c0fa698 100644 --- a/src/services/common/IdNotService/IdNotService.ts +++ b/src/services/common/IdNotService/IdNotService.ts @@ -122,23 +122,14 @@ export default class IdNotService extends BaseService { grant_type: "authorization_code", }); - const url = this.variables.IDNOT_BASE_URL + this.variables.IDNOT_CONNEXION_URL + "?" + query; - - console.log("getIdNotToken"); - console.log("Tentative de connexion à l'URL :", url); - try { - const token = await fetch(url, { method: "POST" }); - console.log("Réponse du fetch :", token); + const token = await fetch(this.variables.IDNOT_BASE_URL + this.variables.IDNOT_CONNEXION_URL + "?" + query, { method: "POST" }); if (token.status !== 200) console.error(await token.text()); - const decodedToken = (await token.json()) as IIdNotToken; - const decodedIdToken = jwt.decode(decodedToken.id_token) as IdNotJwtPayload; - return decodedIdToken; } catch (error) { - console.error("Erreur lors de l'appel à fetch :", error); + console.error(error); return null; } } @@ -232,22 +223,37 @@ export default class IdNotService extends BaseService { const searchParams = new URLSearchParams({ key: this.variables.IDNOT_API_KEY, }); - let userData = (await ( - await fetch( - `${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/rattachements/${user.idNot}_${user.office_membership!.idNot}?` + - searchParams, - { - method: "GET", - }, - ) - ).json()) as IRattachementData; + + let userData: IRattachementData; + try { + userData = (await ( + await fetch( + `${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/rattachements/${user.idNot}_${user.office_membership!.idNot}?` + + searchParams, + { + method: "GET", + }, + ) + ).json()) as IRattachementData; + } catch (error) { + console.error("Error fetching user data", error); + return; + } if (!userData.statutDuRattachement) { - const rattachements = (await ( - await fetch(`${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/personnes/${user.idNot}/rattachements?` + searchParams, { - method: "GET", - }) - ).json()) as any; + let rattachements: any; + + try { + rattachements = (await ( + await fetch(`${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/personnes/${user.idNot}/rattachements?` + searchParams, { + method: "GET", + }) + ).json()) as any; + } catch (error) { + console.error("Error fetching rattachements", error); + return; + } + if (rattachements.totalResultCount === 0) { await this.userService.updateCheckedAt(user.uid!); //await this.userService.delete(user.uid!); @@ -257,11 +263,19 @@ export default class IdNotService extends BaseService { if (!rattachementsResults) return; rattachementsResults.forEach(async (rattachement) => { if (rattachement.statutDuRattachement) { - const officeData = (await ( - await fetch(`${this.variables.IDNOT_API_BASE_URL + rattachement.entiteUrl}?` + searchParams, { - method: "GET", - }) - ).json()) as IOfficeData; + let officeData: IOfficeData; + + try { + officeData = (await ( + await fetch(`${this.variables.IDNOT_API_BASE_URL + rattachement.entiteUrl}?` + searchParams, { + method: "GET", + }) + ).json()) as IOfficeData; + } catch (error) { + console.error("Error fetching office data", error); + return; + } + if (officeData.typeEntite.name === "office") { userData = rattachement; } @@ -275,9 +289,19 @@ export default class IdNotService extends BaseService { updates++; let officeData = (await this.officeService.get({ where: { idNot: userData.entite.ou } }))[0]; if (!officeData) { - const officeLocationData = (await ( - await fetch(`${this.variables.IDNOT_API_BASE_URL + userData.entite.locationsUrl}?` + searchParams, { method: "GET" }) - ).json()) as IOfficeLocation; + let officeLocationData: IOfficeLocation; + + try { + officeLocationData = (await ( + await fetch(`${this.variables.IDNOT_API_BASE_URL + userData.entite.locationsUrl}?` + searchParams, { + method: "GET", + }) + ).json()) as IOfficeLocation; + } catch (error) { + console.error("Error fetching office location data", error); + return; + } + const office = { idNot: userData.entite.ou, name: userData.entite.denominationSociale, @@ -311,20 +335,33 @@ export default class IdNotService extends BaseService { } public async updateOffice(officeId: string) { - const officeInfos = await this.officeService.getByUid(officeId); + const officeInfos = await this.officeService.getByUid(officeId, { address: true }); const office = Office.hydrate(officeInfos!); const searchParams = new URLSearchParams({ key: this.variables.IDNOT_API_KEY, }); - const officeRawData = await fetch(`${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/entites/${office.idNot}?` + searchParams, { - method: "GET", - }); + + let officeRawData; + try { + officeRawData = await fetch(`${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/entites/${office.idNot}?` + searchParams, { + method: "GET", + }); + } catch (error) { + console.error("Error fetching office data", error); + return; + } + if (officeRawData.status === 404) { + console.error("Fetching office raw data failed with status 404"); await this.officeService.updateCheckedAt(office.uid!); //await this.officeService.delete(office.uid!); return; } const officeData = (await officeRawData.json()) as IOfficeData; + console.log("office", office); + + console.log("officeData", officeData); + let updates = 0; if (office.name !== officeData.denominationSociale) { updates++; @@ -345,19 +382,38 @@ export default class IdNotService extends BaseService { key: this.variables.IDNOT_API_KEY, }); - const userData = (await ( - await fetch(`${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/rattachements/${decodedToken.profile_idn}?` + searchParams, { - method: "GET", - }) - ).json()) as IRattachementData; + let userData: IRattachementData; - if (!userData.statutDuRattachement || userData.entite.typeEntite.name !== "office") { + try { + userData = (await ( + await fetch( + `${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/rattachements/${decodedToken.profile_idn}?` + searchParams, + { + method: "GET", + }, + ) + ).json()) as IRattachementData; + console.log("userData", userData); + } catch (error) { + console.error(error); return null; } - const officeLocationData = (await ( - await fetch(`${this.variables.IDNOT_API_BASE_URL + userData.entite.locationsUrl}?` + searchParams, { method: "GET" }) - ).json()) as IOfficeLocation; + if (!userData.statutDuRattachement || userData.entite.typeEntite.name !== "office") { + console.info("User not attached to an office (May be a partner)"); + return null; + } + + let officeLocationData: IOfficeLocation; + + try { + officeLocationData = (await ( + await fetch(`${this.variables.IDNOT_API_BASE_URL + userData.entite.locationsUrl}?` + searchParams, { method: "GET" }) + ).json()) as IOfficeLocation; + } catch (error) { + console.error(error); + return null; + } const office = await this.officeService.get({ where: { idNot: decodedToken.entity_idn } }); @@ -398,6 +454,7 @@ export default class IdNotService extends BaseService { }; if (!userToAdd.contact.email) { + console.error("User pro email empty"); return null; }