Skip to content

Commit 1022051

Browse files
pix-service-auto-mergeHEYGUL
authored andcommitted
[TECH] Migration du usecase computeCertificabilitydans le BC Prescription (PIX-15342)
#10693
2 parents b85b551 + 5b2bf9d commit 1022051

File tree

21 files changed

+167
-157
lines changed

21 files changed

+167
-157
lines changed

Diff for: api/lib/domain/usecases/generate-username-with-temporary-password.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ const generateUsernameWithTemporaryPassword = async function ({
1313
userService,
1414
authenticationMethodRepository,
1515
userRepository,
16-
organizationLearnerRepository,
16+
prescriptionOrganizationLearnerRepository,
1717
}) {
18-
const organizationLearner = await organizationLearnerRepository.get(organizationLearnerId);
18+
const organizationLearner = await prescriptionOrganizationLearnerRepository.getLearnerInfo(organizationLearnerId);
1919
_checkIfStudentHasAccessToOrganization(organizationLearner, organizationId);
2020

2121
const studentAccount = await userRepository.get(organizationLearner.userId);

Diff for: api/lib/domain/usecases/update-organization-learner-dependent-user-password.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ const updateOrganizationLearnerDependentUserPassword = async function ({
1111
cryptoService,
1212
passwordGenerator,
1313
authenticationMethodRepository,
14-
organizationLearnerRepository,
14+
prescriptionOrganizationLearnerRepository,
1515
userRepository,
1616
}) {
1717
const userWithMemberships = await userRepository.getWithMemberships(userId);
18-
const organizationLearner = await organizationLearnerRepository.get(organizationLearnerId);
18+
const organizationLearner = await prescriptionOrganizationLearnerRepository.getLearnerInfo(organizationLearnerId);
1919

2020
if (
2121
!userWithMemberships.hasAccessToOrganization(organizationId) ||

Diff for: api/lib/infrastructure/repositories/organization-learner-repository.js

+1-33
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import { knex } from '../../../db/knex-database-connection.js';
22
import { ORGANIZATION_FEATURE } from '../../../src/shared/domain/constants.js';
3-
import {
4-
NotFoundError,
5-
OrganizationLearnerCertificabilityNotUpdatedError,
6-
OrganizationLearnerNotFound,
7-
UserNotFoundError,
8-
} from '../../../src/shared/domain/errors.js';
3+
import { OrganizationLearnerNotFound, UserNotFoundError } from '../../../src/shared/domain/errors.js';
94
import { OrganizationLearner } from '../../../src/shared/domain/models/OrganizationLearner.js';
105
import { ParticipantRepartition } from '../../../src/shared/domain/models/ParticipantRepartition.js';
116
import { fetchPage } from '../../../src/shared/infrastructure/utils/knex-utils.js';
@@ -93,19 +88,6 @@ function _queryBuilderDissociation(knexConn) {
9388
});
9489
}
9590

96-
const get = async function (organizationLearnerId) {
97-
const organizationLearner = await knex
98-
.select('*')
99-
.from('view-active-organization-learners')
100-
.where({ id: organizationLearnerId })
101-
.first();
102-
103-
if (!organizationLearner) {
104-
throw new NotFoundError(`Student not found for ID ${organizationLearnerId}`);
105-
}
106-
return new OrganizationLearner(organizationLearner);
107-
};
108-
10991
const getLatestOrganizationLearner = async function ({ nationalStudentId, birthdate }) {
11092
const organizationLearner = await knex
11193
.where({ nationalStudentId, birthdate })
@@ -148,18 +130,6 @@ const isActive = async function ({ userId, campaignId }) {
148130
return !learner?.isDisabled;
149131
};
150132

151-
async function updateCertificability(organizationLearner) {
152-
const result = await knex('organization-learners').where({ id: organizationLearner.id }).update({
153-
isCertifiable: organizationLearner.isCertifiable,
154-
certifiableAt: organizationLearner.certifiableAt,
155-
});
156-
if (result === 0) {
157-
throw new OrganizationLearnerCertificabilityNotUpdatedError(
158-
`Could not update certificability for OrganizationLearner with ID ${organizationLearner.id}.`,
159-
);
160-
}
161-
}
162-
163133
async function countByOrganizationsWhichNeedToComputeCertificability({
164134
skipLoggedLastDayCheck = false,
165135
onlyNotComputed = false,
@@ -298,9 +268,7 @@ export {
298268
findByOrganizationIdAndUpdatedAtOrderByDivision,
299269
findByOrganizationsWhichNeedToComputeCertificability,
300270
findByUserId,
301-
get,
302271
getLatestOrganizationLearner,
303272
isActive,
304-
updateCertificability,
305273
updateUserIdWhereNull,
306274
};

Diff for: api/src/identity-access-management/domain/usecases/get-account-recovery-details.usecase.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @param {{
33
* temporaryKey: string,
44
* accountRecoveryDemandRepository: AccountRecoveryDemandRepository,
5-
* organizationLearnerRepository: OrganizationLearnerRepository,
5+
* prescriptionOrganizationLearnerRepository: PrescriptionOrganizationLearnerRepository,
66
* userRepository: UserRepository,
77
* scoAccountRecoveryService: ScoAccountRecoveryService,
88
* }} params
@@ -11,7 +11,7 @@
1111
export const getAccountRecoveryDetails = async function ({
1212
temporaryKey,
1313
accountRecoveryDemandRepository,
14-
organizationLearnerRepository,
14+
prescriptionOrganizationLearnerRepository,
1515
userRepository,
1616
scoAccountRecoveryService,
1717
}) {
@@ -22,7 +22,7 @@ export const getAccountRecoveryDetails = async function ({
2222
userRepository,
2323
});
2424

25-
const { firstName } = await organizationLearnerRepository.get(organizationLearnerId);
25+
const { firstName } = await prescriptionOrganizationLearnerRepository.getLearnerInfo(organizationLearnerId);
2626

2727
return {
2828
id,

Diff for: api/src/identity-access-management/domain/usecases/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import * as campaignRepository from '../../../../lib/infrastructure/repositories
1010
import * as organizationLearnerRepository from '../../../../lib/infrastructure/repositories/organization-learner-repository.js';
1111
import * as userRecommendedTrainingRepository from '../../../devcomp/infrastructure/repositories/user-recommended-training-repository.js';
1212
import { repositories as campaignRepositories } from '../../../prescription/campaign/infrastructure/repositories/index.js';
13+
import * as prescriptionOrganizationLearnerRepository from '../../../prescription/learner-management/infrastructure/repositories/organization-learner-repository.js';
1314
import { config } from '../../../shared/config.js';
1415
import { cryptoService } from '../../../shared/domain/services/crypto-service.js';
1516
import { tokenService } from '../../../shared/domain/services/token-service.js';
@@ -59,6 +60,7 @@ const repositories = {
5960
membershipRepository,
6061
oidcProviderRepository,
6162
organizationLearnerRepository,
63+
prescriptionOrganizationLearnerRepository,
6264
privacyUsersApiRepository,
6365
refreshTokenRepository,
6466
resetPasswordDemandRepository,

Diff for: api/src/prescription/learner-management/application/jobs/compute-certificability-job-controller.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { usecases } from '../../../../../lib/domain/usecases/index.js';
1+
import { usecases } from '../../../../../src/prescription/learner-management/domain/usecases/index.js';
22
import { JobController } from '../../../../shared/application/jobs/job-controller.js';
33
import { ComputeCertificabilityJob } from '../../domain/models/ComputeCertificabilityJob.js';
44

Diff for: api/src/prescription/learner-management/domain/errors.js

+7
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,17 @@ class CouldNotDeleteLearnersError extends DomainError {
4747
}
4848
}
4949

50+
class OrganizationLearnerCertificabilityNotUpdatedError extends DomainError {
51+
constructor(message) {
52+
super(message);
53+
}
54+
}
55+
5056
export {
5157
AggregateImportError,
5258
CouldNotDeleteLearnersError,
5359
OrganizationDoesNotHaveFeatureEnabledError,
60+
OrganizationLearnerCertificabilityNotUpdatedError,
5461
OrganizationLearnerImportFormatNotFoundError,
5562
OrganizationLearnersCouldNotBeSavedError,
5663
ReconcileCommonOrganizationLearnerError,

Diff for: api/lib/domain/usecases/compute-organization-learner-certificability.js renamed to api/src/prescription/learner-management/domain/usecases/compute-organization-learner-certificability.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const computeOrganizationLearnerCertificability = async function ({
33
organizationLearnerRepository,
44
placementProfileService,
55
}) {
6-
const organizationLearner = await organizationLearnerRepository.get(organizationLearnerId);
6+
const organizationLearner = await organizationLearnerRepository.getLearnerInfo(organizationLearnerId);
77

88
const placementProfile = await placementProfileService.getPlacementProfile({
99
userId: organizationLearner.userId,

Diff for: api/src/prescription/learner-management/domain/usecases/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as campaignRepository from '../../../../../lib/infrastructure/repositor
77
import * as libOrganizationLearnerRepository from '../../../../../lib/infrastructure/repositories/organization-learner-repository.js';
88
import * as userRepository from '../../../../identity-access-management/infrastructure/repositories/user.repository.js';
99
import * as organizationFeatureApi from '../../../../organizational-entities/application/api/organization-features-api.js';
10+
import * as placementProfileService from '../../../../shared/domain/services/placement-profile-service.js';
1011
import { logErrorWithCorrelationIds } from '../../../../shared/infrastructure/monitoring-tools.js';
1112
import * as organizationRepository from '../../../../shared/infrastructure/repositories/organization-repository.js';
1213
import { injectDependencies } from '../../../../shared/infrastructure/utils/dependency-injection.js';
@@ -46,6 +47,7 @@ import { importStorage } from '../../infrastructure/storage/import-storage.js';
4647
* @typedef {import ('../../infrastructure/repositories/organization-learner-import-format-repository.js')} OrganizationLearnerImportFormatRepository
4748
* @typedef {import ('../../infrastructure/repositories/organization-learner-repository.js')} OrganizationLearnerRepository
4849
* @typedef {import ('../../../../shared/infrastructure/repositories/organization-repository.js')} OrganizationRepository
50+
* @typedef {import ('../../../../shared/domain/services/placement-profile-service.js')} placementProfileService
4951
* @typedef {import('../../../organization-learner/infrastructure/repositories/registration-organization-learner-repository.js')} registrationOrganizationLearnerRepository
5052
* @typedef {import ('../../infrastructure/repositories/student-repository.js')} studentRepository
5153
* @typedef {import ('../../infrastructure/repositories/sup-organization-learner-repository.js')} SupOrganizationLearnerRepository
@@ -72,6 +74,7 @@ const dependencies = {
7274
organizationLearnerImportFormatRepository,
7375
organizationLearnerRepository,
7476
organizationRepository,
77+
placementProfileService,
7578
registrationOrganizationLearnerRepository,
7679
studentRepository,
7780
supOrganizationLearnerRepository,
@@ -96,6 +99,7 @@ const usecasesWithoutInjectedDependencies = {
9699

97100
/**
98101
* @typedef PrescriptionLearnerManagementUsecases
102+
* @property {computeOrganizationLearnerCertificability} computeOrganizationLearnerCertificability
99103
* @property {saveOrganizationLearnersFile} saveOrganizationLearnersFile
100104
* @property {sendOrganizationLearnersFile} sendOrganizationLearnersFile
101105
* @property {validateOrganizationLearnersFile} validateOrganizationLearnersFile

Diff for: api/src/prescription/learner-management/infrastructure/repositories/organization-learner-repository.js

+31
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
UserCouldNotBeReconciledError,
99
} from '../../../../shared/domain/errors.js';
1010
import { OrganizationLearner } from '../../../../shared/domain/models/index.js';
11+
import { OrganizationLearnerCertificabilityNotUpdatedError } from '../../domain/errors.js';
1112
import { CommonOrganizationLearner } from '../../domain/models/CommonOrganizationLearner.js';
1213
import { OrganizationLearnerForAdmin } from '../../domain/read-models/OrganizationLearnerForAdmin.js';
1314
import * as studentRepository from './student-repository.js';
@@ -302,6 +303,34 @@ const reconcileUserToOrganizationLearner = async function ({ userId, organizatio
302303
}
303304
};
304305

306+
async function updateCertificability(organizationLearner) {
307+
const knexConn = DomainTransaction.getConnection();
308+
const result = await knexConn('organization-learners').where({ id: organizationLearner.id }).update({
309+
isCertifiable: organizationLearner.isCertifiable,
310+
certifiableAt: organizationLearner.certifiableAt,
311+
});
312+
if (result === 0) {
313+
throw new OrganizationLearnerCertificabilityNotUpdatedError(
314+
`Could not update certificability for OrganizationLearner with ID ${organizationLearner.id}.`,
315+
);
316+
}
317+
}
318+
319+
async function getLearnerInfo(organizationLearnerId) {
320+
const knexConn = DomainTransaction.getConnection();
321+
322+
const organizationLearner = await knexConn
323+
.select('*')
324+
.from('view-active-organization-learners')
325+
.where({ id: organizationLearnerId })
326+
.first();
327+
328+
if (!organizationLearner) {
329+
throw new NotFoundError(`Student not found for ID ${organizationLearnerId}`);
330+
}
331+
return new OrganizationLearner(organizationLearner);
332+
}
333+
305334
/**
306335
* @function
307336
* @name findOrganizationLearnerIdsBeforeImportFeatureFromOrganizationId
@@ -325,10 +354,12 @@ export {
325354
findByUserId,
326355
findOrganizationLearnerIdsBeforeImportFeatureFromOrganizationId,
327356
findOrganizationLearnerIdsByOrganizationId,
357+
getLearnerInfo,
328358
getOrganizationLearnerForAdmin,
329359
reconcileUserByNationalStudentIdAndOrganizationId,
330360
reconcileUserToOrganizationLearner,
331361
removeByIds,
332362
saveCommonOrganizationLearners,
333363
update,
364+
updateCertificability,
334365
};

Diff for: api/src/shared/application/usecases/checkUserBelongsToLearnersOrganization.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import _ from 'lodash';
22

3-
import * as organizationLearnerRepository from '../../../../lib/infrastructure/repositories/organization-learner-repository.js';
3+
import * as organizationLearnerRepository from '../../../../src/prescription/learner-management/infrastructure/repositories/organization-learner-repository.js';
44
import * as membershipRepository from '../../../team/infrastructure/repositories/membership.repository.js';
55

66
const execute = async function (
77
userId,
88
organizationLearnerId,
99
dependencies = { membershipRepository, organizationLearnerRepository },
1010
) {
11-
const organizationLearner = await dependencies.organizationLearnerRepository.get(organizationLearnerId);
11+
const organizationLearner = await dependencies.organizationLearnerRepository.getLearnerInfo(organizationLearnerId);
1212
const memberships = await dependencies.membershipRepository.findByUserIdAndOrganizationId({
1313
userId,
1414
organizationId: organizationLearner.organizationId,

Diff for: api/src/shared/domain/errors.js

-7
Original file line numberDiff line numberDiff line change
@@ -1056,12 +1056,6 @@ class AuditLoggerApiError extends DomainError {
10561056
}
10571057
}
10581058

1059-
class OrganizationLearnerCertificabilityNotUpdatedError extends DomainError {
1060-
constructor(message) {
1061-
super(message);
1062-
}
1063-
}
1064-
10651059
export {
10661060
AccountRecoveryDemandExpired,
10671061
AccountRecoveryUserAlreadyConfirmEmail,
@@ -1162,7 +1156,6 @@ export {
11621156
OrganizationLearnerAlreadyLinkedToInvalidUserError,
11631157
OrganizationLearnerAlreadyLinkedToUserError,
11641158
OrganizationLearnerCannotBeDissociatedError,
1165-
OrganizationLearnerCertificabilityNotUpdatedError,
11661159
OrganizationLearnerDisabledError,
11671160
OrganizationLearnerNotFound,
11681161
OrganizationLearnersConstraintError,

Diff for: api/tests/identity-access-management/unit/domain/usecases/get-account-recovery-details.usecase.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ describe('Unit | Identity Access Management | Domain | UseCase | get-account-rec
55
it('returns new email and firstName of account recovery demand', async function () {
66
// given
77
const temporaryKey = 'ZHABCDEFJSJ';
8-
const organizationLearnerRepository = {
9-
get: sinon.stub(),
8+
const prescriptionOrganizationLearnerRepository = {
9+
getLearnerInfo: sinon.stub(),
1010
};
1111
const scoAccountRecoveryService = {
1212
retrieveAndValidateAccountRecoveryDemand: sinon.stub(),
@@ -16,12 +16,12 @@ describe('Unit | Identity Access Management | Domain | UseCase | get-account-rec
1616
const firstName = 'Emma';
1717

1818
scoAccountRecoveryService.retrieveAndValidateAccountRecoveryDemand.resolves({ organizationLearnerId, newEmail });
19-
organizationLearnerRepository.get.withArgs(organizationLearnerId).resolves({ firstName });
19+
prescriptionOrganizationLearnerRepository.getLearnerInfo.withArgs(organizationLearnerId).resolves({ firstName });
2020

2121
// when
2222
const result = await getAccountRecoveryDetails({
2323
temporaryKey,
24-
organizationLearnerRepository,
24+
prescriptionOrganizationLearnerRepository,
2525
scoAccountRecoveryService,
2626
});
2727

0 commit comments

Comments
 (0)