From b303bfb1d95707e7020cc9795fdc89a268cc1847 Mon Sep 17 00:00:00 2001 From: LionelB Date: Wed, 29 Jan 2025 18:23:34 +0100 Subject: [PATCH] feat(api): retrieves snapshots by campaignParticipationIds --- .../knowledge-element-snapshot-repository.js | 28 +++++++- ...wledge-element-snapshot-repository_test.js | 66 +++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) 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..49b191648e3 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,30 @@ const findMultipleUsersFromUserIdsAndSnappedAtDates = async function (userIdsAnd }); }; -export { findByUserIdsAndSnappedAtDates, findMultipleUsersFromUserIdsAndSnappedAtDates, save }; +/** + * + * @param {number[]} campaignParticipationIds + * @returns {Object.} + */ +const findByCampaignParticipationIds = async function (campaignParticipationIds) { + const results = await knex + .select('campaignParticipationId', 'snapshot') + .from('knowledge-element-snapshots') + .whereIn('campaignParticipationId', campaignParticipationIds); + + return Object.fromEntries( + results.map(({ campaignParticipationId, snapshot }) => [ + campaignParticipationId, + snapshot.map(({ createdAt, ...data }) => { + return new KnowledgeElement({ ...data, createdAt: new Date(createdAt) }); + }), + ]), + ); +}; + +export { + findByCampaignParticipationIds, + 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..1d112c1b0fd 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('#findByCampaignParticipationIds', 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.findByCampaignParticipationIds([ + 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;