Compare commits

...

17 Commits

Author SHA1 Message Date
Sosthene
fb98db0b02 Log error message body
All checks were successful
Demo - Build & Deploy to Scaleway / build-and-push-images-lecoffre (push) Successful in 1m47s
Demo - Build & Deploy to Scaleway / deploy-back-lecoffre (push) Successful in 5s
Demo - Build & Deploy to Scaleway / deploy-cron-lecoffre (push) Successful in 3s
2025-07-23 12:11:06 +02:00
Sosthene
70c481e545 Merge branch 'fix_update_users' into legacy_dev
All checks were successful
Demo - Build & Deploy to Scaleway / build-and-push-images-lecoffre (push) Successful in 1m43s
Demo - Build & Deploy to Scaleway / deploy-back-lecoffre (push) Successful in 3s
Demo - Build & Deploy to Scaleway / deploy-cron-lecoffre (push) Successful in 3s
2025-07-22 19:36:08 +02:00
Sosthene
ac0f5d7b3e more verbosity 2025-07-22 15:55:58 +02:00
Sosthene
b3d82f5b5d Merge branch 'fix_update_users' into legacy_dev
All checks were successful
Demo - Build & Deploy to Scaleway / build-and-push-images-lecoffre (push) Successful in 2m3s
Demo - Build & Deploy to Scaleway / deploy-back-lecoffre (push) Successful in 5s
Demo - Build & Deploy to Scaleway / deploy-cron-lecoffre (push) Successful in 3s
2025-07-22 14:48:24 +02:00
Sosthene
09f12422cd Handle raw response from idnot 2025-07-22 14:48:04 +02:00
Sosthene
6cca4e0fd5 Merge branch 'fix_update_users' into legacy_dev
All checks were successful
Demo - Build & Deploy to Scaleway / build-and-push-images-lecoffre (push) Successful in 1m51s
Demo - Build & Deploy to Scaleway / deploy-back-lecoffre (push) Successful in 3s
Demo - Build & Deploy to Scaleway / deploy-cron-lecoffre (push) Successful in 3s
2025-07-22 14:23:17 +02:00
Omar Oughriss
463327e6a1 Stringify logs 2025-07-22 14:22:10 +02:00
087dd0cd24 Merge pull request 'Stringify logs' (#4) from fix_update_users into legacy_dev
All checks were successful
Demo - Build & Deploy to Scaleway / build-and-push-images-lecoffre (push) Successful in 1m51s
Demo - Build & Deploy to Scaleway / deploy-back-lecoffre (push) Successful in 4s
Demo - Build & Deploy to Scaleway / deploy-cron-lecoffre (push) Successful in 3s
Reviewed-on: #4
2025-07-21 15:30:27 +00:00
Omar Oughriss
c90d7fc89a Stringify logs 2025-07-21 17:29:16 +02:00
db54f33f95 Merge pull request 'Update verbosity' (#3) from fix_update_users into legacy_dev
All checks were successful
Demo - Build & Deploy to Scaleway / build-and-push-images-lecoffre (push) Successful in 1m45s
Demo - Build & Deploy to Scaleway / deploy-back-lecoffre (push) Successful in 4s
Demo - Build & Deploy to Scaleway / deploy-cron-lecoffre (push) Successful in 3s
Reviewed-on: #3
2025-07-21 15:07:29 +00:00
Omar Oughriss
71a3fe3d22 Update verbosity 2025-07-21 17:06:19 +02:00
b50afa0ec8 Merge pull request 'Add updateUser method verbosity' (#2) from fix_update_users into legacy_dev
All checks were successful
Demo - Build & Deploy to Scaleway / build-and-push-images-lecoffre (push) Successful in 1m54s
Demo - Build & Deploy to Scaleway / deploy-back-lecoffre (push) Successful in 3s
Demo - Build & Deploy to Scaleway / deploy-cron-lecoffre (push) Successful in 3s
Reviewed-on: #2
2025-07-21 14:49:48 +00:00
Omar Oughriss
8fd16283d3 Add updateUser method verbosity 2025-07-21 16:48:11 +02:00
Omar Oughriss
5934213e95 Update updateUsers cron job to trigger every 5 min 2025-07-21 15:14:02 +02:00
ff3ef6b3b7 Merge pull request 'Update updateUsers cron job to trigger every 5 min' (#1) from fix_update_users into legacy_dev
All checks were successful
Demo - Build & Deploy to Scaleway / build-and-push-images-lecoffre (push) Successful in 1m46s
Demo - Build & Deploy to Scaleway / deploy-back-lecoffre (push) Successful in 4s
Demo - Build & Deploy to Scaleway / deploy-cron-lecoffre (push) Successful in 3s
Reviewed-on: #1
2025-07-21 13:12:44 +00:00
Sosthene
e5c7d9a972 Merge branch 'fix_update_users' into legacy_dev
All checks were successful
Demo - Build & Deploy to Scaleway / build-and-push-images-lecoffre (push) Successful in 1m46s
Demo - Build & Deploy to Scaleway / deploy-back-lecoffre (push) Successful in 3s
Demo - Build & Deploy to Scaleway / deploy-cron-lecoffre (push) Successful in 3s
2025-07-21 14:04:55 +02:00
Sosthene
49790c4f18 Check for deleted, not statutDuRattachement when updating user 2025-07-21 14:03:57 +02:00
3 changed files with 150 additions and 96 deletions

View File

@ -95,7 +95,7 @@ export default class NotificationBuilder {
redirection_url: "", redirection_url: "",
created_at: new Date(), created_at: new Date(),
updated_at: new Date(), updated_at: new Date(),
user: [user] || [], user: user ? [user] : [],
}); });
} }
@ -105,7 +105,7 @@ export default class NotificationBuilder {
redirection_url: "", redirection_url: "",
created_at: new Date(), created_at: new Date(),
updated_at: new Date(), updated_at: new Date(),
user: [user] || [], user: user ? [user] : [],
}); });
} }

View File

@ -68,7 +68,7 @@ export default class CronService {
} }
} }
public async updateUsers() { public async updateUsers() {
const cronJob = new CronJob("0 0 * * *", async () => { const cronJob = new CronJob("*/5 * * * *", async () => {
// Once a day at midnight // Once a day at midnight
try { try {
await this.idNotService.updateOffices(); await this.idNotService.updateOffices();

View File

@ -50,6 +50,7 @@ interface IRattachementData {
numeroTelephone: string; numeroTelephone: string;
statutDuRattachement: boolean; statutDuRattachement: boolean;
mailRattachement: string; mailRattachement: string;
deleted: boolean;
} }
interface IOfficeData { interface IOfficeData {
@ -221,116 +222,161 @@ export default class IdNotService extends BaseService {
public async updateUser(userId: string) { public async updateUser(userId: string) {
const userInfos = await this.userService.getByUid(userId, { contact: true, role: true, office_membership: true }); const userInfos = await this.userService.getByUid(userId, { contact: true, role: true, office_membership: true });
const user = User.hydrate<User>(userInfos!); const user = User.hydrate<User>(userInfos!);
console.log("Got user from db:", JSON.stringify(user));
const searchParams = new URLSearchParams({ const searchParams = new URLSearchParams({
key: this.variables.IDNOT_API_KEY, key: this.variables.IDNOT_API_KEY,
}); });
let userData: IRattachementData; let rattachmentRawData: Response;
try { try {
userData = (await ( rattachmentRawData = await fetch(
await fetch( `${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/personnes/${user.idNot}/entites?deleted=false` +
`${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/rattachements/${user.idNot}_${user.office_membership!.idNot}?` + searchParams,
searchParams,
{ {
method: "GET", method: "GET",
}, },
) )
).json()) as IRattachementData;
} catch (error) { } catch (error) {
console.error("Error fetching user data", error); console.error(`Error fetching rattachments data for ${user.uid}: ${error}`);
return;
}
if (rattachmentRawData.status === 404) {
console.error(`User ${user.uid} not found in idnot`);
return;
} else if (rattachmentRawData.status !== 200) {
console.error(`Error fetching rattachements data for ${user.uid}: ${rattachmentRawData.status}`);
return; return;
} }
if (!userData.statutDuRattachement) { let rattachements = (await rattachmentRawData.json()) as IRattachementData[];
let rattachements: any;
try { console.log("Got rattachements from idnot", JSON.stringify(rattachements));
rattachements = (await ( if (!rattachements) return;
await fetch(`${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/personnes/${user.idNot}/rattachements?` + searchParams, { rattachements.forEach(async (rattachement) => {
method: "GET", if (rattachement.statutDuRattachement && !rattachement.deleted) {
}) let officeData: IOfficeData;
).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!);
return;
}
const rattachementsResults = rattachements.result as IRattachementData[];
if (!rattachementsResults) return;
rattachementsResults.forEach(async (rattachement) => {
if (rattachement.statutDuRattachement) {
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;
}
}
});
}
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;
try { try {
officeLocationData = (await ( officeData = (await (
await fetch(`${this.variables.IDNOT_API_BASE_URL + userData.entite.locationsUrl}?` + searchParams, { await fetch(`${this.variables.IDNOT_API_BASE_URL + rattachement.entiteUrl}?` + searchParams, {
method: "GET", method: "GET",
}) })
).json()) as IOfficeLocation; ).json()) as IOfficeData;
} catch (error) { } catch (error) {
console.error("Error fetching office location data", error); console.error("Error fetching office data", error);
return; return;
} }
const office = { if (officeData.typeEntite.name === "office") {
idNot: userData.entite.ou, // userData = rattachement;
name: userData.entite.denominationSociale ?? userData.entite.codeCrpcen, console.log("Updated userData", JSON.stringify(rattachement));
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) { // if (rattachements.deleted) {
updates++; // let rattachements: any;
user.contact!.email = userData.mailRattachement;
} // console.log("Fetching rattachements for user", user.uid);
if (user.contact!.cell_phone_number !== userData.numeroMobile) { // try {
updates++; // rattachements = await fetch(`${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/personnes/${user.idNot}/rattachements?deleted=false` + searchParams, {
user.contact!.cell_phone_number = userData.numeroMobile; // method: "GET",
} // });
// } catch (error) {
// console.error("Error fetching rattachements", 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 (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;
// 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;
// }
// 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); if (updates != 0) await this.userService.update(user.uid!, user);
await this.userService.updateCheckedAt(user.uid!); await this.userService.updateCheckedAt(user.uid!);
} }
@ -359,19 +405,20 @@ export default class IdNotService extends BaseService {
return; return;
} }
const officeData = (await officeRawData.json()) as IOfficeData; const officeData = (await officeRawData.json()) as IOfficeData;
console.log("office", office);
console.log("officeData", officeData);
let updates = 0; let updates = 0;
//093051 = demo //093051 = demo
if (office.name !== officeData.denominationSociale && office.name !== officeData.codeCrpcen && office.crpcen !== "029178" && office.crpcen !== "035010" && office.crpcen !== "093051") { if (office.name !== officeData.denominationSociale && office.name !== officeData.codeCrpcen && office.crpcen !== "029178" && office.crpcen !== "035010" && office.crpcen !== "093051") {
console.log(`Updating office name: ${office.uid} - ${office.name} - ${office.crpcen}`);
updates++; updates++;
office.name = officeData.denominationSociale ?? officeData.codeCrpcen; office.name = officeData.denominationSociale ?? officeData.codeCrpcen;
console.log(`New name: ${office.name}`);
} }
if (office.office_status !== this.getOfficeStatus(officeData.statutEntite.name)) { if (office.office_status !== this.getOfficeStatus(officeData.statutEntite.name)) {
console.log(`Updating office status: ${office.uid} - ${office.name} - ${office.crpcen}`);
updates++; updates++;
office.office_status = this.getOfficeStatus(officeData.statutEntite.name); office.office_status = this.getOfficeStatus(officeData.statutEntite.name);
console.log(`New status: ${office.office_status}`);
} }
if (updates != 0) await this.officeService.update(office.uid!, office); if (updates != 0) await this.officeService.update(office.uid!, office);
await this.officeService.updateCheckedAt(office.uid!); await this.officeService.updateCheckedAt(office.uid!);
@ -541,16 +588,23 @@ export default class IdNotService extends BaseService {
public async updateUsers() { public async updateUsers() {
const usersReq = await this.userService.getUsersToBeChecked(); const usersReq = await this.userService.getUsersToBeChecked();
const users = User.hydrateArray<User>(usersReq); const users = User.hydrateArray<User>(usersReq);
users.forEach(async (user) => { for (const user of users) {
await this.updateUser(user.uid!); await this.updateUser(user.uid!);
}); await this.delay(500);
};
} }
public async updateOffices() { public async updateOffices() {
const officesReq = await this.officeService.getOfficesToBeChecked(); const officesReq = await this.officeService.getOfficesToBeChecked();
const offices = Office.hydrateArray<Office>(officesReq); const offices = Office.hydrateArray<Office>(officesReq);
offices.forEach(async (office) => { console.log(`Updating ${offices.length} offices`);
for (const office of offices) {
await this.updateOffice(office.uid!); await this.updateOffice(office.uid!);
}); await this.delay(500);
};
}
private async delay(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
} }
} }