Skip to content

Commit

Permalink
feat(api): add additionInfos on csv profile collection line
Browse files Browse the repository at this point in the history
  • Loading branch information
lionelB committed Sep 23, 2024
1 parent 116fe8b commit 670ce72
Show file tree
Hide file tree
Showing 8 changed files with 604 additions and 271 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,37 @@ const startWritingCampaignProfilesCollectionResultsToStream = async function ({
campaignParticipationRepository,
organizationRepository,
placementProfileService,
organizationFeatureApi,
organizationLearnerImportFormatRepository,
}) {
const campaign = await campaignRepository.get(campaignId);
const translate = i18n.__;
let additionalHeaders = [];

if (!campaign.isProfilesCollection()) {
throw new CampaignTypeError();
}

const organizationFeatures = await organizationFeatureApi.getAllFeaturesFromOrganization(campaign.organizationId);
if (organizationFeatures.hasLearnersImportFeature) {
const importFormat = await organizationLearnerImportFormatRepository.get(campaign.organizationId);
additionalHeaders = importFormat.exportableColumns;
}

const [allPixCompetences, organization, campaignParticipationResultDatas] = await Promise.all([
competenceRepository.listPixCompetencesOnly({ locale: i18n.getLocale() }),
organizationRepository.get(campaign.organizationId),
campaignParticipationRepository.findProfilesCollectionResultDataByCampaignId(campaign.id),
]);

const campaignProfilesCollectionExport = new CampaignProfilesCollectionExport(
writableStream,
const campaignProfilesCollectionExport = new CampaignProfilesCollectionExport({
outputStream: writableStream,
organization,
campaign,
allPixCompetences,
competences: allPixCompetences,
translate,
);
additionalHeaders,
});

// No return/await here, we need the writing to continue in the background
// after this function's returned promise resolves. If we await the map
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@ import * as csvSerializer from '../../../../../shared/infrastructure/serializers
const EMPTY_ARRAY = [];

class CampaignProfilesCollectionResultLine {
constructor(campaign, organization, campaignParticipationResult, competences, placementProfile, translate) {
constructor({
campaign,
organization,
campaignParticipationResult,
competences,
placementProfile,
translate,
additionalHeaders,
}) {
this.organization = organization;
this.campaign = campaign;
this.campaignParticipationResult = campaignParticipationResult;
this.competences = competences;
this.placementProfile = placementProfile;
this.translate = translate;
this.additionalHeaders = additionalHeaders;

this.notShared = translate('campaign-export.common.not-available');
}
Expand All @@ -25,6 +34,7 @@ class CampaignProfilesCollectionResultLine {
this.campaign.name,
this.campaignParticipationResult.participantLastName,
this.campaignParticipationResult.participantFirstName,
...this.#makeAdditionalInfos(),
...this._getGroupColumn(),
...this._getDivisionColumn(),
...this._getStudentNumberColumn(),
Expand All @@ -40,6 +50,12 @@ class CampaignProfilesCollectionResultLine {
return csvSerializer.serializeLine(line);
}

#makeAdditionalInfos() {
if (!this.additionalHeaders) return [];

return this.additionalHeaders.map((header) => this.campaignParticipationResult.additionalInfos[header.columnName]);
}

_getDivisionColumn() {
if (this.organization.isSco && this.organization.isManagingStudents) {
return [this.campaignParticipationResult.division || ''];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import { PromiseUtils } from '../../../../../shared/infrastructure/utils/promise
import { CampaignProfilesCollectionResultLine } from '../../exports/campaigns/campaign-profiles-collection-result-line.js';

class CampaignProfilesCollectionExport {
constructor(outputStream, organization, campaign, competences, translate) {
constructor({ outputStream, organization, campaign, competences, translate, additionalHeaders = [] }) {
this.stream = outputStream;
this.organization = organization;
this.campaign = campaign;
this.idPixLabel = campaign.idPixLabel;
this.competences = competences;
this.translate = translate;
this.additionalHeaders = additionalHeaders;
}

export(
Expand Down Expand Up @@ -50,13 +51,16 @@ class CampaignProfilesCollectionExport {
const displayGroup = this.organization.isSup && this.organization.isManagingStudents;
const displayDivision = this.organization.isSco && this.organization.isManagingStudents;

const extraHeaders = this.additionalHeaders.map((header) => header.columnName);

const header = [
this.translate('campaign-export.common.organization-name'),
this.translate('campaign-export.common.campaign-id'),
this.translate('campaign-export.common.campaign-code'),
this.translate('campaign-export.common.campaign-name'),
this.translate('campaign-export.common.participant-lastname'),
this.translate('campaign-export.common.participant-firstname'),
...extraHeaders,
displayGroup && this.translate('campaign-export.common.participant-group'),
displayDivision && this.translate('campaign-export.common.participant-division'),
displayStudentNumber && this.translate('campaign-export.common.participant-student-number'),
Expand Down Expand Up @@ -101,14 +105,15 @@ class CampaignProfilesCollectionExport {
return sameUserId && sameDate;
});

const line = new CampaignProfilesCollectionResultLine(
this.campaign,
this.organization,
campaignParticipationResultData,
this.competences,
const line = new CampaignProfilesCollectionResultLine({
campaign: this.campaign,
organization: this.organization,
campaignParticipationResult: campaignParticipationResultData,
additionalHeaders: this.additionalHeaders,
competences: this.competences,
placementProfile,
this.translate,
);
translate: this.translate,
});

return line.toCsvLine();
});
Expand Down
Loading

0 comments on commit 670ce72

Please sign in to comment.