mirror of
https://github.com/fosrl/pangolin.git
synced 2026-08-07 21:19:03 +02:00
🚧 wip
This commit is contained in:
@@ -1,6 +1,11 @@
|
|||||||
import logger from "@server/logger";
|
import logger from "@server/logger";
|
||||||
import type { TestAlertContext } from "@server/routers/alertRule/types";
|
import type {
|
||||||
|
EmailAlertAction,
|
||||||
|
TestAlertContext
|
||||||
|
} from "@server/routers/alertRule/types";
|
||||||
import { sendAlertEmail } from "./sendAlertEmail";
|
import { sendAlertEmail } from "./sendAlertEmail";
|
||||||
|
import type { db, alertEmailRecipients, users, userOrgRoles } from "@server/db";
|
||||||
|
import type { eq } from "drizzle-orm";
|
||||||
|
|
||||||
export async function processTestAlerts(context: TestAlertContext) {
|
export async function processTestAlerts(context: TestAlertContext) {
|
||||||
const emailActions = context.actions.filter(
|
const emailActions = context.actions.filter(
|
||||||
@@ -9,9 +14,7 @@ export async function processTestAlerts(context: TestAlertContext) {
|
|||||||
// Process email actions
|
// Process email actions
|
||||||
for (const action of emailActions) {
|
for (const action of emailActions) {
|
||||||
try {
|
try {
|
||||||
const recipients = await resolveEmailRecipients(
|
const recipients = await resolveEmailRecipients(action);
|
||||||
action.emailActionId
|
|
||||||
);
|
|
||||||
if (recipients.length > 0) {
|
if (recipients.length > 0) {
|
||||||
await sendAlertEmail(recipients, context);
|
await sendAlertEmail(recipients, context);
|
||||||
}
|
}
|
||||||
@@ -20,3 +23,51 @@ export async function processTestAlerts(context: TestAlertContext) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolves all email addresses for a given `emailActionId`.
|
||||||
|
*
|
||||||
|
* Recipients may be:
|
||||||
|
* - Direct users (by `userId`)
|
||||||
|
* - All users in a role (by `roleId`, resolved via `userOrgRoles`)
|
||||||
|
* - Direct external email addresses
|
||||||
|
*/
|
||||||
|
async function resolveEmailRecipients(
|
||||||
|
action: EmailAlertAction
|
||||||
|
): Promise<string[]> {
|
||||||
|
const emailSet = new Set<string>();
|
||||||
|
|
||||||
|
// for (const row of rows) {
|
||||||
|
// if (row.email) {
|
||||||
|
// emailSet.add(row.email);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if (row.userId) {
|
||||||
|
// const [user] = await db
|
||||||
|
// .select({ email: users.email })
|
||||||
|
// .from(users)
|
||||||
|
// .where(eq(users.userId, row.userId))
|
||||||
|
// .limit(1);
|
||||||
|
// if (user?.email) {
|
||||||
|
// emailSet.add(user.email);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if (row.roleId) {
|
||||||
|
// // Find all users with this role via userOrgRoles
|
||||||
|
// const roleUsers = await db
|
||||||
|
// .select({ email: users.email })
|
||||||
|
// .from(userOrgRoles)
|
||||||
|
// .innerJoin(users, eq(userOrgRoles.userId, users.userId))
|
||||||
|
// .where(eq(userOrgRoles.roleId, Number(row.roleId)));
|
||||||
|
|
||||||
|
// for (const u of roleUsers) {
|
||||||
|
// if (u.email) {
|
||||||
|
// emailSet.add(u.email);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
return Array.from(emailSet);
|
||||||
|
}
|
||||||
|
|||||||
@@ -125,14 +125,14 @@ export interface AlertContext {
|
|||||||
data: Record<string, unknown>;
|
data: Record<string, unknown>;
|
||||||
}
|
}
|
||||||
|
|
||||||
type EmailAlertAction = {
|
export type EmailAlertAction = {
|
||||||
type: "email";
|
type: "email";
|
||||||
userIds?: string[];
|
userIds?: string[];
|
||||||
roleIds?: string[];
|
roleIds?: string[];
|
||||||
emails?: string[];
|
emails?: string[];
|
||||||
};
|
};
|
||||||
|
|
||||||
type WebhookAlertAction = {
|
export type WebhookAlertAction = {
|
||||||
type: "webhook";
|
type: "webhook";
|
||||||
webhookUrl: string;
|
webhookUrl: string;
|
||||||
enabled: boolean;
|
enabled: boolean;
|
||||||
@@ -143,6 +143,7 @@ type AlertAction = EmailAlertAction | WebhookAlertAction;
|
|||||||
export interface TestAlertContext {
|
export interface TestAlertContext {
|
||||||
eventType: AlertEventType;
|
eventType: AlertEventType;
|
||||||
actions: AlertAction[];
|
actions: AlertAction[];
|
||||||
|
orgId: string;
|
||||||
/** Human-readable context data included in emails and webhook payloads */
|
/** Human-readable context data included in emails and webhook payloads */
|
||||||
data: Record<string, unknown>;
|
data: Record<string, unknown>;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user