🚧 write process test alert function

This commit is contained in:
Fred KISSIE
2026-08-11 20:52:13 +02:00
parent 403b8a12e4
commit 899c47e9a3
+61 -34
View File
@@ -1,11 +1,15 @@
import { db, userOrgRoles, users } from "@server/db";
import logger from "@server/logger"; import logger from "@server/logger";
import type { import type {
EmailAlertAction, EmailAlertAction,
TestAlertContext TestAlertContext,
WebhookAlertConfig
} from "@server/routers/alertRule/types"; } from "@server/routers/alertRule/types";
import { eq, inArray } from "drizzle-orm";
import { sendAlertEmail } from "./sendAlertEmail"; import { sendAlertEmail } from "./sendAlertEmail";
import type { db, alertEmailRecipients, users, userOrgRoles } from "@server/db"; import { decrypt } from "@server/lib/crypto";
import type { eq } from "drizzle-orm"; import config from "@server/lib/config";
import { sendAlertWebhook } from "./sendAlertWebhook";
export async function processTestAlerts(context: TestAlertContext) { export async function processTestAlerts(context: TestAlertContext) {
const emailActions = context.actions.filter( const emailActions = context.actions.filter(
@@ -19,7 +23,38 @@ export async function processTestAlerts(context: TestAlertContext) {
await sendAlertEmail(recipients, context); await sendAlertEmail(recipients, context);
} }
} catch (err) { } catch (err) {
logger.error(`processAlerts: failed to send alert email`, err); logger.error(`processTestAlerts: failed to send alert email`, err);
}
}
const webhookActions = context.actions.filter(
(action) => action.type === "webhook"
);
const serverSecret = config.getRawConfig().server.secret!;
for (const action of webhookActions) {
try {
let webhookConfig: WebhookAlertConfig = { authType: "none" };
if (action.config) {
try {
const decrypted = decrypt(action.config, serverSecret);
webhookConfig = JSON.parse(decrypted) as WebhookAlertConfig;
} catch (err) {
logger.error(
`processTestAlerts: failed to decrypt webhook`,
err
);
continue;
}
}
await sendAlertWebhook(action.webhookUrl, webhookConfig, context);
} catch (err) {
logger.error(
`processTestAlerts: failed to send alert webhook `,
err
);
} }
} }
} }
@@ -35,39 +70,31 @@ export async function processTestAlerts(context: TestAlertContext) {
async function resolveEmailRecipients( async function resolveEmailRecipients(
action: EmailAlertAction action: EmailAlertAction
): Promise<string[]> { ): Promise<string[]> {
const emailSet = new Set<string>(); const emailList: string[] = [];
// for (const row of rows) { emailList.push(...(action.emails ?? []));
// if (row.email) {
// emailSet.add(row.email);
// }
// if (row.userId) { if (action.userIds && action.userIds?.length > 0) {
// const [user] = await db const userList = await db
// .select({ email: users.email }) .select({ email: users.email })
// .from(users) .from(users)
// .where(eq(users.userId, row.userId)) .where(inArray(users.userId, action.userIds));
// .limit(1);
// if (user?.email) {
// emailSet.add(user.email);
// }
// }
// if (row.roleId) { emailList.push(
// // Find all users with this role via userOrgRoles ...userList.filter((u) => u.email !== null).map((u) => u.email!)
// const roleUsers = await db );
// .select({ email: users.email }) }
// .from(userOrgRoles) if (action.roleIds && action.roleIds?.length > 0) {
// .innerJoin(users, eq(userOrgRoles.userId, users.userId)) const userList = await db
// .where(eq(userOrgRoles.roleId, Number(row.roleId))); .select({ email: users.email })
.from(userOrgRoles)
.innerJoin(users, eq(userOrgRoles.userId, users.userId))
.where(inArray(userOrgRoles.roleId, action.roleIds.map(Number)));
// for (const u of roleUsers) { emailList.push(
// if (u.email) { ...userList.filter((u) => u.email !== null).map((u) => u.email!)
// emailSet.add(u.email); );
// } }
// }
// }
// }
return Array.from(emailSet); return [...new Set(emailList)];
} }