Compare commits
17 Commits
86725b53fe
...
fb98db0b02
Author | SHA1 | Date | |
---|---|---|---|
![]() |
fb98db0b02 | ||
![]() |
70c481e545 | ||
![]() |
ac0f5d7b3e | ||
![]() |
b3d82f5b5d | ||
![]() |
09f12422cd | ||
![]() |
6cca4e0fd5 | ||
![]() |
463327e6a1 | ||
087dd0cd24 | |||
![]() |
c90d7fc89a | ||
db54f33f95 | |||
![]() |
71a3fe3d22 | ||
b50afa0ec8 | |||
![]() |
8fd16283d3 | ||
![]() |
5934213e95 | ||
ff3ef6b3b7 | |||
![]() |
e5c7d9a972 | ||
![]() |
49790c4f18 |
@ -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] : [],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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();
|
||||||
|
@ -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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user