diff --git a/src/application/queries/get-detail-conseiller.query.handler.db.ts b/src/application/queries/get-detail-conseiller.query.handler.db.ts index 45d594afc..c668aef63 100644 --- a/src/application/queries/get-detail-conseiller.query.handler.db.ts +++ b/src/application/queries/get-detail-conseiller.query.handler.db.ts @@ -14,6 +14,7 @@ import { StructureMiloSqlModel } from '../../infrastructure/sequelize/models/str import { ConseillerAuthorizer } from '../authorizers/conseiller-authorizer' import { DetailConseillerQueryModel } from './query-models/conseillers.query-model' import { ConfigService } from '@nestjs/config' +import { Includeable } from 'sequelize' export interface GetDetailConseillerQuery extends Query { idConseiller: string @@ -41,17 +42,20 @@ export class GetDetailConseillerQueryHandler extends QueryHandler< 'features.recupererStructureMilo' ) + const include: Includeable[] = [AgenceSqlModel] + if (estMilo(query.structure) && FT_RECUPERER_STRUCTURE_MILO) { await this.conseillerMiloService.recupererEtMettreAJourStructure( query.idConseiller, query.token ) + include.push(StructureMiloSqlModel) } const conseillerSqlModel = await ConseillerSqlModel.findByPk( query.idConseiller, { - include: [AgenceSqlModel, StructureMiloSqlModel] + include } ) diff --git a/src/application/queries/query-models/conseillers.query-model.ts b/src/application/queries/query-models/conseillers.query-model.ts index 23320e3e3..02ac406fd 100644 --- a/src/application/queries/query-models/conseillers.query-model.ts +++ b/src/application/queries/query-models/conseillers.query-model.ts @@ -9,6 +9,14 @@ class AgenceDuConseillerQueryModel { nom: string } +class StructureMiloDuConseillerQueryModel { + @ApiProperty({ required: false }) + id?: string + + @ApiProperty() + nom: string +} + export class DetailConseillerQueryModel { @ApiProperty() id: string @@ -25,6 +33,9 @@ export class DetailConseillerQueryModel { @ApiProperty({ required: false }) agence?: AgenceDuConseillerQueryModel + @ApiProperty({ required: false }) + structureMilo?: StructureMiloDuConseillerQueryModel + @ApiProperty() notificationsSonores: boolean diff --git a/src/domain/milo/conseiller.milo.ts b/src/domain/milo/conseiller.milo.ts index 1a9ba548b..f403837a9 100644 --- a/src/domain/milo/conseiller.milo.ts +++ b/src/domain/milo/conseiller.milo.ts @@ -1,4 +1,4 @@ -import { Inject, Injectable } from '@nestjs/common' +import { Inject, Injectable, Logger } from '@nestjs/common' import { Result, isFailure, @@ -9,6 +9,9 @@ import { MiloClient } from '../../infrastructure/clients/milo-client' import { StructureMiloSqlModel } from '../../infrastructure/sequelize/models/structure-milo.sql-model' import { Conseiller } from '../conseiller/conseiller' import { NonTrouveError } from '../../building-blocks/types/domain-error' +import { buildError } from '../../utils/logger.module' +import { getAPMInstance } from '../../infrastructure/monitoring/apm.init' +import * as APM from 'elastic-apm-node' export const ConseillerMiloRepositoryToken = 'ConseillerMilo.Repository' @@ -37,60 +40,76 @@ export namespace ConseillerMilo { } @Injectable() export class Service { + private logger: Logger + private apmService: APM.Agent + constructor( @Inject(ConseillerMiloRepositoryToken) private conseillerMiloRepository: Conseiller.Milo.Repository, private conseillerMiloFactory: Factory, private miloClient: MiloClient, private keycloakClient: KeycloakClient - ) {} + ) { + this.logger = new Logger('ConseillerMiloService') + this.apmService = getAPMInstance() + } async recupererEtMettreAJourStructure( idConseiller: string, token: string ): Promise { - const resultConseiller = await this.conseillerMiloRepository.get( - idConseiller - ) - - // Conseiller introuvable - if ( - isFailure(resultConseiller) && - resultConseiller.error.code === NonTrouveError.CODE - ) { - return - } + try { + const resultConseiller = await this.conseillerMiloRepository.get( + idConseiller + ) - const idpToken = await this.keycloakClient.exchangeTokenConseillerMilo( - token - ) - const structure = await this.miloClient.getStructureConseiller(idpToken) + // Conseiller introuvable + if ( + isFailure(resultConseiller) && + resultConseiller.error.code === NonTrouveError.CODE + ) { + return + } - if (isFailure(structure)) { - return - } + const idpToken = await this.keycloakClient.exchangeTokenConseillerMilo( + token + ) + const structure = await this.miloClient.getStructureConseiller(idpToken) - // Conseiller trouvé mais structure Milo non modifiée - if (isSuccess(resultConseiller)) { - const structureConseillerNonModifiee = - resultConseiller.data.idStructure === structure.data.id.toString() - if (structureConseillerNonModifiee) { + if (isFailure(structure)) { return } - } - const conseillerMiloAvecStructure = - this.conseillerMiloFactory.mettreAJourStructure( - idConseiller, - structure.data.id.toString() - ) + // Conseiller trouvé mais structure Milo non modifiée + if (isSuccess(resultConseiller)) { + const structureConseillerNonModifiee = + resultConseiller.data.idStructure === structure.data.id.toString() + if (structureConseillerNonModifiee) { + return + } + } - await StructureMiloSqlModel.upsert({ - id: structure.data.id, - nomOfficiel: structure.data.nomOfficiel, - nomUsuel: structure.data.nomUsuel - }) - await this.conseillerMiloRepository.update(conseillerMiloAvecStructure) + const conseillerMiloAvecStructure = + this.conseillerMiloFactory.mettreAJourStructure( + idConseiller, + structure.data.id.toString() + ) + + await StructureMiloSqlModel.upsert({ + id: structure.data.id, + nomOfficiel: structure.data.nomOfficiel, + nomUsuel: structure.data.nomUsuel + }) + await this.conseillerMiloRepository.update(conseillerMiloAvecStructure) + } catch (e) { + this.logger.error( + buildError( + `La récupération de la structure Milo du conseiller ${idConseiller} a échoué`, + e + ) + ) + this.apmService.captureError(e) + } } } } diff --git a/src/infrastructure/repositories/mappers/conseillers.mappers.ts b/src/infrastructure/repositories/mappers/conseillers.mappers.ts index 5577a12bb..8ba61fa48 100644 --- a/src/infrastructure/repositories/mappers/conseillers.mappers.ts +++ b/src/infrastructure/repositories/mappers/conseillers.mappers.ts @@ -27,7 +27,7 @@ export function fromSqlToDetailConseillerQueryModel( nom: conseillerSqlModel.nomManuelAgence } } else if (conseillerSqlModel.structureMilo) { - conseiller.agence = { + conseiller.structureMilo = { id: conseillerSqlModel.structureMilo.id, nom: conseillerSqlModel.structureMilo.nomOfficiel } diff --git a/test/application/queries/get-detail-conseiller.query.handler.db.test.ts b/test/application/queries/get-detail-conseiller.query.handler.db.test.ts index 677eee149..472119dbe 100644 --- a/test/application/queries/get-detail-conseiller.query.handler.db.test.ts +++ b/test/application/queries/get-detail-conseiller.query.handler.db.test.ts @@ -165,11 +165,12 @@ describe('GetDetailConseillerQueryHandler', () => { firstName: 'toto', lastName: 'tata', email: 'nils.tavernier@passemploi.com', - agence: { + structureMilo: { id: structureMiloSql.id, nom: structureMiloSql.nomOfficiel }, - notificationsSonores: false + notificationsSonores: false, + agence: undefined }) ) )