chore: change the email creation dto

This commit is contained in:
mahyargdz
2025-07-07 10:13:49 +03:30
parent 9ae6f260a8
commit 6565d2ae8b
12 changed files with 1169 additions and 925 deletions
+24 -18
View File
@@ -10,7 +10,7 @@ import {
DANAK_SMTPS_PORT,
DANAK_SMTPS_SERVER,
} from "../../../common/constants";
import { BusinessMessage, DomainMessage, MailServerMessage, UserMessage } from "../../../common/enums/message.enum";
import { BusinessMessage, DomainMessage, MailServerMessage, RoleMessage, UserMessage } from "../../../common/enums/message.enum";
import { Business } from "../../businesses/entities/business.entity";
import { DomainStatus } from "../../domains/enums/domain-status.enum";
import { DomainsService } from "../../domains/services/domains.service";
@@ -66,29 +66,21 @@ export class UsersService {
const business = await this.em.findOne(Business, { id: businessId });
if (!business) throw new BadRequestException(BusinessMessage.NOT_FOUND);
const domain = await this.domainsService.getDomainById(domainId);
const domain = await this.domainsService.getDomainById(domainId, businessId);
if (domain.business.id !== businessId) {
throw new BadRequestException(DomainMessage.NOT_BELONG_TO_BUSINESS);
}
if (domain.business.id !== businessId) throw new BadRequestException(DomainMessage.NOT_BELONG_TO_BUSINESS);
if (domain.status !== DomainStatus.VERIFIED) {
throw new BadRequestException(DomainMessage.NOT_VERIFIED);
}
if (domain.status !== DomainStatus.VERIFIED) throw new BadRequestException(DomainMessage.NOT_VERIFIED);
const emailAddress = `${username}@${domain.name}`;
// Check if email address already exists
const existingUser = await this.userRepository.findOne({ emailAddress });
if (existingUser) throw new BadRequestException(UserMessage.EMAIL_ADDRESS_ALREADY_EXISTS);
const hashedPassword = await this.passwordService.hashPassword(password);
let userRole = await this.em.findOne(Role, { name: RoleEnum.USER });
if (!userRole) {
userRole = this.em.create(Role, { name: RoleEnum.USER });
await this.em.persistAndFlush(userRole);
}
const userRole = await this.em.findOne(Role, { name: RoleEnum.USER });
if (!userRole) throw new BadRequestException(RoleMessage.NOT_FOUND);
try {
const mailServerUser = await firstValueFrom(
@@ -97,8 +89,8 @@ export class UsersService {
password,
address: emailAddress,
name: displayName || username,
quota: quota || 1073741824, // 1GB default
tags: ["dmail-user"],
quota: quota || 1073741824,
language: "fa",
}),
);
@@ -121,7 +113,6 @@ export class UsersService {
await this.em.persistAndFlush(user);
// Create aliases if provided
if (aliases && aliases.length > 0) {
for (const alias of aliases) {
try {
@@ -164,7 +155,7 @@ export class UsersService {
encryption: DANAK_SMTPS_ENCRYPTION,
},
},
message: "Email user created successfully",
message: UserMessage.EMAIL_USER_CREATED_SUCCESSFULLY,
};
} catch (error) {
this.logger.error(`Failed to create email user ${emailAddress}:`, error);
@@ -252,4 +243,19 @@ export class UsersService {
throw new BadRequestException(UserMessage.FAILED_TO_UPDATE_EMAIL_USER_QUOTA);
}
}
//=====================================================
async updateEmailUserStatus(userId: string, businessId: string) {
const user = await this.userRepository.findOne({ id: userId, business: { id: businessId }, emailEnabled: true });
if (!user) throw new NotFoundException(UserMessage.EMAIL_USER_NOT_FOUND);
user.isActive = !user.isActive;
await this.em.flush();
return {
message: UserMessage.STATUS_UPDATED,
status: user.isActive,
};
}
}