diff --git a/api/src/prescription/target-profile/infrastructure/repositories/target-profile-summary-for-admin-repository.js b/api/src/prescription/target-profile/infrastructure/repositories/target-profile-summary-for-admin-repository.js index b6c58aa80c5..bbdd1ad163e 100644 --- a/api/src/prescription/target-profile/infrastructure/repositories/target-profile-summary-for-admin-repository.js +++ b/api/src/prescription/target-profile/infrastructure/repositories/target-profile-summary-for-admin-repository.js @@ -36,12 +36,15 @@ const findByTraining = async function ({ trainingId }) { export { findByTraining, findPaginatedFiltered }; function _applyFilters(qb, filter) { - const { name, id } = filter; + const { name, id, categories } = filter; if (name) { qb.whereILike('name', `%${name}%`); } if (id) { qb.where({ id }); } + if (categories) { + qb.whereIn('category', categories); + } return qb; } diff --git a/api/tests/prescription/target-profile/integration/infrastructure/repositories/target-profile-summary-for-admin-repository_test.js b/api/tests/prescription/target-profile/integration/infrastructure/repositories/target-profile-summary-for-admin-repository_test.js index 991916c8989..8015f3087ee 100644 --- a/api/tests/prescription/target-profile/integration/infrastructure/repositories/target-profile-summary-for-admin-repository_test.js +++ b/api/tests/prescription/target-profile/integration/infrastructure/repositories/target-profile-summary-for-admin-repository_test.js @@ -1,6 +1,7 @@ import * as targetProfileSummaryForAdminRepository from '../../../../../../src/prescription/target-profile/infrastructure/repositories/target-profile-summary-for-admin-repository.js'; import { TargetProfile } from '../../../../../../src/shared/domain/models/TargetProfile.js'; import { databaseBuilder, domainBuilder, expect } from '../../../../../test-helper.js'; + describe('Integration | Repository | Target-profile-summary-for-admin', function () { describe('#findPaginatedFiltered', function () { it('return TargetProfileSummaryForAdmins model', async function () { @@ -147,46 +148,39 @@ describe('Integration | Repository | Target-profile-summary-for-admin', function }); context('when passing a filter', function () { + let disciplineTargetProfile, otherTargetProfile, backToSchoolTargetProfile; + beforeEach(function () { + disciplineTargetProfile = { + id: 1, + name: 'TP DISCIPLINE', + outdated: false, + createdAt: new Date('2021-01-01'), + category: TargetProfile.categories.DISCIPLINE, + }; + otherTargetProfile = { + id: 2, + name: 'TP OTHER', + outdated: true, + createdAt: new Date('2021-01-01'), + category: TargetProfile.categories.OTHER, + }; + backToSchoolTargetProfile = { + id: 3, + name: 'TP BACK_TO_SCHOOL', + outdated: false, + createdAt: new Date('2021-01-01'), + category: TargetProfile.categories.BACK_TO_SCHOOL, + }; + + [disciplineTargetProfile, otherTargetProfile, backToSchoolTargetProfile].map( + databaseBuilder.factory.buildTargetProfile, + ); + return databaseBuilder.commit(); + }); context('name filter', function () { - let targetProfileData; - beforeEach(function () { - targetProfileData = [ - { - id: 1, - name: 'paTtErN', - outdated: false, - createdAt: new Date('2021-01-01'), - category: TargetProfile.categories.DISCIPLINE, - }, - { - id: 2, - name: 'AApatterNOo', - outdated: true, - createdAt: new Date('2021-01-01'), - category: TargetProfile.categories.DISCIPLINE, - }, - { - id: 3, - name: 'NotUnderTheRadar', - outdated: false, - createdAt: new Date('2021-01-01'), - category: TargetProfile.categories.DISCIPLINE, - }, - { - id: 4, - name: 'PaTternXXXX', - outdated: true, - createdAt: new Date('2021-01-01'), - category: TargetProfile.categories.DISCIPLINE, - }, - ]; - targetProfileData.map(databaseBuilder.factory.buildTargetProfile); - return databaseBuilder.commit(); - }); - - it('should return only target profiles matching "name" pattern in filter', async function () { + it('should return only target profiles matching "name" discipline in filter', async function () { // given - const filter = { name: 'pattern' }; + const filter = { name: 'discipline' }; const page = { number: 1, size: 10 }; // when @@ -197,51 +191,10 @@ describe('Integration | Repository | Target-profile-summary-for-admin', function }); // then - const expectedTargetProfileSummaries = [ - domainBuilder.buildTargetProfileSummaryForAdmin({ - id: 1, - name: 'paTtErN', - outdated: false, - createdAt: new Date('2021-01-01'), - category: TargetProfile.categories.DISCIPLINE, - }), - domainBuilder.buildTargetProfileSummaryForAdmin({ - id: 2, - name: 'AApatterNOo', - outdated: true, - createdAt: new Date('2021-01-01'), - category: TargetProfile.categories.DISCIPLINE, - }), - domainBuilder.buildTargetProfileSummaryForAdmin({ - id: 4, - name: 'PaTternXXXX', - outdated: true, - createdAt: new Date('2021-01-01'), - category: TargetProfile.categories.DISCIPLINE, - }), - ]; - expect(actualTargetProfileSummaries).to.deepEqualArray(expectedTargetProfileSummaries); + expect(actualTargetProfileSummaries).to.deep.include(disciplineTargetProfile); }); }); context('id filter', function () { - let targetProfileData; - beforeEach(function () { - targetProfileData = [ - { - id: 1, - name: 'TPA', - outdated: false, - createdAt: new Date('2021-01-01'), - category: TargetProfile.categories.PIX_PLUS, - }, - { id: 11, name: 'TPB', outdated: true, createdAt: new Date('2021-01-01') }, - { id: 21, name: 'TPC', outdated: false, createdAt: new Date('2021-01-01') }, - { id: 4, name: 'TPD', outdated: true, createdAt: new Date('2021-01-01') }, - ]; - targetProfileData.map(databaseBuilder.factory.buildTargetProfile); - return databaseBuilder.commit(); - }); - it('should return only target profiles with exact match ID', async function () { // given const filter = { id: 1 }; @@ -255,29 +208,44 @@ describe('Integration | Repository | Target-profile-summary-for-admin', function }); // then - const expectedTargetProfileSummaries = [ - domainBuilder.buildTargetProfileSummaryForAdmin({ - id: 1, - name: 'TPA', - outdated: false, - createdAt: new Date('2021-01-01'), - category: TargetProfile.categories.PIX_PLUS, - }), - ]; - expect(actualTargetProfileSummaries).to.deepEqualArray(expectedTargetProfileSummaries); + expect(actualTargetProfileSummaries).to.deep.includes(disciplineTargetProfile); }); }); - context('no match', function () { - let targetProfileData; - beforeEach(function () { - targetProfileData = [ - { id: 1, name: 'HELLO', outdated: false }, - { id: 2, name: 'SALUT', outdated: true }, - ]; - targetProfileData.map(databaseBuilder.factory.buildTargetProfile); - return databaseBuilder.commit(); + context('category filter', function () { + it('should return only target profiles with matching category discipline', async function () { + // given + const filter = { categories: [TargetProfile.categories.DISCIPLINE, TargetProfile.categories.BACK_TO_SCHOOL] }; + const page = { number: 1, size: 10 }; + + // when + const { models: actualTargetProfileSummaries } = + await targetProfileSummaryForAdminRepository.findPaginatedFiltered({ + filter, + page, + }); + + // then + + expect(actualTargetProfileSummaries[0]).to.deep.include(backToSchoolTargetProfile); + expect(actualTargetProfileSummaries[1]).to.deep.include(disciplineTargetProfile); }); + it('should return not result', async function () { + // given + const filter = { categories: [TargetProfile.categories.COMPETENCES] }; + const page = { number: 1, size: 10 }; + + // when + const { models: actualTargetProfileSummaries } = + await targetProfileSummaryForAdminRepository.findPaginatedFiltered({ + filter, + page, + }); + // then + expect(actualTargetProfileSummaries).to.have.lengthOf(0); + }); + }); + context('no match', function () { it('should return an empty array when no records match the filter', async function () { // given const filter = { name: 'COUCOU' }; @@ -291,7 +259,7 @@ describe('Integration | Repository | Target-profile-summary-for-admin', function }); // then - expect(actualTargetProfileSummaries).to.deepEqualArray([]); + expect(actualTargetProfileSummaries).to.have.lengthOf(0); }); }); });