email settings

This commit is contained in:
Alex Holliday committed 2025-11-19 23:55:29 +00:00
1 parent 819c0ca05a
commit 3faff5dc54
16 files changed
+665 -33

No files matched your search

@@ -14,6 +14,7 @@ import {
} from "./NotificationServices/index.js";
import ApiError from "@/utils/ApiError.js";
import { getChildLogger } from "@/logger/Logger.js";
import type { IEmailTransport } from "./NotificationServices/Email.js";
const SERVICE_NAME = "NotificationService";
const logger = getChildLogger(SERVICE_NAME);
@@ -36,6 +37,7 @@ export interface INotificationService {
testNotificationChannel: (
notificationChannel: INotificationChannel
) => Promise<Boolean>;
testTransport: (transport: IEmailTransport) => Promise<boolean>;
}
class NotificationService implements INotificationService {
@@ -181,6 +183,10 @@ class NotificationService implements INotificationService {
}
return false;
};
testEmailTransport = async (transport: IEmailTransport) => {
return this.emailService.testTransport(transport);
};
}
export default NotificationService;
@@ -8,8 +8,24 @@ import nodemailer, { Transporter } from "nodemailer";
import { config } from "@/config/index.js";
import UserService from "../../business/UserService.js";
import ApiError from "@/utils/ApiError.js";
import { getChildLogger } from "@/logger/Logger.js";
const SERVICE_NAME = "EmailService";
const logger = getChildLogger(SERVICE_NAME);
export interface IEmailTransport {
systemEmailHost?: string;
systemEmailPort?: number;
systemEmailAddress?: string;
systemEmailPassword?: string;
systemEmailUser?: string;
systemEmailConnectionHost?: string;
systemEmailTLSServername?: string;
systemEmailSecure: boolean;
systemEmailPool: boolean;
systemEmailIgnoreTLS: boolean;
systemEmailRequireTLS: boolean;
systemEmailRejectUnauthorized: boolean;
}
export interface IEmailService extends IMessageService {
sendGeneric: (
@@ -17,6 +33,7 @@ export interface IEmailService extends IMessageService {
subject: string,
content: string
) => Promise<boolean>;
testTransport: (transport: IEmailTransport) => Promise<boolean>;
}
class EmailService implements IEmailService {
@@ -108,6 +125,51 @@ class EmailService implements IEmailService {
throw error;
}
};
testTransport = async (transport: IEmailTransport) => {
const host = (transport.systemEmailHost ?? "").trim();
const baseOptions = {
port: Number(transport.systemEmailPort),
secure: transport.systemEmailSecure,
auth: {
user: transport.systemEmailUser || transport.systemEmailAddress,
pass: transport.systemEmailPassword,
},
name: transport.systemEmailConnectionHost || undefined,
connectionTimeout: 5000,
tls: {
rejectUnauthorized: transport.systemEmailRejectUnauthorized,
ignoreTLS: transport.systemEmailIgnoreTLS,
requireTLS: transport.systemEmailRequireTLS,
servername: transport.systemEmailTLSServername,
},
};
const transportOptions = transport.systemEmailPool
? {
...baseOptions,
pool: true,
host,
}
: {
...baseOptions,
host: host || undefined,
};
const testTransport = nodemailer.createTransport(transportOptions);
try {
await testTransport.sendMail({
from: `"Checkmate Test" <${transport.systemEmailAddress}>`,
to: transport.systemEmailUser || transport.systemEmailAddress,
subject: "SMTP transport test",
text: "This is a transport verification message.",
});
return true;
} catch (error: any) {
logger.error(error);
throw new ApiError(`Email transport test failed`, 400);
}
};
}
export default EmailService;
@@ -15,12 +15,18 @@ class SettingsService implements ISettingsService {
}
get = async (): Promise<ISystemSettings> => {
let settings = await SystemSettings.findById("global");
let settings = await SystemSettings.findById("global").select(
"-_id -__v -createdAt -updatedAt"
);
if (!settings) {
settings = await SystemSettings.create({ _id: "global" });
await SystemSettings.create({ _id: "global" });
}
settings = await SystemSettings.findById("global").select(
"-_id -__v -createdAt -updatedAt"
);
if (!settings) {
throw new ApiError("Unable to load system settings", 500);
}