diff --git a/src/app/api/customer/DocumentsController.ts b/src/app/api/customer/DocumentsController.ts index 42fdd63b..d802ba36 100644 --- a/src/app/api/customer/DocumentsController.ts +++ b/src/app/api/customer/DocumentsController.ts @@ -92,9 +92,7 @@ export default class DocumentsController extends ApiController { protected async post(req: Request, response: Response) { try { //init Document resource with request body values - const documentEntity = Document.hydrate(req.body); - console.log(documentEntity); - + const documentEntity = Document.hydrate(req.body); //validate document await validateOrReject(documentEntity, { groups: ["createDocument"], forbidUnknownValues: false }); diff --git a/src/app/api/customer/FilesController.ts b/src/app/api/customer/FilesController.ts index 83f595ec..4be714ef 100644 --- a/src/app/api/customer/FilesController.ts +++ b/src/app/api/customer/FilesController.ts @@ -90,8 +90,7 @@ export default class FilesController extends ApiController { //init File resource with request body values const fileEntity = File.hydrate(JSON.parse(req.body["q"])); - console.log(fileEntity); - + //validate File // await validateOrReject(fileEntity, { groups: ["createFile"] }); diff --git a/src/app/api/idnot/UserController.ts b/src/app/api/idnot/UserController.ts index e15b0c47..8540aa56 100644 --- a/src/app/api/idnot/UserController.ts +++ b/src/app/api/idnot/UserController.ts @@ -7,7 +7,6 @@ import { JwtPayload } from "jsonwebtoken"; import IdNotService from "@Services/common/IdNotService/IdNotService"; - @Controller() @Service() export default class UserController extends ApiController { @@ -29,14 +28,18 @@ export default class UserController extends ApiController { const idNotToken = await this.idNotService.getIdNotToken(code); const user = await this.idNotService.getOrCreateUser(idNotToken); + if(!user) { + this.httpUnauthorized(response); + return; + } await this.idNotService.updateUser(user.uid); await this.idNotService.updateOffice(user.office_uid); const payload = await this.authService.getUserJwtPayload(user.idNot); const accessToken = this.authService.generateAccessToken(payload); const refreshToken = this.authService.generateRefreshToken(payload); - - this.httpSuccess(response, {accessToken, refreshToken}); + + this.httpSuccess(response, { accessToken, refreshToken }); } catch (error) { console.log(error); this.httpInternalError(response); @@ -77,7 +80,7 @@ export default class UserController extends ApiController { let accessToken; this.authService.verifyRefreshToken(token, (err, userPayload) => { if (err) { - console.log(err) + console.log(err); this.httpUnauthorized(response); return; } @@ -89,7 +92,7 @@ export default class UserController extends ApiController { }); //success - this.httpSuccess(response, {accessToken}); + this.httpSuccess(response, { accessToken }); } catch (error) { this.httpInternalError(response); return; diff --git a/src/services/common/CronService/CronService.ts b/src/services/common/CronService/CronService.ts index d8c6cd38..09879344 100644 --- a/src/services/common/CronService/CronService.ts +++ b/src/services/common/CronService/CronService.ts @@ -39,7 +39,7 @@ export default class CronService { } } public async updateUsers() { - const cronJob = new CronJob("*/15 * * * *", async () => { // Every 15 minutes + const cronJob = new CronJob("0 0 * * *", async () => { // Once a day at midnight try { await this.idNotService.updateOffices(); await this.idNotService.updateUsers(); diff --git a/src/services/common/IdNotService/IdNotService.ts b/src/services/common/IdNotService/IdNotService.ts index 17c3378b..588af538 100644 --- a/src/services/common/IdNotService/IdNotService.ts +++ b/src/services/common/IdNotService/IdNotService.ts @@ -62,6 +62,9 @@ interface IOfficeData { statutEntite: { name: string; }; + typeEntite: { + name: string; + } } interface IOfficeLocation { @@ -165,19 +168,43 @@ export default class IdNotService extends BaseService { const searchParams = new URLSearchParams({ key: this.variables.IDNOT_API_KEY, }); - const userRawData = await (await fetch( - `${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/personnes/${user.idNot}/rattachements?` + + 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 any; - if (userRawData.totalResultCount === 0) { - await this.userService.updateCheckedAt(user.uid!); - //await this.userService.delete(user.uid!); - return; + )).json() as IRattachementData; + + 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; + if (rattachements.totalResultCount === 0) { + await this.userService.updateCheckedAt(user.uid!); + //await this.userService.delete(user.uid!); + return; + } + const rattachementsResults = rattachements.result as IRattachementData[]; + 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; + if(officeData.typeEntite.name === "office") { + userData = rattachement; + } + } + }); } - const userData = userRawData.result[0] as IRattachementData; const roleFromIdNot = await this.getRole(userData.typeLien.name); let updates = 0; @@ -186,24 +213,22 @@ export default class IdNotService extends BaseService { user.role = roleFromIdNot; } - if (user.office_membership!.idNot !== userData.entiteUrl.split("/")[5]!) { + if (user.office_membership!.idNot !== userData.entite.ou) { updates++; - let officeData = (await this.officeService.get({ where: { idNot: userData.entiteUrl.split("/")[5]! } }))[0]; + let officeData = (await this.officeService.get({ where: { idNot:userData.entite.ou } }))[0]; if (!officeData) { - const officeIdNotData = (await ( - await fetch(`${this.variables.IDNOT_API_BASE_URL + userData.entiteUrl}?` + searchParams, { method: "GET" }) - ).json()) as IOfficeData; + const officeLocationData = (await ( + await fetch(`${this.variables.IDNOT_API_BASE_URL + userData.entite.locationsUrl}?` + searchParams, { method: "GET" }) + ).json()) as IOfficeLocation; const office = { - idNot: userData.entiteUrl.split("/")[5]!, - name: officeIdNotData.denominationSociale - ? officeIdNotData.denominationSociale - : `office ${userData.entiteUrl.split("/")[5]!}`, - crpcen: officeIdNotData.codeCrpcen, - office_status: this.getOfficeStatus(officeIdNotData.statutEntite.name), + idNot: userData.entite.ou, + name: userData.entite.denominationSociale, + crpcen: userData.entite.codeCrpcen, + office_status: this.getOfficeStatus(userData.entite.statutEntite.name), address: { - address: officeIdNotData.departementResidence[0]!.libelle, //officeLocationData.result[0]!.adrPostale4, - city: "city", //officeLocationData.result[0]!.adrPostaleVille, - zip_code: Number(officeIdNotData.departementResidence[0]!.code), + address: officeLocationData.result[0]!.adrGeo4, + city: officeLocationData.result[0]!.adrGeoVille.split(" ")[0] ?? officeLocationData.result[0]!.adrGeoVille, //officeLocationData.result[0]!.adrPostaleVille, + zip_code: Number(officeLocationData.result[0]!.adrGeoCodePostal), created_at: null, updated_at: null, }, @@ -272,10 +297,19 @@ export default class IdNotService extends BaseService { }) ).json()) as IRattachementData; + + if(!userData.statutDuRattachement || userData.entite.typeEntite.name !== "office") { + return null; + } + const officeLocationData = (await ( await fetch(`${this.variables.IDNOT_API_BASE_URL + userData.entite.locationsUrl}?` + searchParams, { method: "GET" }) ).json()) as IOfficeLocation; + // if(officeLocationData.result[0]!.adrGeoCodePostal.slice(0,2) !== "35") { + // return null; + // } + const role = await this.getRole(userData.typeLien.name); const userToAdd = {