chore: add assign template to user for email sending and template

This commit is contained in:
mahyargdz
2025-07-15 13:08:00 +03:30
parent 964026286f
commit a37317e22c
11 changed files with 360 additions and 272 deletions
+23 -8
View File
@@ -1,8 +1,8 @@
import { EntityManager } from "@mikro-orm/postgresql";
import { BadRequestException, Injectable, Logger, NotFoundException } from "@nestjs/common";
import { BadRequestException, Injectable, Logger } from "@nestjs/common";
import { firstValueFrom } from "rxjs";
import { BusinessMessage, DomainMessage, MailServerMessage, RoleMessage, UserMessage } from "../../../common/enums/message.enum";
import { BusinessMessage, DomainMessage, MailServerMessage, RoleMessage, TemplateMessage, UserMessage } from "../../../common/enums/message.enum";
import { QUOTA_CONSTANTS } from "../../businesses/constant";
import { Business } from "../../businesses/entities/business.entity";
import { DomainStatus } from "../../domains/enums/domain-status.enum";
@@ -10,6 +10,7 @@ import { DomainAutomationService } from "../../domains/services/domain-automatio
import { DomainsService } from "../../domains/services/domains.service";
import { UpdateUserDto } from "../../mail-server/DTO";
import { MailServerService } from "../../mail-server/services/mail-server.service";
import { Template } from "../../templates/entities/template.entity";
import { PasswordService } from "../../utils/services/password.service";
import { CreateEmailUserDto } from "../DTO/create-email-user.dto";
import { UpdateEmailUserDto } from "../DTO/update-email-user.dto";
@@ -80,7 +81,7 @@ export class UsersService {
/*******************************/
async createEmailUser(createEmailUserDto: CreateEmailUserDto, businessId: string) {
const { username, password, domainId, displayName, quota, aliases, title } = createEmailUserDto;
const { username, password, domainId, displayName, quota, aliases, title, templateId } = createEmailUserDto;
const business = await this.em.findOne(Business, { id: businessId });
if (!business) throw new BadRequestException(BusinessMessage.NOT_FOUND);
@@ -145,6 +146,12 @@ export class UsersService {
await this.em.persistAndFlush([user, business]);
if (templateId) {
const template = await this.em.findOne(Template, { id: templateId, business: { id: businessId } });
if (!template) throw new BadRequestException(TemplateMessage.TEMPLATE_NOT_FOUND);
user.template = template;
}
if (aliases && aliases.length > 0) {
for (const alias of aliases) {
try {
@@ -160,6 +167,8 @@ export class UsersService {
}
}
await this.em.flush();
this.logger.log(`Email user created: ${emailAddress} for business ${businessId}`);
await this.domainAutomationService.createDefaultMailboxes(mailServerUser.id);
@@ -199,7 +208,7 @@ export class UsersService {
emailEnabled: true,
});
if (!user) throw new NotFoundException(UserMessage.EMAIL_USER_NOT_FOUND);
if (!user) throw new BadRequestException(UserMessage.EMAIL_USER_NOT_FOUND);
try {
if (user.wildduckUserId) {
@@ -224,7 +233,7 @@ export class UsersService {
/*******************************/
async updateEmailUser(userId: string, businessId: string, updateEmailUserDto: UpdateEmailUserDto) {
const { password, displayName, quota, aliases, title } = updateEmailUserDto;
const { password, displayName, quota, aliases, title, templateId } = updateEmailUserDto;
const user = await this.userRepository.findOne(
{
@@ -235,7 +244,13 @@ export class UsersService {
{ populate: ["business"] },
);
if (!user) throw new NotFoundException(UserMessage.EMAIL_USER_NOT_FOUND);
if (!user) throw new BadRequestException(UserMessage.EMAIL_USER_NOT_FOUND);
if (templateId) {
const template = await this.em.findOne(Template, { id: templateId, business: { id: businessId } });
if (!template) throw new BadRequestException(TemplateMessage.TEMPLATE_NOT_FOUND);
user.template = template;
}
// Validate business quota if quota is being updated
if (quota) {
@@ -335,7 +350,7 @@ export class UsersService {
{ populate: ["business"] },
);
if (!user) throw new NotFoundException(UserMessage.EMAIL_USER_NOT_FOUND);
if (!user) throw new BadRequestException(UserMessage.EMAIL_USER_NOT_FOUND);
// Validate business quota before updating
const currentQuota = user.emailQuota || 0;
@@ -370,7 +385,7 @@ export class UsersService {
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);
if (!user) throw new BadRequestException(UserMessage.EMAIL_USER_NOT_FOUND);
user.isActive = !user.isActive;
await this.em.flush();