Skip to content

Commit

Permalink
refactor(api): introduce refreshLearningContentCache usecase
Browse files Browse the repository at this point in the history
  • Loading branch information
HEYGUL committed Jul 9, 2024
1 parent 513a295 commit fcccb95
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 43 deletions.
6 changes: 2 additions & 4 deletions api/lib/application/cache/cache-controller.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import _ from 'lodash';

import { knex } from '../../../db/knex-database-connection.js';
import { sharedUsecases as usecases } from '../../../src/shared/domain/usecases/index.js';
import * as LearningContentDatasources from '../../../src/shared/infrastructure/datasources/learning-content/index.js';
import { LcmsRefreshCacheJob } from '../../infrastructure/jobs/lcms/LcmsRefreshCacheJob.js';

const refreshCacheEntries = async function (_, h) {
const refreshJob = new LcmsRefreshCacheJob(knex);
await refreshJob.schedule();
await usecases.refreshLearningContentCache();
return h.response({}).code(202);
};

Expand Down
3 changes: 3 additions & 0 deletions api/src/shared/domain/usecases/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';

import { knex } from '../../../../db/knex-database-connection.js';
import { LcmsRefreshCacheJob } from '../../../../lib/infrastructure/jobs/lcms/LcmsRefreshCacheJob.js';
import * as complementaryCertificationBadgeRepository from '../../../certification/complementary-certification/infrastructure/repositories/complementary-certification-badge-repository.js';
import * as badgeRepository from '../../../evaluation/infrastructure/repositories/badge-repository.js';
import { injectDependencies } from '../../../shared/infrastructure/utils/dependency-injection.js';
Expand All @@ -15,6 +17,7 @@ const usecasesWithoutInjectedDependencies = {
const dependencies = {
complementaryCertificationBadgeRepository,
badgeRepository,
lcmsRefreshCacheJob: new LcmsRefreshCacheJob(knex),
};

const sharedUsecases = injectDependencies(usecasesWithoutInjectedDependencies, dependencies);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const refreshLearningContentCache = async function ({ lcmsRefreshCacheJob }) {
await lcmsRefreshCacheJob.schedule();
};
export { refreshLearningContentCache };
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { refreshLearningContentCache } from '../../../../../src/shared/domain/usecases/refresh-learning-content-cache.js';
import { expect, sinon } from '../../../../test-helper.js';

describe('Unit | Domain | Usecases | Refresh Learning Content Cache', function () {
it('should use repository to schedule reefresh job', async function () {
// given
const lcmsRefreshCacheJob = { schedule: sinon.stub() };

// when
await refreshLearningContentCache({ lcmsRefreshCacheJob });

// then
expect(lcmsRefreshCacheJob.schedule).to.have.been.calledOnce;
});
});
43 changes: 4 additions & 39 deletions api/tests/unit/application/cache/cache-controller_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { cacheController } from '../../../../lib/application/cache/cache-controller.js';
import { sharedUsecases as usecases } from '../../../../src/shared/domain/usecases/index.js';
import * as learningContentDatasources from '../../../../src/shared/infrastructure/datasources/learning-content/index.js';
import { logger } from '../../../../src/shared/infrastructure/utils/logger.js';
import { expect, hFake, sinon } from '../../../test-helper.js';

describe('Unit | Controller | cache-controller', function () {
Expand Down Expand Up @@ -79,53 +79,18 @@ describe('Unit | Controller | cache-controller', function () {
});

describe('#refreshCacheEntries', function () {
const request = {};
let learningContentDatasourceStub;

beforeEach(function () {
learningContentDatasourceStub = { refreshLearningContentCacheRecords: sinon.stub() };
});

context('nominal case', function () {
it('should reply with http status 202', async function () {
// given
const numberOfDeletedKeys = 0;
learningContentDatasourceStub.refreshLearningContentCacheRecords.resolves(numberOfDeletedKeys);

// when
const response = await cacheController.refreshCacheEntries(request, hFake, {
learningContentDatasource: learningContentDatasourceStub,
});

// then
expect(learningContentDatasourceStub.refreshLearningContentCacheRecords).to.have.been.calledOnce;
expect(response.statusCode).to.equal(202);
});
});

context('error case', function () {
let response;

beforeEach(async function () {
// given
sinon.stub(logger, 'error');
learningContentDatasourceStub.refreshLearningContentCacheRecords.rejects();
sinon.stub(usecases, 'refreshLearningContentCache').resolves();

// when
response = await cacheController.refreshCacheEntries(request, hFake, {
learningContentDatasource: learningContentDatasourceStub,
});
});
const response = await cacheController.refreshCacheEntries({}, hFake);

it('should reply with http status 202', async function () {
// then
expect(usecases.refreshLearningContentCache).to.have.been.calledOnce;
expect(response.statusCode).to.equal(202);
});

it('should call log errors', async function () {
// then
expect(logger.error).to.have.been.calledOnce;
});
});
});
});

0 comments on commit fcccb95

Please sign in to comment.