Skip to content

Commit 08b693b

Browse files
committed
feat(api): compute module version
in module repository
1 parent fae6ecb commit 08b693b

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
import crypto from 'node:crypto';
2+
13
import { NotFoundError } from '../../../shared/domain/errors.js';
24
import { LearningContentResourceNotFound } from '../../../shared/domain/errors.js';
35
import { ModuleFactory } from '../factories/module-factory.js';
46

57
async function getBySlug({ slug, moduleDatasource }) {
68
try {
79
const moduleData = await moduleDatasource.getBySlug(slug);
10+
const version = _computeModuleVersion(moduleData);
811

9-
return ModuleFactory.build(moduleData);
12+
return ModuleFactory.build({ ...moduleData, version });
1013
} catch (e) {
1114
if (e instanceof LearningContentResourceNotFound) {
1215
throw new NotFoundError();
@@ -15,4 +18,10 @@ async function getBySlug({ slug, moduleDatasource }) {
1518
}
1619
}
1720

21+
function _computeModuleVersion(moduleData) {
22+
const hash = crypto.createHash('sha256');
23+
hash.update(JSON.stringify(moduleData));
24+
return hash.copy().digest('hex');
25+
}
26+
1827
export { getBySlug };

api/tests/devcomp/integration/repositories/module-repository_test.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import crypto from 'node:crypto';
2+
13
import { Module } from '../../../../src/devcomp/domain/models/module/Module.js';
24
import moduleDatasource from '../../../../src/devcomp/infrastructure/datasources/learning-content/module-datasource.js';
35
import { ModuleFactory } from '../../../../src/devcomp/infrastructure/factories/module-factory.js';
@@ -42,7 +44,7 @@ describe('Integration | DevComp | Repositories | ModuleRepository', function ()
4244
});
4345
});
4446

45-
it('should return a Module instance', async function () {
47+
it('should return a Module instance with its version', async function () {
4648
const existingModuleSlug = 'bien-ecrire-son-adresse-mail';
4749
const expectedFoundModule = {
4850
id: 'f7b3a2e1-0d5c-4c6c-9c4d-1a3d8f7e9f5d',
@@ -87,15 +89,30 @@ describe('Integration | DevComp | Repositories | ModuleRepository', function ()
8789
moduleDatasourceStub.getBySlug.withArgs(existingModuleSlug).resolves(expectedFoundModule);
8890
sinon.spy(ModuleFactory, 'build');
8991

92+
const version = 'AZERTY123456';
93+
const digestStub = sinon.stub().returns(version);
94+
const updateStub = sinon.stub();
95+
const createHashStub = sinon.stub(crypto, 'createHash').returns({
96+
copy: () => {
97+
return {
98+
digest: digestStub,
99+
};
100+
},
101+
update: updateStub,
102+
});
103+
90104
// when
91105
const module = await moduleRepository.getBySlug({
92106
slug: existingModuleSlug,
93107
moduleDatasource: moduleDatasourceStub,
94108
});
95109

96110
// then
97-
expect(ModuleFactory.build).to.have.been.calledWith(expectedFoundModule);
111+
expect(ModuleFactory.build).to.have.been.calledWith({ ...expectedFoundModule, version });
98112
expect(module).to.be.instanceof(Module);
113+
expect(createHashStub).to.have.been.calledOnceWith('sha256');
114+
expect(updateStub).to.have.been.calledOnceWith(JSON.stringify(expectedFoundModule));
115+
expect(digestStub).to.have.been.calledOnceWith('hex');
99116
});
100117
});
101118
});

0 commit comments

Comments
 (0)