This commit is contained in:
mahyargdz
2025-07-10 11:07:21 +03:30
parent 85fdc7af4e
commit 560e86b9b4
2 changed files with 47 additions and 6 deletions
@@ -19,11 +19,45 @@ export class DomainAutomationService {
private readonly passwordService: PasswordService,
) {}
/**
* Check if a user exists in WildDuck by username or email address
*/
private async checkUserExistsInWildDuck(username: string, email: string): Promise<boolean> {
try {
// First try to search by username
const usersByUsername = await firstValueFrom(this.mailServerService.users.listUsers({ query: username, limit: 10 }));
// Check if any user has the exact username
const userWithUsername = usersByUsername.results.find((user) => user.username === username);
if (userWithUsername) {
this.logger.debug(`User with username "${username}" already exists in WildDuck`);
return true;
}
// Then search by email address
const addressSearch = await firstValueFrom(this.mailServerService.addresses.searchAddresses({ query: email, limit: 10 }));
// Check if the exact email address exists
const addressExists = addressSearch.results.find((addr) => addr.address === email);
if (addressExists) {
this.logger.debug(`Email address "${email}" already exists in WildDuck`);
return true;
}
return false;
} catch (error) {
this.logger.warn(`Failed to check user existence in WildDuck: ${(error as Error).message}`);
// If we can't check, assume user doesn't exist to avoid blocking domain verification
return false;
}
}
/**
* Create info@domain.com address when domain is verified
*/
async createInfoAddress(domain: Domain): Promise<void> {
const infoEmail = `info@${domain.name}`;
const uniqueUsername = `info-${domain.name}`;
try {
this.logger.log(`Creating info address for domain: ${domain.name}`);
@@ -35,7 +69,14 @@ export class DomainAutomationService {
});
if (existingUser) {
this.logger.warn(`Info address ${infoEmail} already exists, skipping creation`);
this.logger.warn(`Info address ${infoEmail} already exists in local database, skipping creation`);
return;
}
// Check if user exists in WildDuck
const userExistsInWildDuck = await this.checkUserExistsInWildDuck(uniqueUsername, infoEmail);
if (userExistsInWildDuck) {
this.logger.warn(`Info address ${infoEmail} or username "${uniqueUsername}" already exists in WildDuck, skipping creation`);
return;
}
@@ -52,7 +93,7 @@ export class DomainAutomationService {
// Create user in mail server (WildDuck)
const mailServerUser = await firstValueFrom(
this.mailServerService.users.createUser({
username: "info",
username: uniqueUsername,
password,
address: infoEmail,
name: `Info - ${domain.business.name}`,
@@ -68,7 +109,7 @@ export class DomainAutomationService {
// Create user in local database
const user = this.em.create(User, {
title: `Info - ${domain.business.name}`,
userName: "info",
userName: uniqueUsername,
password: hashedPassword,
emailAddress: infoEmail,
emailEnabled: true,
@@ -83,8 +124,8 @@ export class DomainAutomationService {
await this.em.persistAndFlush(user);
this.logger.log(`Successfully created info address: ${infoEmail} for business ${domain.business.name}`);
this.logger.log(`Info account credentials - Email: ${infoEmail}, Password: [GENERATED]`);
this.logger.log(`Successfully created info address: ${infoEmail} (username: ${uniqueUsername}) for business ${domain.business.name}`);
this.logger.log(`Info account credentials - Email: ${infoEmail}, Username: ${uniqueUsername}, Password: [GENERATED]`);
} catch (error) {
this.logger.error(`Failed to create info address for domain ${domain.name}:`, error);
// Don't throw the error to prevent domain verification from failing
+1 -1
View File
@@ -92,7 +92,7 @@ export class UsersService {
try {
const mailServerUser = await firstValueFrom(
this.mailServerService.users.createUser({
username,
username: `${username}-${business.name}`,
password,
address: emailAddress,
name: displayName || username,