Skip to content

Commit

Permalink
feat(api): add KnowledgeElementCollection model
Browse files Browse the repository at this point in the history
  • Loading branch information
lionelB committed Feb 5, 2025
1 parent a2c8317 commit d6d819f
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import _ from 'lodash';

import { KnowledgeElement } from '../../../../shared/domain/models/KnowledgeElement.js';

class KnowledgeElementCollection {
constructor(knowledgeElements = []) {
this.knowledgeElements = knowledgeElements;
}

get latestUniqNonResetKnowledgeElements() {
return _(this.knowledgeElements)
.orderBy('createdAt', 'desc')
.reject({ status: KnowledgeElement.StatusType.RESET })
.uniqBy('skillId')
.value();
}

toSnapshot() {
return JSON.stringify(this.latestUniqNonResetKnowledgeElements.map((ke) => _.omit(ke, ['assessmentId', 'userId'])));
}
}

export { KnowledgeElementCollection };
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { buildKnowledgeElement } from '../../../../../../db/database-builder/factory/build-knowledge-element.js';
import { KnowledgeElementCollection } from '../../../../../../src/prescription/shared/domain/models/KnowledgeElementCollection.js';
import { KnowledgeElement } from '../../../../../../src/shared/domain/models/KnowledgeElement.js';
import { expect } from '../../../../../test-helper.js';

describe('Unit | Domain | Models | KnowledgeElementCollection', function () {
describe('#toSnapshot', function () {
it('should return a stringified JSON array of skills', function () {
const ke1 = buildKnowledgeElement({ skillId: 'rec1', answerId: 1, createdAt: new Date('2025-01-10'), id: 1 });
const ke2 = buildKnowledgeElement({ skillId: 'rec2', answerId: 2, createdAt: new Date('2025-01-11'), id: 2 });
const keCollection = new KnowledgeElementCollection([ke1, ke2]);
expect(keCollection.toSnapshot()).equal(
'[{"id":2,"source":"direct","status":"validated","createdAt":"2025-01-11T00:00:00.000Z","earnedPix":2,"skillId":"rec2","answerId":2,"competenceId":"recCHA789"},{"id":1,"source":"direct","status":"validated","createdAt":"2025-01-10T00:00:00.000Z","earnedPix":2,"skillId":"rec1","answerId":1,"competenceId":"recCHA789"}]',
);
});

it('should return most recent uniq skillId', function () {
const ke1 = buildKnowledgeElement({ skillId: 'rec1', answerId: 1, createdAt: new Date('2025-01-10'), id: 1 });
const ke2 = buildKnowledgeElement({ skillId: 'rec1', answerId: 2, createdAt: new Date('2025-01-11'), id: 2 });
const keCollection = new KnowledgeElementCollection([ke1, ke2]);
expect(keCollection.toSnapshot()).equal(
'[{"id":2,"source":"direct","status":"validated","createdAt":"2025-01-11T00:00:00.000Z","earnedPix":2,"skillId":"rec1","answerId":2,"competenceId":"recCHA789"}]',
);
});

it('should drop reset skillId', function () {
const ke1 = buildKnowledgeElement({
skillId: 'rec1',
answerId: 1,
status: KnowledgeElement.StatusType.RESET,
createdAt: new Date('2025-01-10'),
id: 1,
});
const keCollection = new KnowledgeElementCollection([ke1]);
expect(keCollection.toSnapshot()).equal('[]');
});
});

describe('latestUniqNonResetKnowledgeElements', function () {
it('should returns ke', function () {
const ke1 = buildKnowledgeElement({ skillId: 'rec1', answerId: 1, createdAt: new Date('2025-01-10'), id: 1 });
const ke2 = buildKnowledgeElement({ skillId: 'rec2', answerId: 2, createdAt: new Date('2025-01-11'), id: 2 });
const keCollection = new KnowledgeElementCollection([ke1, ke2]);
expect(keCollection.latestUniqNonResetKnowledgeElements).to.deep.equal([ke2, ke1]);
});

it('should drop reset skillId', function () {
const ke1 = buildKnowledgeElement({
skillId: 'rec1',
answerId: 1,
status: KnowledgeElement.StatusType.RESET,
createdAt: new Date('2025-01-10'),
id: 1,
});
const keCollection = new KnowledgeElementCollection([ke1]);
expect(keCollection.latestUniqNonResetKnowledgeElements).to.deep.equal([]);
});

it('should return most recent uniq skillId', function () {
const ke1 = buildKnowledgeElement({ skillId: 'rec1', answerId: 1, createdAt: new Date('2025-01-10'), id: 1 });
const ke2 = buildKnowledgeElement({ skillId: 'rec1', answerId: 2, createdAt: new Date('2025-01-11'), id: 2 });
const keCollection = new KnowledgeElementCollection([ke1, ke2]);
expect(keCollection.latestUniqNonResetKnowledgeElements).to.deep.equal([ke2]);
});
});
});

0 comments on commit d6d819f

Please sign in to comment.