From d6d819fbeed057fae0330077f2124ff8ad5e4e1a Mon Sep 17 00:00:00 2001 From: LionelB Date: Wed, 5 Feb 2025 12:15:12 +0100 Subject: [PATCH] feat(api): add KnowledgeElementCollection model --- .../models/KnowledgeElementCollection.js | 23 +++++++ .../models/KnowledgeElementCollection_test.js | 66 +++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 api/src/prescription/shared/domain/models/KnowledgeElementCollection.js create mode 100644 api/tests/prescription/shared/unit/domain/models/KnowledgeElementCollection_test.js diff --git a/api/src/prescription/shared/domain/models/KnowledgeElementCollection.js b/api/src/prescription/shared/domain/models/KnowledgeElementCollection.js new file mode 100644 index 00000000000..2381db6ee2d --- /dev/null +++ b/api/src/prescription/shared/domain/models/KnowledgeElementCollection.js @@ -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 }; diff --git a/api/tests/prescription/shared/unit/domain/models/KnowledgeElementCollection_test.js b/api/tests/prescription/shared/unit/domain/models/KnowledgeElementCollection_test.js new file mode 100644 index 00000000000..0ba0aad87aa --- /dev/null +++ b/api/tests/prescription/shared/unit/domain/models/KnowledgeElementCollection_test.js @@ -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]); + }); + }); +});