mirror of
https://github.com/Portabase/portabase.git
synced 2026-07-14 11:16:13 +02:00
Working on notifications channels.
This commit is contained in:
@@ -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,5 +1,4 @@
|
||||
// export type ProviderKind = 'slack' | 'smtp';
|
||||
export type ProviderKind = 'smtp';
|
||||
export type ProviderKind = 'slack' | 'smtp';
|
||||
|
||||
export interface DispatchResult {
|
||||
success: boolean;
|
||||
|
||||
Reference in New Issue
Block a user