chore: fix

This commit is contained in:
mahyargdz
2025-07-10 10:55:35 +03:30
parent 784f168e8c
commit 85fdc7af4e
9 changed files with 388 additions and 56 deletions
+149 -29
View File
@@ -1,10 +1,11 @@
import { Injectable, Logger } from "@nestjs/common";
import { firstValueFrom } from "rxjs";
import { MailboxResolverService } from "./mailbox-resolver.service";
import { EmailMessage } from "../../../common/enums/message.enum";
import { MailServerService } from "../../mail-server/services/mail-server.service";
import { MessageListQueryDto, SearchMessagesQueryDto } from "../DTO/email-query.dto";
import { SendEmailDto } from "../DTO/send-email.dto";
import { SendEmailDto, UpdateDraftDto } from "../DTO/send-email.dto";
@Injectable()
export class EmailService {
@@ -12,6 +13,7 @@ export class EmailService {
constructor(
private readonly mailServerService: MailServerService,
private readonly mailboxResolverService: MailboxResolverService,
// private readonly userService: UsersService,
) {}
@@ -82,13 +84,14 @@ export class EmailService {
}
//########################################################
async listMessages(userId: string, inboxId: string, query: MessageListQueryDto) {
this.logger.log(`Listing messages in mailbox ${inboxId} for user: ${userId}`);
async listMessages(userId: string, query: MessageListQueryDto) {
try {
const { results, total: count } = await firstValueFrom(this.mailServerService.messages.listMessages(userId, inboxId, query));
const inboxMailboxId = await this.mailboxResolverService.getInboxMailboxId(userId);
this.logger.log(`Listing messages in mailbox ${inboxMailboxId} for user: ${userId}`);
this.logger.log(`Found ${count} messages in mailbox ${inboxId} for user: ${userId}`);
const { results, total: count } = await firstValueFrom(this.mailServerService.messages.listMessages(userId, inboxMailboxId, query));
this.logger.log(`Found ${count} messages in mailbox ${inboxMailboxId} for user: ${userId}`);
return {
message: EmailMessage.MESSAGES_LISTED_SUCCESSFULLY,
@@ -97,20 +100,19 @@ export class EmailService {
paginate: true,
};
} catch (error) {
this.logger.error(
`Failed to list messages for mailbox ${inboxId} and user ${userId}: ${error instanceof Error ? error.message : "Unknown error"}`,
);
this.logger.error(`Failed to list messages for user ${userId}: ${error instanceof Error ? error.message : "Unknown error"}`);
throw error;
}
}
//########################################################
async getMessage(userId: string, inboxId: string, messageId: number) {
this.logger.log(`Getting message ${messageId} from inbox ${inboxId} for user: ${userId}`);
async getMessage(userId: string, messageId: number) {
try {
await this.markMessageAsSeen(userId, inboxId, messageId);
const message = await firstValueFrom(this.mailServerService.messages.getMessage(userId, inboxId, messageId));
const inboxMailboxId = await this.mailboxResolverService.getInboxMailboxId(userId);
this.logger.log(`Getting message ${messageId} from inbox ${inboxMailboxId} for user: ${userId}`);
await this.markMessageAsSeen(userId, messageId);
const message = await firstValueFrom(this.mailServerService.messages.getMessage(userId, inboxMailboxId, messageId));
this.logger.log(`Retrieved message ${messageId} for user: ${userId}`);
@@ -124,9 +126,10 @@ export class EmailService {
}
//########################################################
async deleteMessage(userId: string, inboxId: string, messageId: number) {
async deleteMessage(userId: string, messageId: number) {
try {
const result = await firstValueFrom(this.mailServerService.messages.deleteMessage(userId, inboxId, messageId));
const inboxMailboxId = await this.mailboxResolverService.getInboxMailboxId(userId);
const result = await firstValueFrom(this.mailServerService.messages.deleteMessage(userId, inboxMailboxId, messageId));
return {
success: true,
@@ -140,9 +143,10 @@ export class EmailService {
}
//==============================================
async markMessageAsSeen(userId: string, inboxId: string, messageId: number) {
async markMessageAsSeen(userId: string, messageId: number) {
try {
const result = await firstValueFrom(this.mailServerService.messages.updateMessage(userId, inboxId, messageId, { seen: true }));
const inboxMailboxId = await this.mailboxResolverService.getInboxMailboxId(userId);
const result = await firstValueFrom(this.mailServerService.messages.updateMessage(userId, inboxMailboxId, messageId, { seen: true }));
return {
success: true,
@@ -158,14 +162,15 @@ export class EmailService {
}
//==============================================
async getSentMessages(userId: string, sentId: string, query: MessageListQueryDto) {
async getSentMessages(userId: string, query: MessageListQueryDto) {
try {
const result = await firstValueFrom(this.mailServerService.messages.listMessages(userId, sentId, query));
const sentMailboxId = await this.mailboxResolverService.getSentMailboxId(userId);
const { results: sentMails, total: count } = await firstValueFrom(this.mailServerService.messages.listMessages(userId, sentMailboxId, query));
return {
success: true,
message: EmailMessage.SENT_MESSAGES_RETRIEVED_SUCCESSFULLY,
data: result,
sentMails,
count,
paginate: true,
};
} catch (error) {
this.logger.error(`Failed to get sent messages for user ${userId}: ${error instanceof Error ? error.message : "Unknown error"}`);
@@ -174,18 +179,133 @@ export class EmailService {
}
//==============================================
async getTrashMessages(userId: string, trashId: string, query: MessageListQueryDto) {
async getTrashMessages(userId: string, query: MessageListQueryDto) {
try {
const result = await firstValueFrom(this.mailServerService.messages.listMessages(userId, trashId, query));
const trashMailboxId = await this.mailboxResolverService.getTrashMailboxId(userId);
const { results: trashMails, total: count } = await firstValueFrom(this.mailServerService.messages.listMessages(userId, trashMailboxId, query));
return {
success: true,
message: EmailMessage.TRASH_MESSAGES_RETRIEVED_SUCCESSFULLY,
data: result,
trashMails,
count,
paginate: true,
};
} catch (error) {
this.logger.error(`Failed to get trash messages for user ${userId}: ${error instanceof Error ? error.message : "Unknown error"}`);
throw error;
}
}
async getDraftsMessages(userId: string, query: MessageListQueryDto) {
try {
const draftsMailboxId = await this.mailboxResolverService.getDraftsMailboxId(userId);
const { results: draftsMail, total: count } = await firstValueFrom(
this.mailServerService.messages.listMessages(userId, draftsMailboxId, query),
);
return {
draftsMail,
count,
paginate: true,
};
} catch (error) {
this.logger.error(`Failed to get drafts messages for user ${userId}: ${error instanceof Error ? error.message : "Unknown error"}`);
throw error;
}
}
//==============================================
async updateDraft(userEmailId: string, messageId: number, updateDraftDto: UpdateDraftDto) {
this.logger.log(`Updating draft ${messageId} for user ${userEmailId}`);
try {
const draftsMailboxId = await this.mailboxResolverService.getDraftsMailboxId(userEmailId);
const existingDraft = await firstValueFrom(this.mailServerService.messages.getMessage(userEmailId, draftsMailboxId, messageId));
const draftData: SendEmailDto = {
mailbox: updateDraftDto.mailbox,
from: updateDraftDto.from || { address: existingDraft.from?.address || "" },
to: updateDraftDto.to || [],
cc: updateDraftDto.cc,
bcc: updateDraftDto.bcc,
replyTo: updateDraftDto.replyTo,
headers: updateDraftDto.headers,
subject: updateDraftDto.subject,
text: updateDraftDto.text,
html: updateDraftDto.html,
attachments: updateDraftDto.attachments,
meta: updateDraftDto.meta,
sess: updateDraftDto.sess,
ip: updateDraftDto.ip,
reference: updateDraftDto.reference,
isDraft: true,
uploadOnly: true,
draft: {
mailbox: draftsMailboxId,
id: messageId,
},
};
const response = await firstValueFrom(this.mailServerService.submission.submitMessage(userEmailId, draftData));
this.logger.log(`Draft updated successfully: ${response.id}`);
return {
message: EmailMessage.DRAFT_UPDATED_SUCCESSFULLY,
messageId: response.id,
from: response.from,
to: response.to,
subject: response.subject || "Draft",
updated: response.created,
isDraft: true,
};
} catch (error) {
this.logger.error(`Failed to update draft ${messageId} for user ${userEmailId}: ${error instanceof Error ? error.message : "Unknown error"}`);
throw error;
}
}
//==============================================
async sendDraft(userId: string, messageId: number) {
this.logger.log(`Sending draft ${messageId} for user: ${userId}`);
try {
const draftsMailboxId = await this.mailboxResolverService.getDraftsMailboxId(userId);
const result = await firstValueFrom(
this.mailServerService.messages.submitDraft(userId, draftsMailboxId, messageId, {
deleteFiles: false,
}),
);
this.logger.log(`Draft ${messageId} sent successfully`);
return {
message: EmailMessage.DRAFT_SENT_SUCCESSFULLY,
messageId: result.id,
queueId: result.queueId,
result,
};
} catch (error) {
this.logger.error(`Failed to send draft ${messageId} for user ${userId}: ${error instanceof Error ? error.message : "Unknown error"}`);
throw error;
}
}
//==============================================
async deleteDraft(userId: string, messageId: number) {
this.logger.log(`Deleting draft ${messageId} for user: ${userId}`);
try {
const draftsMailboxId = await this.mailboxResolverService.getDraftsMailboxId(userId);
const result = await firstValueFrom(this.mailServerService.messages.deleteMessage(userId, draftsMailboxId, messageId));
this.logger.log(`Draft ${messageId} deleted successfully`);
return {
message: EmailMessage.DRAFT_DELETED_SUCCESSFULLY,
result,
};
} catch (error) {
this.logger.error(`Failed to delete draft ${messageId} for user ${userId}: ${error instanceof Error ? error.message : "Unknown error"}`);
throw error;
}
}
}