144 lines
5.3 KiB
TypeScript
144 lines
5.3 KiB
TypeScript
import { Injectable, Logger } from "@nestjs/common";
|
|
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";
|
|
|
|
@Injectable()
|
|
export class EmailService {
|
|
private readonly logger = new Logger(EmailService.name);
|
|
|
|
constructor(
|
|
private readonly mailServerService: MailServerService,
|
|
private readonly userService: UsersService,
|
|
) {}
|
|
|
|
//########################################################
|
|
async sendEmail(userId: string, sendEmailDto: SendEmailDto) {
|
|
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(userEmailId, sendEmailDto));
|
|
|
|
this.logger.log(`Email sent successfully: ${response.id} (Queue: ${response.queueId})`);
|
|
|
|
return {
|
|
message: EmailMessage.EMAIL_SENDING_SUCCESS,
|
|
messageId: response.id,
|
|
queueId: response.queueId,
|
|
from: response.from,
|
|
to: response.to,
|
|
subject: response.subject,
|
|
created: response.created,
|
|
};
|
|
} catch (error) {
|
|
this.logger.error(`Failed to send email for user ${userEmailId}: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
//########################################################
|
|
async getEmailStatus(queueId: string) {
|
|
this.logger.log(`Getting email status for queue: ${queueId}`);
|
|
|
|
try {
|
|
const response = await firstValueFrom(this.mailServerService.submission.getQueueStatus(queueId));
|
|
|
|
this.logger.log(`Retrieved status for queue ${queueId}: ${response.status}`);
|
|
|
|
return {
|
|
message: EmailMessage.EMAIL_STATUS_RETRIEVED_SUCCESSFULLY,
|
|
...response,
|
|
};
|
|
} catch (error) {
|
|
this.logger.error(`Failed to get email status for queue ${queueId}: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
//########################################################
|
|
async searchMessages(userId: string, query: SearchMessagesQueryDto) {
|
|
this.logger.log(`Searching messages for user ${userId} with query: ${query.q}`);
|
|
|
|
try {
|
|
const response = await firstValueFrom(
|
|
this.mailServerService.messages.searchMessages(userId, { query: query.q || "", limit: query.limit, page: query.page }),
|
|
);
|
|
|
|
this.logger.log(`Found ${response.results.length} messages matching search for user: ${userId}`);
|
|
|
|
return {
|
|
message: EmailMessage.MESSAGES_SEARCHED_SUCCESSFULLY,
|
|
...response,
|
|
};
|
|
} catch (error) {
|
|
this.logger.error(`Failed to search messages for user ${userId}: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
//########################################################
|
|
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, inboxId, query));
|
|
|
|
this.logger.log(`Found ${response.results.length} messages in mailbox ${inboxId} for user: ${userId}`);
|
|
|
|
return {
|
|
message: EmailMessage.MESSAGES_LISTED_SUCCESSFULLY,
|
|
...response,
|
|
};
|
|
} catch (error) {
|
|
this.logger.error(
|
|
`Failed to list messages for mailbox ${inboxId} and user ${userId}: ${error instanceof Error ? error.message : "Unknown error"}`,
|
|
);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
//########################################################
|
|
async getMessage(userId: string, mailboxId: string, messageId: number) {
|
|
this.logger.log(`Getting message ${messageId} from mailbox ${mailboxId} for user: ${userId}`);
|
|
|
|
try {
|
|
const response = await firstValueFrom(this.mailServerService.messages.getMessage(userId, mailboxId, messageId));
|
|
|
|
this.logger.log(`Retrieved message ${messageId} for user: ${userId}`);
|
|
|
|
return {
|
|
message: EmailMessage.MESSAGE_RETRIEVED_SUCCESSFULLY,
|
|
...response,
|
|
};
|
|
} catch (error) {
|
|
this.logger.error(`Failed to get message ${messageId} for user ${userId}: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
//########################################################
|
|
async deleteMessage(userId: string, mailboxId: string, messageId: number) {
|
|
this.logger.log(`Deleting message ${messageId} from mailbox ${mailboxId} for user: ${userId}`);
|
|
|
|
try {
|
|
const response = await firstValueFrom(this.mailServerService.messages.deleteMessage(userId, mailboxId, messageId));
|
|
|
|
this.logger.log(`Deleted message ${messageId} for user: ${userId}`);
|
|
|
|
return {
|
|
message: EmailMessage.MESSAGE_DELETED_SUCCESSFULLY,
|
|
...response,
|
|
};
|
|
} catch (error) {
|
|
this.logger.error(`Failed to delete message ${messageId} for user ${userId}: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
throw error;
|
|
}
|
|
}
|
|
}
|