Skip to content

Commit

Permalink
chore(api): remove batchCreateOrganizations which is now useless
Browse files Browse the repository at this point in the history
Co-authored-by: <aurelie.crouilleboix@pix.fr>
Co-authored-by: <yaelle.marsiac@pix.fr>
Co-authored-by: <gwenvael.laskar@pix.fr>
  • Loading branch information
HEYGUL committed Jul 7, 2024
1 parent 64fd467 commit 271b8af
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 212 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import bluebird from 'bluebird';
import _ from 'lodash';

import { knex } from '../../../../db/knex-database-connection.js';
import { DomainTransaction } from '../../../../lib/infrastructure/DomainTransaction.js';
import { Organization } from '../../../organizational-entities/domain/models/Organization.js';
import { Tag } from '../../../organizational-entities/domain/models/Tag.js';
import { NotFoundError } from '../../domain/errors.js';
import { CONCURRENCY_HEAVY_OPERATIONS } from '../constants.js';
import { fetchPage } from '../utils/knex-utils.js';

const ORGANIZATIONS_TABLE_NAME = 'organizations';
Expand Down Expand Up @@ -75,56 +72,6 @@ const create = function (organization) {
.then(([organization]) => _toDomain(organization));
};

// Uses OrganizationForAdmin to centralize features enablement by organization type
const batchCreateOrganizations = async function (
organizations,
domainTransaction = DomainTransaction.emptyTransaction(),
) {
const knexConn = domainTransaction.knexTransaction ?? knex;
const featuresByKey = _.keyBy(await knexConn('features'), (feature) => feature.key);

return bluebird.map(
organizations,
async (organizationCsvData) => {
const { organization } = organizationCsvData;
const [createdOrganization] = await knexConn(ORGANIZATIONS_TABLE_NAME)
.insert(
_.pick(organization, [
'name',
'type',
'email',
'externalId',
'provinceCode',
'isManagingStudents',
'identityProviderForCampaigns',
'credit',
'createdBy',
'documentationUrl',
]),
)
.returning('*');

const enabledFeatures = _.keys(organization.features).filter((key) => organization.features[key] === true);

for (const featureKey of enabledFeatures) {
const feature = featuresByKey[featureKey];
await knexConn('organization-features').insert({
organizationId: createdOrganization.id,
featureId: feature.id,
});
}

return {
createdOrganization,
organizationToCreate: organizationCsvData,
};
},
{
concurrency: CONCURRENCY_HEAVY_OPERATIONS,
},
);
};

const update = async function (organization) {
const organizationRawData = _.pick(organization, [
'name',
Expand Down Expand Up @@ -243,7 +190,6 @@ const findPaginatedFilteredByTargetProfile = async function ({ targetProfileId,
};

export {
batchCreateOrganizations,
create,
findActiveScoOrganizationsByExternalId,
findByExternalIdsFetchingIdsOnly,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import _ from 'lodash';

import { Organization, OrganizationForAdmin } from '../../../../lib/domain/models/index.js';
import { Organization } from '../../../../lib/domain/models/index.js';
import { NON_OIDC_IDENTITY_PROVIDERS } from '../../../../src/identity-access-management/domain/constants/identity-providers.js';
import { ORGANIZATION_FEATURE } from '../../../../src/shared/domain/constants.js';
import { NotFoundError } from '../../../../src/shared/domain/errors.js';
import * as organizationRepository from '../../../../src/shared/infrastructure/repositories/organization-repository.js';
import { catchErr, databaseBuilder, domainBuilder, expect, knex } from '../../../test-helper.js';
Expand Down Expand Up @@ -1027,159 +1026,4 @@ describe('Integration | Repository | Organization', function () {
});
});
});

describe('#batchCreateOrganizations', function () {
let computeOrganizationLearnerCertificabilityId;

beforeEach(async function () {
computeOrganizationLearnerCertificabilityId = databaseBuilder.factory.buildFeature(
ORGANIZATION_FEATURE.COMPUTE_ORGANIZATION_LEARNER_CERTIFICABILITY,
).id;
await databaseBuilder.commit();
});

it('should add rows in the table "organizations"', async function () {
// given
const organization1 = domainBuilder.buildOrganizationForAdmin();
const organization2 = domainBuilder.buildOrganizationForAdmin();

// when
await organizationRepository.batchCreateOrganizations([
{ organization: organization1 },
{ organization: organization2 },
]);

// then
const foundOrganizations = await knex('organizations').select();
expect(foundOrganizations.length).to.equal(2);
});

it('should save organization attributes', async function () {
// given
const userId = databaseBuilder.factory.buildUser().id;
await databaseBuilder.commit();

const organization = domainBuilder.buildOrganizationForAdmin({
id: null,
externalId: '1237457A',
name: 'Orga 1',
provinceCode: '12',
credit: 100,
createdBy: userId,
documentationUrl: 'http://example.net/',
});

// when
await organizationRepository.batchCreateOrganizations([{ organization }]);

// then
const foundOrganizations = await knex('organizations').select();
expect(foundOrganizations[0].id).to.not.be.undefined;
expect(foundOrganizations[0].name).to.equal(organization.name);
expect(foundOrganizations[0].externalId).to.equal(organization.externalId);
expect(foundOrganizations[0].provinceCode).to.equal(organization.provinceCode);
expect(foundOrganizations[0].createdBy).to.equal(organization.createdBy);
expect(foundOrganizations[0].documentationUrl).to.equal(organization.documentationUrl);
});

context('when organization is added', function () {
it('returns organization data to create and created organization', async function () {
// given
const userId = databaseBuilder.factory.buildUser().id;
const organizationFormattedCsvData = {
organization: domainBuilder.buildOrganizationForAdmin({
id: null,
externalId: '1237457A',
name: 'Orga 1',
createdBy: userId,
}),
};
const organizationsToCreate = [organizationFormattedCsvData];

await databaseBuilder.commit();

// when
const createdOrganizations = await organizationRepository.batchCreateOrganizations(organizationsToCreate);

// then
expect(createdOrganizations).to.have.lengthOf(1);

const { createdOrganization, organizationToCreate } = createdOrganizations[0];
expect(organizationToCreate).to.include(organizationFormattedCsvData);
expect(createdOrganization).to.include({
id: createdOrganization.id,
externalId: '1237457A',
name: 'Orga 1',
createdBy: userId,
});
});
});

it('should enable compute organization learner certificability feature for sco organization managing students', async function () {
// given
const superAdminUserId = databaseBuilder.factory.buildUser.withRole().id;
await databaseBuilder.commit();
const otherOrganization = new OrganizationForAdmin({
name: 'other organization',
type: 'SUP',
isManagingStudents: false,
createdBy: superAdminUserId,
});
const organizationScoManagingStudent = new OrganizationForAdmin({
name: 'Organization SCO',
type: 'SCO',
isManagingStudents: true,
createdBy: superAdminUserId,
});

organizationScoManagingStudent.features[ORGANIZATION_FEATURE.COMPUTE_ORGANIZATION_LEARNER_CERTIFICABILITY.key] =
organizationScoManagingStudent.isManagingStudents;

// when
await organizationRepository.batchCreateOrganizations([
{
organization: organizationScoManagingStudent,
},
{
organization: otherOrganization,
},
]);

const savedOrganizationFeatures = await knex('organization-features');

// then
expect(savedOrganizationFeatures.length).to.equal(1);
expect(savedOrganizationFeatures[0].featureId).to.equal(computeOrganizationLearnerCertificabilityId);
});

it('should associate organizations to enabled features', async function () {
// given
const missionManagementFeatureId = databaseBuilder.factory.buildFeature(
ORGANIZATION_FEATURE.MISSIONS_MANAGEMENT,
).id;
const superAdminUserId = databaseBuilder.factory.buildUser.withRole().id;
await databaseBuilder.commit();
const features = {};
features[ORGANIZATION_FEATURE.COMPUTE_ORGANIZATION_LEARNER_CERTIFICABILITY.key] = false;
features[ORGANIZATION_FEATURE.MISSIONS_MANAGEMENT.key] = true;
const organization = new OrganizationForAdmin({
name: 'Organization',
type: 'PRO',
features,
createdBy: superAdminUserId,
});

// when
await organizationRepository.batchCreateOrganizations([{ organization }]);

const savedOrganizationFeatures = await knex('organization-features');
const savedOrganizations = await knex('organizations');

// then
expect(savedOrganizationFeatures.length).to.equal(1);
expect(savedOrganizationFeatures[0].featureId).to.equal(missionManagementFeatureId);
expect(savedOrganizations.length).to.equal(1);
expect(savedOrganizationFeatures[0].organizationId).to.equal(savedOrganizations[0].id);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ describe('Unit | UseCase | create-organizations-with-tags-and-target-profiles',
beforeEach(function () {
organizationRepositoryStub = {
findByExternalIdsFetchingIdsOnly: sinon.stub(),
batchCreateOrganizations: sinon.stub(),
};
organizationForAdminRepositoryStub = {
save: sinon.stub(),
Expand Down

0 comments on commit 271b8af

Please sign in to comment.