-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): add KnowledgeElementCollection model
- Loading branch information
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
api/src/prescription/shared/domain/models/KnowledgeElementCollection.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
66 changes: 66 additions & 0 deletions
66
api/tests/prescription/shared/unit/domain/models/KnowledgeElementCollection_test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
}); | ||
}); | ||
}); |