Skip to content

Commit

Permalink
feat(api): refresh learning content cache writes learning content to pg
Browse files Browse the repository at this point in the history
Co-authored-by: Laura Bergoens <[email protected]>
  • Loading branch information
nlepage and laura-bergoens committed Nov 26, 2024
1 parent 90c1192 commit f167e84
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 6 deletions.
8 changes: 8 additions & 0 deletions api/src/learning-content/domain/usecases/dependencies.js
Original file line number Diff line number Diff line change
@@ -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,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
},
);
Original file line number Diff line number Diff line change
@@ -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);
});
});
});

0 comments on commit f167e84

Please sign in to comment.