From 9ea1e58c4b329c2d28633cc4fdf6eb6ac3689320 Mon Sep 17 00:00:00 2001 From: "kevin.b.le" Date: Thu, 22 Jun 2023 15:36:47 +0200 Subject: [PATCH] =?UTF-8?q?feat(suggestion):=20am=C3=A9liore=20la=20remont?= =?UTF-8?q?=C3=A9e=20des=20suggestions=20en=20cas=20de=20probl=C3=A8mes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rafraichir-suggestions.command.handler.ts | 61 +++++++++++-------- .../routes/recherches-jeunes.controller.ts | 6 +- ...aichir-suggestions.command.handler.test.ts | 21 ++++--- .../recherches-jeunes.controller.test.ts | 4 +- 4 files changed, 53 insertions(+), 39 deletions(-) diff --git a/src/application/commands/rafraichir-suggestions.command.handler.ts b/src/application/commands/rafraichir-suggestions.command.handler.ts index b700f57f3..b87c677ab 100644 --- a/src/application/commands/rafraichir-suggestions.command.handler.ts +++ b/src/application/commands/rafraichir-suggestions.command.handler.ts @@ -19,6 +19,7 @@ import { Core, estPoleEmploiBRSA } from '../../domain/core' import { NonTrouveError } from '../../building-blocks/types/domain-error' import { Jeune, JeunesRepositoryToken } from '../../domain/jeune/jeune' import { DiagorienteClient } from 'src/infrastructure/clients/diagoriente-client' +import { buildError } from '../../utils/logger.module' export interface RafraichirSuggestionsCommand extends Command { idJeune: string @@ -66,15 +67,20 @@ export class RafraichirSuggestionsCommandHandler extends CommandHandler< await this.suggestionPoleEmploiRepository.findAll(idpToken) if (isFailure(suggestionsPEResult)) { - return suggestionsPEResult - } - - suggestionsPE = - this.suggestionFactory.buildListeSuggestionsOffresFromPoleEmploi( - suggestionsPEResult.data, - command.idJeune, - command.structure + this.logger.error( + buildError( + `Impossible de récupérer les suggestions depuis PE`, + Error(suggestionsPEResult.error.message) + ) ) + } else { + suggestionsPE = + this.suggestionFactory.buildListeSuggestionsOffresFromPoleEmploi( + suggestionsPEResult.data, + command.idJeune, + command.structure + ) + } } if (rafraichirSuggestionsDiagoriente) { @@ -82,25 +88,32 @@ export class RafraichirSuggestionsCommandHandler extends CommandHandler< await this.diagorienteClient.getMetiersFavoris(jeune.id) if (isFailure(metiersFavorisDiagorienteResult)) { - return metiersFavorisDiagorienteResult - } - - const metiersFavorisDiagoriente = - metiersFavorisDiagorienteResult.data.data.userByPartner.favorites.filter( - favori => favori.favorited - ) - - suggestionsDiagoriente = - this.suggestionFactory.buildListeSuggestionsOffresFromDiagoriente( - metiersFavorisDiagoriente, - command.idJeune + this.logger.error( + buildError( + 'Impossible de récupérer les métiers favoris depuis Diagoriente', + Error(metiersFavorisDiagorienteResult.error.message) + ) ) + } else { + const metiersFavorisDiagoriente = + metiersFavorisDiagorienteResult.data.data.userByPartner.favorites.filter( + favori => favori.favorited + ) + suggestionsDiagoriente = + this.suggestionFactory.buildListeSuggestionsOffresFromDiagoriente( + metiersFavorisDiagoriente, + command.idJeune + ) + } } - await this.suggestionPoleEmploiService.rafraichir( - [...suggestionsPE, ...suggestionsDiagoriente], - command.idJeune - ) + const suggestionsARafraichir = [...suggestionsPE, ...suggestionsDiagoriente] + if (suggestionsARafraichir.length > 0) { + await this.suggestionPoleEmploiService.rafraichir( + suggestionsARafraichir, + command.idJeune + ) + } return emptySuccess() } diff --git a/src/infrastructure/routes/recherches-jeunes.controller.ts b/src/infrastructure/routes/recherches-jeunes.controller.ts index 71064f029..8b1af59f8 100644 --- a/src/infrastructure/routes/recherches-jeunes.controller.ts +++ b/src/infrastructure/routes/recherches-jeunes.controller.ts @@ -165,7 +165,7 @@ export class RecherchesJeunesController { ): Promise { const avecDiagoriente = findSuggestionsQueryParams.avecDiagoriente ?? false - const result = await this.rafraichirSuggestionsCommandHandler.execute( + await this.rafraichirSuggestionsCommandHandler.execute( { idJeune, token: accessToken, @@ -175,10 +175,6 @@ export class RecherchesJeunesController { utilisateur ) - if (isFailure(result)) { - throw handleFailure(result) - } - return this.getSuggestionsQueryHandler.execute( { idJeune, avecDiagoriente }, utilisateur diff --git a/test/application/commands/rafraichir-suggestions.command.handler.test.ts b/test/application/commands/rafraichir-suggestions.command.handler.test.ts index dd8a6043b..06deaec13 100644 --- a/test/application/commands/rafraichir-suggestions.command.handler.test.ts +++ b/test/application/commands/rafraichir-suggestions.command.handler.test.ts @@ -9,7 +9,11 @@ import { uneSuggestion, uneSuggestionPE } from '../../fixtures/suggestion.fixture' -import { failure, success } from '../../../src/building-blocks/types/result' +import { + emptySuccess, + failure, + success +} from '../../../src/building-blocks/types/result' import { ErreurHttp } from '../../../src/building-blocks/types/domain-error' import { SuggestionPoleEmploiService } from '../../../src/domain/offre/recherche/suggestion/pole-emploi.service' import { Core } from '../../../src/domain/core' @@ -83,7 +87,7 @@ describe('RafraichirSuggestionPoleEmploiCommandHandler', () => { }) describe("quand l'utilisateur a une structure MILO", () => { - it('test', async () => { + it('recupere suggestions PE et Diagoriente', async () => { const suggestionDiagoriente: Diagoriente = { tag: { code: 'B1301', @@ -246,25 +250,26 @@ describe('RafraichirSuggestionPoleEmploiCommandHandler', () => { }) }) - describe('quand Pole Emploi est down', () => { - it("retourne l'erreur", async () => { + describe('quand Pole Emploi et Diago sont down', () => { + it("ne retourne pas l'erreur", async () => { // Given suggestionPoleEmploiRepository.findAll.resolves( failure(new ErreurHttp('Service down', 500)) ) + diagorienteClient.getMetiersFavoris.resolves( + failure(new ErreurHttp('Service down', 500)) + ) // When const result = await handler.handle({ idJeune: 'idJeune', token: 'token', structure: Core.Structure.POLE_EMPLOI, - avecDiagoriente: false + avecDiagoriente: true }) // Then - expect(result).to.deep.equal( - failure(new ErreurHttp('Service down', 500)) - ) + expect(result).to.deep.equal(emptySuccess()) }) }) }) diff --git a/test/infrastructure/routes/recherches-jeunes.controller.test.ts b/test/infrastructure/routes/recherches-jeunes.controller.test.ts index f2040fd59..beede1e5b 100644 --- a/test/infrastructure/routes/recherches-jeunes.controller.test.ts +++ b/test/infrastructure/routes/recherches-jeunes.controller.test.ts @@ -409,7 +409,7 @@ describe('RecherchesController', () => { }) }) describe('quand PE est down', () => { - it('retourne une erreur', async () => { + it("ne retourne pas d'erreur", async () => { // Given rafraichirSuggestionsCommandHandler.execute.resolves( failure(new ErreurHttp('Erreur', 500)) @@ -420,7 +420,7 @@ describe('RecherchesController', () => { .set('authorization', unHeaderAuthorization()) // Then - .expect(HttpStatus.INTERNAL_SERVER_ERROR) + .expect(HttpStatus.OK) }) }) })