From 809fb7d22b5200be81bafc3bf7afe2d026e386e7 Mon Sep 17 00:00:00 2001 From: Arthur L-Brjc <7021678+arthurlbrjc@users.noreply.github.com> Date: Thu, 13 Feb 2025 09:59:14 +0100 Subject: [PATCH] feat: handle conseiller inactif when autoinscription session --- src/building-blocks/types/domain-error.ts | 10 +++++++ .../authentification-sql.repository.db.ts | 26 +++++++++++++------ src/infrastructure/routes/result.handler.ts | 2 ++ ...authentification-sql.repository.db.test.ts | 25 +++++++++++++++++- 4 files changed, 54 insertions(+), 9 deletions(-) diff --git a/src/building-blocks/types/domain-error.ts b/src/building-blocks/types/domain-error.ts index b5f67b1a2..aa3e46782 100644 --- a/src/building-blocks/types/domain-error.ts +++ b/src/building-blocks/types/domain-error.ts @@ -202,6 +202,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 diff --git a/src/infrastructure/repositories/authentification-sql.repository.db.ts b/src/infrastructure/repositories/authentification-sql.repository.db.ts index cc72f8e0e..571b98de8 100644 --- a/src/infrastructure/repositories/authentification-sql.repository.db.ts +++ b/src/infrastructure/repositories/authentification-sql.repository.db.ts @@ -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' @@ -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 + } } } diff --git a/src/infrastructure/routes/result.handler.ts b/src/infrastructure/routes/result.handler.ts index 346235c41..582dc6128 100644 --- a/src/infrastructure/routes/result.handler.ts +++ b/src/infrastructure/routes/result.handler.ts @@ -12,6 +12,7 @@ import { CampagneExisteDejaError, CampagneNonActive, CompteDiagorienteInvalideError, + ConseillerInactifError, ConseillerMiloSansStructure, ConseillerNonValide, ConseillerSansAgenceError, @@ -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) diff --git a/test/infrastructure/repositories/authentification-sql.repository.db.test.ts b/test/infrastructure/repositories/authentification-sql.repository.db.test.ts index dc260e199..af2a87c96 100644 --- a/test/infrastructure/repositories/authentification-sql.repository.db.test.ts +++ b/test/infrastructure/repositories/authentification-sql.repository.db.test.ts @@ -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, @@ -550,5 +554,24 @@ describe('AuthentificationSqlRepository', () => { (resultAccesPartenaireConseiller as Failure).error ).to.be.an.instanceOf(NonTrouveError) }) + + it('échoue si le conseiller est inactif depuis trop longtemps', async () => { + // Given + 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) + }) }) })