-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Mettre à jour la route de récupération de stages dans orga (P…
- Loading branch information
Showing
5 changed files
with
199 additions
and
34 deletions.
There are no files selected for viewing
44 changes: 30 additions & 14 deletions
44
api/lib/domain/usecases/get-campaign-participations-counts-by-stage.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,62 @@ | ||
// eslint-disable-next-line eslint-comments/disable-enable-pair | ||
/* eslint-disable import/no-restricted-paths */ | ||
import * as stageCollectionRepository from '../../infrastructure/repositories/user-campaign-results/stage-collection-repository.js'; | ||
import * as defaultStageRepository from '../../infrastructure/repositories/stage-repository.js'; | ||
import * as defaultStageAcquisitionRepository from '../../infrastructure/repositories/stage-acquisition-repository.js'; | ||
import { UserNotAuthorizedToAccessEntityError, NoStagesForCampaign } from '../errors.js'; | ||
import * as defaultStageAndStageAcquisitionComparisonService from '../services/stages/stage-and-stage-acquisition-comparison-service.js'; | ||
|
||
const getCampaignParticipationsCountByStage = async function ({ | ||
userId, | ||
campaignId, | ||
campaignRepository, | ||
campaignParticipationRepository, | ||
stageRepository = defaultStageRepository, | ||
stageAndStageAcquisitionComparisonService = defaultStageAndStageAcquisitionComparisonService, | ||
stageAcquisitionRepository = defaultStageAcquisitionRepository, | ||
}) { | ||
if (!(await campaignRepository.checkIfUserOrganizationHasAccessToCampaign(campaignId, userId))) { | ||
throw new UserNotAuthorizedToAccessEntityError('User does not belong to the organization that owns the campaign'); | ||
} | ||
|
||
const stageCollection = await stageCollectionRepository.findStageCollection({ campaignId }); | ||
const stages = await stageRepository.getByCampaignId(campaignId); | ||
|
||
if (!stageCollection.hasStage) { | ||
if (!stages.length) { | ||
throw new NoStagesForCampaign(); | ||
} | ||
|
||
const participantsResults = await campaignParticipationRepository.getAllParticipationsByCampaignId(campaignId); | ||
const stageAcquisitions = await stageAcquisitionRepository.getByCampaignId(campaignId); | ||
|
||
const participantsByStage = stageCollection.stages.map((stage, index) => ({ | ||
const uniqueCampaignParticipationIds = [ | ||
...new Set(stageAcquisitions.map((stageAcquisition) => stageAcquisition.campaignParticipationId)), | ||
]; | ||
|
||
const stagesInfos = stages.map((stage, index) => ({ | ||
id: stage.id, | ||
value: 0, | ||
reachedStage: index + 1, | ||
totalStage: stageCollection.totalStages, | ||
totalStage: stages.length, | ||
title: stage.prescriberTitle, | ||
description: stage.prescriberDescription, | ||
})); | ||
|
||
participantsResults.forEach((participantResult) => { | ||
const stageReached = stageCollection.getReachedStage( | ||
participantResult.validatedSkillsCount, | ||
participantResult.masteryRate * 100, | ||
uniqueCampaignParticipationIds.forEach((campaignParticipationId) => { | ||
const stageAcquisitionForCampaignParticipation = stageAcquisitions.filter( | ||
(stageAcquisition) => stageAcquisition.campaignParticipationId === campaignParticipationId, | ||
); | ||
|
||
const stagesInformation = stageAndStageAcquisitionComparisonService.compare( | ||
stages, | ||
stageAcquisitionForCampaignParticipation, | ||
); | ||
|
||
const stageIndex = participantsByStage.findIndex((data) => data.id === stageReached.id); | ||
participantsByStage[stageIndex].value++; | ||
const stageReached = stagesInformation.reachedStage; | ||
|
||
if (!stageReached) return; | ||
|
||
const stageIndex = stagesInfos.findIndex((data) => data.id === stageReached.id); | ||
stagesInfos[stageIndex].value++; | ||
}); | ||
|
||
return participantsByStage; | ||
return stagesInfos; | ||
}; | ||
|
||
export { getCampaignParticipationsCountByStage }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters