-
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 …
…(Revert)(PIX-8914) "
- Loading branch information
Showing
5 changed files
with
34 additions
and
199 deletions.
There are no files selected for viewing
44 changes: 14 additions & 30 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,62 +1,46 @@ | ||
// eslint-disable-next-line eslint-comments/disable-enable-pair | ||
/* eslint-disable import/no-restricted-paths */ | ||
import * as defaultStageRepository from '../../infrastructure/repositories/stage-repository.js'; | ||
import * as defaultStageAcquisitionRepository from '../../infrastructure/repositories/stage-acquisition-repository.js'; | ||
import * as stageCollectionRepository from '../../infrastructure/repositories/user-campaign-results/stage-collection-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, | ||
stageRepository = defaultStageRepository, | ||
stageAndStageAcquisitionComparisonService = defaultStageAndStageAcquisitionComparisonService, | ||
stageAcquisitionRepository = defaultStageAcquisitionRepository, | ||
campaignParticipationRepository, | ||
}) { | ||
if (!(await campaignRepository.checkIfUserOrganizationHasAccessToCampaign(campaignId, userId))) { | ||
throw new UserNotAuthorizedToAccessEntityError('User does not belong to the organization that owns the campaign'); | ||
} | ||
|
||
const stages = await stageRepository.getByCampaignId(campaignId); | ||
const stageCollection = await stageCollectionRepository.findStageCollection({ campaignId }); | ||
|
||
if (!stages.length) { | ||
if (!stageCollection.hasStage) { | ||
throw new NoStagesForCampaign(); | ||
} | ||
|
||
const stageAcquisitions = await stageAcquisitionRepository.getByCampaignId(campaignId); | ||
const participantsResults = await campaignParticipationRepository.getAllParticipationsByCampaignId(campaignId); | ||
|
||
const uniqueCampaignParticipationIds = [ | ||
...new Set(stageAcquisitions.map((stageAcquisition) => stageAcquisition.campaignParticipationId)), | ||
]; | ||
|
||
const stagesInfos = stages.map((stage, index) => ({ | ||
const participantsByStage = stageCollection.stages.map((stage, index) => ({ | ||
id: stage.id, | ||
value: 0, | ||
reachedStage: index + 1, | ||
totalStage: stages.length, | ||
totalStage: stageCollection.totalStages, | ||
title: stage.prescriberTitle, | ||
description: stage.prescriberDescription, | ||
})); | ||
|
||
uniqueCampaignParticipationIds.forEach((campaignParticipationId) => { | ||
const stageAcquisitionForCampaignParticipation = stageAcquisitions.filter( | ||
(stageAcquisition) => stageAcquisition.campaignParticipationId === campaignParticipationId, | ||
); | ||
|
||
const stagesInformation = stageAndStageAcquisitionComparisonService.compare( | ||
stages, | ||
stageAcquisitionForCampaignParticipation, | ||
participantsResults.forEach((participantResult) => { | ||
const stageReached = stageCollection.getReachedStage( | ||
participantResult.validatedSkillsCount, | ||
participantResult.masteryRate * 100, | ||
); | ||
|
||
const stageReached = stagesInformation.reachedStage; | ||
|
||
if (!stageReached) return; | ||
|
||
const stageIndex = stagesInfos.findIndex((data) => data.id === stageReached.id); | ||
stagesInfos[stageIndex].value++; | ||
const stageIndex = participantsByStage.findIndex((data) => data.id === stageReached.id); | ||
participantsByStage[stageIndex].value++; | ||
}); | ||
|
||
return stagesInfos; | ||
return participantsByStage; | ||
}; | ||
|
||
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