Skip to content

Commit

Permalink
feat(structure-milo): isoler la recuperation de la structure du conse…
Browse files Browse the repository at this point in the history
…iller
  • Loading branch information
Mzem committed Jun 22, 2023
1 parent 69971f2 commit cd08b36
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
)

Expand Down
11 changes: 11 additions & 0 deletions src/application/queries/query-models/conseillers.query-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ class AgenceDuConseillerQueryModel {
nom: string
}

class StructureMiloDuConseillerQueryModel {
@ApiProperty({ required: false })
id?: string

@ApiProperty()
nom: string
}

export class DetailConseillerQueryModel {
@ApiProperty()
id: string
Expand All @@ -25,6 +33,9 @@ export class DetailConseillerQueryModel {
@ApiProperty({ required: false })
agence?: AgenceDuConseillerQueryModel

@ApiProperty({ required: false })
structureMilo?: StructureMiloDuConseillerQueryModel

@ApiProperty()
notificationsSonores: boolean

Expand Down
93 changes: 56 additions & 37 deletions src/domain/milo/conseiller.milo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Inject, Injectable } from '@nestjs/common'
import { Inject, Injectable, Logger } from '@nestjs/common'
import {
Result,
isFailure,
Expand All @@ -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'

Expand Down Expand Up @@ -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<void> {
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)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
)
)
Expand Down

0 comments on commit cd08b36

Please sign in to comment.