Skip to content

Commit

Permalink
feat(faq): ne pas passer de mapper au strapi service
Browse files Browse the repository at this point in the history
  • Loading branch information
suli-octo committed Oct 24, 2023
1 parent fa16472 commit 43bb016
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 53 deletions.
4 changes: 2 additions & 2 deletions src/server/faq/domain/FAQ.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const aListeDeQuestion = (): Array<FAQ.Question> => {
export const aQuestion = (override?: Partial<FAQ.Question>): FAQ.Question => {
return {
problématique: 'Comment constituer un dossier locatif ?',
slug: 'question-slug',
slug: 'Comment-constituer-un-dossier-locatif ?',
...override,
};
};
Expand All @@ -19,7 +19,7 @@ export const aQuestionEtReponse = (override?: Partial<FAQ.QuestionEtReponse>): F
return {
contenu: 'mon contenu explicatif',
problématique: 'Comment constituer un dossier locatif ?',
slug: 'question-slug',
slug: 'Comment-constituer-un-dossier-locatif ?',
...override,
};
};
Expand Down
7 changes: 7 additions & 0 deletions src/server/faq/infra/strapiFAQ.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ export const aStrapiQuestion = (override?: Partial<FAQResponseStrapi.Question>):
...override,
};
};

export const aStrapiQuestionSlug = (override?: Partial<FAQResponseStrapi.QuestionSlug>): FAQResponseStrapi.QuestionSlug => {
return {
slug: 'Comment-constituer-un-dossier-locatif ?',
...override,
};
};
17 changes: 2 additions & 15 deletions src/server/faq/infra/strapiFAQ.mapper.test.ts
Original file line number Diff line number Diff line change
@@ -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é', () => {
Expand Down Expand Up @@ -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-?');
});
});
3 changes: 0 additions & 3 deletions src/server/faq/infra/strapiFAQ.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

107 changes: 81 additions & 26 deletions src/server/faq/infra/strapiFAQ.repository.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
31 changes: 24 additions & 7 deletions src/server/faq/infra/strapiFAQ.repository.ts
Original file line number Diff line number Diff line change
@@ -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<Either<FAQ.QuestionEtReponse>> {
const query = `filters[slug][$eq]=${slug}`;
return await this.strapiService.getFirstFromCollectionType<FAQResponseStrapi.QuestionEtReponse, FAQ.QuestionEtReponse>(RESOURCE_FAQ, query, mapQuestionRéponse);
const strapiQuestionEtReponse = await this.strapiService.getFirstFromCollectionType<FAQResponseStrapi.QuestionEtReponse>(RESOURCE_FAQ, query);

if(isSuccess(strapiQuestionEtReponse))
return createSuccess(mapQuestionRéponse(strapiQuestionEtReponse.result));

return strapiQuestionEtReponse;
}

async getAllFAQ(): Promise<Either<Array<FAQ.Question>>> {
const query = 'fields[0]=problematique&fields[1]=slug';
return await this.strapiService.getCollectionType<FAQResponseStrapi.Question, FAQ.Question>(RESOURCE_FAQ, query, mapQuestion);
const strapiQuestions = await this.strapiService.getCollectionType<FAQResponseStrapi.Question>(RESOURCE_FAQ, query);

if (isSuccess(strapiQuestions))
return createSuccess(strapiQuestions.result.map((strapiQuestion) => mapQuestion(strapiQuestion)));

return strapiQuestions;
}

async listAllFAQSlug(): Promise<Either<Array<FAQ.Slug>>> {
const query = '[fields][0]=slug';
return await this.strapiService.getCollectionType<FAQResponseStrapi.QuestionSlug, FAQ.Slug>(RESOURCE_FAQ, query, flatMapSlug);
const strapiQuestionSlugs = await this.strapiService.getCollectionType<FAQResponseStrapi.QuestionSlug>(RESOURCE_FAQ, query);

if (isSuccess(strapiQuestionSlugs)) {
return createSuccess(strapiQuestionSlugs.result.map((strapiQuestionSlug) => strapiQuestionSlug.slug));
}

return strapiQuestionSlugs;
}

}

0 comments on commit 43bb016

Please sign in to comment.