Skip to content

Commit

Permalink
feat: update portailDF API call
Browse files Browse the repository at this point in the history
  • Loading branch information
loicguillois committed Jan 9, 2025
1 parent 90a8f09 commit a7d081a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 13 deletions.
1 change: 1 addition & 0 deletions server/src/models/UserFiltersApi.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export interface UserFiltersApi {
establishmentIds?: string[];
disabled?: boolean;
}
3 changes: 3 additions & 0 deletions server/src/repositories/userRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
}

Expand Down
46 changes: 40 additions & 6 deletions server/src/services/ceremaService/ceremaService.ts
Original file line number Diff line number Diff line change
@@ -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 {

Expand Down Expand Up @@ -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;
}
Expand Down
15 changes: 8 additions & 7 deletions server/src/tasks/ceremaAccountTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,30 @@ function delay(ms: number): Promise<void> {

async function run(): Promise<void> {
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}`);
}
}

Expand Down

0 comments on commit a7d081a

Please sign in to comment.