Skip to content

Commit

Permalink
feat(api): send account creation email with a job
Browse files Browse the repository at this point in the history
  • Loading branch information
bpetetot authored Nov 14, 2024
1 parent 1acab3c commit 89c499b
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 142 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import lodash from 'lodash';
const { isNil } = lodash;

import { createAccountCreationEmail } from '../../../src/identity-access-management/domain/emails/create-account-creation.email.js';
import { User } from '../../../src/identity-access-management/domain/models/User.js';
import { STUDENT_RECONCILIATION_ERRORS } from '../../../src/shared/domain/constants.js';
import { EntityValidationError } from '../../../src/shared/domain/errors.js';
Expand All @@ -19,18 +20,17 @@ const createAndReconcileUserToOrganizationLearner = async function ({
userAttributes,
authenticationMethodRepository,
campaignRepository,
emailRepository,
emailValidationDemandRepository,
organizationLearnerRepository,
userRepository,
userToCreateRepository,
cryptoService,
mailService,
obfuscationService,
userReconciliationService,
userService,
passwordValidator,
userValidator,
i18n,
}) {
const campaign = await campaignRepository.getByCode(campaignCode);
if (!campaign) {
Expand Down Expand Up @@ -84,14 +84,16 @@ const createAndReconcileUserToOrganizationLearner = async function ({
if (!isUsernameMode) {
const redirectionUrl = urlBuilder.getCampaignUrl(locale, campaignCode);
const token = await emailValidationDemandRepository.save(createdUser.id);
await mailService.sendAccountCreationEmail({
email: createdUser.email,
firstName: createdUser.firstName,
locale,
token,
redirectionUrl,
i18n,
});

await emailRepository.sendEmailAsync(
createAccountCreationEmail({
locale,
email: createdUser.email,
firstName: createdUser.firstName,
token,
redirectionUrl,
}),
);
}
return createdUser;
};
Expand Down
2 changes: 2 additions & 0 deletions api/lib/domain/usecases/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ import * as writeCsvUtils from '../../../src/shared/infrastructure/utils/csv/wri
import * as dateUtils from '../../../src/shared/infrastructure/utils/date-utils.js';
import { injectDependencies } from '../../../src/shared/infrastructure/utils/dependency-injection.js';
import { importNamedExportsFromDirectory } from '../../../src/shared/infrastructure/utils/import-named-exports-from-directory.js';
import * as emailRepository from '../../../src/shared/mail/infrastructure/repositories/email.repository.js';
import * as certificationCenterInvitationService from '../../../src/team/domain/services/certification-center-invitation-service.js';
import { organizationInvitationService } from '../../../src/team/domain/services/organization-invitation.service.js';
import * as certificationCenterInvitationRepository from '../../../src/team/infrastructure/repositories/certification-center-invitation-repository.js';
Expand Down Expand Up @@ -254,6 +255,7 @@ const dependencies = {
dataProtectionOfficerRepository,
dateUtils,
divisionRepository,
emailRepository,
emailValidationDemandRepository,
finalizedSessionRepository,
flashAlgorithmConfigurationRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { withTransaction } from '../../../shared/domain/DomainTransaction.js';
import { AlreadyRegisteredEmailError } from '../../../shared/domain/errors.js';
import { EntityValidationError } from '../../../shared/domain/errors.js';
import { urlBuilder } from '../../../shared/infrastructure/utils/url-builder.js';
import { createAccountCreationEmail } from '../emails/create-account-creation.email.js';

/**
* @param {Object} params
Expand All @@ -27,15 +28,14 @@ const createUser = withTransaction(async function ({
user,
authenticationMethodRepository,
campaignRepository,
emailRepository,
emailValidationDemandRepository,
userRepository,
userToCreateRepository,
cryptoService,
mailService,
userService,
userValidator,
passwordValidator,
i18n,
}) {
await _assertValidData({
password,
Expand Down Expand Up @@ -68,14 +68,16 @@ const createUser = withTransaction(async function ({
}

const token = await emailValidationDemandRepository.save(savedUser.id);
await mailService.sendAccountCreationEmail({
email: savedUser.email,
firstName: savedUser.firstName,
locale: localeFromHeader,
token,
redirectionUrl,
i18n,
});

await emailRepository.sendEmailAsync(
createAccountCreationEmail({
locale: localeFromHeader,
email: savedUser.email,
firstName: savedUser.firstName,
token,
redirectionUrl,
}),
);

return savedUser;
});
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 @@ -21,6 +21,7 @@ import * as userLoginRepository from '../../../shared/infrastructure/repositorie
import * as codeUtils from '../../../shared/infrastructure/utils/code-utils.js';
import { injectDependencies } from '../../../shared/infrastructure/utils/dependency-injection.js';
import { importNamedExportsFromDirectory } from '../../../shared/infrastructure/utils/import-named-exports-from-directory.js';
import * as emailRepository from '../../../shared/mail/infrastructure/repositories/email.repository.js';
import { accountRecoveryDemandRepository } from '../../infrastructure/repositories/account-recovery-demand.repository.js';
import * as authenticationMethodRepository from '../../infrastructure/repositories/authentication-method.repository.js';
import { emailValidationDemandRepository } from '../../infrastructure/repositories/email-validation-demand.repository.js';
Expand Down Expand Up @@ -48,6 +49,7 @@ const repositories = {
campaignRepository,
campaignToJoinRepository: campaignRepositories.campaignToJoinRepository,
emailValidationDemandRepository,
emailRepository,
eventLoggingJobRepository,
oidcProviderRepository,
organizationLearnerRepository,
Expand Down
Loading

0 comments on commit 89c499b

Please sign in to comment.