diff --git a/src/common/notifications/NotificationBuilder.ts b/src/common/notifications/NotificationBuilder.ts index e51abb68..01ddae5d 100644 --- a/src/common/notifications/NotificationBuilder.ts +++ b/src/common/notifications/NotificationBuilder.ts @@ -95,7 +95,7 @@ export default class NotificationBuilder { redirection_url: "", created_at: new Date(), updated_at: new Date(), - user: [user] || [], + user: user ? [user] : [], }); } @@ -105,7 +105,7 @@ export default class NotificationBuilder { redirection_url: "", created_at: new Date(), updated_at: new Date(), - user: [user] || [], + user: user ? [user] : [], }); } diff --git a/src/services/common/IdNotService/IdNotService.ts b/src/services/common/IdNotService/IdNotService.ts index 331300ea..11a444a0 100644 --- a/src/services/common/IdNotService/IdNotService.ts +++ b/src/services/common/IdNotService/IdNotService.ts @@ -227,133 +227,157 @@ export default class IdNotService extends BaseService { key: this.variables.IDNOT_API_KEY, }); - let userRawData: Response; + let rattachmentRawData: Response; try { - userRawData = await fetch( - `${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/rattachements/${user.idNot}_${user.office_membership!.idNot}?` + - searchParams, + const url = `${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/personnes/${user.idNot}` + searchParams; + console.log("Fetching rattachements data for", url); + rattachmentRawData = await fetch( + url, { method: "GET", - }, - ) + } + ); } catch (error) { - console.error(`Error fetching user data for ${user.uid}: ${error}`); + console.error(`Error fetching rattachments data for ${user.uid}: ${error}`); return; } - if (userRawData.status === 404) { + if (rattachmentRawData.status === 404) { console.error(`User ${user.uid} not found in idnot`); return; - } else if (userRawData.status !== 200) { - console.error(`Error fetching user data for ${user.uid}: ${userRawData.status} - ${userRawData.statusText}`); - console.error(await userRawData.text()); + } else if (rattachmentRawData.status !== 200) { + console.error(`Error fetching rattachements data for ${user.uid}: ${rattachmentRawData.status}`); return; } - let userData = (await userRawData.json()) as IRattachementData; + let rattachements = (await rattachmentRawData.json()) as IRattachementData[]; - console.log("Got userData from idnot", JSON.stringify(userData)); + console.log("Got rattachements from idnot", JSON.stringify(rattachements)); + // if (!rattachements) return; + // rattachements.forEach(async (rattachement) => { + // if (rattachement.statutDuRattachement && !rattachement.deleted) { + // let officeData: IOfficeData; - if (userData.deleted) { - let rattachements: any; + // 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; + // } - console.log("Fetching rattachements for user", user.uid); - console.log(searchParams.toString()); - try { - rattachements = await fetch(`${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/personnes/${user.idNot}/rattachements?deleted=false` + searchParams, { - method: "GET", - }); - } catch (error) { - console.error("Error fetching rattachements", error); - return; - } + // if (officeData.typeEntite.name === "office") { + // // userData = rattachement; + // console.log("Updated userData", JSON.stringify(rattachement)); + // } + // } + // }); - if (rattachements.status === 404) { - console.error(`User ${user.uid} not found in idnot`); - return; - } else if (rattachements.status !== 200) { - console.error(`Error fetching rattachements for ${user.uid}: ${rattachements.status} - ${rattachements.statusText}`); - return; - } + // if (rattachements.deleted) { + // let rattachements: any; - if (rattachements.totalResultCount === 0) { - console.warn("User has no valid rattachements", user.uid); - await this.userService.updateCheckedAt(user.uid!); - return; - } - const rattachementsResults = rattachements.result as IRattachementData[]; - if (!rattachementsResults) return; - rattachementsResults.forEach(async (rattachement) => { - if (rattachement.statutDuRattachement && !rattachement.deleted) { - let officeData: IOfficeData; + // console.log("Fetching rattachements for user", user.uid); + // try { + // rattachements = await fetch(`${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/personnes/${user.idNot}/rattachements?deleted=false` + searchParams, { + // method: "GET", + // }); + // } catch (error) { + // console.error("Error fetching rattachements", error); + // return; + // } - 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 (rattachements.status === 404) { + // console.error(`User ${user.uid} not found in idnot`); + // return; + // } else if (rattachements.status !== 200) { + // console.error(`Error fetching rattachements for ${user.uid}: ${rattachements.status} - ${await rattachements.text()}`); + // return; + // } - if (officeData.typeEntite.name === "office") { - userData = rattachement; - console.log("Updated userData", JSON.stringify(userData)); - } - } - }); - } + // if (rattachements.totalResultCount === 0) { + // console.warn("User has no valid rattachements", user.uid); + // await this.userService.updateCheckedAt(user.uid!); + // return; + // } + // const rattachementsResults = rattachements.result as IRattachementData[]; + + // console.log("rattachementsResults", JSON.stringify(rattachementsResults)); + + // if (!rattachementsResults) return; + // rattachementsResults.forEach(async (rattachement) => { + // if (rattachement.statutDuRattachement && !rattachement.deleted) { + // 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; + // console.log("Updated userData", JSON.stringify(userData)); + // } + // } + // }); + // } let updates = 0; - if (user.office_membership!.idNot !== userData.entite.ou) { - updates++; - let officeData = (await this.officeService.get({ where: { idNot: userData.entite.ou } }))[0]; - if (!officeData) { - let officeLocationData: IOfficeLocation; + // if (user.office_membership!.idNot !== userData.entite.ou) { + // updates++; + // let officeData = (await this.officeService.get({ where: { idNot: userData.entite.ou } }))[0]; + // if (!officeData) { + // 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; - } + // 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 ?? userData.entite.codeCrpcen, - crpcen: userData.entite.codeCrpcen, - office_status: this.getOfficeStatus(userData.entite.statutEntite.name), - address: { - address: officeLocationData.result[0]!.adrGeo4, - city: officeLocationData.result[0]!.adrGeoVille.split(" ")[0] ?? officeLocationData.result[0]!.adrGeoVille, - zip_code: Number(officeLocationData.result[0]!.adrGeoCodePostal), - created_at: null, - updated_at: null, - }, - created_at: null, - updated_at: null, - }; - officeData = await this.officeService.create(office); - } - user.office_membership = officeData; - } + // const office = { + // idNot: userData.entite.ou, + // name: userData.entite.denominationSociale ?? userData.entite.codeCrpcen, + // crpcen: userData.entite.codeCrpcen, + // office_status: this.getOfficeStatus(userData.entite.statutEntite.name), + // address: { + // address: officeLocationData.result[0]!.adrGeo4, + // city: officeLocationData.result[0]!.adrGeoVille.split(" ")[0] ?? officeLocationData.result[0]!.adrGeoVille, + // zip_code: Number(officeLocationData.result[0]!.adrGeoCodePostal), + // created_at: null, + // updated_at: null, + // }, + // created_at: null, + // updated_at: null, + // }; + // officeData = await this.officeService.create(office); + // } + // user.office_membership = officeData; + // } - if (user.contact!.email !== userData.mailRattachement) { - updates++; - user.contact!.email = userData.mailRattachement; - } - if (user.contact!.cell_phone_number !== userData.numeroMobile) { - updates++; - user.contact!.cell_phone_number = userData.numeroMobile; - } + // if (user.contact!.email !== userData.mailRattachement) { + // updates++; + // user.contact!.email = userData.mailRattachement; + // } + // if (user.contact!.cell_phone_number !== userData.numeroMobile) { + // updates++; + // user.contact!.cell_phone_number = userData.numeroMobile; + // } if (updates != 0) await this.userService.update(user.uid!, user); await this.userService.updateCheckedAt(user.uid!); } @@ -565,17 +589,23 @@ export default class IdNotService extends BaseService { public async updateUsers() { const usersReq = await this.userService.getUsersToBeChecked(); const users = User.hydrateArray(usersReq); - users.forEach(async (user) => { + for (const user of users) { await this.updateUser(user.uid!); - }); + await this.delay(500); + }; } public async updateOffices() { const officesReq = await this.officeService.getOfficesToBeChecked(); const offices = Office.hydrateArray(officesReq); console.log(`Updating ${offices.length} offices`); - offices.forEach(async (office) => { + for (const office of offices) { await this.updateOffice(office.uid!); - }); + await this.delay(500); + }; + } + + private async delay(ms: number) { + return new Promise((resolve) => setTimeout(resolve, ms)); } }