Skip to content

Commit

Permalink
[TECH] Migration du usecase computeCertificabilitydans le BC Prescr…
Browse files Browse the repository at this point in the history
…iption (PIX-15342)

 #10693
  • Loading branch information
pix-service-auto-merge authored Dec 4, 2024
2 parents 2d8bdaf + 3a82dd9 commit 9cc5616
Show file tree
Hide file tree
Showing 21 changed files with 167 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ const generateUsernameWithTemporaryPassword = async function ({
userService,
authenticationMethodRepository,
userRepository,
organizationLearnerRepository,
prescriptionOrganizationLearnerRepository,
}) {
const organizationLearner = await organizationLearnerRepository.get(organizationLearnerId);
const organizationLearner = await prescriptionOrganizationLearnerRepository.getLearnerInfo(organizationLearnerId);
_checkIfStudentHasAccessToOrganization(organizationLearner, organizationId);

const studentAccount = await userRepository.get(organizationLearner.userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ const updateOrganizationLearnerDependentUserPassword = async function ({
cryptoService,
passwordGenerator,
authenticationMethodRepository,
organizationLearnerRepository,
prescriptionOrganizationLearnerRepository,
userRepository,
}) {
const userWithMemberships = await userRepository.getWithMemberships(userId);
const organizationLearner = await organizationLearnerRepository.get(organizationLearnerId);
const organizationLearner = await prescriptionOrganizationLearnerRepository.getLearnerInfo(organizationLearnerId);

if (
!userWithMemberships.hasAccessToOrganization(organizationId) ||
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { knex } from '../../../db/knex-database-connection.js';
import { ORGANIZATION_FEATURE } from '../../../src/shared/domain/constants.js';
import {
NotFoundError,
OrganizationLearnerCertificabilityNotUpdatedError,
OrganizationLearnerNotFound,
UserNotFoundError,
} from '../../../src/shared/domain/errors.js';
import { OrganizationLearnerNotFound, UserNotFoundError } from '../../../src/shared/domain/errors.js';
import { OrganizationLearner } from '../../../src/shared/domain/models/OrganizationLearner.js';
import { ParticipantRepartition } from '../../../src/shared/domain/models/ParticipantRepartition.js';
import { fetchPage } from '../../../src/shared/infrastructure/utils/knex-utils.js';
Expand Down Expand Up @@ -93,19 +88,6 @@ function _queryBuilderDissociation(knexConn) {
});
}

const get = async function (organizationLearnerId) {
const organizationLearner = await knex
.select('*')
.from('view-active-organization-learners')
.where({ id: organizationLearnerId })
.first();

if (!organizationLearner) {
throw new NotFoundError(`Student not found for ID ${organizationLearnerId}`);
}
return new OrganizationLearner(organizationLearner);
};

const getLatestOrganizationLearner = async function ({ nationalStudentId, birthdate }) {
const organizationLearner = await knex
.where({ nationalStudentId, birthdate })
Expand Down Expand Up @@ -148,18 +130,6 @@ const isActive = async function ({ userId, campaignId }) {
return !learner?.isDisabled;
};

async function updateCertificability(organizationLearner) {
const result = await knex('organization-learners').where({ id: organizationLearner.id }).update({
isCertifiable: organizationLearner.isCertifiable,
certifiableAt: organizationLearner.certifiableAt,
});
if (result === 0) {
throw new OrganizationLearnerCertificabilityNotUpdatedError(
`Could not update certificability for OrganizationLearner with ID ${organizationLearner.id}.`,
);
}
}

async function countByOrganizationsWhichNeedToComputeCertificability({
skipLoggedLastDayCheck = false,
onlyNotComputed = false,
Expand Down Expand Up @@ -298,9 +268,7 @@ export {
findByOrganizationIdAndUpdatedAtOrderByDivision,
findByOrganizationsWhichNeedToComputeCertificability,
findByUserId,
get,
getLatestOrganizationLearner,
isActive,
updateCertificability,
updateUserIdWhereNull,
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @param {{
* temporaryKey: string,
* accountRecoveryDemandRepository: AccountRecoveryDemandRepository,
* organizationLearnerRepository: OrganizationLearnerRepository,
* prescriptionOrganizationLearnerRepository: PrescriptionOrganizationLearnerRepository,
* userRepository: UserRepository,
* scoAccountRecoveryService: ScoAccountRecoveryService,
* }} params
Expand All @@ -11,7 +11,7 @@
export const getAccountRecoveryDetails = async function ({
temporaryKey,
accountRecoveryDemandRepository,
organizationLearnerRepository,
prescriptionOrganizationLearnerRepository,
userRepository,
scoAccountRecoveryService,
}) {
Expand All @@ -22,7 +22,7 @@ export const getAccountRecoveryDetails = async function ({
userRepository,
});

const { firstName } = await organizationLearnerRepository.get(organizationLearnerId);
const { firstName } = await prescriptionOrganizationLearnerRepository.getLearnerInfo(organizationLearnerId);

return {
id,
Expand Down
2 changes: 2 additions & 0 deletions api/src/identity-access-management/domain/usecases/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as campaignRepository from '../../../../lib/infrastructure/repositories
import * as organizationLearnerRepository from '../../../../lib/infrastructure/repositories/organization-learner-repository.js';
import * as userRecommendedTrainingRepository from '../../../devcomp/infrastructure/repositories/user-recommended-training-repository.js';
import { repositories as campaignRepositories } from '../../../prescription/campaign/infrastructure/repositories/index.js';
import * as prescriptionOrganizationLearnerRepository from '../../../prescription/learner-management/infrastructure/repositories/organization-learner-repository.js';
import { config } from '../../../shared/config.js';
import { cryptoService } from '../../../shared/domain/services/crypto-service.js';
import { tokenService } from '../../../shared/domain/services/token-service.js';
Expand Down Expand Up @@ -59,6 +60,7 @@ const repositories = {
membershipRepository,
oidcProviderRepository,
organizationLearnerRepository,
prescriptionOrganizationLearnerRepository,
privacyUsersApiRepository,
refreshTokenRepository,
resetPasswordDemandRepository,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { usecases } from '../../../../../lib/domain/usecases/index.js';
import { usecases } from '../../../../../src/prescription/learner-management/domain/usecases/index.js';
import { JobController } from '../../../../shared/application/jobs/job-controller.js';
import { ComputeCertificabilityJob } from '../../domain/models/ComputeCertificabilityJob.js';

Expand Down
7 changes: 7 additions & 0 deletions api/src/prescription/learner-management/domain/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,17 @@ class CouldNotDeleteLearnersError extends DomainError {
}
}

class OrganizationLearnerCertificabilityNotUpdatedError extends DomainError {
constructor(message) {
super(message);
}
}

export {
AggregateImportError,
CouldNotDeleteLearnersError,
OrganizationDoesNotHaveFeatureEnabledError,
OrganizationLearnerCertificabilityNotUpdatedError,
OrganizationLearnerImportFormatNotFoundError,
OrganizationLearnersCouldNotBeSavedError,
ReconcileCommonOrganizationLearnerError,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const computeOrganizationLearnerCertificability = async function ({
organizationLearnerRepository,
placementProfileService,
}) {
const organizationLearner = await organizationLearnerRepository.get(organizationLearnerId);
const organizationLearner = await organizationLearnerRepository.getLearnerInfo(organizationLearnerId);

const placementProfile = await placementProfileService.getPlacementProfile({
userId: organizationLearner.userId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as campaignRepository from '../../../../../lib/infrastructure/repositor
import * as libOrganizationLearnerRepository from '../../../../../lib/infrastructure/repositories/organization-learner-repository.js';
import * as userRepository from '../../../../identity-access-management/infrastructure/repositories/user.repository.js';
import * as organizationFeatureApi from '../../../../organizational-entities/application/api/organization-features-api.js';
import * as placementProfileService from '../../../../shared/domain/services/placement-profile-service.js';
import { logErrorWithCorrelationIds } from '../../../../shared/infrastructure/monitoring-tools.js';
import * as organizationRepository from '../../../../shared/infrastructure/repositories/organization-repository.js';
import { injectDependencies } from '../../../../shared/infrastructure/utils/dependency-injection.js';
Expand Down Expand Up @@ -46,6 +47,7 @@ import { importStorage } from '../../infrastructure/storage/import-storage.js';
* @typedef {import ('../../infrastructure/repositories/organization-learner-import-format-repository.js')} OrganizationLearnerImportFormatRepository
* @typedef {import ('../../infrastructure/repositories/organization-learner-repository.js')} OrganizationLearnerRepository
* @typedef {import ('../../../../shared/infrastructure/repositories/organization-repository.js')} OrganizationRepository
* @typedef {import ('../../../../shared/domain/services/placement-profile-service.js')} placementProfileService
* @typedef {import('../../../organization-learner/infrastructure/repositories/registration-organization-learner-repository.js')} registrationOrganizationLearnerRepository
* @typedef {import ('../../infrastructure/repositories/student-repository.js')} studentRepository
* @typedef {import ('../../infrastructure/repositories/sup-organization-learner-repository.js')} SupOrganizationLearnerRepository
Expand All @@ -72,6 +74,7 @@ const dependencies = {
organizationLearnerImportFormatRepository,
organizationLearnerRepository,
organizationRepository,
placementProfileService,
registrationOrganizationLearnerRepository,
studentRepository,
supOrganizationLearnerRepository,
Expand All @@ -96,6 +99,7 @@ const usecasesWithoutInjectedDependencies = {

/**
* @typedef PrescriptionLearnerManagementUsecases
* @property {computeOrganizationLearnerCertificability} computeOrganizationLearnerCertificability
* @property {saveOrganizationLearnersFile} saveOrganizationLearnersFile
* @property {sendOrganizationLearnersFile} sendOrganizationLearnersFile
* @property {validateOrganizationLearnersFile} validateOrganizationLearnersFile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
UserCouldNotBeReconciledError,
} from '../../../../shared/domain/errors.js';
import { OrganizationLearner } from '../../../../shared/domain/models/index.js';
import { OrganizationLearnerCertificabilityNotUpdatedError } from '../../domain/errors.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 @@ -302,6 +303,34 @@ const reconcileUserToOrganizationLearner = async function ({ userId, organizatio
}
};

async function updateCertificability(organizationLearner) {
const knexConn = DomainTransaction.getConnection();
const result = await knexConn('organization-learners').where({ id: organizationLearner.id }).update({
isCertifiable: organizationLearner.isCertifiable,
certifiableAt: organizationLearner.certifiableAt,
});
if (result === 0) {
throw new OrganizationLearnerCertificabilityNotUpdatedError(
`Could not update certificability for OrganizationLearner with ID ${organizationLearner.id}.`,
);
}
}

async function getLearnerInfo(organizationLearnerId) {
const knexConn = DomainTransaction.getConnection();

const organizationLearner = await knexConn
.select('*')
.from('view-active-organization-learners')
.where({ id: organizationLearnerId })
.first();

if (!organizationLearner) {
throw new NotFoundError(`Student not found for ID ${organizationLearnerId}`);
}
return new OrganizationLearner(organizationLearner);
}

/**
* @function
* @name findOrganizationLearnerIdsBeforeImportFeatureFromOrganizationId
Expand All @@ -325,10 +354,12 @@ export {
findByUserId,
findOrganizationLearnerIdsBeforeImportFeatureFromOrganizationId,
findOrganizationLearnerIdsByOrganizationId,
getLearnerInfo,
getOrganizationLearnerForAdmin,
reconcileUserByNationalStudentIdAndOrganizationId,
reconcileUserToOrganizationLearner,
removeByIds,
saveCommonOrganizationLearners,
update,
updateCertificability,
};
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import _ from 'lodash';

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

const execute = async function (
userId,
organizationLearnerId,
dependencies = { membershipRepository, organizationLearnerRepository },
) {
const organizationLearner = await dependencies.organizationLearnerRepository.get(organizationLearnerId);
const organizationLearner = await dependencies.organizationLearnerRepository.getLearnerInfo(organizationLearnerId);
const memberships = await dependencies.membershipRepository.findByUserIdAndOrganizationId({
userId,
organizationId: organizationLearner.organizationId,
Expand Down
7 changes: 0 additions & 7 deletions api/src/shared/domain/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1056,12 +1056,6 @@ class AuditLoggerApiError extends DomainError {
}
}

class OrganizationLearnerCertificabilityNotUpdatedError extends DomainError {
constructor(message) {
super(message);
}
}

export {
AccountRecoveryDemandExpired,
AccountRecoveryUserAlreadyConfirmEmail,
Expand Down Expand Up @@ -1162,7 +1156,6 @@ export {
OrganizationLearnerAlreadyLinkedToInvalidUserError,
OrganizationLearnerAlreadyLinkedToUserError,
OrganizationLearnerCannotBeDissociatedError,
OrganizationLearnerCertificabilityNotUpdatedError,
OrganizationLearnerDisabledError,
OrganizationLearnerNotFound,
OrganizationLearnersConstraintError,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ describe('Unit | Identity Access Management | Domain | UseCase | get-account-rec
it('returns new email and firstName of account recovery demand', async function () {
// given
const temporaryKey = 'ZHABCDEFJSJ';
const organizationLearnerRepository = {
get: sinon.stub(),
const prescriptionOrganizationLearnerRepository = {
getLearnerInfo: sinon.stub(),
};
const scoAccountRecoveryService = {
retrieveAndValidateAccountRecoveryDemand: sinon.stub(),
Expand All @@ -16,12 +16,12 @@ describe('Unit | Identity Access Management | Domain | UseCase | get-account-rec
const firstName = 'Emma';

scoAccountRecoveryService.retrieveAndValidateAccountRecoveryDemand.resolves({ organizationLearnerId, newEmail });
organizationLearnerRepository.get.withArgs(organizationLearnerId).resolves({ firstName });
prescriptionOrganizationLearnerRepository.getLearnerInfo.withArgs(organizationLearnerId).resolves({ firstName });

// when
const result = await getAccountRecoveryDetails({
temporaryKey,
organizationLearnerRepository,
prescriptionOrganizationLearnerRepository,
scoAccountRecoveryService,
});

Expand Down
Loading

0 comments on commit 9cc5616

Please sign in to comment.