Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
lionelB committed Sep 23, 2024
1 parent 116fe8b commit 86ec06a
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,23 @@ 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),
Expand All @@ -36,6 +45,7 @@ const startWritingCampaignProfilesCollectionResultsToStream = async function ({
campaign,
allPixCompetences,
translate,
additionalHeaders,
);

// No return/await here, we need the writing to continue in the background
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, additionnalHeaders = [] }) {
this.stream = outputStream;
this.organization = organization;
this.campaign = campaign;
this.idPixLabel = campaign.idPixLabel;
this.competences = competences;
this.translate = translate;
this.additionnalHeaders = additionnalHeaders;
}

export(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import * as userRepository from '../../../../../../src/identity-access-managemen
import { startWritingCampaignProfilesCollectionResultsToStream } from '../../../../../../src/prescription/campaign/domain/usecases/start-writing-campaign-profiles-collection-results-to-stream.js';
import * as campaignParticipationRepository from '../../../../../../src/prescription/campaign-participation/infrastructure/repositories/campaign-participation-repository.js';
import { CampaignParticipationStatuses } from '../../../../../../src/prescription/shared/domain/constants.js';
import { MAX_REACHABLE_LEVEL, MAX_REACHABLE_PIX_BY_COMPETENCE } from '../../../../../../src/shared/domain/constants.js';
import {
MAX_REACHABLE_LEVEL,
MAX_REACHABLE_PIX_BY_COMPETENCE,
ORGANIZATION_FEATURE,
} from '../../../../../../src/shared/domain/constants.js';
import * as placementProfileService from '../../../../../../src/shared/domain/services/placement-profile-service.js';
import * as competenceRepository from '../../../../../../src/shared/infrastructure/repositories/competence-repository.js';
import * as organizationRepository from '../../../../../../src/shared/infrastructure/repositories/organization-repository.js';
Expand Down Expand Up @@ -188,6 +192,74 @@ describe('Integration | Domain | Use Cases | start-writing-profiles-collection-c
`"Observatoire de Pix";${campaign.id};"QWERTY456";"'@Campagne de Test N°2";"'=Bono";"'@Jean";"'+Mon mail pro";"Non";"NA";"NA";"NA";"NA";"NA";"NA";"NA";"NA"`,
);
});

context('extra columns', function () {
beforeEach(async function () {
// Import Configuration
const importConfig = {
name: 'MY_TEST_EXPORT',
fileType: 'csv',
config: {
acceptedEncoding: ['utf-8'],
unicityColumns: ['my_column1'],
validationRules: {
formats: [
{ name: 'my_column1', type: 'string' },
{ name: 'my_column2', type: 'string' },
],
},
headers: [
{ name: 'my_column1', required: true, property: 'lastName' },
{ name: 'my_column2', required: true, property: 'firstName' },
{ name: 'hobby', required: true, config: { exportable: true } },
],
},
};
const feature = databaseBuilder.factory.buildFeature({
key: ORGANIZATION_FEATURE.LEARNER_IMPORT.key,
});
const organizationLearnerImportFormatId =
databaseBuilder.factory.buildOrganizationLearnerImportFormat(importConfig).id;

databaseBuilder.factory.buildOrganizationFeature({
featureId: feature.id,
organizationId: organization.id,
params: { organizationLearnerImportFormatId },
});
participant = databaseBuilder.factory.buildUser();
organizationLearner = databaseBuilder.factory.prescription.organizationLearners.buildOrganizationLearner({
firstName: '@Jean',
lastName: '=Bono',
organizationId: organization.id,
userId: participant.id,
attributes: { hobby: 'genky', sleep: '8h' },
});
await databaseBuilder.commit();
});
it('should return extra columns', async function () {
startWritingCampaignProfilesCollectionResultsToStream({
campaignId: campaign.id,
writableStream,
i18n,
campaignRepository,
userRepository,
competenceRepository,
organizationRepository,
campaignParticipationRepository,
placementProfileService,
});

const csv = await csvPromise;
const cells = csv.split('\n');

expect(cells[0]).to.be.equals(
'\uFEFF"Nom de l\'organisation";"ID Campagne";"Code";"Nom de la campagne";"Nom du Participant";"Prénom du Participant";"hobby";"Mail Perso";"Envoi (O/N)";"Date de l\'envoi";"Nombre de pix total";"Certifiable (O/N)";"Nombre de compétences certifiables";"Niveau pour la compétence ";"Nombre de pix pour la compétence ";"Niveau pour la compétence ";"Nombre de pix pour la compétence "',
);
expect(cells[1]).to.be.equals(
`"Observatoire de Pix";${campaign.id};"QWERTY456";"'@Campagne de Test N°2";"'=Bono";"'@Jean";"genky";"'+Mon mail pro";"Oui";2019-03-01;52;"Non";2;1;12;5;40`,
);
});
});
});

context('When the organization is PRO', function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ describe('Unit | Serializer | CSV | campaign-profiles-collection-export', functi
const campaign = {};
const competences = [];
const translateStub = sinon.stub();
const exporter = new CampaignProfilesCollectionExport(
noOpStream,
const additionnalHeaders = [{ columnName: 'Classe' }];
const exporter = new CampaignProfilesCollectionExport({
outputStream: noOpStream,
organization,
campaign,
competences,
translateStub,
);
translate: translateStub,
additionnalHeaders,
});
const placementProfileStub = {
getPlacementProfilesWithSnapshotting: sinon.stub().resolves([]),
};
Expand All @@ -35,6 +37,7 @@ describe('Unit | Serializer | CSV | campaign-profiles-collection-export', functi
division: '3C',
pixScore: 136,
group: null,
addtionnalInfos: { Classe: 'CM1' },
},
{
id: 2,
Expand All @@ -50,6 +53,7 @@ describe('Unit | Serializer | CSV | campaign-profiles-collection-export', functi
division: '3C',
pixScore: 200,
group: null,
addtionnalInfos: { Classe: 'CM2' },
},
];

Expand All @@ -62,6 +66,9 @@ describe('Unit | Serializer | CSV | campaign-profiles-collection-export', functi
// then
expect(noOpStream.write.getCall(1).args[0]).to.includes('John');
expect(noOpStream.write.getCall(2).args[0]).to.includes('Jane');
// check additionnalInfos
expect(noOpStream.write.getCall(1).args[0]).to.includes('CM1');
expect(noOpStream.write.getCall(2).args[0]).to.includes('CM2');
});
});
});

0 comments on commit 86ec06a

Please sign in to comment.