chore: add default mailbox for the user
This commit is contained in:
@@ -3,6 +3,7 @@ import { Injectable, Logger } from "@nestjs/common";
|
|||||||
import { firstValueFrom } from "rxjs";
|
import { firstValueFrom } from "rxjs";
|
||||||
|
|
||||||
import { MailServerService } from "../../mail-server/services/mail-server.service";
|
import { MailServerService } from "../../mail-server/services/mail-server.service";
|
||||||
|
import { MailboxEnum } from "../../mailbox/enums/mailbox.enum";
|
||||||
import { Role } from "../../users/entities/role.entity";
|
import { Role } from "../../users/entities/role.entity";
|
||||||
import { User } from "../../users/entities/user.entity";
|
import { User } from "../../users/entities/user.entity";
|
||||||
import { RoleEnum } from "../../users/enums/role.enum";
|
import { RoleEnum } from "../../users/enums/role.enum";
|
||||||
@@ -52,6 +53,33 @@ export class DomainAutomationService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create default mailboxes for a user (Archive and Favorite)
|
||||||
|
*/
|
||||||
|
async createDefaultMailboxes(wildduckUserId: string): Promise<void> {
|
||||||
|
try {
|
||||||
|
const mailboxesToCreate = [
|
||||||
|
{ path: MailboxEnum.ARCHIVE, name: "Archive" },
|
||||||
|
{ path: MailboxEnum.FAVORITE, name: "Favorite" },
|
||||||
|
];
|
||||||
|
|
||||||
|
for (const mailbox of mailboxesToCreate) {
|
||||||
|
try {
|
||||||
|
await firstValueFrom(
|
||||||
|
this.mailServerService.mailboxes.createMailbox(wildduckUserId, {
|
||||||
|
path: mailbox.path,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
this.logger.log(`Created ${mailbox.name} mailbox for user: ${wildduckUserId}`);
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.warn(`Failed to create ${mailbox.name} mailbox for user ${wildduckUserId}: ${(error as Error).message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.error(`Failed to create default mailboxes for user ${wildduckUserId}: ${(error as Error).message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create info@domain.com address when domain is verified
|
* Create info@domain.com address when domain is verified
|
||||||
*/
|
*/
|
||||||
@@ -124,6 +152,9 @@ export class DomainAutomationService {
|
|||||||
|
|
||||||
await this.em.persistAndFlush(user);
|
await this.em.persistAndFlush(user);
|
||||||
|
|
||||||
|
// Create default mailboxes (Archive and Favorite)
|
||||||
|
await this.createDefaultMailboxes(mailServerUser.id);
|
||||||
|
|
||||||
this.logger.log(`Successfully created info address: ${infoEmail} (username: ${uniqueUsername}) for business ${domain.business.name}`);
|
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]`);
|
this.logger.log(`Info account credentials - Email: ${infoEmail}, Username: ${uniqueUsername}, Password: [GENERATED]`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { Injectable, Logger, NotFoundException } from "@nestjs/common";
|
import { Injectable, Logger, NotFoundException } from "@nestjs/common";
|
||||||
import { firstValueFrom } from "rxjs";
|
import { firstValueFrom } from "rxjs";
|
||||||
|
|
||||||
import { MailboxEnum } from "../../mail-server/enums/mailbox.enum";
|
|
||||||
import { MailServerService } from "../../mail-server/services/mail-server.service";
|
import { MailServerService } from "../../mail-server/services/mail-server.service";
|
||||||
|
import { MailboxEnum } from "../../mailbox/enums/mailbox.enum";
|
||||||
|
|
||||||
export interface UserMailboxIds {
|
export interface UserMailboxIds {
|
||||||
inbox: string;
|
inbox: string;
|
||||||
|
|||||||
@@ -1,21 +1,29 @@
|
|||||||
import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger";
|
import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger";
|
||||||
import { IsBoolean, IsNumber, IsOptional, IsString } from "class-validator";
|
import { IsBoolean, IsNotEmpty, IsNumber, IsOptional, IsString } from "class-validator";
|
||||||
|
|
||||||
export class CreateMailboxDto {
|
export class CreateMailboxDto {
|
||||||
@ApiProperty({
|
@IsNotEmpty()
|
||||||
description: "Mailbox path with slashes as folder separators",
|
|
||||||
example: "Important/Work",
|
|
||||||
})
|
|
||||||
@IsString()
|
@IsString()
|
||||||
|
@ApiProperty({ description: "Mailbox path with slashes as folder separators", example: "Important/Work" })
|
||||||
path: string;
|
path: string;
|
||||||
|
|
||||||
@ApiPropertyOptional({
|
|
||||||
description: "Retention time in milliseconds for messages in this mailbox",
|
|
||||||
example: 2592000000,
|
|
||||||
})
|
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
|
@IsNotEmpty()
|
||||||
@IsNumber()
|
@IsNumber()
|
||||||
|
@ApiPropertyOptional({ description: "Retention time in milliseconds for messages in this mailbox", example: 2592000000 })
|
||||||
retention?: number;
|
retention?: number;
|
||||||
|
|
||||||
|
@IsOptional()
|
||||||
|
@IsNotEmpty()
|
||||||
|
@IsBoolean()
|
||||||
|
@ApiPropertyOptional({ description: "Encrypt messages in this mailbox", example: false })
|
||||||
|
encryptMessages?: boolean;
|
||||||
|
|
||||||
|
@IsNotEmpty()
|
||||||
|
@IsOptional()
|
||||||
|
@IsBoolean()
|
||||||
|
@ApiPropertyOptional({ description: "Is the folder hidden or not. Hidden folders can not be opened in IMAP.", example: false })
|
||||||
|
hidden?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class UpdateMailboxDto {
|
export class UpdateMailboxDto {
|
||||||
|
|||||||
@@ -1,9 +0,0 @@
|
|||||||
export enum MailboxEnum {
|
|
||||||
INBOX = "INBOX",
|
|
||||||
DRAFTS = "Drafts",
|
|
||||||
ARCHIVE = "Archive",
|
|
||||||
Junk = "Junk",
|
|
||||||
SENT = "Sent Mail",
|
|
||||||
TRASH = "Trash",
|
|
||||||
SPAM = "spam",
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
export enum MailboxEnum {
|
||||||
|
INBOX = "INBOX",
|
||||||
|
DRAFTS = "Drafts",
|
||||||
|
ARCHIVE = "Archive", //should created when user created
|
||||||
|
FAVORITE = "FAVORITE", //should created when user created
|
||||||
|
Junk = "Junk",
|
||||||
|
SENT = "Sent Mail",
|
||||||
|
TRASH = "Trash",
|
||||||
|
}
|
||||||
|
|
||||||
|
// SPAM = "spam", //should created when user created
|
||||||
@@ -13,6 +13,7 @@ import {
|
|||||||
import { BusinessMessage, DomainMessage, MailServerMessage, RoleMessage, 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 { Business } from "../../businesses/entities/business.entity";
|
||||||
import { DomainStatus } from "../../domains/enums/domain-status.enum";
|
import { DomainStatus } from "../../domains/enums/domain-status.enum";
|
||||||
|
import { DomainAutomationService } from "../../domains/services/domain-automation.service";
|
||||||
import { DomainsService } from "../../domains/services/domains.service";
|
import { DomainsService } from "../../domains/services/domains.service";
|
||||||
import { MailServerService } from "../../mail-server/services/mail-server.service";
|
import { MailServerService } from "../../mail-server/services/mail-server.service";
|
||||||
import { PasswordService } from "../../utils/services/password.service";
|
import { PasswordService } from "../../utils/services/password.service";
|
||||||
@@ -33,6 +34,7 @@ export class UsersService {
|
|||||||
private readonly passwordService: PasswordService,
|
private readonly passwordService: PasswordService,
|
||||||
private readonly mailServerService: MailServerService,
|
private readonly mailServerService: MailServerService,
|
||||||
private readonly domainsService: DomainsService,
|
private readonly domainsService: DomainsService,
|
||||||
|
private readonly domainAutomationService: DomainAutomationService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async getUserEmailIdFromId(userId: string) {
|
async getUserEmailIdFromId(userId: string) {
|
||||||
@@ -137,6 +139,8 @@ export class UsersService {
|
|||||||
|
|
||||||
this.logger.log(`Email user created: ${emailAddress} for business ${businessId}`);
|
this.logger.log(`Email user created: ${emailAddress} for business ${businessId}`);
|
||||||
|
|
||||||
|
await this.domainAutomationService.createDefaultMailboxes(mailServerUser.id);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
user: {
|
user: {
|
||||||
id: user.id,
|
id: user.id,
|
||||||
|
|||||||
Reference in New Issue
Block a user