Skip to content

Commit

Permalink
feat(suggestion): améliore la remontée des suggestions en cas de prob…
Browse files Browse the repository at this point in the history
…lèmes
  • Loading branch information
Tensa-K authored and Mzem committed Jun 23, 2023
1 parent 0a4d33f commit 9ea1e58
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 39 deletions.
61 changes: 37 additions & 24 deletions src/application/commands/rafraichir-suggestions.command.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -66,41 +67,53 @@ 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) {
const metiersFavorisDiagorienteResult =
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()
}
Expand Down
6 changes: 1 addition & 5 deletions src/infrastructure/routes/recherches-jeunes.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export class RecherchesJeunesController {
): Promise<SuggestionQueryModel[]> {
const avecDiagoriente = findSuggestionsQueryParams.avecDiagoriente ?? false

const result = await this.rafraichirSuggestionsCommandHandler.execute(
await this.rafraichirSuggestionsCommandHandler.execute(
{
idJeune,
token: accessToken,
Expand All @@ -175,10 +175,6 @@ export class RecherchesJeunesController {
utilisateur
)

if (isFailure(result)) {
throw handleFailure(result)
}

return this.getSuggestionsQueryHandler.execute(
{ idJeune, avecDiagoriente },
utilisateur
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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())
})
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -420,7 +420,7 @@ describe('RecherchesController', () => {
.set('authorization', unHeaderAuthorization())

// Then
.expect(HttpStatus.INTERNAL_SERVER_ERROR)
.expect(HttpStatus.OK)
})
})
})
Expand Down

0 comments on commit 9ea1e58

Please sign in to comment.