From edcdb1a15cfdc4db451544325a6afc68f31d2416 Mon Sep 17 00:00:00 2001 From: Vins Date: Tue, 8 Oct 2024 09:06:31 +0200 Subject: [PATCH] Added try catch and logs for connexion --- src/app/api/idnot/UserController.ts | 18 +- .../common/IdNotService/IdNotService.ts | 156 +++++++++++++----- 2 files changed, 126 insertions(+), 48 deletions(-) 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/common/IdNotService/IdNotService.ts b/src/services/common/IdNotService/IdNotService.ts index e9a8cda1..ea96b0e9 100644 --- a/src/services/common/IdNotService/IdNotService.ts +++ b/src/services/common/IdNotService/IdNotService.ts @@ -122,15 +122,16 @@ export default class IdNotService extends BaseService { grant_type: "authorization_code", }); - 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; + try { + 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(error); + return null; + } } public async getRole(roleName: string): Promise { @@ -211,22 +212,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!); @@ -236,11 +252,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; } @@ -254,9 +278,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, @@ -289,22 +323,34 @@ export default class IdNotService extends BaseService { await this.userService.updateCheckedAt(user.uid!); } - public async updateOffice(officeId: string) { - const officeInfos = await this.officeService.getByUid(officeId); + public async updateOffice(officeId: string) { + 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++; @@ -325,19 +371,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 } }); @@ -378,6 +443,7 @@ export default class IdNotService extends BaseService { }; if (!userToAdd.contact.email) { + console.error("User pro email empty"); return null; }