Skip to content

Commit

Permalink
chore(api): remove findByUserIdsAndSnappedAtDates
Browse files Browse the repository at this point in the history
  • Loading branch information
lionelB committed Feb 5, 2025
1 parent 7b9e578 commit 1fe039c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 108 deletions.
16 changes: 8 additions & 8 deletions api/scripts/prod/compute-participation-results.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ async function _getCampaignParticipationChunks(campaign) {
}

async function _computeResults(skillIds, competences, campaignParticipations) {
const knowledgeElementByUser = await _getKnowledgeElementsByUser(campaignParticipations);
const knowledgeElementByCampaignParticipationId =
await _getKnowledgeElementsByCampaignParticipation(campaignParticipations);

const participationInfos = campaignParticipations.map(({ userId, sharedAt, id }) => {
return { userId, sharedAt, campaignParticipationId: id };
Expand All @@ -115,19 +116,18 @@ async function _computeResults(skillIds, competences, campaignParticipations) {
return campaignParticipations.map(({ userId, id }) => {
return new ParticipantResultsShared({
campaignParticipationId: id,
knowledgeElements: knowledgeElementByUser[userId],
knowledgeElements: knowledgeElementByCampaignParticipationId[id],
skillIds,
placementProfile: placementProfiles.find((placementProfile) => placementProfile.userId === userId),
});
});
}

async function _getKnowledgeElementsByUser(campaignParticipations) {
const sharingDateByUserId = {};
campaignParticipations.forEach(({ userId, sharedAt }) => (sharingDateByUserId[userId] = sharedAt));
const knowledgeElementByUser =
await knowlegeElementSnapshotRepository.findByUserIdsAndSnappedAtDates(sharingDateByUserId);
return knowledgeElementByUser;
async function _getKnowledgeElementsByCampaignParticipation(campaignParticipations) {
const campaignParticipationIds = campaignParticipations.map(({ id }) => id);
const knowledgeElementByCampaingParticipationId =
await knowlegeElementSnapshotRepository.findByCampaignParticipationIds(campaignParticipationIds);
return knowledgeElementByCampaingParticipationId;
}

function _toSQLValues(participantsResults) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
import _ from 'lodash';

import { knex } from '../../../../../db/knex-database-connection.js';
import { DomainTransaction } from '../../../../shared/domain/DomainTransaction.js';
import { AlreadyExistingEntityError } from '../../../../shared/domain/errors.js';
import { KnowledgeElement } from '../../../../shared/domain/models/KnowledgeElement.js';
import * as knexUtils from '../../../../shared/infrastructure/utils/knex-utils.js';
import { CampaignParticipationKnowledgeElementSnapshots } from '../../../shared/domain/read-models/CampaignParticipationKnowledgeElementSnapshots.js';

function _toKnowledgeElementCollection({ snapshot } = {}) {
if (!snapshot) return null;
return snapshot.map(
(data) =>
new KnowledgeElement({
...data,
createdAt: new Date(data.createdAt),
}),
);
}

const save = async function ({ userId, snappedAt, knowledgeElements, campaignParticipationId }) {
try {
const knexConn = DomainTransaction.getConnection();
Expand All @@ -36,36 +23,6 @@ const save = async function ({ userId, snappedAt, knowledgeElements, campaignPar
}
};

const findByUserIdsAndSnappedAtDates = async function (userIdsAndSnappedAtDates = {}) {
const params = Object.entries(userIdsAndSnappedAtDates);
const results = await knex
.select('userId', 'snapshot')
.from('knowledge-element-snapshots')
.whereIn(['userId', 'snappedAt'], params);

const knowledgeElementsByUserId = {};
for (const result of results) {
knowledgeElementsByUserId[result.userId] = _toKnowledgeElementCollection(result);
}

const userIdsWithoutSnapshot = _.difference(
Object.keys(userIdsAndSnappedAtDates),
Object.keys(knowledgeElementsByUserId),
);
for (const userId of userIdsWithoutSnapshot) {
knowledgeElementsByUserId[userId] = null;
}

return knowledgeElementsByUserId;
};

/**
* @typedef FindMultipleSnapshotsPayload
* @type {object}
* @property {number} userId
* @property {date} sharedAt
*/

/**
* @function
* @name findCampaignParticipationKnowledgeElementSnapshots
Expand Down Expand Up @@ -108,9 +65,4 @@ const findByCampaignParticipationIds = async function (campaignParticipationIds)
);
};

export {
findByCampaignParticipationIds,
findByUserIdsAndSnappedAtDates,
findCampaignParticipationKnowledgeElementSnapshots,
save,
};
export { findByCampaignParticipationIds, findCampaignParticipationKnowledgeElementSnapshots, save };
Original file line number Diff line number Diff line change
Expand Up @@ -121,57 +121,6 @@ describe('Integration | Repository | KnowledgeElementSnapshotRepository', functi
});
});

describe('#findByUserIdsAndSnappedAtDates', function () {
let userId1, userId2, campaignParticipationId;

beforeEach(function () {
userId1 = databaseBuilder.factory.buildUser().id;
userId2 = databaseBuilder.factory.buildUser().id;
campaignParticipationId = databaseBuilder.factory.buildCampaignParticipation().id;
return databaseBuilder.commit();
});

it('should find knowledge elements snapshoted grouped by userId for userIds and their respective dates', 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,
});
await databaseBuilder.commit();

// when
const knowledgeElementsByUserId = await knowledgeElementSnapshotRepository.findByUserIdsAndSnappedAtDates({
[userId1]: snappedAt1,
[userId2]: snappedAt2,
});

// then
expect(knowledgeElementsByUserId[userId1]).to.deep.equal([knowledgeElement1]);
expect(knowledgeElementsByUserId[userId2]).to.deep.equal([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('#findByCampaignParticipationIds', function () {
let userId1, userId2, campaignParticipationId, secondCampaignParticipationId, otherCampaignParticipationId;

Expand Down

0 comments on commit 1fe039c

Please sign in to comment.