chore: send email and recive

This commit is contained in:
mahyargdz
2025-07-09 16:18:54 +03:30
parent b3ab6c8a10
commit 092abd2895
13 changed files with 212 additions and 98 deletions
+15 -9
View File
@@ -3,6 +3,7 @@ import { firstValueFrom } from "rxjs";
import { EmailMessage } from "../../../common/enums/message.enum";
import { MailServerService } from "../../mail-server/services/mail-server.service";
import { UsersService } from "../../users/services/users.service";
import { MessageListQueryDto, SearchMessagesQueryDto } from "../DTO/email-query.dto";
import { SendEmailDto } from "../DTO/send-email.dto";
@@ -10,14 +11,19 @@ import { SendEmailDto } from "../DTO/send-email.dto";
export class EmailService {
private readonly logger = new Logger(EmailService.name);
constructor(private readonly mailServerService: MailServerService) {}
constructor(
private readonly mailServerService: MailServerService,
private readonly userService: UsersService,
) {}
//########################################################
async sendEmail(userId: string, sendEmailDto: SendEmailDto) {
this.logger.log(`Sending email from user ${userId} to ${sendEmailDto.to.map((t) => t.address).join(", ")}`);
const userEmailId = await this.userService.getUserEmailIdFromId(userId);
this.logger.log(`Sending email from user ${userEmailId} to ${sendEmailDto.to.map((t) => t.address).join(", ")}`);
try {
const response = await firstValueFrom(this.mailServerService.submission.submitMessage(userId, sendEmailDto));
const response = await firstValueFrom(this.mailServerService.submission.submitMessage(userEmailId, sendEmailDto));
this.logger.log(`Email sent successfully: ${response.id} (Queue: ${response.queueId})`);
@@ -31,7 +37,7 @@ export class EmailService {
created: response.created,
};
} catch (error) {
this.logger.error(`Failed to send email for user ${userId}: ${error instanceof Error ? error.message : "Unknown error"}`);
this.logger.error(`Failed to send email for user ${userEmailId}: ${error instanceof Error ? error.message : "Unknown error"}`);
throw error;
}
}
@@ -77,13 +83,13 @@ export class EmailService {
}
//########################################################
async listMessages(userId: string, mailboxId: string, query: MessageListQueryDto) {
this.logger.log(`Listing messages in mailbox ${mailboxId} for user: ${userId}`);
async listMessages(userId: string, inboxId: string, query: MessageListQueryDto) {
this.logger.log(`Listing messages in mailbox ${inboxId} for user: ${userId}`);
try {
const response = await firstValueFrom(this.mailServerService.messages.listMessages(userId, mailboxId, query));
const response = await firstValueFrom(this.mailServerService.messages.listMessages(userId, inboxId, query));
this.logger.log(`Found ${response.results.length} messages in mailbox ${mailboxId} for user: ${userId}`);
this.logger.log(`Found ${response.results.length} messages in mailbox ${inboxId} for user: ${userId}`);
return {
message: EmailMessage.MESSAGES_LISTED_SUCCESSFULLY,
@@ -91,7 +97,7 @@ export class EmailService {
};
} catch (error) {
this.logger.error(
`Failed to list messages for mailbox ${mailboxId} and user ${userId}: ${error instanceof Error ? error.message : "Unknown error"}`,
`Failed to list messages for mailbox ${inboxId} and user ${userId}: ${error instanceof Error ? error.message : "Unknown error"}`,
);
throw error;
}