From 560e86b9b43402f4c25c4d885c1b98c4f8c1c89a Mon Sep 17 00:00:00 2001 From: mahyargdz Date: Thu, 10 Jul 2025 11:07:21 +0330 Subject: [PATCH] fix: fix --- .../services/domain-automation.service.ts | 51 +++++++++++++++++-- src/modules/users/services/users.service.ts | 2 +- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/modules/domains/services/domain-automation.service.ts b/src/modules/domains/services/domain-automation.service.ts index 5fd6cd9..4252704 100644 --- a/src/modules/domains/services/domain-automation.service.ts +++ b/src/modules/domains/services/domain-automation.service.ts @@ -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 { + 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 { 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 diff --git a/src/modules/users/services/users.service.ts b/src/modules/users/services/users.service.ts index aebb976..8aa6d7d 100644 --- a/src/modules/users/services/users.service.ts +++ b/src/modules/users/services/users.service.ts @@ -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,