Skip to content

Commit

Permalink
feat: handle conseiller inactif when autoinscription session
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurlbrjc committed Feb 13, 2025
1 parent 0480270 commit 4833aa4
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 9 deletions.
10 changes: 10 additions & 0 deletions src/building-blocks/types/domain-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,16 @@ export class ConseillerNonValide implements DomainError {
}
}

export class ConseillerInactifError implements DomainError {
static CODE = 'CONSEILLER_INACTIF'
readonly code: string = ConseillerInactifError.CODE
readonly message: string

constructor() {
this.message = 'Le conseiller est inactif depuis trop longtemps'
}
}

export class DroitsInsuffisants implements DomainError {
static CODE = 'DROITS_INSUFFISANTS'
readonly code: string = DroitsInsuffisants.CODE
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Injectable, Logger } from '@nestjs/common'
import { Injectable, Logger, UnauthorizedException } from '@nestjs/common'
import { Sequelize } from 'sequelize-typescript'
import { NonTrouveError } from 'src/building-blocks/types/domain-error'
import {
ConseillerInactifError,
NonTrouveError
} from 'src/building-blocks/types/domain-error'
import { failure, Result, success } from 'src/building-blocks/types/result'
import { Authentification } from '../../domain/authentification'
import { Core, estMilo, getStructureDeReference } from '../../domain/core'
Expand Down Expand Up @@ -218,13 +221,20 @@ export class AuthentificationSqlOidcRepository
if (!conseillerSqlModel)
return failure(new NonTrouveError('Conseiller', idConseiller))

const accesConseiller = await this.oidcClient.exchangeToken(
bearer,
structure,
conseillerSqlModel.idAuthentification
)
try {
const accesConseiller = await this.oidcClient.exchangeToken(
bearer,
structure,
conseillerSqlModel.idAuthentification
)

return success(accesConseiller)
return success(accesConseiller)
} catch (e) {
if (e instanceof UnauthorizedException) {
return failure(new ConseillerInactifError())
}
throw e
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/infrastructure/routes/result.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
CampagneExisteDejaError,
CampagneNonActive,
CompteDiagorienteInvalideError,
ConseillerInactifError,
ConseillerMiloSansStructure,
ConseillerNonValide,
ConseillerSansAgenceError,
Expand Down Expand Up @@ -56,6 +57,7 @@ function handleFailure(result: Failure): never {
}
throw new RuntimeException(result.error.message)
case NonTraitableError.CODE:
case ConseillerInactifError.CODE:
throw new UnprocessableEntityException(result.error)
case NonTrouveError.CODE:
throw new NotFoundException(result.error.message)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { NonTrouveError } from 'src/building-blocks/types/domain-error'
import { UnauthorizedException } from '@nestjs/common'
import {
ConseillerInactifError,
NonTrouveError
} from 'src/building-blocks/types/domain-error'
import {
Failure,
isFailure,
Expand Down Expand Up @@ -550,5 +554,30 @@ describe('AuthentificationSqlRepository', () => {
(resultAccesPartenaireConseiller as Failure).error
).to.be.an.instanceOf(NonTrouveError)
})

it('échoue si le conseiller est inactif depuis trop longtemps', async () => {
// Given
await ConseillerSqlModel.creer(
unConseillerDto({
id: 'id-conseiller',
idAuthentification: 'id-authentification-conseiller'
})
)
oidcClient.exchangeToken.throws(new UnauthorizedException())

// When
const resultAccesPartenaireConseiller =
await repository.seFairePasserPourUnConseiller(
'id-conseiller',
'bearer',
Core.Structure.MILO
)

// Then
expect(isFailure(resultAccesPartenaireConseiller)).to.equal(true)
expect(
(resultAccesPartenaireConseiller as Failure).error
).to.be.an.instanceOf(ConseillerInactifError)
})
})
})

0 comments on commit 4833aa4

Please sign in to comment.