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,8 +1,8 @@
/**
* @module OrganizationFeatureRepository
*/
import { knex } from '../../../../db/knex-database-connection.js';
import * as knexUtils from '../../../../src/shared/infrastructure/utils/knex-utils.js';
import { DomainTransaction } from '../../../shared/domain/DomainTransaction.js';
import { AlreadyExistingOrganizationFeatureError, FeatureNotFound, OrganizationNotFound } from '../../domain/errors.js';
import { OrganizationFeatureItem } from '../../domain/models/OrganizationFeatureItem.js';

Expand All @@ -21,7 +21,8 @@ const DEFAULT_BATCH_SIZE = 100;
*/
async function saveInBatch(organizationFeatures, batchSize = DEFAULT_BATCH_SIZE) {
try {
await knex.batchInsert('organization-features', organizationFeatures, batchSize);
const knexConn = DomainTransaction.getConnection();
lionelB marked this conversation as resolved.
Show resolved Hide resolved
await knexConn.batchInsert('organization-features', organizationFeatures, batchSize);
} catch (err) {
if (knexUtils.isUniqConstraintViolated(err)) {
throw new AlreadyExistingOrganizationFeatureError();
Expand Down Expand Up @@ -51,7 +52,8 @@ async function saveInBatch(organizationFeatures, batchSize = DEFAULT_BATCH_SIZE)
* @returns {Promise<OrganizationFeatureItem>}
*/
async function findAllOrganizationFeaturesFromOrganizationId({ organizationId }) {
const organizationFeatures = await knex
const knexConn = DomainTransaction.getConnection();
const organizationFeatures = await knexConn
.select('key', 'params')
.from('organization-features')
.join('features', 'features.id', 'organization-features.featureId')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
UserCouldNotBeReconciledError,
} from '../../../../shared/domain/errors.js';
import { OrganizationLearner } from '../../../../shared/domain/models/index.js';
import { ApplicationTransaction } from '../../../shared/infrastructure/ApplicationTransaction.js';
import { CommonOrganizationLearner } from '../../domain/models/CommonOrganizationLearner.js';
import { OrganizationLearnerForAdmin } from '../../domain/read-models/OrganizationLearnerForAdmin.js';
import * as studentRepository from './student-repository.js';
Expand Down Expand Up @@ -163,7 +162,7 @@ function _shouldStudentToImportBeReconciled(
}

const saveCommonOrganizationLearners = function (learners) {
const knex = ApplicationTransaction.getConnection();
const knex = DomainTransaction.getConnection();

return Promise.all(
learners.map((learner) => {
Expand All @@ -182,7 +181,7 @@ const disableCommonOrganizationLearnersFromOrganizationId = function ({
organizationId,
excludeOrganizationLearnerIds = [],
}) {
const knex = ApplicationTransaction.getConnection();
const knex = DomainTransaction.getConnection();
return knex('organization-learners')
.where({ organizationId, isDisabled: false })
.whereNull('deletedAt')
Expand All @@ -191,7 +190,7 @@ const disableCommonOrganizationLearnersFromOrganizationId = function ({
};

const findAllCommonLearnersFromOrganizationId = async function ({ organizationId }) {
const knex = ApplicationTransaction.getConnection();
const knex = DomainTransaction.getConnection();

const existingLearners = await knex('view-active-organization-learners')
.select(['firstName', 'id', 'lastName', 'userId', 'organizationId', 'attributes'])
Expand All @@ -215,7 +214,7 @@ const findAllCommonOrganizationLearnerByReconciliationInfos = async function ({
organizationId,
reconciliationInformations,
}) {
const knex = ApplicationTransaction.getConnection();
const knex = DomainTransaction.getConnection();

const query = knex('view-active-organization-learners')
.select('firstName', 'lastName', 'id', 'attributes', 'userId')
Expand All @@ -234,7 +233,7 @@ const findAllCommonOrganizationLearnerByReconciliationInfos = async function ({
};

const update = async function (organizationLearner) {
const knex = ApplicationTransaction.getConnection();
const knex = DomainTransaction.getConnection();

const { id, ...attributes } = organizationLearner;
const updatedRows = await knex('organization-learners').update(attributes).where({ id });
Expand Down Expand Up @@ -303,6 +302,18 @@ const reconcileUserToOrganizationLearner = async function ({ userId, organizatio
}
};

/**
* @function
* @name findOrganizationLearnerIdsBeforeImportFeatureFromOrganizationId
* @param {Object} params
* @param {number} params.organizationId
* @returns {Promise<number[]>}
*/
const findOrganizationLearnerIdsBeforeImportFeatureFromOrganizationId = async function ({ organizationId }) {
const knexConn = DomainTransaction.getConnection();
return knexConn('view-active-organization-learners').where({ organizationId }).whereNull('attributes').pluck('id');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question Est ce que ne devrait pas renvoyer des objets du model OrganizationLearner ?

};

export {
addOrUpdateOrganizationOfOrganizationLearners,
countByUserId,
Expand All @@ -312,6 +323,7 @@ export {
findAllCommonLearnersFromOrganizationId,
findAllCommonOrganizationLearnerByReconciliationInfos,
findByUserId,
findOrganizationLearnerIdsBeforeImportFeatureFromOrganizationId,
findOrganizationLearnerIdsByOrganizationId,
getOrganizationLearnerForAdmin,
reconcileUserByNationalStudentIdAndOrganizationId,
Expand Down
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import _ from 'lodash';

import { DomainTransaction } from '../../../../../../lib/infrastructure/DomainTransaction.js';
import * as organizationLearnerRepository from '../../../../../../lib/infrastructure/repositories/organization-learner-repository.js';
import { CommonOrganizationLearner } from '../../../../../../src/prescription/learner-management/domain/models/CommonOrganizationLearner.js';
import { OrganizationLearnerForAdmin } from '../../../../../../src/prescription/learner-management/domain/read-models/OrganizationLearnerForAdmin.js';
Expand All @@ -11,6 +10,7 @@ import {
disableCommonOrganizationLearnersFromOrganizationId,
findAllCommonLearnersFromOrganizationId,
findAllCommonOrganizationLearnerByReconciliationInfos,
findOrganizationLearnerIdsBeforeImportFeatureFromOrganizationId,
findOrganizationLearnerIdsByOrganizationId,
getOrganizationLearnerForAdmin,
reconcileUserByNationalStudentIdAndOrganizationId,
Expand All @@ -19,7 +19,7 @@ import {
saveCommonOrganizationLearners,
update,
} from '../../../../../../src/prescription/learner-management/infrastructure/repositories/organization-learner-repository.js';
import { ApplicationTransaction } from '../../../../../../src/prescription/shared/infrastructure/ApplicationTransaction.js';
import { DomainTransaction } from '../../../../../../src/shared/domain/DomainTransaction.js';
import {
NotFoundError,
OrganizationLearnersCouldNotBeSavedError,
Expand Down Expand Up @@ -1087,7 +1087,7 @@ describe('Integration | Repository | Organization Learner Management | Organizat
});

try {
await ApplicationTransaction.execute(async () => {
await DomainTransaction.execute(async () => {
await saveCommonOrganizationLearners([learnerSacha]);
throw new Error();
});
Expand Down Expand Up @@ -1243,7 +1243,7 @@ describe('Integration | Repository | Organization Learner Management | Organizat
await databaseBuilder.commit();

try {
await ApplicationTransaction.execute(async () => {
await DomainTransaction.execute(async () => {
await disableCommonOrganizationLearnersFromOrganizationId({ organizationId });
throw new Error();
});
Expand Down Expand Up @@ -1953,4 +1953,90 @@ describe('Integration | Repository | Organization Learner Management | Organizat
});
});
});

describe('#findOrganizationLearnerIdsBeforeImportFeatureFromOrganizationId ', function () {
let organizationId, organizationLearnerIdOfYesYes;

beforeEach(async function () {
organizationId = databaseBuilder.factory.buildOrganization().id;

organizationLearnerIdOfYesYes =
databaseBuilder.factory.prescription.organizationLearners.buildOrganizationLearner({
organizationId,
firstName: 'Oui',
lastName: 'Oui',
userId: null,
attributes: null,
}).id;

await databaseBuilder.commit();
});

it('should return an array of organization learner id given organizationId', async function () {
const otherOrganizationLearnerId =
databaseBuilder.factory.prescription.organizationLearners.buildOrganizationLearner({
organizationId,
firstName: 'Non',
lastName: 'Non',
userId: null,
attributes: null,
}).id;

await databaseBuilder.commit();

const results = await findOrganizationLearnerIdsBeforeImportFeatureFromOrganizationId({ organizationId });

expect([organizationLearnerIdOfYesYes, otherOrganizationLearnerId]).to.be.deep.members(results);
});

it('should not return organization learners from other organization', async function () {
databaseBuilder.factory.prescription.organizationLearners.buildOrganizationLearner({
organizationId: databaseBuilder.factory.buildOrganization().id,
firstName: 'Non',
lastName: 'Non',
userId: null,
attributes: null,
}).id;

await databaseBuilder.commit();

const results = await findOrganizationLearnerIdsBeforeImportFeatureFromOrganizationId({ organizationId });

expect([organizationLearnerIdOfYesYes]).to.be.deep.members(results);
});

it('should not return organization learners already deleted', async function () {
databaseBuilder.factory.prescription.organizationLearners.buildOrganizationLearner({
organizationId,
firstName: 'Non',
lastName: 'Non',
userId: null,
attributes: null,
deletedAt: new Date('2020-01-01'),
}).id;

await databaseBuilder.commit();

const results = await findOrganizationLearnerIdsBeforeImportFeatureFromOrganizationId({ organizationId });

expect([organizationLearnerIdOfYesYes]).to.be.deep.members(results);
});

it('should not return organization learners with attributes', async function () {
databaseBuilder.factory.prescription.organizationLearners.buildOrganizationLearner({
organizationId,
firstName: 'Non',
lastName: 'Non',
userId: null,
attributes: { test: 'toto' },
deletedAt: null,
}).id;

await databaseBuilder.commit();

const results = await findOrganizationLearnerIdsBeforeImportFeatureFromOrganizationId({ organizationId });

expect([organizationLearnerIdOfYesYes]).to.be.deep.members(results);
});
});
});