74 lines
3.9 KiB
JavaScript
74 lines
3.9 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.CreateUserCommandHandler = void 0;
|
|
const UserAggregate_1 = require("../../../Domain/User/UserAggregate");
|
|
const UserMapper_1 = require("../../DTOs/Mappers/UserMapper");
|
|
const PasswordService_1 = require("../../Services/PasswordService");
|
|
const EmailService_1 = require("../../Services/EmailService");
|
|
const TokenService_1 = require("../../Services/TokenService");
|
|
const Logger_1 = require("../../Services/Logger");
|
|
class CreateUserCommandHandler {
|
|
constructor(userRepo) {
|
|
this.userRepo = userRepo;
|
|
this.emailService = new EmailService_1.EmailService();
|
|
}
|
|
async execute(cmd) {
|
|
try {
|
|
// Validate password strength
|
|
const passwordValidation = PasswordService_1.PasswordService.validatePasswordStrength(cmd.password);
|
|
if (!passwordValidation.isValid) {
|
|
throw new Error(`Password validation failed: ${passwordValidation.errors.join(', ')}`);
|
|
}
|
|
const user = new UserAggregate_1.UserAggregate();
|
|
user.username = cmd.username;
|
|
// Hash the password before storing
|
|
user.password = await PasswordService_1.PasswordService.hashPassword(cmd.password);
|
|
// Generate verification token
|
|
const verificationTokenData = TokenService_1.TokenService.generateVerificationToken();
|
|
user.token = await TokenService_1.TokenService.hashToken(verificationTokenData.token);
|
|
user.TokenExpires = verificationTokenData.expiresAt;
|
|
user.email = cmd.email;
|
|
user.fname = cmd.fname;
|
|
user.lname = cmd.lname;
|
|
user.orgid = cmd.orgid || null;
|
|
user.token = cmd.code || null;
|
|
user.type = cmd.type;
|
|
user.phone = cmd.phone || null;
|
|
user.state = UserAggregate_1.UserState.REGISTERED_NOT_VERIFIED;
|
|
const created = await this.userRepo.create(user);
|
|
// Send verification email
|
|
try {
|
|
const baseUrl = process.env.APP_BASE_URL || 'http://localhost:3000';
|
|
const verificationUrl = TokenService_1.TokenService.generateVerificationUrl(baseUrl, verificationTokenData.token);
|
|
const emailSent = await this.emailService.sendVerificationEmail(created.email, `${created.fname} ${created.lname}`, verificationTokenData.token, verificationUrl);
|
|
if (!emailSent) {
|
|
(0, Logger_1.logWarning)('Failed to send verification email', { email: created.email, userId: created.id });
|
|
// Don't throw error - user creation should still succeed even if email fails
|
|
}
|
|
else {
|
|
(0, Logger_1.logAuth)('Verification email sent successfully', created.id, { email: created.email });
|
|
}
|
|
}
|
|
catch (emailError) {
|
|
(0, Logger_1.logError)('Error sending verification email', emailError);
|
|
// Don't throw error - user creation should still succeed even if email fails
|
|
}
|
|
return UserMapper_1.UserMapper.toShortDto(created);
|
|
}
|
|
catch (error) {
|
|
(0, Logger_1.logError)('CreateUserCommandHandler error', error);
|
|
// Re-throw validation errors as-is
|
|
if (error instanceof Error && error.message.includes('Password validation failed')) {
|
|
throw error;
|
|
}
|
|
// Handle database constraint errors
|
|
if (error instanceof Error && (error.message.includes('duplicate') || error.message.includes('unique'))) {
|
|
throw new Error('User with this username or email already exists');
|
|
}
|
|
// Generic error for other cases
|
|
throw new Error('Failed to create user');
|
|
}
|
|
}
|
|
}
|
|
exports.CreateUserCommandHandler = CreateUserCommandHandler;
|
|
//# sourceMappingURL=CreateUserCommandHandler.js.map
|