Skip to content

Commit

Permalink
feat(api): return organization learners by divisions
Browse files Browse the repository at this point in the history
  • Loading branch information
Libouk authored and er-lim committed Oct 9, 2024
1 parent 09ff699 commit 8d9d893
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const getAttestationZipForDivisions = ({ organizationId, divisions, organizationLearnerRepository }) => {
const organizationLearners = organizationLearnerRepository.get;

return {};
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { knex } from '../../../../../db/knex-database-connection.js';
import { NotFoundError } from '../../../../../src/shared/domain/errors.js';
import { DomainTransaction } from '../../../../shared/domain/DomainTransaction.js';
import { filterByFullName } from '../../../../shared/infrastructure/utils/filter-utils.js';
import { fetchPage } from '../../../../shared/infrastructure/utils/knex-utils.js';
import { CampaignParticipationStatuses, CampaignTypes } from '../../../shared/domain/constants.js';
Expand Down Expand Up @@ -105,6 +106,15 @@ async function findPaginatedLearners({ organizationId, page, filter }) {
return { learners, pagination };
}

async function findOrganizationLearnersByDivisions({ organizationId, divisions }) {
const knexConnection = DomainTransaction.getConnection();
const organizationLearners = await knexConnection
.from('view-active-organization-learners')
.where({ organizationId })
.whereIn('division', divisions);
return organizationLearners.map((organizationLearner) => new OrganizationLearner(organizationLearner));
}

async function getAttestationsForOrganizationLearnersAndKey({ attestationKey, organizationLearners, attestationsApi }) {
const userIds = organizationLearners.map((learner) => learner.userId);
return attestationsApi.generateAttestations({
Expand All @@ -113,4 +123,9 @@ async function getAttestationsForOrganizationLearnersAndKey({ attestationKey, or
});
}

export { findPaginatedLearners, get, getAttestationsForOrganizationLearnersAndKey };
export {
findOrganizationLearnersByDivisions,
findPaginatedLearners,
get,
getAttestationsForOrganizationLearnersAndKey,
};
Original file line number Diff line number Diff line change
Expand Up @@ -641,4 +641,71 @@ describe('Integration | Infrastructure | Repository | Organization Learner', fun
});
});
});

describe('#findOrganizationLearnersByDivisions', function () {
let organizationId;

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

await databaseBuilder.commit();
});

it('should return organization learners', async function () {
// given
const learner1 = databaseBuilder.factory.buildOrganizationLearner({ organizationId, division: '6eme A' });
const learner2 = databaseBuilder.factory.buildOrganizationLearner({ organizationId, division: '6eme B' });

await databaseBuilder.commit();

// when
const learners = await organizationLearnerRepository.findOrganizationLearnersByDivisions({
organizationId,
divisions: ['6eme A', '6eme B'],
});

// then
expect(learners).to.deep.equal([new OrganizationLearner(learner1), new OrganizationLearner(learner2)]);
});

context('when there is no organization with organizationId', function () {
const notExistingOrganizationId = '999999';

it('should return an empty array', async function () {
// given
databaseBuilder.factory.buildOrganizationLearner({ organizationId, division: '6eme A' });
databaseBuilder.factory.buildOrganizationLearner({ organizationId, division: '6eme B' });

await databaseBuilder.commit();

// when
const result = await organizationLearnerRepository.findOrganizationLearnersByDivisions({
organizationId: notExistingOrganizationId,
divisions: ['6eme A', '6eme B'],
});

// then
expect(result).to.be.empty;
});
});
context('when there is no organizationLearners with these divisions', function () {
it('should return an empty array', async function () {
// given
databaseBuilder.factory.buildOrganizationLearner({ organizationId, division: '6eme A' });
databaseBuilder.factory.buildOrganizationLearner({ organizationId, division: '6eme B' });
const notExistingDivisions = ['6eme C', '6eme D'];

await databaseBuilder.commit();

// when
const result = await organizationLearnerRepository.findOrganizationLearnersByDivisions({
organizationId,
divisions: notExistingDivisions,
});

// then
expect(result).to.be.empty;
});
});
});
});

0 comments on commit 8d9d893

Please sign in to comment.