diff --git a/api/src/learning-content/domain/usecases/dependencies.js b/api/src/learning-content/domain/usecases/dependencies.js index f51a20b41e8..ab5e7ea0490 100644 --- a/api/src/learning-content/domain/usecases/dependencies.js +++ b/api/src/learning-content/domain/usecases/dependencies.js @@ -1,7 +1,15 @@ import { LearningContentCache } from '../../../shared/infrastructure/caches/learning-content-cache.js'; +import { areaRepository } from '../../infrastructure/repositories/area-repository.js'; +import { competenceRepository } from '../../infrastructure/repositories/competence-repository.js'; +import { frameworkRepository } from '../../infrastructure/repositories/framework-repository.js'; import { lcmsRefreshCacheJobRepository } from '../../infrastructure/repositories/jobs/lcms-refresh-cache-job-repository.js'; +import { thematicRepository } from '../../infrastructure/repositories/thematic-repository.js'; export const dependencies = { + frameworkRepository, + areaRepository, + competenceRepository, + thematicRepository, lcmsRefreshCacheJobRepository, LearningContentCache, }; diff --git a/api/src/learning-content/domain/usecases/refresh-learning-content-cache.js b/api/src/learning-content/domain/usecases/refresh-learning-content-cache.js index db51549c555..9d77ee1ab2b 100644 --- a/api/src/learning-content/domain/usecases/refresh-learning-content-cache.js +++ b/api/src/learning-content/domain/usecases/refresh-learning-content-cache.js @@ -1,4 +1,19 @@ -/** @param {import('./dependencies.js').Dependencies} */ -export async function refreshLearningContentCache({ LearningContentCache }) { - await LearningContentCache.instance.reset(); -} +import { withTransaction } from '../../../shared/domain/DomainTransaction.js'; + +export const refreshLearningContentCache = withTransaction( + /** @param {import('./dependencies.js').Dependencies} */ + async function refreshLearningContentCache({ + LearningContentCache, + frameworkRepository, + areaRepository, + competenceRepository, + thematicRepository, + }) { + const learningContent = await LearningContentCache.instance.reset(); + + await frameworkRepository.save(learningContent.frameworks); + await areaRepository.save(learningContent.areas); + await competenceRepository.save(learningContent.competences); + await thematicRepository.save(learningContent.thematics); + }, +); diff --git a/api/tests/learning-content/unit/domain/usecases/refresh-learning-content-cache_test.js b/api/tests/learning-content/unit/domain/usecases/refresh-learning-content-cache_test.js index 175cfe8378b..7f66ed6c84a 100644 --- a/api/tests/learning-content/unit/domain/usecases/refresh-learning-content-cache_test.js +++ b/api/tests/learning-content/unit/domain/usecases/refresh-learning-content-cache_test.js @@ -1,21 +1,61 @@ +import { DomainTransaction } from '../../../../../lib/infrastructure/DomainTransaction.js'; import { refreshLearningContentCache } from '../../../../../src/learning-content/domain/usecases/refresh-learning-content-cache.js'; import { expect, sinon } from '../../../../test-helper.js'; describe('Unit | Domain | Usecase | Refresh learning content cache', function () { + beforeEach(function () { + sinon.stub(DomainTransaction, 'execute').callsFake((callback) => { + return callback(); + }); + }); + describe('#refreshLearningContentCache', function () { it('should trigger a reset of the learning content cache', async function () { // given + const frameworks = Symbol('frameworks'); + const areas = Symbol('areas'); + const competences = Symbol('competences'); + const thematics = Symbol('thematics'); + const LearningContentCache = { instance: { - reset: sinon.stub(), + reset: sinon.stub().resolves({ + frameworks, + areas, + competences, + thematics, + }), }, }; + const frameworkRepository = { + save: sinon.stub(), + }; + const areaRepository = { + save: sinon.stub(), + }; + const competenceRepository = { + save: sinon.stub(), + }; + const thematicRepository = { + save: sinon.stub(), + }; + // when - await refreshLearningContentCache({ LearningContentCache }); + await refreshLearningContentCache({ + LearningContentCache, + frameworkRepository, + areaRepository, + competenceRepository, + thematicRepository, + }); // then expect(LearningContentCache.instance.reset).to.have.been.calledOnce; + expect(frameworkRepository.save).to.have.been.calledOnceWithExactly(frameworks); + expect(areaRepository.save).to.have.been.calledOnceWithExactly(areas); + expect(competenceRepository.save).to.have.been.calledOnceWithExactly(competences); + expect(thematicRepository.save).to.have.been.calledOnceWithExactly(thematics); }); }); });