Skip to content

Commit

Permalink
tech(api): replacing userId sharedAt combo in findMultipleUsersFromUs…
Browse files Browse the repository at this point in the history
…erIdsAndSnappedAtDates wip
  • Loading branch information
alicegoarnisson authored and lionelB committed Feb 5, 2025
1 parent 08dfb53 commit 7b9e578
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 141 deletions.
6 changes: 3 additions & 3 deletions api/scripts/prod/compute-participation-results.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ async function _getCampaignParticipationChunks(campaign) {
async function _computeResults(skillIds, competences, campaignParticipations) {
const knowledgeElementByUser = await _getKnowledgeElementsByUser(campaignParticipations);

const userIdsAndDates = campaignParticipations.map(({ userId, sharedAt }) => {
return { userId, sharedAt };
const participationInfos = campaignParticipations.map(({ userId, sharedAt, id }) => {
return { userId, sharedAt, campaignParticipationId: id };
});
const placementProfiles = await placementProfileService.getPlacementProfilesWithSnapshotting({
userIdsAndDates,
participationInfos,
competences,
allowExcessPixAndLevels: false,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ async function _makeMemoizedGetPlacementProfileForUser(results) {
const sharedResultsChunks = await PromiseUtils.mapSeries(
chunk(sharedResults, CHUNK_SIZE_CAMPAIGN_RESULT_PROCESSING),
(sharedResultsChunk) => {
const userIdsAndDates = sharedResultsChunk.map(({ userId, sharedAt }) => {
return { userId, sharedAt };
const participationInfos = sharedResultsChunk.map(({ campaignParticipationId, userId, sharedAt }) => {
return { campaignParticipationId, userId, sharedAt };
});

return placementProfileService.getPlacementProfilesWithSnapshotting({
userIdsAndDates,
participationInfos,
allowExcessPixAndLevels: false,
competences,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,31 +68,23 @@ const findByUserIdsAndSnappedAtDates = async function (userIdsAndSnappedAtDates

/**
* @function
* @name findMultipleUsersFromUserIdsAndSnappedAtDates
* @name findCampaignParticipationKnowledgeElementSnapshots
*
* @param {Array<FindMultipleSnapshotsPayload>} userIdsAndSnappedAtDates
* @param {number[]} campaignParticipationIds
* @returns {Promise<Array<CampaignParticipationKnowledgeElementSnapshots>>}
*/
const findMultipleUsersFromUserIdsAndSnappedAtDates = async function (userIdsAndSnappedAtDates) {
const params = userIdsAndSnappedAtDates.map((userIdAndDate) => {
return [userIdAndDate.userId, userIdAndDate.sharedAt];
});

const results = await knex
.select('userId', 'snapshot', 'snappedAt', 'campaignParticipationId')
.from('knowledge-element-snapshots')
.whereIn(['knowledge-element-snapshots.userId', 'snappedAt'], params);

return results.map((result) => {
const mappedKnowledgeElements = _toKnowledgeElementCollection({ snapshot: result.snapshot });

return new CampaignParticipationKnowledgeElementSnapshots({
userId: result.userId,
snappedAt: result.snappedAt,
knowledgeElements: mappedKnowledgeElements,
campaignParticipationId: result.campaignParticipationId,
});
});
const findCampaignParticipationKnowledgeElementSnapshots = async function (campaignParticipationIds) {
const knowledgeElementsByCampaignParticipation = await findByCampaignParticipationIds(campaignParticipationIds);
const knowledgeElementsList = [];
campaignParticipationIds.map((campaignParticipationId) =>
knowledgeElementsList.push(
new CampaignParticipationKnowledgeElementSnapshots({
knowledgeElements: knowledgeElementsByCampaignParticipation[campaignParticipationId],
campaignParticipationId: campaignParticipationId,
}),
),
);
return knowledgeElementsList;
};

/**
Expand All @@ -119,6 +111,6 @@ const findByCampaignParticipationIds = async function (campaignParticipationIds)
export {
findByCampaignParticipationIds,
findByUserIdsAndSnappedAtDates,
findMultipleUsersFromUserIdsAndSnappedAtDates,
findCampaignParticipationKnowledgeElementSnapshots,
save,
};
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,16 @@ class CampaignAssessmentExport {
let participantKnowledgeElementsByCompetenceId = [];
if (campaignParticipationInfo.isShared) {
const sharedKnowledgeElementsByUserIdAndCompetenceId =
await knowledgeElementSnapshotRepository.findMultipleUsersFromUserIdsAndSnappedAtDates(
sharedParticipations.map(({ userId, sharedAt }) => ({ userId, sharedAt })),
await knowledgeElementSnapshotRepository.findCampaignParticipationKnowledgeElementSnapshots(
sharedParticipations.map(({ campaignParticipationId }) => campaignParticipationId),
);
const sharedResultInfo = sharedKnowledgeElementsByUserIdAndCompetenceId.find(
(knowledElementForSharedParticipation) => {
const sameUserId = campaignParticipationInfo.userId === knowledElementForSharedParticipation.userId;
const sameDate =
campaignParticipationInfo.sharedAt &&
campaignParticipationInfo.sharedAt.getTime() === knowledElementForSharedParticipation.snappedAt.getTime();
const sameParticipationId =
campaignParticipationInfo.campaignParticipationId ===
knowledElementForSharedParticipation.campaignParticipationId;

return sameUserId && sameDate;
return sameParticipationId;
},
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,14 @@ class CampaignProfilesCollectionExport {
}

async _getUsersPlacementProfiles(campaignParticipationResultDataChunk, placementProfileService) {
const userIdsAndDates = campaignParticipationResultDataChunk.map(({ userId, sharedAt }) => {
return { userId, sharedAt };
});
const participationInfos = campaignParticipationResultDataChunk.map(
({ id: campaignParticipationId, userId, sharedAt }) => {
return { campaignParticipationId, userId, sharedAt };
},
);

const placementProfiles = await placementProfileService.getPlacementProfilesWithSnapshotting({
userIdsAndDates,
participationInfos,
competences: this.competences,
allowExcessPixAndLevels: false,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
class CampaignParticipationKnowledgeElementSnapshots {
constructor({ userId, snappedAt, knowledgeElements, campaignParticipationId } = {}) {
this.userId = userId;
this.snappedAt = snappedAt;
constructor({ knowledgeElements, campaignParticipationId } = {}) {
this.knowledgeElements = knowledgeElements;
this.campaignParticipationId = campaignParticipationId;
}
Expand Down
30 changes: 18 additions & 12 deletions api/src/shared/domain/services/placement-profile-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,25 @@ async function _generatePlacementProfile({ userId, profileDate, competences, all
});
}

async function getPlacementProfilesWithSnapshotting({ userIdsAndDates, competences, allowExcessPixAndLevels = true }) {
const knowledgeElementsByUserIdAndDates =
await knowledgeElementSnapshotRepository.findMultipleUsersFromUserIdsAndSnappedAtDates(userIdsAndDates);

return userIdsAndDates.map(({ userId, sharedAt }) => {
const keForUser = knowledgeElementsByUserIdAndDates.find((knowledgeElementsByUserIdAndDates) => {
const sameUserId = knowledgeElementsByUserIdAndDates.userId === userId;
const sameDate = sharedAt && knowledgeElementsByUserIdAndDates.snappedAt.getTime() === sharedAt.getTime();
async function getPlacementProfilesWithSnapshotting({
participationInfos,
competences,
allowExcessPixAndLevels = true,
}) {
const campaignParticipationIds = participationInfos.map(({ campaignParticipationId }) => campaignParticipationId);
const knowledgeElementsParticipations =
await knowledgeElementSnapshotRepository.findCampaignParticipationKnowledgeElementSnapshots(
campaignParticipationIds,
);

return sameUserId && sameDate;
return participationInfos.map((participationInfos) => {
const keForParticipation = knowledgeElementsParticipations.find((knowledgeElementsParticipation) => {
return knowledgeElementsParticipation.campaignParticipationId === participationInfos.campaignParticipationId;
});

const knowledgeElementsByCompetence = keForUser ? _.groupBy(keForUser.knowledgeElements, 'competenceId') : [];
const knowledgeElementsByCompetence = keForParticipation
? _.groupBy(keForParticipation.knowledgeElements, 'competenceId')
: [];

const userCompetences = _createUserCompetencesV2({
knowledgeElementsByCompetence,
Expand All @@ -151,8 +157,8 @@ async function getPlacementProfilesWithSnapshotting({ userIdsAndDates, competenc
});

return new PlacementProfile({
userId,
profileDate: sharedAt,
userId: participationInfos.userId,
profileDate: participationInfos.sharedAt,
userCompetences,
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ function _buildParticipationWithSnapshot(participationAttributes, knowledgeEleme
userId: participation.userId,
snappedAt: participation.sharedAt,
knowledgeElementsAttributes,
campaignParticipationId: participation.id,
});

return participation;
Expand Down
Loading

0 comments on commit 7b9e578

Please sign in to comment.