Skip to content

Commit

Permalink
feat(api): retrieves snapshots by campaignParticipationIds
Browse files Browse the repository at this point in the history
  • Loading branch information
lionelB authored and alicegoarnisson committed Jan 29, 2025
1 parent 1fab8b9 commit ed2f60c
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,30 @@ const findMultipleUsersFromUserIdsAndSnappedAtDates = async function (userIdsAnd
});
};

export { findByUserIdsAndSnappedAtDates, findMultipleUsersFromUserIdsAndSnappedAtDates, save };
/**
*
* @param {number[]} campaignParticipationIds
* @returns {Object.<number, KnowledgeElement[]>}
*/
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,
};
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit ed2f60c

Please sign in to comment.