feat(notifications): enhance notification router with custom notification handling and additional notification types

This commit is contained in:
ChristoferMendes
2025-09-26 17:07:40 -03:00
parent 39d0b9649f
commit b5bc384664
+72 -12
View File
@@ -1,4 +1,5 @@
import { import {
createCustomNotification,
createDiscordNotification, createDiscordNotification,
createEmailNotification, createEmailNotification,
createGotifyNotification, createGotifyNotification,
@@ -8,6 +9,7 @@ import {
findNotificationById, findNotificationById,
IS_CLOUD, IS_CLOUD,
removeNotificationById, removeNotificationById,
sendCustomNotification,
sendDiscordNotification, sendDiscordNotification,
sendEmailNotification, sendEmailNotification,
sendGotifyNotification, sendGotifyNotification,
@@ -15,6 +17,7 @@ import {
sendServerThresholdNotifications, sendServerThresholdNotifications,
sendSlackNotification, sendSlackNotification,
sendTelegramNotification, sendTelegramNotification,
updateCustomNotification,
updateDiscordNotification, updateDiscordNotification,
updateEmailNotification, updateEmailNotification,
updateGotifyNotification, updateGotifyNotification,
@@ -33,6 +36,7 @@ import {
} from "@/server/api/trpc"; } from "@/server/api/trpc";
import { db } from "@/server/db"; import { db } from "@/server/db";
import { import {
apiCreateCustom,
apiCreateDiscord, apiCreateDiscord,
apiCreateEmail, apiCreateEmail,
apiCreateGotify, apiCreateGotify,
@@ -40,12 +44,14 @@ import {
apiCreateSlack, apiCreateSlack,
apiCreateTelegram, apiCreateTelegram,
apiFindOneNotification, apiFindOneNotification,
apiTestCustomConnection,
apiTestDiscordConnection, apiTestDiscordConnection,
apiTestEmailConnection, apiTestEmailConnection,
apiTestGotifyConnection, apiTestGotifyConnection,
apiTestNtfyConnection, apiTestNtfyConnection,
apiTestSlackConnection, apiTestSlackConnection,
apiTestTelegramConnection, apiTestTelegramConnection,
apiUpdateCustom,
apiUpdateDiscord, apiUpdateDiscord,
apiUpdateEmail, apiUpdateEmail,
apiUpdateGotify, apiUpdateGotify,
@@ -64,7 +70,7 @@ export const notificationRouter = createTRPCRouter({
try { try {
return await createSlackNotification( return await createSlackNotification(
input, input,
ctx.session.activeOrganizationId, ctx.session.activeOrganizationId
); );
} catch (error) { } catch (error) {
throw new TRPCError({ throw new TRPCError({
@@ -116,7 +122,7 @@ export const notificationRouter = createTRPCRouter({
try { try {
return await createTelegramNotification( return await createTelegramNotification(
input, input,
ctx.session.activeOrganizationId, ctx.session.activeOrganizationId
); );
} catch (error) { } catch (error) {
throw new TRPCError({ throw new TRPCError({
@@ -170,7 +176,7 @@ export const notificationRouter = createTRPCRouter({
try { try {
return await createDiscordNotification( return await createDiscordNotification(
input, input,
ctx.session.activeOrganizationId, ctx.session.activeOrganizationId
); );
} catch (error) { } catch (error) {
throw new TRPCError({ throw new TRPCError({
@@ -233,7 +239,7 @@ export const notificationRouter = createTRPCRouter({
try { try {
return await createEmailNotification( return await createEmailNotification(
input, input,
ctx.session.activeOrganizationId, ctx.session.activeOrganizationId
); );
} catch (error) { } catch (error) {
throw new TRPCError({ throw new TRPCError({
@@ -273,7 +279,7 @@ export const notificationRouter = createTRPCRouter({
await sendEmailNotification( await sendEmailNotification(
input, input,
"Test Email", "Test Email",
"<p>Hi, From Dokploy 👋</p>", "<p>Hi, From Dokploy 👋</p>"
); );
return true; return true;
} catch (error) { } catch (error) {
@@ -328,6 +334,7 @@ export const notificationRouter = createTRPCRouter({
email: true, email: true,
gotify: true, gotify: true,
ntfy: true, ntfy: true,
custom: true,
}, },
orderBy: desc(notifications.createdAt), orderBy: desc(notifications.createdAt),
where: eq(notifications.organizationId, ctx.session.activeOrganizationId), where: eq(notifications.organizationId, ctx.session.activeOrganizationId),
@@ -343,7 +350,7 @@ export const notificationRouter = createTRPCRouter({
Message: z.string(), Message: z.string(),
Timestamp: z.string(), Timestamp: z.string(),
Token: z.string(), Token: z.string(),
}), })
) )
.mutation(async ({ input }) => { .mutation(async ({ input }) => {
try { try {
@@ -354,7 +361,7 @@ export const notificationRouter = createTRPCRouter({
.select() .select()
.from(users_temp) .from(users_temp)
.where( .where(
sql`${users_temp.metricsConfig}::jsonb -> 'server' ->> 'token' = ${input.Token}`, sql`${users_temp.metricsConfig}::jsonb -> 'server' ->> 'token' = ${input.Token}`
); );
if (!result?.[0]?.id) { if (!result?.[0]?.id) {
@@ -371,7 +378,7 @@ export const notificationRouter = createTRPCRouter({
.select() .select()
.from(server) .from(server)
.where( .where(
sql`${server.metricsConfig}::jsonb -> 'server' ->> 'token' = ${input.Token}`, sql`${server.metricsConfig}::jsonb -> 'server' ->> 'token' = ${input.Token}`
); );
if (!result?.[0]?.organizationId) { if (!result?.[0]?.organizationId) {
@@ -403,7 +410,7 @@ export const notificationRouter = createTRPCRouter({
try { try {
return await createGotifyNotification( return await createGotifyNotification(
input, input,
ctx.session.activeOrganizationId, ctx.session.activeOrganizationId
); );
} catch (error) { } catch (error) {
throw new TRPCError({ throw new TRPCError({
@@ -442,7 +449,7 @@ export const notificationRouter = createTRPCRouter({
await sendGotifyNotification( await sendGotifyNotification(
input, input,
"Test Notification", "Test Notification",
"Hi, From Dokploy 👋", "Hi, From Dokploy 👋"
); );
return true; return true;
} catch (error) { } catch (error) {
@@ -459,7 +466,7 @@ export const notificationRouter = createTRPCRouter({
try { try {
return await createNtfyNotification( return await createNtfyNotification(
input, input,
ctx.session.activeOrganizationId, ctx.session.activeOrganizationId
); );
} catch (error) { } catch (error) {
throw new TRPCError({ throw new TRPCError({
@@ -500,7 +507,7 @@ export const notificationRouter = createTRPCRouter({
"Test Notification", "Test Notification",
"", "",
"view, visit Dokploy on Github, https://github.com/dokploy/dokploy, clear=true;", "view, visit Dokploy on Github, https://github.com/dokploy/dokploy, clear=true;",
"Hi, From Dokploy 👋", "Hi, From Dokploy 👋"
); );
return true; return true;
} catch (error) { } catch (error) {
@@ -511,6 +518,59 @@ export const notificationRouter = createTRPCRouter({
}); });
} }
}), }),
createCustom: adminProcedure
.input(apiCreateCustom)
.mutation(async ({ input, ctx }) => {
try {
return await createCustomNotification(
input,
ctx.session.activeOrganizationId
);
} catch (error) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Error creating the notification",
cause: error,
});
}
}),
updateCustom: adminProcedure
.input(apiUpdateCustom)
.mutation(async ({ input, ctx }) => {
try {
const notification = await findNotificationById(input.notificationId);
if (notification.organizationId !== ctx.session.activeOrganizationId) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "You are not authorized to update this notification",
});
}
return await updateCustomNotification({
...input,
organizationId: ctx.session.activeOrganizationId,
});
} catch (error) {
throw error;
}
}),
testCustomConnection: adminProcedure
.input(apiTestCustomConnection)
.mutation(async ({ input }) => {
try {
await sendCustomNotification(input, {
title: "Test Notification",
message: "Hi, From Dokploy 👋",
timestamp: new Date().toISOString(),
});
return true;
} catch (error) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Error testing the notification",
cause: error,
});
}
}),
getEmailProviders: adminProcedure.query(async ({ ctx }) => { getEmailProviders: adminProcedure.query(async ({ ctx }) => {
return await db.query.notifications.findMany({ return await db.query.notifications.findMany({
where: eq(notifications.organizationId, ctx.session.activeOrganizationId), where: eq(notifications.organizationId, ctx.session.activeOrganizationId),