email settings
This commit is contained in:
1 parent
819c0ca05a
commit
3faff5dc54
16 files changed
+665
-33
No files matched your search
@@ -1,16 +1,22 @@
|
||||
import { Request, Response, NextFunction } from "express";
|
||||
import ApiError from "@/utils/ApiError.js";
|
||||
import SettingsService from "@/services/system/SettingsService.js";
|
||||
import { NotificationService, SettingsService } from "@/services/index.js";
|
||||
|
||||
export interface ISettingsController {
|
||||
get: (req: Request, res: Response, next: NextFunction) => void;
|
||||
update: (req: Request, res: Response, next: NextFunction) => void;
|
||||
testTransport: (req: Request, res: Response, next: NextFunction) => void;
|
||||
}
|
||||
|
||||
class SettingsController implements ISettingsController {
|
||||
private settingsService: SettingsService;
|
||||
constructor(settingsService: SettingsService) {
|
||||
private notificationService: NotificationService;
|
||||
constructor(
|
||||
settingsService: SettingsService,
|
||||
notificationService: NotificationService
|
||||
) {
|
||||
this.settingsService = settingsService;
|
||||
this.notificationService = notificationService;
|
||||
}
|
||||
|
||||
get = async (req: Request, res: Response, next: NextFunction) => {
|
||||
@@ -36,6 +42,21 @@ class SettingsController implements ISettingsController {
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
testTransport = async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const testResult = await this.notificationService.testEmailTransport(
|
||||
req.body
|
||||
);
|
||||
|
||||
res.status(200).json({
|
||||
message: "Test transport successful",
|
||||
data: testResult,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export default SettingsController;
|
||||
@@ -67,7 +67,8 @@ export const initControllers = (services: any) => {
|
||||
);
|
||||
|
||||
controllers.settingsController = new SettingsController(
|
||||
services.settingsService
|
||||
services.settingsService,
|
||||
services.notificationService
|
||||
);
|
||||
|
||||
return controllers;
|
||||
|
||||
@@ -34,6 +34,14 @@ class SettingsRoutes {
|
||||
validateBody(systemSettingsSchema),
|
||||
this.controller.update
|
||||
);
|
||||
this.router.post(
|
||||
"/test-transport",
|
||||
verifyToken,
|
||||
addUserContext,
|
||||
verifyTeamPermission([PERMISSIONS.master]),
|
||||
validateBody(systemSettingsSchema),
|
||||
this.controller.testTransport
|
||||
);
|
||||
};
|
||||
|
||||
getRouter() {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -537,24 +537,45 @@ export const patchIncidentsBodySchema = z.object({
|
||||
|
||||
export const systemSettingsSchema = z
|
||||
.object({
|
||||
systemEmailHost: z.string().trim().min(1).max(255).optional(),
|
||||
systemEmailPort: z
|
||||
.union([z.number().int(), z.string().regex(/^\d+$/)])
|
||||
.optional()
|
||||
.transform((val) => (typeof val === "string" ? Number(val) : val))
|
||||
.refine(
|
||||
(val) =>
|
||||
val === undefined ||
|
||||
(Number.isInteger(val) && val > 0 && val < 65536),
|
||||
{
|
||||
message: "Port must be between 1 and 65535",
|
||||
}
|
||||
),
|
||||
systemEmailAddress: z.email().optional(),
|
||||
systemEmailPassword: z.string().optional(),
|
||||
systemEmailUser: z.string().trim().max(255).optional(),
|
||||
systemEmailConnectionHost: z.string().trim().max(255).optional(),
|
||||
systemEmailTLSServername: z.string().trim().max(255).optional(),
|
||||
systemEmailHost: z.preprocess(
|
||||
(val) => (val === "" || val === null ? undefined : val),
|
||||
z.string().trim().min(1).max(255).optional()
|
||||
),
|
||||
systemEmailPort: z.preprocess(
|
||||
(val) => (val === "" || val === null ? undefined : val),
|
||||
z
|
||||
.union([z.number().int(), z.string().regex(/^\d+$/)])
|
||||
.transform((port) => (typeof port === "string" ? Number(port) : port))
|
||||
.refine(
|
||||
(port) =>
|
||||
port === undefined ||
|
||||
(Number.isInteger(port) && port > 0 && port < 65536),
|
||||
{
|
||||
message: "Port must be between 1 and 65535",
|
||||
}
|
||||
)
|
||||
.optional()
|
||||
),
|
||||
systemEmailAddress: z.preprocess(
|
||||
(val) => (val === "" || val === null ? undefined : val),
|
||||
z.string().email().optional()
|
||||
),
|
||||
systemEmailPassword: z.preprocess(
|
||||
(val) => (val === "" || val === null ? undefined : val),
|
||||
z.string().trim().max(255).optional()
|
||||
),
|
||||
systemEmailUser: z.preprocess(
|
||||
(val) => (val === "" || val === null ? undefined : val),
|
||||
z.string().trim().max(255).optional()
|
||||
),
|
||||
systemEmailConnectionHost: z.preprocess(
|
||||
(val) => (val === "" || val === null ? undefined : val),
|
||||
z.string().trim().max(255).optional()
|
||||
),
|
||||
systemEmailTLSServername: z.preprocess(
|
||||
(val) => (val === "" || val === null ? undefined : val),
|
||||
z.string().trim().max(255).optional()
|
||||
),
|
||||
systemEmailSecure: z
|
||||
.union([z.boolean(), z.string()])
|
||||
.optional()
|
||||
|
||||
Reference in new issue
Block a user