diff --git a/api/src/prescription/campaign/infrastructure/repositories/knowledge-element-snapshot-repository.js b/api/src/prescription/campaign/infrastructure/repositories/knowledge-element-snapshot-repository.js index 8c73aaf6e1c..452a2a0e95a 100644 --- a/api/src/prescription/campaign/infrastructure/repositories/knowledge-element-snapshot-repository.js +++ b/api/src/prescription/campaign/infrastructure/repositories/knowledge-element-snapshot-repository.js @@ -97,4 +97,25 @@ const findMultipleUsersFromUserIdsAndSnappedAtDates = async function (userIdsAnd }); }; -export { findByUserIdsAndSnappedAtDates, findMultipleUsersFromUserIdsAndSnappedAtDates, save }; +/** + * + * @param {number[]} campaignIds + * @returns {Object.} + */ +const findByCampaignIds = async function (campaignIds) { + const results = await knex + .select('campaignParticipationId', 'snapshot') + .from('knowledge-element-snapshots') + .whereIn('campaignParticipationId', campaignIds); + + return Object.fromEntries( + results.map(({ campaignParticipationId, snapshot }) => [ + campaignParticipationId, + snapshot.map(({ createdAt, ...data }) => { + return new KnowledgeElement({ ...data, createdAt: new Date(createdAt) }); + }), + ]), + ); +}; + +export { findByCampaignIds, findByUserIdsAndSnappedAtDates, findMultipleUsersFromUserIdsAndSnappedAtDates, save }; diff --git a/api/tests/prescription/campaign/integration/infrastructure/repositories/knowledge-element-snapshot-repository_test.js b/api/tests/prescription/campaign/integration/infrastructure/repositories/knowledge-element-snapshot-repository_test.js index 626e489c18b..bd9daee7aa8 100644 --- a/api/tests/prescription/campaign/integration/infrastructure/repositories/knowledge-element-snapshot-repository_test.js +++ b/api/tests/prescription/campaign/integration/infrastructure/repositories/knowledge-element-snapshot-repository_test.js @@ -172,6 +172,72 @@ describe('Integration | Repository | KnowledgeElementSnapshotRepository', functi }); }); + describe('#findByCampaignIds', function () { + let userId1, userId2, campaignParticipationId, secondCampaignParticipationId, otherCampaignParticipationId; + + beforeEach(function () { + userId1 = databaseBuilder.factory.buildUser().id; + userId2 = databaseBuilder.factory.buildUser().id; + + campaignParticipationId = databaseBuilder.factory.buildCampaignParticipation({ userId: userId1 }).id; + secondCampaignParticipationId = databaseBuilder.factory.buildCampaignParticipation({ userId: userId2 }).id; + otherCampaignParticipationId = databaseBuilder.factory.buildCampaignParticipation({ userId: userId2 }).id; + return databaseBuilder.commit(); + }); + + it('should find knowledge elements snapshoted grouped by campaignParticipationIds', async function () { + // given + const snappedAt1 = new Date('2020-01-02'); + const knowledgeElement1 = databaseBuilder.factory.buildKnowledgeElement({ userId: userId1 }); + databaseBuilder.factory.buildKnowledgeElementSnapshot({ + userId: userId1, + snappedAt: snappedAt1, + snapshot: JSON.stringify([knowledgeElement1]), + campaignParticipationId, + }); + const snappedAt2 = new Date('2020-02-02'); + const knowledgeElement2 = databaseBuilder.factory.buildKnowledgeElement({ userId: userId2 }); + databaseBuilder.factory.buildKnowledgeElementSnapshot({ + userId: userId2, + snappedAt: snappedAt2, + snapshot: JSON.stringify([knowledgeElement2]), + campaignParticipationId: secondCampaignParticipationId, + }); + + const snappedAt3 = new Date('2020-02-03'); + const knowledgeElement3 = databaseBuilder.factory.buildKnowledgeElement({ userId: userId2 }); + databaseBuilder.factory.buildKnowledgeElementSnapshot({ + userId: userId2, + snappedAt: snappedAt3, + snapshot: JSON.stringify([knowledgeElement3]), + campaignParticipationId: otherCampaignParticipationId, + }); + + await databaseBuilder.commit(); + + // when + const knowledgeElementsByUserId = await knowledgeElementSnapshotRepository.findByCampaignIds([ + campaignParticipationId, + secondCampaignParticipationId, + ]); + + // then + expect(knowledgeElementsByUserId).to.deep.equals({ + [campaignParticipationId]: [knowledgeElement1], + [secondCampaignParticipationId]: [knowledgeElement2], + }); + }); + + it('should return null associated to userId when user does not have a snapshot', async function () { + // when + const knowledgeElementsByUserId = await knowledgeElementSnapshotRepository.findByUserIdsAndSnappedAtDates({ + [userId1]: new Date('2020-04-01T00:00:00Z'), + }); + + expect(knowledgeElementsByUserId[userId1]).to.be.null; + }); + }); + describe('#findMultipleUsersFromUserIdsAndSnappedAtDates', function () { let userId1, userId2; let snappedAt1, snappedAt2, snappedAt3;