diff --git a/server/src/models/UserFiltersApi.ts b/server/src/models/UserFiltersApi.ts index cf48be4fa..61d21b500 100644 --- a/server/src/models/UserFiltersApi.ts +++ b/server/src/models/UserFiltersApi.ts @@ -1,3 +1,4 @@ export interface UserFiltersApi { establishmentIds?: string[]; + disabled?: boolean; } diff --git a/server/src/repositories/userRepository.ts b/server/src/repositories/userRepository.ts index f1d109059..94351d5bf 100644 --- a/server/src/repositories/userRepository.ts +++ b/server/src/repositories/userRepository.ts @@ -74,6 +74,9 @@ function filter(filters?: UserFiltersApi) { if (filters?.establishmentIds?.length) { builder.whereIn('establishment_id', filters.establishmentIds); } + if (filters?.disabled !== undefined) { + builder.where('disabled', filters.disabled); + } }; } diff --git a/server/src/services/ceremaService/ceremaService.ts b/server/src/services/ceremaService/ceremaService.ts index 8c04aa5b1..b3742eec4 100644 --- a/server/src/services/ceremaService/ceremaService.ts +++ b/server/src/services/ceremaService/ceremaService.ts @@ -1,8 +1,10 @@ +import EstablishmentMissingError from '~/errors/establishmentMissingError'; import { ConsultUserService } from './consultUserService'; import { CeremaUser } from '@zerologementvacant/models'; import config from '~/infra/config'; import { logger } from '~/infra/logger'; +import establishmentRepository from '~/repositories/establishmentRepository'; export class CeremaService implements ConsultUserService { @@ -52,20 +54,52 @@ export class CeremaService implements ConsultUserService { }, ); + // TODO vérifier les droits au niveau structure + const establishmentContent: any = await establishmentResponse.json(); if (establishmentResponse.status !== 200) { throw establishmentContent.detail; } - const u = { + const permissionResponse = await fetch( + `${config.cerema.api}/api/permissions?email=${email}`, + { + method: 'GET', + headers: { + Authorization: `Token ${token}`, + 'Content-Type': 'application/json', + }, + }, + ); + + const permissionContent: any = await permissionResponse.json(); + if (permissionResponse.status !== 200) { + throw permissionContent.detail; + } + + let siren; + try { + siren = establishmentContent.siret.substring(0, 9); + } catch { + throw new EstablishmentMissingError(establishmentContent.id_structure); + } + + const establishment = await establishmentRepository.findOne({siren: Number(siren)}); + if(establishment === null) { + throw new EstablishmentMissingError(siren); + } + const franceEntiere = establishment.geoCodes.length === 0; + const hasCommitment = franceEntiere ? permissionContent.lovac.fr_entiere : establishment.geoCodes.every(element => permissionContent.lovac.comm.includes(element)); + const cguValid = permissionContent.cgu_valide !== null; + + return { email: user.email, - establishmentSiren: parseInt(establishmentContent.siret.substring(0, 9)), + establishmentSiren: siren, hasAccount: true, - hasCommitment: establishmentContent.acces_lovac !== null, - cguValid: establishmentContent.cgu_valide !== null, - isValid: establishmentContent.acces_lovac !== null && establishmentContent.cgu_valide !== null, + hasCommitment, + cguValid, + isValid: cguValid && hasCommitment, }; - return u; })); return users; } diff --git a/server/src/tasks/ceremaAccountTask.ts b/server/src/tasks/ceremaAccountTask.ts index f03261c39..408dbbb1e 100644 --- a/server/src/tasks/ceremaAccountTask.ts +++ b/server/src/tasks/ceremaAccountTask.ts @@ -11,29 +11,30 @@ function delay(ms: number): Promise { async function run(): Promise { logger.info('Starting cerema account check...'); - const users = await userRepository.find(); + const users = await userRepository.find({ filters: { disabled: false}}); for (const user of users) { const ceremaUsers = await ceremaService.consultUsers(user.email); if(ceremaUsers.length === 0) { logger.warn(`No cerema user found for ${user.email}`); await userRepository.remove(user.id); - } - - if(ceremaUsers.length === 1) { + } else if(ceremaUsers.length === 1) { if(!(ceremaUsers[0].isValid)) { logger.warn(`No cerema valid account found for ${user.email}`); user.disabled = true; await userRepository.update(user); + } else { + logger.info(`Cerema user found for ${user.email}`); } - } - if(ceremaUsers.length > 1) { + } else if(ceremaUsers.length > 1) { logger.info(`Multiple cerema users found for ${user.email}`); - const ceremaUser = ceremaUsers.filter((user) => user.hasCommitment)[0]; + const ceremaUser = ceremaUsers.filter((user) => user.isValid)[0]; if(!ceremaUser) { logger.warn(`No cerema valid account found for ${user.email}`); user.disabled = true; await userRepository.update(user); + } else { + logger.info(`Cerema user found for ${user.email}`); } }