Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Supprimer les learners précédent l'ajout de l'import à format (Pix-15428) #10661

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ const attachChildOrganization = async function (request, h) {
};

const addOrganizationFeatureInBatch = async function (request, h) {
await usecases.addOrganizationFeatureInBatch({ filePath: request.payload.path });
await usecases.addOrganizationFeatureInBatch({
userId: request.auth.credentials.userId,
xav-car marked this conversation as resolved.
Show resolved Hide resolved
filePath: request.payload.path,
});
return h.response().code(204);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { createReadStream } from 'node:fs';

import { CsvColumn } from '../../../../lib/infrastructure/serializers/csv/csv-column.js';
import { getDataBuffer } from '../../../prescription/learner-management/infrastructure/utils/bufferize/get-data-buffer.js';
import { withTransaction } from '../../../shared/domain/DomainTransaction.js';
import { CsvParser } from '../../../shared/infrastructure/serializers/csv/csv-parser.js';
import { FeatureParamsNotProcessable } from '../errors.js';
import { OrganizationFeature } from '../models/OrganizationFeature.js';
Expand All @@ -30,28 +31,28 @@ const organizationFeatureCsvHeader = {
],
};

/**
* @param {Object} params - A parameter object.
* @param {string} params.featureId - feature id to add.
* @param {string} params.filePath - path of csv file wich contains organizations and params.
* @param {OrganizationFeatureRepository} params.organizationFeatureRepository - organizationRepository to use.
* @param {Object} params.dependencies
* @returns {Promise<void>}
*/
async function addOrganizationFeatureInBatch({ filePath, organizationFeatureRepository }) {
const stream = createReadStream(filePath);
const buffer = await getDataBuffer(stream);

const csvParser = new CsvParser(buffer, organizationFeatureCsvHeader);
const csvData = csvParser.parse();
const data = csvData.map(({ featureId, organizationId, params }) => {
try {
return new OrganizationFeature({ featureId, organizationId, params: params });
} catch (err) {
throw new FeatureParamsNotProcessable();
}
});
return organizationFeatureRepository.saveInBatch(data);
}
export const addOrganizationFeatureInBatch = withTransaction(
/**
* @param {Object} params - A parameter object.
* @param {Number} params.userId - user connected performing action
xav-car marked this conversation as resolved.
Show resolved Hide resolved
* @param {string} params.filePath - path of csv file wich contains organizations and params.
* @param {OrganizationFeatureRepository} params.organizationFeatureRepository - organizationRepository to use.
* @param {Object} params.dependencies
* @returns {Promise<void>}
*/
async ({ filePath, organizationFeatureRepository }) => {
lionelB marked this conversation as resolved.
Show resolved Hide resolved
const stream = createReadStream(filePath);
const buffer = await getDataBuffer(stream);

export { addOrganizationFeatureInBatch };
const csvParser = new CsvParser(buffer, organizationFeatureCsvHeader);
const csvData = csvParser.parse();
const data = csvData.map(({ featureId, organizationId, params }) => {
try {
return new OrganizationFeature({ featureId, organizationId, params: params });
} catch (err) {
throw new FeatureParamsNotProcessable();
}
});
return organizationFeatureRepository.saveInBatch(data);
},
);
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { FeatureParamsNotProcessable } from '../../../../../src/organizational-entities/domain/errors.js';
import { OrganizationFeature } from '../../../../../src/organizational-entities/domain/models/OrganizationFeature.js';
import { addOrganizationFeatureInBatch } from '../../../../../src/organizational-entities/domain/usecases/add-organization-feature-in-batch.js';
import { DomainTransaction } from '../../../../../src/shared/domain/DomainTransaction.js';
import { catchErr, createTempFile, expect, removeTempFile, sinon } from '../../../../test-helper.js';

describe('Unit | Domain | UseCases | add-organization-feature-in-batch', function () {
let organizationFeatureRepository, featureId, filePath, csvData;

beforeEach(function () {
sinon.stub(DomainTransaction, 'execute').callsFake((callback) => {
return callback();
});

featureId = 1;
csvData = [
new OrganizationFeature({ featureId, organizationId: 123, params: `{ "id": 123 }` }),
Expand Down