From 43bb01688ca8f9dc4a83f36624ca04df3393264e Mon Sep 17 00:00:00 2001 From: Suxue LI Date: Tue, 24 Oct 2023 18:45:09 +0200 Subject: [PATCH] feat(faq): ne pas passer de mapper au strapi service --- src/server/faq/domain/FAQ.fixture.ts | 4 +- src/server/faq/infra/strapiFAQ.fixture.ts | 7 ++ src/server/faq/infra/strapiFAQ.mapper.test.ts | 17 +-- src/server/faq/infra/strapiFAQ.mapper.ts | 3 - .../faq/infra/strapiFAQ.repository.test.ts | 107 +++++++++++++----- src/server/faq/infra/strapiFAQ.repository.ts | 31 +++-- 6 files changed, 116 insertions(+), 53 deletions(-) diff --git a/src/server/faq/domain/FAQ.fixture.ts b/src/server/faq/domain/FAQ.fixture.ts index 83d5e28a2f..49e337735d 100644 --- a/src/server/faq/domain/FAQ.fixture.ts +++ b/src/server/faq/domain/FAQ.fixture.ts @@ -10,7 +10,7 @@ export const aListeDeQuestion = (): Array => { export const aQuestion = (override?: Partial): FAQ.Question => { return { problématique: 'Comment constituer un dossier locatif ?', - slug: 'question-slug', + slug: 'Comment-constituer-un-dossier-locatif ?', ...override, }; }; @@ -19,7 +19,7 @@ export const aQuestionEtReponse = (override?: Partial): F return { contenu: 'mon contenu explicatif', problématique: 'Comment constituer un dossier locatif ?', - slug: 'question-slug', + slug: 'Comment-constituer-un-dossier-locatif ?', ...override, }; }; diff --git a/src/server/faq/infra/strapiFAQ.fixture.ts b/src/server/faq/infra/strapiFAQ.fixture.ts index 1dd5344060..9cb031194f 100644 --- a/src/server/faq/infra/strapiFAQ.fixture.ts +++ b/src/server/faq/infra/strapiFAQ.fixture.ts @@ -15,3 +15,10 @@ export const aStrapiQuestion = (override?: Partial): ...override, }; }; + +export const aStrapiQuestionSlug = (override?: Partial): FAQResponseStrapi.QuestionSlug => { + return { + slug: 'Comment-constituer-un-dossier-locatif ?', + ...override, + }; +}; diff --git a/src/server/faq/infra/strapiFAQ.mapper.test.ts b/src/server/faq/infra/strapiFAQ.mapper.test.ts index cba635d4d4..bccfb2a559 100644 --- a/src/server/faq/infra/strapiFAQ.mapper.test.ts +++ b/src/server/faq/infra/strapiFAQ.mapper.test.ts @@ -1,9 +1,6 @@ -import { - aQuestion, - aQuestionEtReponse, -} from '~/server/faq/domain/FAQ.fixture'; +import { aQuestion, aQuestionEtReponse } from '~/server/faq/domain/FAQ.fixture'; import { aStrapiQuestion, aStrapiQuestionEtReponse } from '~/server/faq/infra/strapiFAQ.fixture'; -import { flatMapSlug, mapQuestion, mapQuestionRéponse } from '~/server/faq/infra/strapiFAQ.mapper'; +import { mapQuestion, mapQuestionRéponse } from '~/server/faq/infra/strapiFAQ.mapper'; describe('mapQuestion', () => { it('retourne le résultat formaté', () => { @@ -34,13 +31,3 @@ describe('mapQuestionRéponse', () => { })); }); }); - -describe('flatMapSlug', () => { - it('renvoie le slug', () => { - const result= flatMapSlug({ - slug: 'Comment-constituer-un-dossier-locatif-?', - }); - - expect(result).toEqual('Comment-constituer-un-dossier-locatif-?'); - }); -}); diff --git a/src/server/faq/infra/strapiFAQ.mapper.ts b/src/server/faq/infra/strapiFAQ.mapper.ts index b741712df3..a0f8797635 100644 --- a/src/server/faq/infra/strapiFAQ.mapper.ts +++ b/src/server/faq/infra/strapiFAQ.mapper.ts @@ -15,6 +15,3 @@ export const mapQuestion = (faq: FAQResponseStrapi.Question): FAQ.Question => { slug: faq.slug, }; }; - -export const flatMapSlug = (faq: FAQResponseStrapi.QuestionSlug): FAQ.Slug => faq.slug; - diff --git a/src/server/faq/infra/strapiFAQ.repository.test.ts b/src/server/faq/infra/strapiFAQ.repository.test.ts index da52287a30..a34a4a85dd 100644 --- a/src/server/faq/infra/strapiFAQ.repository.test.ts +++ b/src/server/faq/infra/strapiFAQ.repository.test.ts @@ -1,82 +1,137 @@ import { aStrapiCmsRepository } from '~/server/cms/infra/repositories/strapi.repository.fixture'; -import { createSuccess } from '~/server/errors/either'; -import { aListeDeQuestion, aListeFAQSlug, aQuestionEtReponse } from '~/server/faq/domain/FAQ.fixture'; -import { flatMapSlug, mapQuestion, mapQuestionRéponse } from '~/server/faq/infra/strapiFAQ.mapper'; +import { createFailure, createSuccess } from '~/server/errors/either'; +import { ErreurMetier } from '~/server/errors/erreurMetier.types'; +import { aQuestion, aQuestionEtReponse } from '~/server/faq/domain/FAQ.fixture'; +import { aStrapiQuestion, aStrapiQuestionEtReponse, aStrapiQuestionSlug } from '~/server/faq/infra/strapiFAQ.fixture'; import { StrapiFAQRepository } from '~/server/faq/infra/strapiFAQ.repository'; const RESOURCE_FAQ = 'faqs'; describe('getAllFAQ', () => { it('appelle le service strapi avec les bons paramètres', async () => { - const strapiCmsRepository = aStrapiCmsRepository(); - const strapiFAQRepository = new StrapiFAQRepository(strapiCmsRepository); + const strapiService = aStrapiCmsRepository(); + jest.spyOn(strapiService, 'getCollectionType').mockResolvedValue(createSuccess([aStrapiQuestion()])); + const strapiFAQRepository = new StrapiFAQRepository(strapiService); const query= 'fields[0]=problematique&fields[1]=slug'; await strapiFAQRepository.getAllFAQ(); - expect(strapiCmsRepository.getCollectionType).toHaveBeenCalledWith(RESOURCE_FAQ, query, mapQuestion); + expect(strapiService.getCollectionType).toHaveBeenCalledWith(RESOURCE_FAQ, query); }); describe('quand la liste des questions est trouvée', () => { it('renvoie la liste de questions', async () => { - const expected = aListeDeQuestion(); - const strapiCmsRepository = aStrapiCmsRepository({ getCollectionType: jest.fn() }); - const strapiFAQRepository = new StrapiFAQRepository(strapiCmsRepository); - jest.spyOn(strapiCmsRepository, 'getCollectionType').mockResolvedValue(createSuccess(expected)); + const strapiService = aStrapiCmsRepository(); + const strapiFAQRepository = new StrapiFAQRepository(strapiService); + const strapiQuestions = [ + aStrapiQuestion(), + aStrapiQuestion({ problematique: 'Que faire dans la vie ?', slug: 'que-faire-dans-la-vie' }), + ]; + jest.spyOn(strapiService, 'getCollectionType').mockResolvedValue(createSuccess(strapiQuestions)); + const expectedQuestions = createSuccess([ + aQuestion(), + aQuestion({ + problématique: 'Que faire dans la vie ?', + slug: 'que-faire-dans-la-vie', + })]); + + const result = await strapiFAQRepository.getAllFAQ(); + + expect(result).toStrictEqual(expectedQuestions); + }); + }); + + describe('quand la récupération de la liste des questions est en échec', () => { + it('relais l’échec du strapi service', async () => { + const strapiService = aStrapiCmsRepository(); + const strapiFAQRepository = new StrapiFAQRepository(strapiService); + const expectedStrapiFailure = createFailure(ErreurMetier.CONTENU_INDISPONIBLE); + jest.spyOn(strapiService, 'getCollectionType').mockResolvedValue(expectedStrapiFailure); const result = await strapiFAQRepository.getAllFAQ(); - expect(result).toEqual(createSuccess(expected)); + expect(result).toStrictEqual(expectedStrapiFailure); }); }); }); describe('getFAQBySlug', () => { it('appelle le service strapi avec les bons paramètres', async () => { - const strapiCmsRepository = aStrapiCmsRepository(); - const strapiFAQRepository = new StrapiFAQRepository(strapiCmsRepository); + const strapiService = aStrapiCmsRepository(); + jest.spyOn(strapiService, 'getFirstFromCollectionType').mockResolvedValueOnce(createSuccess(aStrapiQuestionEtReponse())); + const strapiFAQRepository = new StrapiFAQRepository(strapiService); const slug = 'slugName'; const query= 'filters[slug][$eq]=slugName'; await strapiFAQRepository.getFAQBySlug(slug); - expect(strapiCmsRepository.getFirstFromCollectionType).toHaveBeenCalledWith(RESOURCE_FAQ, query, mapQuestionRéponse); + expect(strapiService.getFirstFromCollectionType).toHaveBeenCalledWith(RESOURCE_FAQ, query); }); describe('quand la question est trouvée', () => { it('renvoie la question', async () => { - const expected = aQuestionEtReponse(); - const strapiCmsRepository = aStrapiCmsRepository({ getFirstFromCollectionType: jest.fn() }); - const strapiFAQRepository = new StrapiFAQRepository(strapiCmsRepository); - jest.spyOn(strapiCmsRepository, 'getFirstFromCollectionType').mockResolvedValue(createSuccess(expected)); + const strapiService = aStrapiCmsRepository(); + jest.spyOn(strapiService, 'getFirstFromCollectionType').mockResolvedValueOnce(createSuccess(aStrapiQuestionEtReponse())); + const strapiFAQRepository = new StrapiFAQRepository(strapiService); + const expectedQuestionEtReponse = aQuestionEtReponse(); + + const result = await strapiFAQRepository.getFAQBySlug('slugName'); + + expect(result).toStrictEqual(createSuccess(expectedQuestionEtReponse)); + }); + }); + + describe('quand la récupération est en échec', () => { + it('relais l’échec du strapi service', async () => { + const strapiService = aStrapiCmsRepository(); + const expectedFailure = createFailure(ErreurMetier.CONTENU_INDISPONIBLE); + jest.spyOn(strapiService, 'getFirstFromCollectionType').mockResolvedValueOnce(expectedFailure); + const strapiFAQRepository = new StrapiFAQRepository(strapiService); const result = await strapiFAQRepository.getFAQBySlug('slugName'); - expect(result).toEqual(createSuccess(expected)); + expect(result).toStrictEqual(expectedFailure); }); }); }); describe('listAllFAQSlug', () => { it('appelle le service strapi avec les bons paramètres', async () => { - const strapiCmsRepository = aStrapiCmsRepository(); - const strapiFAQRepository = new StrapiFAQRepository(strapiCmsRepository); + const strapiService = aStrapiCmsRepository(); + jest.spyOn(strapiService, 'getCollectionType').mockResolvedValueOnce(createSuccess([aStrapiQuestionSlug()])); + const strapiFAQRepository = new StrapiFAQRepository(strapiService); const query= '[fields][0]=slug'; await strapiFAQRepository.listAllFAQSlug(); - expect(strapiCmsRepository.getCollectionType).toHaveBeenCalledWith(RESOURCE_FAQ, query, flatMapSlug); + expect(strapiService.getCollectionType).toHaveBeenCalledWith(RESOURCE_FAQ, query); }); describe('quand les slugs sont trouvés', () => { it('renvoie les slugs', async () => { - const expected = aListeFAQSlug(); - const strapiCmsRepository = aStrapiCmsRepository({ getCollectionType: jest.fn() }); + const strapiCmsRepository = aStrapiCmsRepository(); const strapiFAQRepository = new StrapiFAQRepository(strapiCmsRepository); - jest.spyOn(strapiCmsRepository, 'getCollectionType').mockResolvedValue(createSuccess(expected)); + jest.spyOn(strapiCmsRepository, 'getCollectionType').mockResolvedValue(createSuccess([ + aStrapiQuestionSlug(), + aStrapiQuestionSlug({ slug: 'comment-constituer-un-dossier-locatif-jeune' }), + ])); + const expectedSlugs = ['Comment-constituer-un-dossier-locatif ?', 'comment-constituer-un-dossier-locatif-jeune']; + + const result = await strapiFAQRepository.listAllFAQSlug(); + + expect(result).toStrictEqual(createSuccess(expectedSlugs)); + }); + }); + + describe('quand la récupération des slugs est en échec', () => { + it('relais l’échec du strapi service', async () => { + const strapiService = aStrapiCmsRepository(); + const strapiFAQRepository = new StrapiFAQRepository(strapiService); + const expectFailure = createFailure(ErreurMetier.CONTENU_INDISPONIBLE); + jest.spyOn(strapiService, 'getCollectionType').mockResolvedValue( expectFailure); const result = await strapiFAQRepository.listAllFAQSlug(); - expect(result).toEqual(createSuccess(expected)); + expect(result).toStrictEqual(expectFailure); }); }); }); diff --git a/src/server/faq/infra/strapiFAQ.repository.ts b/src/server/faq/infra/strapiFAQ.repository.ts index 479794e93a..0fbc66ba71 100644 --- a/src/server/faq/infra/strapiFAQ.repository.ts +++ b/src/server/faq/infra/strapiFAQ.repository.ts @@ -1,29 +1,46 @@ -import { StrapiRepository } from '~/server/cms/infra/repositories/strapi.repository'; -import { Either } from '~/server/errors/either'; +import { CmsRepository } from '~/server/cms/domain/cms.repository'; +import { createSuccess, Either, isSuccess } from '~/server/errors/either'; import { FAQ } from '~/server/faq/domain/FAQ'; import { FAQRepository } from '~/server/faq/domain/FAQ.repository'; import { FAQResponseStrapi } from '~/server/faq/infra/strapiFAQ'; -import { flatMapSlug, mapQuestion, mapQuestionRéponse } from '~/server/faq/infra/strapiFAQ.mapper'; +import { mapQuestion, mapQuestionRéponse } from '~/server/faq/infra/strapiFAQ.mapper'; const RESOURCE_FAQ = 'faqs'; export class StrapiFAQRepository implements FAQRepository { - constructor(private readonly strapiService: StrapiRepository) { + constructor(private readonly strapiService: CmsRepository) { } async getFAQBySlug(slug: string): Promise> { const query = `filters[slug][$eq]=${slug}`; - return await this.strapiService.getFirstFromCollectionType(RESOURCE_FAQ, query, mapQuestionRéponse); + const strapiQuestionEtReponse = await this.strapiService.getFirstFromCollectionType(RESOURCE_FAQ, query); + + if(isSuccess(strapiQuestionEtReponse)) + return createSuccess(mapQuestionRéponse(strapiQuestionEtReponse.result)); + + return strapiQuestionEtReponse; } async getAllFAQ(): Promise>> { const query = 'fields[0]=problematique&fields[1]=slug'; - return await this.strapiService.getCollectionType(RESOURCE_FAQ, query, mapQuestion); + const strapiQuestions = await this.strapiService.getCollectionType(RESOURCE_FAQ, query); + + if (isSuccess(strapiQuestions)) + return createSuccess(strapiQuestions.result.map((strapiQuestion) => mapQuestion(strapiQuestion))); + + return strapiQuestions; } async listAllFAQSlug(): Promise>> { const query = '[fields][0]=slug'; - return await this.strapiService.getCollectionType(RESOURCE_FAQ, query, flatMapSlug); + const strapiQuestionSlugs = await this.strapiService.getCollectionType(RESOURCE_FAQ, query); + + if (isSuccess(strapiQuestionSlugs)) { + return createSuccess(strapiQuestionSlugs.result.map((strapiQuestionSlug) => strapiQuestionSlug.slug)); + } + + return strapiQuestionSlugs; } + }