Working on notifications channels.

This commit is contained in:
charlesgauthereau
2025-11-29 23:12:06 +01:00
parent ac951378e8
commit 8980e6d4a8
10 changed files with 83 additions and 24 deletions
+4 -1
View File
@@ -11,8 +11,10 @@ export async function dispatchNotification(
payload: EventPayload,
policyId?: string,
channelId?: string,
organizationId?: string,
): Promise<DispatchResult> {
// // 1. Get policy + channel
// const policy = await db
// .select({
@@ -66,7 +68,7 @@ export async function dispatchNotification(
.values({
channelId: channel.id,
// policyId: policy.id,
// organizationId: organizationId || null,
organizationId: organizationId || null,
title: payload.title,
message: payload.message,
level: payload.level,
@@ -74,6 +76,7 @@ export async function dispatchNotification(
success: result.success,
error: result.success ? null : result.error,
providerResponse: result.response || null,
})
.returning({id: notificationLog.id});
@@ -1,13 +1,13 @@
"use server"
import type { ProviderKind, EventPayload, DispatchResult } from '../types';
// import { sendSlack } from './slack';
import { sendSmtp } from './smtp';
import type {ProviderKind, EventPayload, DispatchResult} from '../types';
import {sendSlack} from './slack';
import {sendSmtp} from './smtp';
const handlers: Record<
ProviderKind,
(config: any, payload: EventPayload) => Promise<DispatchResult>
> = {
// slack: sendSlack,
slack: sendSlack,
smtp: sendSmtp,
};
@@ -0,0 +1,46 @@
import type {EventPayload, DispatchResult} from '../types';
export async function sendSlack(
config: { slackWebhook: string },
payload: EventPayload
): Promise<DispatchResult> {
const {slackWebhook: webhookUrl} = config;
const text = `*${payload.title}*\n${payload.message}`;
const blocks = [
{
type: 'section',
text: {type: 'mrkdwn', text: `*${payload.title}*`},
},
{type: 'section', text: {type: 'mrkdwn', text: payload.message}},
payload.data
? {
type: 'context',
elements: [{type: 'mrkdwn', text: `*Data:* \`\`\`${JSON.stringify(payload.data, null, 2)}\`\`\``}],
}
: null,
].filter(Boolean);
const body = {
text,
blocks,
};
const res = await fetch(webhookUrl, {
method: 'POST',
body: JSON.stringify(body),
headers: {'Content-Type': 'application/json'},
});
if (!res.ok) {
const err = await res.text();
throw new Error(`Slack error: ${res.status} ${err}`);
}
return {
success: true,
provider: 'slack',
message: 'Sent to Slack',
response: await res.text(),
};
}
+1 -2
View File
@@ -1,5 +1,4 @@
// export type ProviderKind = 'slack' | 'smtp';
export type ProviderKind = 'smtp';
export type ProviderKind = 'slack' | 'smtp';
export interface DispatchResult {
success: boolean;