feat(notifications): add support for custom notifications in notification service

This commit is contained in:
ChristoferMendes
2025-09-26 17:06:45 -03:00
parent 1ce880bd6d
commit 39d0b9649f
+105 -13
View File
@@ -1,17 +1,20 @@
import { db } from "@dokploy/server/db"; import { db } from "@dokploy/server/db";
import { import {
type apiCreateCustom,
type apiCreateDiscord, type apiCreateDiscord,
type apiCreateEmail, type apiCreateEmail,
type apiCreateGotify, type apiCreateGotify,
type apiCreateNtfy, type apiCreateNtfy,
type apiCreateSlack, type apiCreateSlack,
type apiCreateTelegram, type apiCreateTelegram,
type apiUpdateCustom,
type apiUpdateDiscord, type apiUpdateDiscord,
type apiUpdateEmail, type apiUpdateEmail,
type apiUpdateGotify, type apiUpdateGotify,
type apiUpdateNtfy, type apiUpdateNtfy,
type apiUpdateSlack, type apiUpdateSlack,
type apiUpdateTelegram, type apiUpdateTelegram,
custom,
discord, discord,
email, email,
gotify, gotify,
@@ -27,7 +30,7 @@ export type Notification = typeof notifications.$inferSelect;
export const createSlackNotification = async ( export const createSlackNotification = async (
input: typeof apiCreateSlack._type, input: typeof apiCreateSlack._type,
organizationId: string, organizationId: string
) => { ) => {
await db.transaction(async (tx) => { await db.transaction(async (tx) => {
const newSlack = await tx const newSlack = await tx
@@ -75,7 +78,7 @@ export const createSlackNotification = async (
}; };
export const updateSlackNotification = async ( export const updateSlackNotification = async (
input: typeof apiUpdateSlack._type, input: typeof apiUpdateSlack._type
) => { ) => {
await db.transaction(async (tx) => { await db.transaction(async (tx) => {
const newDestination = await tx const newDestination = await tx
@@ -117,7 +120,7 @@ export const updateSlackNotification = async (
export const createTelegramNotification = async ( export const createTelegramNotification = async (
input: typeof apiCreateTelegram._type, input: typeof apiCreateTelegram._type,
organizationId: string, organizationId: string
) => { ) => {
await db.transaction(async (tx) => { await db.transaction(async (tx) => {
const newTelegram = await tx const newTelegram = await tx
@@ -166,7 +169,7 @@ export const createTelegramNotification = async (
}; };
export const updateTelegramNotification = async ( export const updateTelegramNotification = async (
input: typeof apiUpdateTelegram._type, input: typeof apiUpdateTelegram._type
) => { ) => {
await db.transaction(async (tx) => { await db.transaction(async (tx) => {
const newDestination = await tx const newDestination = await tx
@@ -209,7 +212,7 @@ export const updateTelegramNotification = async (
export const createDiscordNotification = async ( export const createDiscordNotification = async (
input: typeof apiCreateDiscord._type, input: typeof apiCreateDiscord._type,
organizationId: string, organizationId: string
) => { ) => {
await db.transaction(async (tx) => { await db.transaction(async (tx) => {
const newDiscord = await tx const newDiscord = await tx
@@ -257,7 +260,7 @@ export const createDiscordNotification = async (
}; };
export const updateDiscordNotification = async ( export const updateDiscordNotification = async (
input: typeof apiUpdateDiscord._type, input: typeof apiUpdateDiscord._type
) => { ) => {
await db.transaction(async (tx) => { await db.transaction(async (tx) => {
const newDestination = await tx const newDestination = await tx
@@ -299,7 +302,7 @@ export const updateDiscordNotification = async (
export const createEmailNotification = async ( export const createEmailNotification = async (
input: typeof apiCreateEmail._type, input: typeof apiCreateEmail._type,
organizationId: string, organizationId: string
) => { ) => {
await db.transaction(async (tx) => { await db.transaction(async (tx) => {
const newEmail = await tx const newEmail = await tx
@@ -351,7 +354,7 @@ export const createEmailNotification = async (
}; };
export const updateEmailNotification = async ( export const updateEmailNotification = async (
input: typeof apiUpdateEmail._type, input: typeof apiUpdateEmail._type
) => { ) => {
await db.transaction(async (tx) => { await db.transaction(async (tx) => {
const newDestination = await tx const newDestination = await tx
@@ -397,7 +400,7 @@ export const updateEmailNotification = async (
export const createGotifyNotification = async ( export const createGotifyNotification = async (
input: typeof apiCreateGotify._type, input: typeof apiCreateGotify._type,
organizationId: string, organizationId: string
) => { ) => {
await db.transaction(async (tx) => { await db.transaction(async (tx) => {
const newGotify = await tx const newGotify = await tx
@@ -446,7 +449,7 @@ export const createGotifyNotification = async (
}; };
export const updateGotifyNotification = async ( export const updateGotifyNotification = async (
input: typeof apiUpdateGotify._type, input: typeof apiUpdateGotify._type
) => { ) => {
await db.transaction(async (tx) => { await db.transaction(async (tx) => {
const newDestination = await tx const newDestination = await tx
@@ -487,7 +490,7 @@ export const updateGotifyNotification = async (
export const createNtfyNotification = async ( export const createNtfyNotification = async (
input: typeof apiCreateNtfy._type, input: typeof apiCreateNtfy._type,
organizationId: string, organizationId: string
) => { ) => {
await db.transaction(async (tx) => { await db.transaction(async (tx) => {
const newNtfy = await tx const newNtfy = await tx
@@ -536,7 +539,7 @@ export const createNtfyNotification = async (
}; };
export const updateNtfyNotification = async ( export const updateNtfyNotification = async (
input: typeof apiUpdateNtfy._type, input: typeof apiUpdateNtfy._type
) => { ) => {
await db.transaction(async (tx) => { await db.transaction(async (tx) => {
const newDestination = await tx const newDestination = await tx
@@ -575,6 +578,94 @@ export const updateNtfyNotification = async (
}); });
}; };
export const createCustomNotification = async (
input: typeof apiCreateCustom._type,
organizationId: string
) => {
await db.transaction(async (tx) => {
const newCustom = await tx
.insert(custom)
.values({
endpoint: input.endpoint,
headers: input.headers,
})
.returning()
.then((value) => value[0]);
if (!newCustom) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Error input: Inserting custom",
});
}
const newDestination = await tx
.insert(notifications)
.values({
customId: newCustom.customId,
name: input.name,
appDeploy: input.appDeploy,
appBuildError: input.appBuildError,
databaseBackup: input.databaseBackup,
dokployRestart: input.dokployRestart,
dockerCleanup: input.dockerCleanup,
notificationType: "custom",
organizationId: organizationId,
serverThreshold: input.serverThreshold,
})
.returning()
.then((value) => value[0]);
if (!newDestination) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Error input: Inserting notification",
});
}
return newDestination;
});
};
export const updateCustomNotification = async (
input: typeof apiUpdateCustom._type
) => {
await db.transaction(async (tx) => {
const newDestination = await tx
.update(notifications)
.set({
name: input.name,
appDeploy: input.appDeploy,
appBuildError: input.appBuildError,
databaseBackup: input.databaseBackup,
dokployRestart: input.dokployRestart,
dockerCleanup: input.dockerCleanup,
organizationId: input.organizationId,
serverThreshold: input.serverThreshold,
})
.where(eq(notifications.notificationId, input.notificationId))
.returning()
.then((value) => value[0]);
if (!newDestination) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Error Updating notification",
});
}
await tx
.update(custom)
.set({
endpoint: input.endpoint,
headers: input.headers,
})
.where(eq(custom.customId, input.customId));
return newDestination;
});
};
export const findNotificationById = async (notificationId: string) => { export const findNotificationById = async (notificationId: string) => {
const notification = await db.query.notifications.findFirst({ const notification = await db.query.notifications.findFirst({
where: eq(notifications.notificationId, notificationId), where: eq(notifications.notificationId, notificationId),
@@ -585,6 +676,7 @@ export const findNotificationById = async (notificationId: string) => {
email: true, email: true,
gotify: true, gotify: true,
ntfy: true, ntfy: true,
custom: true,
}, },
}); });
if (!notification) { if (!notification) {
@@ -607,7 +699,7 @@ export const removeNotificationById = async (notificationId: string) => {
export const updateNotificationById = async ( export const updateNotificationById = async (
notificationId: string, notificationId: string,
notificationData: Partial<Notification>, notificationData: Partial<Notification>
) => { ) => {
const result = await db const result = await db
.update(notifications) .update(notifications)