diff --git a/apps/dokploy/components/dashboard/settings/notifications/key-value-input.tsx b/apps/dokploy/components/dashboard/settings/notifications/key-value-input.tsx index b8d275e6c..4b73b0afb 100644 --- a/apps/dokploy/components/dashboard/settings/notifications/key-value-input.tsx +++ b/apps/dokploy/components/dashboard/settings/notifications/key-value-input.tsx @@ -6,157 +6,160 @@ import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; interface KeyValuePair { - key: string; - value: string; - enabled: boolean; + key: string; + value: string; + enabled: boolean; } interface KeyValueInputProps { - value: string; - onChange: (value: string) => void; - placeholder?: string; - label: string; - description?: string; + value: string; + onChange: (value: string) => void; + placeholder?: string; + label: string; + description?: string; } const createEmptyPair = (): KeyValuePair => ({ - key: "", - value: "", - enabled: true, + key: "", + value: "", + enabled: true, }); const parseJsonToPairs = (jsonString: string): KeyValuePair[] => { - try { - const parsed = JSON.parse(jsonString); - const pairs = Object.entries(parsed).map(([key, val]) => ({ - key, - value: String(val), - enabled: true, - })); - return pairs.length > 0 ? pairs : [createEmptyPair()]; - } catch { - return [createEmptyPair()]; - } + try { + const parsed = JSON.parse(jsonString); + const pairs = Object.entries(parsed).map(([key, val]) => ({ + key, + value: String(val), + enabled: true, + })); + return pairs.length > 0 ? pairs : [createEmptyPair()]; + } catch { + return [createEmptyPair()]; + } }; const convertPairsToJson = (pairs: KeyValuePair[]): string => { - const enabledPairs = pairs.filter((pair) => pair.enabled && pair.key.trim()); + const enabledPairs = pairs.filter((pair) => pair.enabled && pair.key.trim()); - if (enabledPairs.length === 0) { - return ""; - } + if (enabledPairs.length === 0) { + return ""; + } - const obj = enabledPairs.reduce((acc, pair) => { - acc[pair.key.trim()] = pair.value; - return acc; - }, {} as Record); + const obj = enabledPairs.reduce( + (acc, pair) => { + acc[pair.key.trim()] = pair.value; + return acc; + }, + {} as Record, + ); - return JSON.stringify(obj, null, 2); + return JSON.stringify(obj, null, 2); }; export const KeyValueInput = ({ - value, - onChange, - label, - description, + value, + onChange, + label, + description, }: KeyValueInputProps) => { - const [pairs, setPairs] = useState([]); + const [pairs, setPairs] = useState([]); - useEffect(() => { - const newPairs = value ? parseJsonToPairs(value) : [createEmptyPair()]; - setPairs(newPairs); - }, [value]); + useEffect(() => { + const newPairs = value ? parseJsonToPairs(value) : [createEmptyPair()]; + setPairs(newPairs); + }, [value]); - const syncPairsWithParent = (newPairs: KeyValuePair[]) => { - setPairs(newPairs); - onChange(convertPairsToJson(newPairs)); - }; + const syncPairsWithParent = (newPairs: KeyValuePair[]) => { + setPairs(newPairs); + onChange(convertPairsToJson(newPairs)); + }; - const addPair = () => { - syncPairsWithParent([...pairs, createEmptyPair()]); - }; + const addPair = () => { + syncPairsWithParent([...pairs, createEmptyPair()]); + }; - const removePair = (index: number) => { - const filteredPairs = pairs.filter((_, i) => i !== index); - syncPairsWithParent(filteredPairs); - }; + const removePair = (index: number) => { + const filteredPairs = pairs.filter((_, i) => i !== index); + syncPairsWithParent(filteredPairs); + }; - const updatePair = ( - index: number, - field: keyof KeyValuePair, - newValue: string | boolean - ) => { - const updatedPairs = pairs.map((pair, i) => - i === index ? { ...pair, [field]: newValue } : pair - ); - syncPairsWithParent(updatedPairs); - }; + const updatePair = ( + index: number, + field: keyof KeyValuePair, + newValue: string | boolean, + ) => { + const updatedPairs = pairs.map((pair, i) => + i === index ? { ...pair, [field]: newValue } : pair, + ); + syncPairsWithParent(updatedPairs); + }; - return ( -
-
- - {description && ( -

{description}

- )} -
+ return ( +
+
+ + {description && ( +

{description}

+ )} +
-
- {pairs.map((pair, index) => ( -
-
- - updatePair(index, "enabled", checked) - } - className="mr-2" - /> -
-
- updatePair(index, "key", e.target.value)} - className="text-sm" - disabled={!pair.enabled} - /> -
-
- updatePair(index, "value", e.target.value)} - className="text-sm" - disabled={!pair.enabled} - /> -
- -
- ))} -
+
+ {pairs.map((pair, index) => ( +
+
+ + updatePair(index, "enabled", checked) + } + className="mr-2" + /> +
+
+ updatePair(index, "key", e.target.value)} + className="text-sm" + disabled={!pair.enabled} + /> +
+
+ updatePair(index, "value", e.target.value)} + className="text-sm" + disabled={!pair.enabled} + /> +
+ +
+ ))} +
- -
- ); + +
+ ); }; diff --git a/apps/dokploy/components/dashboard/settings/notifications/show-notifications.tsx b/apps/dokploy/components/dashboard/settings/notifications/show-notifications.tsx index 866ed38ee..866e1df8b 100644 --- a/apps/dokploy/components/dashboard/settings/notifications/show-notifications.tsx +++ b/apps/dokploy/components/dashboard/settings/notifications/show-notifications.tsx @@ -1,164 +1,164 @@ import { - Bell, - Loader2, - Mail, - MessageCircleMore, - PenBoxIcon, - Trash2, + Bell, + Loader2, + Mail, + MessageCircleMore, + PenBoxIcon, + Trash2, } from "lucide-react"; import { toast } from "sonner"; import { - DiscordIcon, - SlackIcon, - TelegramIcon, + DiscordIcon, + SlackIcon, + TelegramIcon, } from "@/components/icons/notification-icons"; import { DialogAction } from "@/components/shared/dialog-action"; import { Button } from "@/components/ui/button"; import { - Card, - CardContent, - CardDescription, - CardHeader, - CardTitle, + Card, + CardContent, + CardDescription, + CardHeader, + CardTitle, } from "@/components/ui/card"; import { api } from "@/utils/api"; import { HandleNotifications } from "./handle-notifications"; export const ShowNotifications = () => { - const { data, isLoading, refetch } = api.notification.all.useQuery(); - const { mutateAsync, isLoading: isRemoving } = - api.notification.remove.useMutation(); + const { data, isLoading, refetch } = api.notification.all.useQuery(); + const { mutateAsync, isLoading: isRemoving } = + api.notification.remove.useMutation(); - return ( -
- -
- - - - Notifications - - - Add your providers to receive notifications, like Discord, Slack, - Telegram, Email. - - - - {isLoading ? ( -
- Loading... - -
- ) : ( - <> - {data?.length === 0 ? ( -
- - - To send notifications it is required to set at least 1 - provider. - - -
- ) : ( -
-
- {data?.map((notification, _index) => ( -
-
- - {notification.notificationType === "slack" && ( -
- -
- )} - {notification.notificationType === "telegram" && ( -
- -
- )} - {notification.notificationType === "discord" && ( -
- -
- )} - {notification.notificationType === "email" && ( -
- -
- )} - {notification.notificationType === "gotify" && ( -
- -
- )} - {notification.notificationType === "ntfy" && ( -
- -
- )} - {notification.notificationType === "custom" && ( -
- -
- )} + return ( +
+ +
+ + + + Notifications + + + Add your providers to receive notifications, like Discord, Slack, + Telegram, Email. + + + + {isLoading ? ( +
+ Loading... + +
+ ) : ( + <> + {data?.length === 0 ? ( +
+ + + To send notifications it is required to set at least 1 + provider. + + +
+ ) : ( +
+
+ {data?.map((notification, _index) => ( +
+
+ + {notification.notificationType === "slack" && ( +
+ +
+ )} + {notification.notificationType === "telegram" && ( +
+ +
+ )} + {notification.notificationType === "discord" && ( +
+ +
+ )} + {notification.notificationType === "email" && ( +
+ +
+ )} + {notification.notificationType === "gotify" && ( +
+ +
+ )} + {notification.notificationType === "ntfy" && ( +
+ +
+ )} + {notification.notificationType === "custom" && ( +
+ +
+ )} - {notification.name} -
-
- + {notification.name} + +
+ - { - await mutateAsync({ - notificationId: notification.notificationId, - }) - .then(() => { - toast.success( - "Notification deleted successfully" - ); - refetch(); - }) - .catch(() => { - toast.error( - "Error deleting notification" - ); - }); - }} - > - - -
-
-
- ))} -
+ { + await mutateAsync({ + notificationId: notification.notificationId, + }) + .then(() => { + toast.success( + "Notification deleted successfully", + ); + refetch(); + }) + .catch(() => { + toast.error( + "Error deleting notification", + ); + }); + }} + > + + +
+
+
+ ))} +
-
- -
-
- )} - - )} - -
- -
- ); +
+ +
+
+ )} + + )} +
+
+
+
+ ); }; diff --git a/apps/dokploy/server/api/routers/notification.ts b/apps/dokploy/server/api/routers/notification.ts index 34eed2ff1..d3c27f2a5 100644 --- a/apps/dokploy/server/api/routers/notification.ts +++ b/apps/dokploy/server/api/routers/notification.ts @@ -1,582 +1,582 @@ import { - createCustomNotification, - createDiscordNotification, - createEmailNotification, - createGotifyNotification, - createNtfyNotification, - createSlackNotification, - createTelegramNotification, - findNotificationById, - IS_CLOUD, - removeNotificationById, - sendCustomNotification, - sendDiscordNotification, - sendEmailNotification, - sendGotifyNotification, - sendNtfyNotification, - sendServerThresholdNotifications, - sendSlackNotification, - sendTelegramNotification, - updateCustomNotification, - updateDiscordNotification, - updateEmailNotification, - updateGotifyNotification, - updateNtfyNotification, - updateSlackNotification, - updateTelegramNotification, + createCustomNotification, + createDiscordNotification, + createEmailNotification, + createGotifyNotification, + createNtfyNotification, + createSlackNotification, + createTelegramNotification, + findNotificationById, + IS_CLOUD, + removeNotificationById, + sendCustomNotification, + sendDiscordNotification, + sendEmailNotification, + sendGotifyNotification, + sendNtfyNotification, + sendServerThresholdNotifications, + sendSlackNotification, + sendTelegramNotification, + updateCustomNotification, + updateDiscordNotification, + updateEmailNotification, + updateGotifyNotification, + updateNtfyNotification, + updateSlackNotification, + updateTelegramNotification, } from "@dokploy/server"; import { TRPCError } from "@trpc/server"; import { desc, eq, sql } from "drizzle-orm"; import { z } from "zod"; import { - adminProcedure, - createTRPCRouter, - protectedProcedure, - publicProcedure, + adminProcedure, + createTRPCRouter, + protectedProcedure, + publicProcedure, } from "@/server/api/trpc"; import { db } from "@/server/db"; import { - apiCreateCustom, - apiCreateDiscord, - apiCreateEmail, - apiCreateGotify, - apiCreateNtfy, - apiCreateSlack, - apiCreateTelegram, - apiFindOneNotification, - apiTestCustomConnection, - apiTestDiscordConnection, - apiTestEmailConnection, - apiTestGotifyConnection, - apiTestNtfyConnection, - apiTestSlackConnection, - apiTestTelegramConnection, - apiUpdateCustom, - apiUpdateDiscord, - apiUpdateEmail, - apiUpdateGotify, - apiUpdateNtfy, - apiUpdateSlack, - apiUpdateTelegram, - notifications, - server, - users_temp, + apiCreateCustom, + apiCreateDiscord, + apiCreateEmail, + apiCreateGotify, + apiCreateNtfy, + apiCreateSlack, + apiCreateTelegram, + apiFindOneNotification, + apiTestCustomConnection, + apiTestDiscordConnection, + apiTestEmailConnection, + apiTestGotifyConnection, + apiTestNtfyConnection, + apiTestSlackConnection, + apiTestTelegramConnection, + apiUpdateCustom, + apiUpdateDiscord, + apiUpdateEmail, + apiUpdateGotify, + apiUpdateNtfy, + apiUpdateSlack, + apiUpdateTelegram, + notifications, + server, + users_temp, } from "@/server/db/schema"; export const notificationRouter = createTRPCRouter({ - createSlack: adminProcedure - .input(apiCreateSlack) - .mutation(async ({ input, ctx }) => { - try { - return await createSlackNotification( - input, - ctx.session.activeOrganizationId - ); - } catch (error) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Error creating the notification", - cause: error, - }); - } - }), - updateSlack: adminProcedure - .input(apiUpdateSlack) - .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 updateSlackNotification({ - ...input, - organizationId: ctx.session.activeOrganizationId, - }); - } catch (error) { - throw error; - } - }), - testSlackConnection: adminProcedure - .input(apiTestSlackConnection) - .mutation(async ({ input }) => { - try { - await sendSlackNotification(input, { - channel: input.channel, - text: "Hi, From Dokploy ๐Ÿ‘‹", - }); - return true; - } catch (error) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Error testing the notification", - cause: error, - }); - } - }), - createTelegram: adminProcedure - .input(apiCreateTelegram) - .mutation(async ({ input, ctx }) => { - try { - return await createTelegramNotification( - input, - ctx.session.activeOrganizationId - ); - } catch (error) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Error creating the notification", - cause: error, - }); - } - }), + createSlack: adminProcedure + .input(apiCreateSlack) + .mutation(async ({ input, ctx }) => { + try { + return await createSlackNotification( + input, + ctx.session.activeOrganizationId, + ); + } catch (error) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error creating the notification", + cause: error, + }); + } + }), + updateSlack: adminProcedure + .input(apiUpdateSlack) + .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 updateSlackNotification({ + ...input, + organizationId: ctx.session.activeOrganizationId, + }); + } catch (error) { + throw error; + } + }), + testSlackConnection: adminProcedure + .input(apiTestSlackConnection) + .mutation(async ({ input }) => { + try { + await sendSlackNotification(input, { + channel: input.channel, + text: "Hi, From Dokploy ๐Ÿ‘‹", + }); + return true; + } catch (error) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error testing the notification", + cause: error, + }); + } + }), + createTelegram: adminProcedure + .input(apiCreateTelegram) + .mutation(async ({ input, ctx }) => { + try { + return await createTelegramNotification( + input, + ctx.session.activeOrganizationId, + ); + } catch (error) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error creating the notification", + cause: error, + }); + } + }), - updateTelegram: adminProcedure - .input(apiUpdateTelegram) - .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 updateTelegramNotification({ - ...input, - organizationId: ctx.session.activeOrganizationId, - }); - } catch (error) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Error updating the notification", - cause: error, - }); - } - }), - testTelegramConnection: adminProcedure - .input(apiTestTelegramConnection) - .mutation(async ({ input }) => { - try { - await sendTelegramNotification(input, "Hi, From Dokploy ๐Ÿ‘‹"); - return true; - } catch (error) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Error testing the notification", - cause: error, - }); - } - }), - createDiscord: adminProcedure - .input(apiCreateDiscord) - .mutation(async ({ input, ctx }) => { - try { - return await createDiscordNotification( - input, - ctx.session.activeOrganizationId - ); - } catch (error) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Error creating the notification", - cause: error, - }); - } - }), + updateTelegram: adminProcedure + .input(apiUpdateTelegram) + .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 updateTelegramNotification({ + ...input, + organizationId: ctx.session.activeOrganizationId, + }); + } catch (error) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error updating the notification", + cause: error, + }); + } + }), + testTelegramConnection: adminProcedure + .input(apiTestTelegramConnection) + .mutation(async ({ input }) => { + try { + await sendTelegramNotification(input, "Hi, From Dokploy ๐Ÿ‘‹"); + return true; + } catch (error) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error testing the notification", + cause: error, + }); + } + }), + createDiscord: adminProcedure + .input(apiCreateDiscord) + .mutation(async ({ input, ctx }) => { + try { + return await createDiscordNotification( + input, + ctx.session.activeOrganizationId, + ); + } catch (error) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error creating the notification", + cause: error, + }); + } + }), - updateDiscord: adminProcedure - .input(apiUpdateDiscord) - .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 updateDiscordNotification({ - ...input, - organizationId: ctx.session.activeOrganizationId, - }); - } catch (error) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Error updating the notification", - cause: error, - }); - } - }), + updateDiscord: adminProcedure + .input(apiUpdateDiscord) + .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 updateDiscordNotification({ + ...input, + organizationId: ctx.session.activeOrganizationId, + }); + } catch (error) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error updating the notification", + cause: error, + }); + } + }), - testDiscordConnection: adminProcedure - .input(apiTestDiscordConnection) - .mutation(async ({ input }) => { - try { - const decorate = (decoration: string, text: string) => - `${input.decoration ? decoration : ""} ${text}`.trim(); + testDiscordConnection: adminProcedure + .input(apiTestDiscordConnection) + .mutation(async ({ input }) => { + try { + const decorate = (decoration: string, text: string) => + `${input.decoration ? decoration : ""} ${text}`.trim(); - await sendDiscordNotification(input, { - title: decorate(">", "`๐Ÿคš` - Test Notification"), - description: decorate(">", "Hi, From Dokploy ๐Ÿ‘‹"), - color: 0xf3f7f4, - }); + await sendDiscordNotification(input, { + title: decorate(">", "`๐Ÿคš` - Test Notification"), + description: decorate(">", "Hi, From Dokploy ๐Ÿ‘‹"), + color: 0xf3f7f4, + }); - return true; - } catch (error) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Error testing the notification", - cause: error, - }); - } - }), - createEmail: adminProcedure - .input(apiCreateEmail) - .mutation(async ({ input, ctx }) => { - try { - return await createEmailNotification( - input, - ctx.session.activeOrganizationId - ); - } catch (error) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Error creating the notification", - cause: error, - }); - } - }), - updateEmail: adminProcedure - .input(apiUpdateEmail) - .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 updateEmailNotification({ - ...input, - organizationId: ctx.session.activeOrganizationId, - }); - } catch (error) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Error updating the notification", - cause: error, - }); - } - }), - testEmailConnection: adminProcedure - .input(apiTestEmailConnection) - .mutation(async ({ input }) => { - try { - await sendEmailNotification( - input, - "Test Email", - "

Hi, From Dokploy ๐Ÿ‘‹

" - ); - return true; - } catch (error) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Error testing the notification", - cause: error, - }); - } - }), - remove: adminProcedure - .input(apiFindOneNotification) - .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 delete this notification", - }); - } - return await removeNotificationById(input.notificationId); - } catch (error) { - const message = - error instanceof Error - ? error.message - : "Error deleting this notification"; - throw new TRPCError({ - code: "BAD_REQUEST", - message, - }); - } - }), - one: protectedProcedure - .input(apiFindOneNotification) - .query(async ({ input, ctx }) => { - const notification = await findNotificationById(input.notificationId); - if (notification.organizationId !== ctx.session.activeOrganizationId) { - throw new TRPCError({ - code: "UNAUTHORIZED", - message: "You are not authorized to access this notification", - }); - } - return notification; - }), - all: adminProcedure.query(async ({ ctx }) => { - return await db.query.notifications.findMany({ - with: { - slack: true, - telegram: true, - discord: true, - email: true, - gotify: true, - ntfy: true, - custom: true, - }, - orderBy: desc(notifications.createdAt), - where: eq(notifications.organizationId, ctx.session.activeOrganizationId), - }); - }), - receiveNotification: publicProcedure - .input( - z.object({ - ServerType: z.enum(["Dokploy", "Remote"]).default("Dokploy"), - Type: z.enum(["Memory", "CPU"]), - Value: z.number(), - Threshold: z.number(), - Message: z.string(), - Timestamp: z.string(), - Token: z.string(), - }) - ) - .mutation(async ({ input }) => { - try { - let organizationId = ""; - let ServerName = ""; - if (input.ServerType === "Dokploy") { - const result = await db - .select() - .from(users_temp) - .where( - sql`${users_temp.metricsConfig}::jsonb -> 'server' ->> 'token' = ${input.Token}` - ); + return true; + } catch (error) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error testing the notification", + cause: error, + }); + } + }), + createEmail: adminProcedure + .input(apiCreateEmail) + .mutation(async ({ input, ctx }) => { + try { + return await createEmailNotification( + input, + ctx.session.activeOrganizationId, + ); + } catch (error) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error creating the notification", + cause: error, + }); + } + }), + updateEmail: adminProcedure + .input(apiUpdateEmail) + .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 updateEmailNotification({ + ...input, + organizationId: ctx.session.activeOrganizationId, + }); + } catch (error) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error updating the notification", + cause: error, + }); + } + }), + testEmailConnection: adminProcedure + .input(apiTestEmailConnection) + .mutation(async ({ input }) => { + try { + await sendEmailNotification( + input, + "Test Email", + "

Hi, From Dokploy ๐Ÿ‘‹

", + ); + return true; + } catch (error) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error testing the notification", + cause: error, + }); + } + }), + remove: adminProcedure + .input(apiFindOneNotification) + .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 delete this notification", + }); + } + return await removeNotificationById(input.notificationId); + } catch (error) { + const message = + error instanceof Error + ? error.message + : "Error deleting this notification"; + throw new TRPCError({ + code: "BAD_REQUEST", + message, + }); + } + }), + one: protectedProcedure + .input(apiFindOneNotification) + .query(async ({ input, ctx }) => { + const notification = await findNotificationById(input.notificationId); + if (notification.organizationId !== ctx.session.activeOrganizationId) { + throw new TRPCError({ + code: "UNAUTHORIZED", + message: "You are not authorized to access this notification", + }); + } + return notification; + }), + all: adminProcedure.query(async ({ ctx }) => { + return await db.query.notifications.findMany({ + with: { + slack: true, + telegram: true, + discord: true, + email: true, + gotify: true, + ntfy: true, + custom: true, + }, + orderBy: desc(notifications.createdAt), + where: eq(notifications.organizationId, ctx.session.activeOrganizationId), + }); + }), + receiveNotification: publicProcedure + .input( + z.object({ + ServerType: z.enum(["Dokploy", "Remote"]).default("Dokploy"), + Type: z.enum(["Memory", "CPU"]), + Value: z.number(), + Threshold: z.number(), + Message: z.string(), + Timestamp: z.string(), + Token: z.string(), + }), + ) + .mutation(async ({ input }) => { + try { + let organizationId = ""; + let ServerName = ""; + if (input.ServerType === "Dokploy") { + const result = await db + .select() + .from(users_temp) + .where( + sql`${users_temp.metricsConfig}::jsonb -> 'server' ->> 'token' = ${input.Token}`, + ); - if (!result?.[0]?.id) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Token not found", - }); - } + if (!result?.[0]?.id) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Token not found", + }); + } - organizationId = result?.[0]?.id; - ServerName = "Dokploy"; - } else { - const result = await db - .select() - .from(server) - .where( - sql`${server.metricsConfig}::jsonb -> 'server' ->> 'token' = ${input.Token}` - ); + organizationId = result?.[0]?.id; + ServerName = "Dokploy"; + } else { + const result = await db + .select() + .from(server) + .where( + sql`${server.metricsConfig}::jsonb -> 'server' ->> 'token' = ${input.Token}`, + ); - if (!result?.[0]?.organizationId) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Token not found", - }); - } + if (!result?.[0]?.organizationId) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Token not found", + }); + } - organizationId = result?.[0]?.organizationId; - ServerName = "Remote"; - } + organizationId = result?.[0]?.organizationId; + ServerName = "Remote"; + } - await sendServerThresholdNotifications(organizationId, { - ...input, - ServerName, - }); - } catch (error) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Error sending the notification", - cause: error, - }); - } - }), - createGotify: adminProcedure - .input(apiCreateGotify) - .mutation(async ({ input, ctx }) => { - try { - return await createGotifyNotification( - input, - ctx.session.activeOrganizationId - ); - } catch (error) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Error creating the notification", - cause: error, - }); - } - }), - updateGotify: adminProcedure - .input(apiUpdateGotify) - .mutation(async ({ input, ctx }) => { - try { - const notification = await findNotificationById(input.notificationId); - if ( - IS_CLOUD && - notification.organizationId !== ctx.session.activeOrganizationId - ) { - throw new TRPCError({ - code: "UNAUTHORIZED", - message: "You are not authorized to update this notification", - }); - } - return await updateGotifyNotification({ - ...input, - organizationId: ctx.session.activeOrganizationId, - }); - } catch (error) { - throw error; - } - }), - testGotifyConnection: adminProcedure - .input(apiTestGotifyConnection) - .mutation(async ({ input }) => { - try { - await sendGotifyNotification( - input, - "Test Notification", - "Hi, From Dokploy ๐Ÿ‘‹" - ); - return true; - } catch (error) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Error testing the notification", - cause: error, - }); - } - }), - createNtfy: adminProcedure - .input(apiCreateNtfy) - .mutation(async ({ input, ctx }) => { - try { - return await createNtfyNotification( - input, - ctx.session.activeOrganizationId - ); - } catch (error) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Error creating the notification", - cause: error, - }); - } - }), - updateNtfy: adminProcedure - .input(apiUpdateNtfy) - .mutation(async ({ input, ctx }) => { - try { - const notification = await findNotificationById(input.notificationId); - if ( - IS_CLOUD && - notification.organizationId !== ctx.session.activeOrganizationId - ) { - throw new TRPCError({ - code: "UNAUTHORIZED", - message: "You are not authorized to update this notification", - }); - } - return await updateNtfyNotification({ - ...input, - organizationId: ctx.session.activeOrganizationId, - }); - } catch (error) { - throw error; - } - }), - testNtfyConnection: adminProcedure - .input(apiTestNtfyConnection) - .mutation(async ({ input }) => { - try { - await sendNtfyNotification( - input, - "Test Notification", - "", - "view, visit Dokploy on Github, https://github.com/dokploy/dokploy, clear=true;", - "Hi, From Dokploy ๐Ÿ‘‹" - ); - return true; - } catch (error) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Error testing the notification", - cause: error, - }); - } - }), - 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 }) => { - return await db.query.notifications.findMany({ - where: eq(notifications.organizationId, ctx.session.activeOrganizationId), - with: { - email: true, - }, - }); - }), + await sendServerThresholdNotifications(organizationId, { + ...input, + ServerName, + }); + } catch (error) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error sending the notification", + cause: error, + }); + } + }), + createGotify: adminProcedure + .input(apiCreateGotify) + .mutation(async ({ input, ctx }) => { + try { + return await createGotifyNotification( + input, + ctx.session.activeOrganizationId, + ); + } catch (error) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error creating the notification", + cause: error, + }); + } + }), + updateGotify: adminProcedure + .input(apiUpdateGotify) + .mutation(async ({ input, ctx }) => { + try { + const notification = await findNotificationById(input.notificationId); + if ( + IS_CLOUD && + notification.organizationId !== ctx.session.activeOrganizationId + ) { + throw new TRPCError({ + code: "UNAUTHORIZED", + message: "You are not authorized to update this notification", + }); + } + return await updateGotifyNotification({ + ...input, + organizationId: ctx.session.activeOrganizationId, + }); + } catch (error) { + throw error; + } + }), + testGotifyConnection: adminProcedure + .input(apiTestGotifyConnection) + .mutation(async ({ input }) => { + try { + await sendGotifyNotification( + input, + "Test Notification", + "Hi, From Dokploy ๐Ÿ‘‹", + ); + return true; + } catch (error) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error testing the notification", + cause: error, + }); + } + }), + createNtfy: adminProcedure + .input(apiCreateNtfy) + .mutation(async ({ input, ctx }) => { + try { + return await createNtfyNotification( + input, + ctx.session.activeOrganizationId, + ); + } catch (error) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error creating the notification", + cause: error, + }); + } + }), + updateNtfy: adminProcedure + .input(apiUpdateNtfy) + .mutation(async ({ input, ctx }) => { + try { + const notification = await findNotificationById(input.notificationId); + if ( + IS_CLOUD && + notification.organizationId !== ctx.session.activeOrganizationId + ) { + throw new TRPCError({ + code: "UNAUTHORIZED", + message: "You are not authorized to update this notification", + }); + } + return await updateNtfyNotification({ + ...input, + organizationId: ctx.session.activeOrganizationId, + }); + } catch (error) { + throw error; + } + }), + testNtfyConnection: adminProcedure + .input(apiTestNtfyConnection) + .mutation(async ({ input }) => { + try { + await sendNtfyNotification( + input, + "Test Notification", + "", + "view, visit Dokploy on Github, https://github.com/dokploy/dokploy, clear=true;", + "Hi, From Dokploy ๐Ÿ‘‹", + ); + return true; + } catch (error) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error testing the notification", + cause: error, + }); + } + }), + 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 }) => { + return await db.query.notifications.findMany({ + where: eq(notifications.organizationId, ctx.session.activeOrganizationId), + with: { + email: true, + }, + }); + }), }); diff --git a/packages/server/src/db/schema/notification.ts b/packages/server/src/db/schema/notification.ts index 107b1e46b..14198c048 100644 --- a/packages/server/src/db/schema/notification.ts +++ b/packages/server/src/db/schema/notification.ts @@ -6,400 +6,400 @@ import { z } from "zod"; import { organization } from "./account"; export const notificationType = pgEnum("notificationType", [ - "slack", - "telegram", - "discord", - "email", - "gotify", - "ntfy", - "custom", + "slack", + "telegram", + "discord", + "email", + "gotify", + "ntfy", + "custom", ]); export const notifications = pgTable("notification", { - notificationId: text("notificationId") - .notNull() - .primaryKey() - .$defaultFn(() => nanoid()), - name: text("name").notNull(), - appDeploy: boolean("appDeploy").notNull().default(false), - appBuildError: boolean("appBuildError").notNull().default(false), - databaseBackup: boolean("databaseBackup").notNull().default(false), - dokployRestart: boolean("dokployRestart").notNull().default(false), - dockerCleanup: boolean("dockerCleanup").notNull().default(false), - serverThreshold: boolean("serverThreshold").notNull().default(false), - notificationType: notificationType("notificationType").notNull(), - createdAt: text("createdAt") - .notNull() - .$defaultFn(() => new Date().toISOString()), - slackId: text("slackId").references(() => slack.slackId, { - onDelete: "cascade", - }), - telegramId: text("telegramId").references(() => telegram.telegramId, { - onDelete: "cascade", - }), - discordId: text("discordId").references(() => discord.discordId, { - onDelete: "cascade", - }), - emailId: text("emailId").references(() => email.emailId, { - onDelete: "cascade", - }), - gotifyId: text("gotifyId").references(() => gotify.gotifyId, { - onDelete: "cascade", - }), - ntfyId: text("ntfyId").references(() => ntfy.ntfyId, { - onDelete: "cascade", - }), - customId: text("customId").references(() => custom.customId, { - onDelete: "cascade", - }), - organizationId: text("organizationId") - .notNull() - .references(() => organization.id, { onDelete: "cascade" }), + notificationId: text("notificationId") + .notNull() + .primaryKey() + .$defaultFn(() => nanoid()), + name: text("name").notNull(), + appDeploy: boolean("appDeploy").notNull().default(false), + appBuildError: boolean("appBuildError").notNull().default(false), + databaseBackup: boolean("databaseBackup").notNull().default(false), + dokployRestart: boolean("dokployRestart").notNull().default(false), + dockerCleanup: boolean("dockerCleanup").notNull().default(false), + serverThreshold: boolean("serverThreshold").notNull().default(false), + notificationType: notificationType("notificationType").notNull(), + createdAt: text("createdAt") + .notNull() + .$defaultFn(() => new Date().toISOString()), + slackId: text("slackId").references(() => slack.slackId, { + onDelete: "cascade", + }), + telegramId: text("telegramId").references(() => telegram.telegramId, { + onDelete: "cascade", + }), + discordId: text("discordId").references(() => discord.discordId, { + onDelete: "cascade", + }), + emailId: text("emailId").references(() => email.emailId, { + onDelete: "cascade", + }), + gotifyId: text("gotifyId").references(() => gotify.gotifyId, { + onDelete: "cascade", + }), + ntfyId: text("ntfyId").references(() => ntfy.ntfyId, { + onDelete: "cascade", + }), + customId: text("customId").references(() => custom.customId, { + onDelete: "cascade", + }), + organizationId: text("organizationId") + .notNull() + .references(() => organization.id, { onDelete: "cascade" }), }); export const slack = pgTable("slack", { - slackId: text("slackId") - .notNull() - .primaryKey() - .$defaultFn(() => nanoid()), - webhookUrl: text("webhookUrl").notNull(), - channel: text("channel"), + slackId: text("slackId") + .notNull() + .primaryKey() + .$defaultFn(() => nanoid()), + webhookUrl: text("webhookUrl").notNull(), + channel: text("channel"), }); export const telegram = pgTable("telegram", { - telegramId: text("telegramId") - .notNull() - .primaryKey() - .$defaultFn(() => nanoid()), - botToken: text("botToken").notNull(), - chatId: text("chatId").notNull(), - messageThreadId: text("messageThreadId"), + telegramId: text("telegramId") + .notNull() + .primaryKey() + .$defaultFn(() => nanoid()), + botToken: text("botToken").notNull(), + chatId: text("chatId").notNull(), + messageThreadId: text("messageThreadId"), }); export const discord = pgTable("discord", { - discordId: text("discordId") - .notNull() - .primaryKey() - .$defaultFn(() => nanoid()), - webhookUrl: text("webhookUrl").notNull(), - decoration: boolean("decoration"), + discordId: text("discordId") + .notNull() + .primaryKey() + .$defaultFn(() => nanoid()), + webhookUrl: text("webhookUrl").notNull(), + decoration: boolean("decoration"), }); export const email = pgTable("email", { - emailId: text("emailId") - .notNull() - .primaryKey() - .$defaultFn(() => nanoid()), - smtpServer: text("smtpServer").notNull(), - smtpPort: integer("smtpPort").notNull(), - username: text("username").notNull(), - password: text("password").notNull(), - fromAddress: text("fromAddress").notNull(), - toAddresses: text("toAddress").array().notNull(), + emailId: text("emailId") + .notNull() + .primaryKey() + .$defaultFn(() => nanoid()), + smtpServer: text("smtpServer").notNull(), + smtpPort: integer("smtpPort").notNull(), + username: text("username").notNull(), + password: text("password").notNull(), + fromAddress: text("fromAddress").notNull(), + toAddresses: text("toAddress").array().notNull(), }); export const gotify = pgTable("gotify", { - gotifyId: text("gotifyId") - .notNull() - .primaryKey() - .$defaultFn(() => nanoid()), - serverUrl: text("serverUrl").notNull(), - appToken: text("appToken").notNull(), - priority: integer("priority").notNull().default(5), - decoration: boolean("decoration"), + gotifyId: text("gotifyId") + .notNull() + .primaryKey() + .$defaultFn(() => nanoid()), + serverUrl: text("serverUrl").notNull(), + appToken: text("appToken").notNull(), + priority: integer("priority").notNull().default(5), + decoration: boolean("decoration"), }); export const ntfy = pgTable("ntfy", { - ntfyId: text("ntfyId") - .notNull() - .primaryKey() - .$defaultFn(() => nanoid()), - serverUrl: text("serverUrl").notNull(), - topic: text("topic").notNull(), - accessToken: text("accessToken").notNull(), - priority: integer("priority").notNull().default(3), + ntfyId: text("ntfyId") + .notNull() + .primaryKey() + .$defaultFn(() => nanoid()), + serverUrl: text("serverUrl").notNull(), + topic: text("topic").notNull(), + accessToken: text("accessToken").notNull(), + priority: integer("priority").notNull().default(3), }); export const custom = pgTable("custom", { - customId: text("customId") - .notNull() - .primaryKey() - .$defaultFn(() => nanoid()), - endpoint: text("endpoint").notNull(), - headers: text("headers"), // JSON string + customId: text("customId") + .notNull() + .primaryKey() + .$defaultFn(() => nanoid()), + endpoint: text("endpoint").notNull(), + headers: text("headers"), // JSON string }); export const notificationsRelations = relations(notifications, ({ one }) => ({ - slack: one(slack, { - fields: [notifications.slackId], - references: [slack.slackId], - }), - telegram: one(telegram, { - fields: [notifications.telegramId], - references: [telegram.telegramId], - }), - discord: one(discord, { - fields: [notifications.discordId], - references: [discord.discordId], - }), - email: one(email, { - fields: [notifications.emailId], - references: [email.emailId], - }), - gotify: one(gotify, { - fields: [notifications.gotifyId], - references: [gotify.gotifyId], - }), - ntfy: one(ntfy, { - fields: [notifications.ntfyId], - references: [ntfy.ntfyId], - }), - custom: one(custom, { - fields: [notifications.customId], - references: [custom.customId], - }), - organization: one(organization, { - fields: [notifications.organizationId], - references: [organization.id], - }), + slack: one(slack, { + fields: [notifications.slackId], + references: [slack.slackId], + }), + telegram: one(telegram, { + fields: [notifications.telegramId], + references: [telegram.telegramId], + }), + discord: one(discord, { + fields: [notifications.discordId], + references: [discord.discordId], + }), + email: one(email, { + fields: [notifications.emailId], + references: [email.emailId], + }), + gotify: one(gotify, { + fields: [notifications.gotifyId], + references: [gotify.gotifyId], + }), + ntfy: one(ntfy, { + fields: [notifications.ntfyId], + references: [ntfy.ntfyId], + }), + custom: one(custom, { + fields: [notifications.customId], + references: [custom.customId], + }), + organization: one(organization, { + fields: [notifications.organizationId], + references: [organization.id], + }), })); export const notificationsSchema = createInsertSchema(notifications); export const apiCreateSlack = notificationsSchema - .pick({ - appBuildError: true, - databaseBackup: true, - dokployRestart: true, - name: true, - appDeploy: true, - dockerCleanup: true, - serverThreshold: true, - }) - .extend({ - webhookUrl: z.string().min(1), - channel: z.string(), - }) - .required(); + .pick({ + appBuildError: true, + databaseBackup: true, + dokployRestart: true, + name: true, + appDeploy: true, + dockerCleanup: true, + serverThreshold: true, + }) + .extend({ + webhookUrl: z.string().min(1), + channel: z.string(), + }) + .required(); export const apiUpdateSlack = apiCreateSlack.partial().extend({ - notificationId: z.string().min(1), - slackId: z.string(), - organizationId: z.string().optional(), + notificationId: z.string().min(1), + slackId: z.string(), + organizationId: z.string().optional(), }); export const apiTestSlackConnection = apiCreateSlack.pick({ - webhookUrl: true, - channel: true, + webhookUrl: true, + channel: true, }); export const apiCreateTelegram = notificationsSchema - .pick({ - appBuildError: true, - databaseBackup: true, - dokployRestart: true, - name: true, - appDeploy: true, - dockerCleanup: true, - serverThreshold: true, - }) - .extend({ - botToken: z.string().min(1), - chatId: z.string().min(1), - messageThreadId: z.string(), - }) - .required(); + .pick({ + appBuildError: true, + databaseBackup: true, + dokployRestart: true, + name: true, + appDeploy: true, + dockerCleanup: true, + serverThreshold: true, + }) + .extend({ + botToken: z.string().min(1), + chatId: z.string().min(1), + messageThreadId: z.string(), + }) + .required(); export const apiUpdateTelegram = apiCreateTelegram.partial().extend({ - notificationId: z.string().min(1), - telegramId: z.string().min(1), - organizationId: z.string().optional(), + notificationId: z.string().min(1), + telegramId: z.string().min(1), + organizationId: z.string().optional(), }); export const apiTestTelegramConnection = apiCreateTelegram.pick({ - botToken: true, - chatId: true, - messageThreadId: true, + botToken: true, + chatId: true, + messageThreadId: true, }); export const apiCreateDiscord = notificationsSchema - .pick({ - appBuildError: true, - databaseBackup: true, - dokployRestart: true, - name: true, - appDeploy: true, - dockerCleanup: true, - serverThreshold: true, - }) - .extend({ - webhookUrl: z.string().min(1), - decoration: z.boolean(), - }) - .required(); + .pick({ + appBuildError: true, + databaseBackup: true, + dokployRestart: true, + name: true, + appDeploy: true, + dockerCleanup: true, + serverThreshold: true, + }) + .extend({ + webhookUrl: z.string().min(1), + decoration: z.boolean(), + }) + .required(); export const apiUpdateDiscord = apiCreateDiscord.partial().extend({ - notificationId: z.string().min(1), - discordId: z.string().min(1), - organizationId: z.string().optional(), + notificationId: z.string().min(1), + discordId: z.string().min(1), + organizationId: z.string().optional(), }); export const apiTestDiscordConnection = apiCreateDiscord - .pick({ - webhookUrl: true, - }) - .extend({ - decoration: z.boolean().optional(), - }); + .pick({ + webhookUrl: true, + }) + .extend({ + decoration: z.boolean().optional(), + }); export const apiCreateEmail = notificationsSchema - .pick({ - appBuildError: true, - databaseBackup: true, - dokployRestart: true, - name: true, - appDeploy: true, - dockerCleanup: true, - serverThreshold: true, - }) - .extend({ - smtpServer: z.string().min(1), - smtpPort: z.number().min(1), - username: z.string().min(1), - password: z.string().min(1), - fromAddress: z.string().min(1), - toAddresses: z.array(z.string()).min(1), - }) - .required(); + .pick({ + appBuildError: true, + databaseBackup: true, + dokployRestart: true, + name: true, + appDeploy: true, + dockerCleanup: true, + serverThreshold: true, + }) + .extend({ + smtpServer: z.string().min(1), + smtpPort: z.number().min(1), + username: z.string().min(1), + password: z.string().min(1), + fromAddress: z.string().min(1), + toAddresses: z.array(z.string()).min(1), + }) + .required(); export const apiUpdateEmail = apiCreateEmail.partial().extend({ - notificationId: z.string().min(1), - emailId: z.string().min(1), - organizationId: z.string().optional(), + notificationId: z.string().min(1), + emailId: z.string().min(1), + organizationId: z.string().optional(), }); export const apiTestEmailConnection = apiCreateEmail.pick({ - smtpServer: true, - smtpPort: true, - username: true, - password: true, - toAddresses: true, - fromAddress: true, + smtpServer: true, + smtpPort: true, + username: true, + password: true, + toAddresses: true, + fromAddress: true, }); export const apiCreateGotify = notificationsSchema - .pick({ - appBuildError: true, - databaseBackup: true, - dokployRestart: true, - name: true, - appDeploy: true, - dockerCleanup: true, - }) - .extend({ - serverUrl: z.string().min(1), - appToken: z.string().min(1), - priority: z.number().min(1), - decoration: z.boolean(), - }) - .required(); + .pick({ + appBuildError: true, + databaseBackup: true, + dokployRestart: true, + name: true, + appDeploy: true, + dockerCleanup: true, + }) + .extend({ + serverUrl: z.string().min(1), + appToken: z.string().min(1), + priority: z.number().min(1), + decoration: z.boolean(), + }) + .required(); export const apiUpdateGotify = apiCreateGotify.partial().extend({ - notificationId: z.string().min(1), - gotifyId: z.string().min(1), - organizationId: z.string().optional(), + notificationId: z.string().min(1), + gotifyId: z.string().min(1), + organizationId: z.string().optional(), }); export const apiTestGotifyConnection = apiCreateGotify - .pick({ - serverUrl: true, - appToken: true, - priority: true, - }) - .extend({ - decoration: z.boolean().optional(), - }); + .pick({ + serverUrl: true, + appToken: true, + priority: true, + }) + .extend({ + decoration: z.boolean().optional(), + }); export const apiCreateNtfy = notificationsSchema - .pick({ - appBuildError: true, - databaseBackup: true, - dokployRestart: true, - name: true, - appDeploy: true, - dockerCleanup: true, - }) - .extend({ - serverUrl: z.string().min(1), - topic: z.string().min(1), - accessToken: z.string().min(1), - priority: z.number().min(1), - }) - .required(); + .pick({ + appBuildError: true, + databaseBackup: true, + dokployRestart: true, + name: true, + appDeploy: true, + dockerCleanup: true, + }) + .extend({ + serverUrl: z.string().min(1), + topic: z.string().min(1), + accessToken: z.string().min(1), + priority: z.number().min(1), + }) + .required(); export const apiUpdateNtfy = apiCreateNtfy.partial().extend({ - notificationId: z.string().min(1), - ntfyId: z.string().min(1), - organizationId: z.string().optional(), + notificationId: z.string().min(1), + ntfyId: z.string().min(1), + organizationId: z.string().optional(), }); export const apiTestNtfyConnection = apiCreateNtfy.pick({ - serverUrl: true, - topic: true, - accessToken: true, - priority: true, + serverUrl: true, + topic: true, + accessToken: true, + priority: true, }); export const apiFindOneNotification = notificationsSchema - .pick({ - notificationId: true, - }) - .required(); + .pick({ + notificationId: true, + }) + .required(); export const apiCreateCustom = notificationsSchema - .pick({ - appBuildError: true, - databaseBackup: true, - dokployRestart: true, - name: true, - appDeploy: true, - dockerCleanup: true, - serverThreshold: true, - }) - .extend({ - endpoint: z.string().min(1), - headers: z.string().optional(), - }); + .pick({ + appBuildError: true, + databaseBackup: true, + dokployRestart: true, + name: true, + appDeploy: true, + dockerCleanup: true, + serverThreshold: true, + }) + .extend({ + endpoint: z.string().min(1), + headers: z.string().optional(), + }); export const apiUpdateCustom = apiCreateCustom.partial().extend({ - notificationId: z.string().min(1), - customId: z.string().min(1), - organizationId: z.string().optional(), + notificationId: z.string().min(1), + customId: z.string().min(1), + organizationId: z.string().optional(), }); export const apiTestCustomConnection = z.object({ - endpoint: z.string().min(1), - headers: z.string().optional(), + endpoint: z.string().min(1), + headers: z.string().optional(), }); export const apiSendTest = notificationsSchema - .extend({ - botToken: z.string(), - chatId: z.string(), - webhookUrl: z.string(), - channel: z.string(), - smtpServer: z.string(), - smtpPort: z.number(), - fromAddress: z.string(), - username: z.string(), - password: z.string(), - toAddresses: z.array(z.string()), - serverUrl: z.string(), - topic: z.string(), - appToken: z.string(), - accessToken: z.string(), - priority: z.number(), - endpoint: z.string(), - headers: z.string(), - }) - .partial(); + .extend({ + botToken: z.string(), + chatId: z.string(), + webhookUrl: z.string(), + channel: z.string(), + smtpServer: z.string(), + smtpPort: z.number(), + fromAddress: z.string(), + username: z.string(), + password: z.string(), + toAddresses: z.array(z.string()), + serverUrl: z.string(), + topic: z.string(), + appToken: z.string(), + accessToken: z.string(), + priority: z.number(), + endpoint: z.string(), + headers: z.string(), + }) + .partial(); diff --git a/packages/server/src/services/notification.ts b/packages/server/src/services/notification.ts index 19db872e6..b96a5a661 100644 --- a/packages/server/src/services/notification.ts +++ b/packages/server/src/services/notification.ts @@ -1,27 +1,27 @@ import { db } from "@dokploy/server/db"; import { - type apiCreateCustom, - type apiCreateDiscord, - type apiCreateEmail, - type apiCreateGotify, - type apiCreateNtfy, - type apiCreateSlack, - type apiCreateTelegram, - type apiUpdateCustom, - type apiUpdateDiscord, - type apiUpdateEmail, - type apiUpdateGotify, - type apiUpdateNtfy, - type apiUpdateSlack, - type apiUpdateTelegram, - custom, - discord, - email, - gotify, - notifications, - ntfy, - slack, - telegram, + type apiCreateCustom, + type apiCreateDiscord, + type apiCreateEmail, + type apiCreateGotify, + type apiCreateNtfy, + type apiCreateSlack, + type apiCreateTelegram, + type apiUpdateCustom, + type apiUpdateDiscord, + type apiUpdateEmail, + type apiUpdateGotify, + type apiUpdateNtfy, + type apiUpdateSlack, + type apiUpdateTelegram, + custom, + discord, + email, + gotify, + notifications, + ntfy, + slack, + telegram, } from "@dokploy/server/db/schema"; import { TRPCError } from "@trpc/server"; import { eq } from "drizzle-orm"; @@ -29,685 +29,685 @@ import { eq } from "drizzle-orm"; export type Notification = typeof notifications.$inferSelect; export const createSlackNotification = async ( - input: typeof apiCreateSlack._type, - organizationId: string + input: typeof apiCreateSlack._type, + organizationId: string, ) => { - await db.transaction(async (tx) => { - const newSlack = await tx - .insert(slack) - .values({ - channel: input.channel, - webhookUrl: input.webhookUrl, - }) - .returning() - .then((value) => value[0]); + await db.transaction(async (tx) => { + const newSlack = await tx + .insert(slack) + .values({ + channel: input.channel, + webhookUrl: input.webhookUrl, + }) + .returning() + .then((value) => value[0]); - if (!newSlack) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Error input: Inserting slack", - }); - } + if (!newSlack) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error input: Inserting slack", + }); + } - const newDestination = await tx - .insert(notifications) - .values({ - slackId: newSlack.slackId, - name: input.name, - appDeploy: input.appDeploy, - appBuildError: input.appBuildError, - databaseBackup: input.databaseBackup, - dokployRestart: input.dokployRestart, - dockerCleanup: input.dockerCleanup, - notificationType: "slack", - organizationId: organizationId, - serverThreshold: input.serverThreshold, - }) - .returning() - .then((value) => value[0]); + const newDestination = await tx + .insert(notifications) + .values({ + slackId: newSlack.slackId, + name: input.name, + appDeploy: input.appDeploy, + appBuildError: input.appBuildError, + databaseBackup: input.databaseBackup, + dokployRestart: input.dokployRestart, + dockerCleanup: input.dockerCleanup, + notificationType: "slack", + organizationId: organizationId, + serverThreshold: input.serverThreshold, + }) + .returning() + .then((value) => value[0]); - if (!newDestination) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Error input: Inserting notification", - }); - } + if (!newDestination) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error input: Inserting notification", + }); + } - return newDestination; - }); + return newDestination; + }); }; export const updateSlackNotification = async ( - input: typeof apiUpdateSlack._type + input: typeof apiUpdateSlack._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]); + 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", - }); - } + if (!newDestination) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error Updating notification", + }); + } - await tx - .update(slack) - .set({ - channel: input.channel, - webhookUrl: input.webhookUrl, - }) - .where(eq(slack.slackId, input.slackId)) - .returning() - .then((value) => value[0]); + await tx + .update(slack) + .set({ + channel: input.channel, + webhookUrl: input.webhookUrl, + }) + .where(eq(slack.slackId, input.slackId)) + .returning() + .then((value) => value[0]); - return newDestination; - }); + return newDestination; + }); }; export const createTelegramNotification = async ( - input: typeof apiCreateTelegram._type, - organizationId: string + input: typeof apiCreateTelegram._type, + organizationId: string, ) => { - await db.transaction(async (tx) => { - const newTelegram = await tx - .insert(telegram) - .values({ - botToken: input.botToken, - chatId: input.chatId, - messageThreadId: input.messageThreadId, - }) - .returning() - .then((value) => value[0]); + await db.transaction(async (tx) => { + const newTelegram = await tx + .insert(telegram) + .values({ + botToken: input.botToken, + chatId: input.chatId, + messageThreadId: input.messageThreadId, + }) + .returning() + .then((value) => value[0]); - if (!newTelegram) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Error input: Inserting telegram", - }); - } + if (!newTelegram) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error input: Inserting telegram", + }); + } - const newDestination = await tx - .insert(notifications) - .values({ - telegramId: newTelegram.telegramId, - name: input.name, - appDeploy: input.appDeploy, - appBuildError: input.appBuildError, - databaseBackup: input.databaseBackup, - dokployRestart: input.dokployRestart, - dockerCleanup: input.dockerCleanup, - notificationType: "telegram", - organizationId: organizationId, - serverThreshold: input.serverThreshold, - }) - .returning() - .then((value) => value[0]); + const newDestination = await tx + .insert(notifications) + .values({ + telegramId: newTelegram.telegramId, + name: input.name, + appDeploy: input.appDeploy, + appBuildError: input.appBuildError, + databaseBackup: input.databaseBackup, + dokployRestart: input.dokployRestart, + dockerCleanup: input.dockerCleanup, + notificationType: "telegram", + organizationId: organizationId, + serverThreshold: input.serverThreshold, + }) + .returning() + .then((value) => value[0]); - if (!newDestination) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Error input: Inserting notification", - }); - } + if (!newDestination) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error input: Inserting notification", + }); + } - return newDestination; - }); + return newDestination; + }); }; export const updateTelegramNotification = async ( - input: typeof apiUpdateTelegram._type + input: typeof apiUpdateTelegram._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]); + 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", - }); - } + if (!newDestination) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error Updating notification", + }); + } - await tx - .update(telegram) - .set({ - botToken: input.botToken, - chatId: input.chatId, - messageThreadId: input.messageThreadId, - }) - .where(eq(telegram.telegramId, input.telegramId)) - .returning() - .then((value) => value[0]); + await tx + .update(telegram) + .set({ + botToken: input.botToken, + chatId: input.chatId, + messageThreadId: input.messageThreadId, + }) + .where(eq(telegram.telegramId, input.telegramId)) + .returning() + .then((value) => value[0]); - return newDestination; - }); + return newDestination; + }); }; export const createDiscordNotification = async ( - input: typeof apiCreateDiscord._type, - organizationId: string + input: typeof apiCreateDiscord._type, + organizationId: string, ) => { - await db.transaction(async (tx) => { - const newDiscord = await tx - .insert(discord) - .values({ - webhookUrl: input.webhookUrl, - decoration: input.decoration, - }) - .returning() - .then((value) => value[0]); + await db.transaction(async (tx) => { + const newDiscord = await tx + .insert(discord) + .values({ + webhookUrl: input.webhookUrl, + decoration: input.decoration, + }) + .returning() + .then((value) => value[0]); - if (!newDiscord) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Error input: Inserting discord", - }); - } + if (!newDiscord) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error input: Inserting discord", + }); + } - const newDestination = await tx - .insert(notifications) - .values({ - discordId: newDiscord.discordId, - name: input.name, - appDeploy: input.appDeploy, - appBuildError: input.appBuildError, - databaseBackup: input.databaseBackup, - dokployRestart: input.dokployRestart, - dockerCleanup: input.dockerCleanup, - notificationType: "discord", - organizationId: organizationId, - serverThreshold: input.serverThreshold, - }) - .returning() - .then((value) => value[0]); + const newDestination = await tx + .insert(notifications) + .values({ + discordId: newDiscord.discordId, + name: input.name, + appDeploy: input.appDeploy, + appBuildError: input.appBuildError, + databaseBackup: input.databaseBackup, + dokployRestart: input.dokployRestart, + dockerCleanup: input.dockerCleanup, + notificationType: "discord", + organizationId: organizationId, + serverThreshold: input.serverThreshold, + }) + .returning() + .then((value) => value[0]); - if (!newDestination) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Error input: Inserting notification", - }); - } + if (!newDestination) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error input: Inserting notification", + }); + } - return newDestination; - }); + return newDestination; + }); }; export const updateDiscordNotification = async ( - input: typeof apiUpdateDiscord._type + input: typeof apiUpdateDiscord._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]); + 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", - }); - } + if (!newDestination) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error Updating notification", + }); + } - await tx - .update(discord) - .set({ - webhookUrl: input.webhookUrl, - decoration: input.decoration, - }) - .where(eq(discord.discordId, input.discordId)) - .returning() - .then((value) => value[0]); + await tx + .update(discord) + .set({ + webhookUrl: input.webhookUrl, + decoration: input.decoration, + }) + .where(eq(discord.discordId, input.discordId)) + .returning() + .then((value) => value[0]); - return newDestination; - }); + return newDestination; + }); }; export const createEmailNotification = async ( - input: typeof apiCreateEmail._type, - organizationId: string + input: typeof apiCreateEmail._type, + organizationId: string, ) => { - await db.transaction(async (tx) => { - const newEmail = await tx - .insert(email) - .values({ - smtpServer: input.smtpServer, - smtpPort: input.smtpPort, - username: input.username, - password: input.password, - fromAddress: input.fromAddress, - toAddresses: input.toAddresses, - }) - .returning() - .then((value) => value[0]); + await db.transaction(async (tx) => { + const newEmail = await tx + .insert(email) + .values({ + smtpServer: input.smtpServer, + smtpPort: input.smtpPort, + username: input.username, + password: input.password, + fromAddress: input.fromAddress, + toAddresses: input.toAddresses, + }) + .returning() + .then((value) => value[0]); - if (!newEmail) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Error input: Inserting email", - }); - } + if (!newEmail) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error input: Inserting email", + }); + } - const newDestination = await tx - .insert(notifications) - .values({ - emailId: newEmail.emailId, - name: input.name, - appDeploy: input.appDeploy, - appBuildError: input.appBuildError, - databaseBackup: input.databaseBackup, - dokployRestart: input.dokployRestart, - dockerCleanup: input.dockerCleanup, - notificationType: "email", - organizationId: organizationId, - serverThreshold: input.serverThreshold, - }) - .returning() - .then((value) => value[0]); + const newDestination = await tx + .insert(notifications) + .values({ + emailId: newEmail.emailId, + name: input.name, + appDeploy: input.appDeploy, + appBuildError: input.appBuildError, + databaseBackup: input.databaseBackup, + dokployRestart: input.dokployRestart, + dockerCleanup: input.dockerCleanup, + notificationType: "email", + organizationId: organizationId, + serverThreshold: input.serverThreshold, + }) + .returning() + .then((value) => value[0]); - if (!newDestination) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Error input: Inserting notification", - }); - } + if (!newDestination) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error input: Inserting notification", + }); + } - return newDestination; - }); + return newDestination; + }); }; export const updateEmailNotification = async ( - input: typeof apiUpdateEmail._type + input: typeof apiUpdateEmail._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]); + 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", - }); - } + if (!newDestination) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error Updating notification", + }); + } - await tx - .update(email) - .set({ - smtpServer: input.smtpServer, - smtpPort: input.smtpPort, - username: input.username, - password: input.password, - fromAddress: input.fromAddress, - toAddresses: input.toAddresses, - }) - .where(eq(email.emailId, input.emailId)) - .returning() - .then((value) => value[0]); + await tx + .update(email) + .set({ + smtpServer: input.smtpServer, + smtpPort: input.smtpPort, + username: input.username, + password: input.password, + fromAddress: input.fromAddress, + toAddresses: input.toAddresses, + }) + .where(eq(email.emailId, input.emailId)) + .returning() + .then((value) => value[0]); - return newDestination; - }); + return newDestination; + }); }; export const createGotifyNotification = async ( - input: typeof apiCreateGotify._type, - organizationId: string + input: typeof apiCreateGotify._type, + organizationId: string, ) => { - await db.transaction(async (tx) => { - const newGotify = await tx - .insert(gotify) - .values({ - serverUrl: input.serverUrl, - appToken: input.appToken, - priority: input.priority, - decoration: input.decoration, - }) - .returning() - .then((value) => value[0]); + await db.transaction(async (tx) => { + const newGotify = await tx + .insert(gotify) + .values({ + serverUrl: input.serverUrl, + appToken: input.appToken, + priority: input.priority, + decoration: input.decoration, + }) + .returning() + .then((value) => value[0]); - if (!newGotify) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Error input: Inserting gotify", - }); - } + if (!newGotify) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error input: Inserting gotify", + }); + } - const newDestination = await tx - .insert(notifications) - .values({ - gotifyId: newGotify.gotifyId, - name: input.name, - appDeploy: input.appDeploy, - appBuildError: input.appBuildError, - databaseBackup: input.databaseBackup, - dokployRestart: input.dokployRestart, - dockerCleanup: input.dockerCleanup, - notificationType: "gotify", - organizationId: organizationId, - }) - .returning() - .then((value) => value[0]); + const newDestination = await tx + .insert(notifications) + .values({ + gotifyId: newGotify.gotifyId, + name: input.name, + appDeploy: input.appDeploy, + appBuildError: input.appBuildError, + databaseBackup: input.databaseBackup, + dokployRestart: input.dokployRestart, + dockerCleanup: input.dockerCleanup, + notificationType: "gotify", + organizationId: organizationId, + }) + .returning() + .then((value) => value[0]); - if (!newDestination) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Error input: Inserting notification", - }); - } + if (!newDestination) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error input: Inserting notification", + }); + } - return newDestination; - }); + return newDestination; + }); }; export const updateGotifyNotification = async ( - input: typeof apiUpdateGotify._type + input: typeof apiUpdateGotify._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, - }) - .where(eq(notifications.notificationId, input.notificationId)) - .returning() - .then((value) => value[0]); + 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, + }) + .where(eq(notifications.notificationId, input.notificationId)) + .returning() + .then((value) => value[0]); - if (!newDestination) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Error Updating notification", - }); - } + if (!newDestination) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error Updating notification", + }); + } - await tx - .update(gotify) - .set({ - serverUrl: input.serverUrl, - appToken: input.appToken, - priority: input.priority, - decoration: input.decoration, - }) - .where(eq(gotify.gotifyId, input.gotifyId)); + await tx + .update(gotify) + .set({ + serverUrl: input.serverUrl, + appToken: input.appToken, + priority: input.priority, + decoration: input.decoration, + }) + .where(eq(gotify.gotifyId, input.gotifyId)); - return newDestination; - }); + return newDestination; + }); }; export const createNtfyNotification = async ( - input: typeof apiCreateNtfy._type, - organizationId: string + input: typeof apiCreateNtfy._type, + organizationId: string, ) => { - await db.transaction(async (tx) => { - const newNtfy = await tx - .insert(ntfy) - .values({ - serverUrl: input.serverUrl, - topic: input.topic, - accessToken: input.accessToken, - priority: input.priority, - }) - .returning() - .then((value) => value[0]); + await db.transaction(async (tx) => { + const newNtfy = await tx + .insert(ntfy) + .values({ + serverUrl: input.serverUrl, + topic: input.topic, + accessToken: input.accessToken, + priority: input.priority, + }) + .returning() + .then((value) => value[0]); - if (!newNtfy) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Error input: Inserting ntfy", - }); - } + if (!newNtfy) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error input: Inserting ntfy", + }); + } - const newDestination = await tx - .insert(notifications) - .values({ - ntfyId: newNtfy.ntfyId, - name: input.name, - appDeploy: input.appDeploy, - appBuildError: input.appBuildError, - databaseBackup: input.databaseBackup, - dokployRestart: input.dokployRestart, - dockerCleanup: input.dockerCleanup, - notificationType: "ntfy", - organizationId: organizationId, - }) - .returning() - .then((value) => value[0]); + const newDestination = await tx + .insert(notifications) + .values({ + ntfyId: newNtfy.ntfyId, + name: input.name, + appDeploy: input.appDeploy, + appBuildError: input.appBuildError, + databaseBackup: input.databaseBackup, + dokployRestart: input.dokployRestart, + dockerCleanup: input.dockerCleanup, + notificationType: "ntfy", + organizationId: organizationId, + }) + .returning() + .then((value) => value[0]); - if (!newDestination) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Error input: Inserting notification", - }); - } + if (!newDestination) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error input: Inserting notification", + }); + } - return newDestination; - }); + return newDestination; + }); }; export const updateNtfyNotification = async ( - input: typeof apiUpdateNtfy._type + input: typeof apiUpdateNtfy._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, - }) - .where(eq(notifications.notificationId, input.notificationId)) - .returning() - .then((value) => value[0]); + 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, + }) + .where(eq(notifications.notificationId, input.notificationId)) + .returning() + .then((value) => value[0]); - if (!newDestination) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Error Updating notification", - }); - } + if (!newDestination) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error Updating notification", + }); + } - await tx - .update(ntfy) - .set({ - serverUrl: input.serverUrl, - topic: input.topic, - accessToken: input.accessToken, - priority: input.priority, - }) - .where(eq(ntfy.ntfyId, input.ntfyId)); + await tx + .update(ntfy) + .set({ + serverUrl: input.serverUrl, + topic: input.topic, + accessToken: input.accessToken, + priority: input.priority, + }) + .where(eq(ntfy.ntfyId, input.ntfyId)); - return newDestination; - }); + return newDestination; + }); }; export const createCustomNotification = async ( - input: typeof apiCreateCustom._type, - organizationId: string + 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]); + 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", - }); - } + 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]); + 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", - }); - } + if (!newDestination) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error input: Inserting notification", + }); + } - return newDestination; - }); + return newDestination; + }); }; export const updateCustomNotification = async ( - input: typeof apiUpdateCustom._type + 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]); + 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", - }); - } + 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)); + await tx + .update(custom) + .set({ + endpoint: input.endpoint, + headers: input.headers, + }) + .where(eq(custom.customId, input.customId)); - return newDestination; - }); + return newDestination; + }); }; export const findNotificationById = async (notificationId: string) => { - const notification = await db.query.notifications.findFirst({ - where: eq(notifications.notificationId, notificationId), - with: { - slack: true, - telegram: true, - discord: true, - email: true, - gotify: true, - ntfy: true, - custom: true, - }, - }); - if (!notification) { - throw new TRPCError({ - code: "NOT_FOUND", - message: "Notification not found", - }); - } - return notification; + const notification = await db.query.notifications.findFirst({ + where: eq(notifications.notificationId, notificationId), + with: { + slack: true, + telegram: true, + discord: true, + email: true, + gotify: true, + ntfy: true, + custom: true, + }, + }); + if (!notification) { + throw new TRPCError({ + code: "NOT_FOUND", + message: "Notification not found", + }); + } + return notification; }; export const removeNotificationById = async (notificationId: string) => { - const result = await db - .delete(notifications) - .where(eq(notifications.notificationId, notificationId)) - .returning(); + const result = await db + .delete(notifications) + .where(eq(notifications.notificationId, notificationId)) + .returning(); - return result[0]; + return result[0]; }; export const updateNotificationById = async ( - notificationId: string, - notificationData: Partial + notificationId: string, + notificationData: Partial, ) => { - const result = await db - .update(notifications) - .set({ - ...notificationData, - }) - .where(eq(notifications.notificationId, notificationId)) - .returning(); + const result = await db + .update(notifications) + .set({ + ...notificationData, + }) + .where(eq(notifications.notificationId, notificationId)) + .returning(); - return result[0]; + return result[0]; }; diff --git a/packages/server/src/utils/notifications/build-error.ts b/packages/server/src/utils/notifications/build-error.ts index 7148afd10..d404cc1f9 100644 --- a/packages/server/src/utils/notifications/build-error.ts +++ b/packages/server/src/utils/notifications/build-error.ts @@ -5,236 +5,236 @@ import { renderAsync } from "@react-email/components"; import { format } from "date-fns"; import { and, eq } from "drizzle-orm"; import { - sendCustomNotification, - sendDiscordNotification, - sendEmailNotification, - sendGotifyNotification, - sendNtfyNotification, - sendSlackNotification, - sendTelegramNotification, + sendCustomNotification, + sendDiscordNotification, + sendEmailNotification, + sendGotifyNotification, + sendNtfyNotification, + sendSlackNotification, + sendTelegramNotification, } from "./utils"; interface Props { - projectName: string; - applicationName: string; - applicationType: string; - errorMessage: string; - buildLink: string; - organizationId: string; + projectName: string; + applicationName: string; + applicationType: string; + errorMessage: string; + buildLink: string; + organizationId: string; } export const sendBuildErrorNotifications = async ({ - projectName, - applicationName, - applicationType, - errorMessage, - buildLink, - organizationId, + projectName, + applicationName, + applicationType, + errorMessage, + buildLink, + organizationId, }: Props) => { - const date = new Date(); - const unixDate = ~~(Number(date) / 1000); - const notificationList = await db.query.notifications.findMany({ - where: and( - eq(notifications.appBuildError, true), - eq(notifications.organizationId, organizationId) - ), - with: { - email: true, - discord: true, - telegram: true, - slack: true, - gotify: true, - ntfy: true, - custom: true, - }, - }); + const date = new Date(); + const unixDate = ~~(Number(date) / 1000); + const notificationList = await db.query.notifications.findMany({ + where: and( + eq(notifications.appBuildError, true), + eq(notifications.organizationId, organizationId), + ), + with: { + email: true, + discord: true, + telegram: true, + slack: true, + gotify: true, + ntfy: true, + custom: true, + }, + }); - for (const notification of notificationList) { - const { email, discord, telegram, slack, gotify, ntfy, custom } = - notification; - if (email) { - const template = await renderAsync( - BuildFailedEmail({ - projectName, - applicationName, - applicationType, - errorMessage: errorMessage, - buildLink, - date: date.toLocaleString(), - }) - ).catch(); - await sendEmailNotification(email, "Build failed for dokploy", template); - } + for (const notification of notificationList) { + const { email, discord, telegram, slack, gotify, ntfy, custom } = + notification; + if (email) { + const template = await renderAsync( + BuildFailedEmail({ + projectName, + applicationName, + applicationType, + errorMessage: errorMessage, + buildLink, + date: date.toLocaleString(), + }), + ).catch(); + await sendEmailNotification(email, "Build failed for dokploy", template); + } - if (discord) { - const decorate = (decoration: string, text: string) => - `${discord.decoration ? decoration : ""} ${text}`.trim(); + if (discord) { + const decorate = (decoration: string, text: string) => + `${discord.decoration ? decoration : ""} ${text}`.trim(); - const limitCharacter = 800; - const truncatedErrorMessage = errorMessage.substring(0, limitCharacter); - await sendDiscordNotification(discord, { - title: decorate(">", "`โš ๏ธ` Build Failed"), - color: 0xed4245, - fields: [ - { - name: decorate("`๐Ÿ› ๏ธ`", "Project"), - value: projectName, - inline: true, - }, - { - name: decorate("`โš™๏ธ`", "Application"), - value: applicationName, - inline: true, - }, - { - name: decorate("`โ”`", "Type"), - value: applicationType, - inline: true, - }, - { - name: decorate("`๐Ÿ“…`", "Date"), - value: ``, - inline: true, - }, - { - name: decorate("`โŒš`", "Time"), - value: ``, - inline: true, - }, - { - name: decorate("`โ“`", "Type"), - value: "Failed", - inline: true, - }, - { - name: decorate("`โš ๏ธ`", "Error Message"), - value: `\`\`\`${truncatedErrorMessage}\`\`\``, - }, - { - name: decorate("`๐Ÿงท`", "Build Link"), - value: `[Click here to access build link](${buildLink})`, - }, - ], - timestamp: date.toISOString(), - footer: { - text: "Dokploy Build Notification", - }, - }); - } + const limitCharacter = 800; + const truncatedErrorMessage = errorMessage.substring(0, limitCharacter); + await sendDiscordNotification(discord, { + title: decorate(">", "`โš ๏ธ` Build Failed"), + color: 0xed4245, + fields: [ + { + name: decorate("`๐Ÿ› ๏ธ`", "Project"), + value: projectName, + inline: true, + }, + { + name: decorate("`โš™๏ธ`", "Application"), + value: applicationName, + inline: true, + }, + { + name: decorate("`โ”`", "Type"), + value: applicationType, + inline: true, + }, + { + name: decorate("`๐Ÿ“…`", "Date"), + value: ``, + inline: true, + }, + { + name: decorate("`โŒš`", "Time"), + value: ``, + inline: true, + }, + { + name: decorate("`โ“`", "Type"), + value: "Failed", + inline: true, + }, + { + name: decorate("`โš ๏ธ`", "Error Message"), + value: `\`\`\`${truncatedErrorMessage}\`\`\``, + }, + { + name: decorate("`๐Ÿงท`", "Build Link"), + value: `[Click here to access build link](${buildLink})`, + }, + ], + timestamp: date.toISOString(), + footer: { + text: "Dokploy Build Notification", + }, + }); + } - if (gotify) { - const decorate = (decoration: string, text: string) => - `${gotify.decoration ? decoration : ""} ${text}\n`; - await sendGotifyNotification( - gotify, - decorate("โš ๏ธ", "Build Failed"), - `${decorate("๐Ÿ› ๏ธ", `Project: ${projectName}`)}` + - `${decorate("โš™๏ธ", `Application: ${applicationName}`)}` + - `${decorate("โ”", `Type: ${applicationType}`)}` + - `${decorate("๐Ÿ•’", `Date: ${date.toLocaleString()}`)}` + - `${decorate("โš ๏ธ", `Error:\n${errorMessage}`)}` + - `${decorate("๐Ÿ”—", `Build details:\n${buildLink}`)}` - ); - } + if (gotify) { + const decorate = (decoration: string, text: string) => + `${gotify.decoration ? decoration : ""} ${text}\n`; + await sendGotifyNotification( + gotify, + decorate("โš ๏ธ", "Build Failed"), + `${decorate("๐Ÿ› ๏ธ", `Project: ${projectName}`)}` + + `${decorate("โš™๏ธ", `Application: ${applicationName}`)}` + + `${decorate("โ”", `Type: ${applicationType}`)}` + + `${decorate("๐Ÿ•’", `Date: ${date.toLocaleString()}`)}` + + `${decorate("โš ๏ธ", `Error:\n${errorMessage}`)}` + + `${decorate("๐Ÿ”—", `Build details:\n${buildLink}`)}`, + ); + } - if (ntfy) { - await sendNtfyNotification( - ntfy, - "Build Failed", - "warning", - `view, Build details, ${buildLink}, clear=true;`, - `๐Ÿ› ๏ธProject: ${projectName}\n` + - `โš™๏ธApplication: ${applicationName}\n` + - `โ”Type: ${applicationType}\n` + - `๐Ÿ•’Date: ${date.toLocaleString()}\n` + - `โš ๏ธError:\n${errorMessage}` - ); - } + if (ntfy) { + await sendNtfyNotification( + ntfy, + "Build Failed", + "warning", + `view, Build details, ${buildLink}, clear=true;`, + `๐Ÿ› ๏ธProject: ${projectName}\n` + + `โš™๏ธApplication: ${applicationName}\n` + + `โ”Type: ${applicationType}\n` + + `๐Ÿ•’Date: ${date.toLocaleString()}\n` + + `โš ๏ธError:\n${errorMessage}`, + ); + } - if (telegram) { - const inlineButton = [ - [ - { - text: "Deployment Logs", - url: buildLink, - }, - ], - ]; + if (telegram) { + const inlineButton = [ + [ + { + text: "Deployment Logs", + url: buildLink, + }, + ], + ]; - await sendTelegramNotification( - telegram, - `โš ๏ธ Build Failed\n\nProject: ${projectName}\nApplication: ${applicationName}\nType: ${applicationType}\nDate: ${format( - date, - "PP" - )}\nTime: ${format( - date, - "pp" - )}\n\nError:\n
${errorMessage}
`, - inlineButton - ); - } + await sendTelegramNotification( + telegram, + `โš ๏ธ Build Failed\n\nProject: ${projectName}\nApplication: ${applicationName}\nType: ${applicationType}\nDate: ${format( + date, + "PP", + )}\nTime: ${format( + date, + "pp", + )}\n\nError:\n
${errorMessage}
`, + inlineButton, + ); + } - if (slack) { - const { channel } = slack; - await sendSlackNotification(slack, { - channel: channel, - attachments: [ - { - color: "#FF0000", - pretext: ":warning: *Build Failed*", - fields: [ - { - title: "Project", - value: projectName, - short: true, - }, - { - title: "Application", - value: applicationName, - short: true, - }, - { - title: "Type", - value: applicationType, - short: true, - }, - { - title: "Time", - value: date.toLocaleString(), - short: true, - }, - { - title: "Error", - value: `\`\`\`${errorMessage}\`\`\``, - short: false, - }, - ], - actions: [ - { - type: "button", - text: "View Build Details", - url: buildLink, - }, - ], - }, - ], - }); - } + if (slack) { + const { channel } = slack; + await sendSlackNotification(slack, { + channel: channel, + attachments: [ + { + color: "#FF0000", + pretext: ":warning: *Build Failed*", + fields: [ + { + title: "Project", + value: projectName, + short: true, + }, + { + title: "Application", + value: applicationName, + short: true, + }, + { + title: "Type", + value: applicationType, + short: true, + }, + { + title: "Time", + value: date.toLocaleString(), + short: true, + }, + { + title: "Error", + value: `\`\`\`${errorMessage}\`\`\``, + short: false, + }, + ], + actions: [ + { + type: "button", + text: "View Build Details", + url: buildLink, + }, + ], + }, + ], + }); + } - if (custom) { - await sendCustomNotification(custom, { - title: "Build Error", - message: "Build failed with errors", - projectName, - applicationName, - applicationType, - errorMessage, - buildLink, - timestamp: date.toISOString(), - date: date.toLocaleString(), - status: "error", - type: "build", - }); - } - } + if (custom) { + await sendCustomNotification(custom, { + title: "Build Error", + message: "Build failed with errors", + projectName, + applicationName, + applicationType, + errorMessage, + buildLink, + timestamp: date.toISOString(), + date: date.toLocaleString(), + status: "error", + type: "build", + }); + } + } }; diff --git a/packages/server/src/utils/notifications/build-success.ts b/packages/server/src/utils/notifications/build-success.ts index 8c84d2e67..6c110eb6c 100644 --- a/packages/server/src/utils/notifications/build-success.ts +++ b/packages/server/src/utils/notifications/build-success.ts @@ -6,231 +6,231 @@ import { renderAsync } from "@react-email/components"; import { format } from "date-fns"; import { and, eq } from "drizzle-orm"; import { - sendCustomNotification, - sendDiscordNotification, - sendEmailNotification, - sendGotifyNotification, - sendNtfyNotification, - sendSlackNotification, - sendTelegramNotification, + sendCustomNotification, + sendDiscordNotification, + sendEmailNotification, + sendGotifyNotification, + sendNtfyNotification, + sendSlackNotification, + sendTelegramNotification, } from "./utils"; interface Props { - projectName: string; - applicationName: string; - applicationType: string; - buildLink: string; - organizationId: string; - domains: Domain[]; + projectName: string; + applicationName: string; + applicationType: string; + buildLink: string; + organizationId: string; + domains: Domain[]; } export const sendBuildSuccessNotifications = async ({ - projectName, - applicationName, - applicationType, - buildLink, - organizationId, - domains, + projectName, + applicationName, + applicationType, + buildLink, + organizationId, + domains, }: Props) => { - const date = new Date(); - const unixDate = ~~(Number(date) / 1000); - const notificationList = await db.query.notifications.findMany({ - where: and( - eq(notifications.appDeploy, true), - eq(notifications.organizationId, organizationId) - ), - with: { - email: true, - discord: true, - telegram: true, - slack: true, - gotify: true, - ntfy: true, - custom: true, - }, - }); + const date = new Date(); + const unixDate = ~~(Number(date) / 1000); + const notificationList = await db.query.notifications.findMany({ + where: and( + eq(notifications.appDeploy, true), + eq(notifications.organizationId, organizationId), + ), + with: { + email: true, + discord: true, + telegram: true, + slack: true, + gotify: true, + ntfy: true, + custom: true, + }, + }); - for (const notification of notificationList) { - const { email, discord, telegram, slack, gotify, ntfy, custom } = - notification; + for (const notification of notificationList) { + const { email, discord, telegram, slack, gotify, ntfy, custom } = + notification; - if (email) { - const template = await renderAsync( - BuildSuccessEmail({ - projectName, - applicationName, - applicationType, - buildLink, - date: date.toLocaleString(), - }) - ).catch(); - await sendEmailNotification(email, "Build success for dokploy", template); - } + if (email) { + const template = await renderAsync( + BuildSuccessEmail({ + projectName, + applicationName, + applicationType, + buildLink, + date: date.toLocaleString(), + }), + ).catch(); + await sendEmailNotification(email, "Build success for dokploy", template); + } - if (discord) { - const decorate = (decoration: string, text: string) => - `${discord.decoration ? decoration : ""} ${text}`.trim(); + if (discord) { + const decorate = (decoration: string, text: string) => + `${discord.decoration ? decoration : ""} ${text}`.trim(); - await sendDiscordNotification(discord, { - title: decorate(">", "`โœ…` Build Success"), - color: 0x57f287, - fields: [ - { - name: decorate("`๐Ÿ› ๏ธ`", "Project"), - value: projectName, - inline: true, - }, - { - name: decorate("`โš™๏ธ`", "Application"), - value: applicationName, - inline: true, - }, - { - name: decorate("`โ”`", "Type"), - value: applicationType, - inline: true, - }, - { - name: decorate("`๐Ÿ“…`", "Date"), - value: ``, - inline: true, - }, - { - name: decorate("`โŒš`", "Time"), - value: ``, - inline: true, - }, - { - name: decorate("`โ“`", "Type"), - value: "Successful", - inline: true, - }, - { - name: decorate("`๐Ÿงท`", "Build Link"), - value: `[Click here to access build link](${buildLink})`, - }, - ], - timestamp: date.toISOString(), - footer: { - text: "Dokploy Build Notification", - }, - }); - } + await sendDiscordNotification(discord, { + title: decorate(">", "`โœ…` Build Success"), + color: 0x57f287, + fields: [ + { + name: decorate("`๐Ÿ› ๏ธ`", "Project"), + value: projectName, + inline: true, + }, + { + name: decorate("`โš™๏ธ`", "Application"), + value: applicationName, + inline: true, + }, + { + name: decorate("`โ”`", "Type"), + value: applicationType, + inline: true, + }, + { + name: decorate("`๐Ÿ“…`", "Date"), + value: ``, + inline: true, + }, + { + name: decorate("`โŒš`", "Time"), + value: ``, + inline: true, + }, + { + name: decorate("`โ“`", "Type"), + value: "Successful", + inline: true, + }, + { + name: decorate("`๐Ÿงท`", "Build Link"), + value: `[Click here to access build link](${buildLink})`, + }, + ], + timestamp: date.toISOString(), + footer: { + text: "Dokploy Build Notification", + }, + }); + } - if (gotify) { - const decorate = (decoration: string, text: string) => - `${gotify.decoration ? decoration : ""} ${text}\n`; - await sendGotifyNotification( - gotify, - decorate("โœ…", "Build Success"), - `${decorate("๐Ÿ› ๏ธ", `Project: ${projectName}`)}` + - `${decorate("โš™๏ธ", `Application: ${applicationName}`)}` + - `${decorate("โ”", `Type: ${applicationType}`)}` + - `${decorate("๐Ÿ•’", `Date: ${date.toLocaleString()}`)}` + - `${decorate("๐Ÿ”—", `Build details:\n${buildLink}`)}` - ); - } + if (gotify) { + const decorate = (decoration: string, text: string) => + `${gotify.decoration ? decoration : ""} ${text}\n`; + await sendGotifyNotification( + gotify, + decorate("โœ…", "Build Success"), + `${decorate("๐Ÿ› ๏ธ", `Project: ${projectName}`)}` + + `${decorate("โš™๏ธ", `Application: ${applicationName}`)}` + + `${decorate("โ”", `Type: ${applicationType}`)}` + + `${decorate("๐Ÿ•’", `Date: ${date.toLocaleString()}`)}` + + `${decorate("๐Ÿ”—", `Build details:\n${buildLink}`)}`, + ); + } - if (ntfy) { - await sendNtfyNotification( - ntfy, - "Build Success", - "white_check_mark", - `view, Build details, ${buildLink}, clear=true;`, - `๐Ÿ› Project: ${projectName}\n` + - `โš™๏ธApplication: ${applicationName}\n` + - `โ”Type: ${applicationType}\n` + - `๐Ÿ•’Date: ${date.toLocaleString()}` - ); - } + if (ntfy) { + await sendNtfyNotification( + ntfy, + "Build Success", + "white_check_mark", + `view, Build details, ${buildLink}, clear=true;`, + `๐Ÿ› Project: ${projectName}\n` + + `โš™๏ธApplication: ${applicationName}\n` + + `โ”Type: ${applicationType}\n` + + `๐Ÿ•’Date: ${date.toLocaleString()}`, + ); + } - if (telegram) { - const chunkArray = (array: T[], chunkSize: number): T[][] => - Array.from({ length: Math.ceil(array.length / chunkSize) }, (_, i) => - array.slice(i * chunkSize, i * chunkSize + chunkSize) - ); + if (telegram) { + const chunkArray = (array: T[], chunkSize: number): T[][] => + Array.from({ length: Math.ceil(array.length / chunkSize) }, (_, i) => + array.slice(i * chunkSize, i * chunkSize + chunkSize), + ); - const inlineButton = [ - [ - { - text: "Deployment Logs", - url: buildLink, - }, - ], - ...chunkArray(domains, 2).map((chunk) => - chunk.map((data) => ({ - text: data.host, - url: `${data.https ? "https" : "http"}://${data.host}`, - })) - ), - ]; + const inlineButton = [ + [ + { + text: "Deployment Logs", + url: buildLink, + }, + ], + ...chunkArray(domains, 2).map((chunk) => + chunk.map((data) => ({ + text: data.host, + url: `${data.https ? "https" : "http"}://${data.host}`, + })), + ), + ]; - await sendTelegramNotification( - telegram, - `โœ… Build Success\n\nProject: ${projectName}\nApplication: ${applicationName}\nType: ${applicationType}\nDate: ${format( - date, - "PP" - )}\nTime: ${format(date, "pp")}`, - inlineButton - ); - } + await sendTelegramNotification( + telegram, + `โœ… Build Success\n\nProject: ${projectName}\nApplication: ${applicationName}\nType: ${applicationType}\nDate: ${format( + date, + "PP", + )}\nTime: ${format(date, "pp")}`, + inlineButton, + ); + } - if (slack) { - const { channel } = slack; - await sendSlackNotification(slack, { - channel: channel, - attachments: [ - { - color: "#00FF00", - pretext: ":white_check_mark: *Build Success*", - fields: [ - { - title: "Project", - value: projectName, - short: true, - }, - { - title: "Application", - value: applicationName, - short: true, - }, - { - title: "Type", - value: applicationType, - short: true, - }, - { - title: "Time", - value: date.toLocaleString(), - short: true, - }, - ], - actions: [ - { - type: "button", - text: "View Build Details", - url: buildLink, - }, - ], - }, - ], - }); - } + if (slack) { + const { channel } = slack; + await sendSlackNotification(slack, { + channel: channel, + attachments: [ + { + color: "#00FF00", + pretext: ":white_check_mark: *Build Success*", + fields: [ + { + title: "Project", + value: projectName, + short: true, + }, + { + title: "Application", + value: applicationName, + short: true, + }, + { + title: "Type", + value: applicationType, + short: true, + }, + { + title: "Time", + value: date.toLocaleString(), + short: true, + }, + ], + actions: [ + { + type: "button", + text: "View Build Details", + url: buildLink, + }, + ], + }, + ], + }); + } - if (custom) { - await sendCustomNotification(custom, { - title: "Build Success", - message: "Build completed successfully", - projectName, - applicationName, - applicationType, - buildLink, - timestamp: date.toISOString(), - date: date.toLocaleString(), - domains: domains.map((domain) => domain.host).join(", "), - status: "success", - type: "build", - }); - } - } + if (custom) { + await sendCustomNotification(custom, { + title: "Build Success", + message: "Build completed successfully", + projectName, + applicationName, + applicationType, + buildLink, + timestamp: date.toISOString(), + date: date.toLocaleString(), + domains: domains.map((domain) => domain.host).join(", "), + status: "success", + type: "build", + }); + } + } }; diff --git a/packages/server/src/utils/notifications/database-backup.ts b/packages/server/src/utils/notifications/database-backup.ts index 24e77e1bf..b3f6884d3 100644 --- a/packages/server/src/utils/notifications/database-backup.ts +++ b/packages/server/src/utils/notifications/database-backup.ts @@ -50,7 +50,8 @@ export const sendDatabaseBackupNotifications = async ({ }); for (const notification of notificationList) { - const { email, discord, telegram, slack, gotify, ntfy, custom } = notification; + const { email, discord, telegram, slack, gotify, ntfy, custom } = + notification; if (email) { const template = await renderAsync( @@ -244,7 +245,10 @@ export const sendDatabaseBackupNotifications = async ({ if (custom) { await sendCustomNotification(custom, { title: `Database Backup ${type === "success" ? "Successful" : "Failed"}`, - message: type === "success" ? "Database backup completed successfully" : "Database backup failed", + message: + type === "success" + ? "Database backup completed successfully" + : "Database backup failed", projectName, applicationName, databaseType, diff --git a/packages/server/src/utils/notifications/docker-cleanup.ts b/packages/server/src/utils/notifications/docker-cleanup.ts index 7897bf92f..2afb7037a 100644 --- a/packages/server/src/utils/notifications/docker-cleanup.ts +++ b/packages/server/src/utils/notifications/docker-cleanup.ts @@ -37,7 +37,8 @@ export const sendDockerCleanupNotifications = async ( }); for (const notification of notificationList) { - const { email, discord, telegram, slack, gotify, ntfy, custom } = notification; + const { email, discord, telegram, slack, gotify, ntfy, custom } = + notification; if (email) { const template = await renderAsync( diff --git a/packages/server/src/utils/notifications/dokploy-restart.ts b/packages/server/src/utils/notifications/dokploy-restart.ts index 32ded5c5f..401db8f88 100644 --- a/packages/server/src/utils/notifications/dokploy-restart.ts +++ b/packages/server/src/utils/notifications/dokploy-restart.ts @@ -31,7 +31,8 @@ export const sendDokployRestartNotifications = async () => { }); for (const notification of notificationList) { - const { email, discord, telegram, slack, gotify, ntfy, custom } = notification; + const { email, discord, telegram, slack, gotify, ntfy, custom } = + notification; if (email) { const template = await renderAsync( diff --git a/packages/server/src/utils/notifications/utils.ts b/packages/server/src/utils/notifications/utils.ts index 3ce5c98bc..3e24e78dd 100644 --- a/packages/server/src/utils/notifications/utils.ts +++ b/packages/server/src/utils/notifications/utils.ts @@ -1,193 +1,193 @@ import type { - custom, - discord, - email, - gotify, - ntfy, - slack, - telegram, + custom, + discord, + email, + gotify, + ntfy, + slack, + telegram, } from "@dokploy/server/db/schema"; import nodemailer from "nodemailer"; export const sendEmailNotification = async ( - connection: typeof email.$inferInsert, - subject: string, - htmlContent: string + connection: typeof email.$inferInsert, + subject: string, + htmlContent: string, ) => { - try { - const { - smtpServer, - smtpPort, - username, - password, - fromAddress, - toAddresses, - } = connection; - const transporter = nodemailer.createTransport({ - host: smtpServer, - port: smtpPort, - auth: { user: username, pass: password }, - }); + try { + const { + smtpServer, + smtpPort, + username, + password, + fromAddress, + toAddresses, + } = connection; + const transporter = nodemailer.createTransport({ + host: smtpServer, + port: smtpPort, + auth: { user: username, pass: password }, + }); - await transporter.sendMail({ - from: fromAddress, - to: toAddresses.join(", "), - subject, - html: htmlContent, - }); - } catch (err) { - console.log(err); - } + await transporter.sendMail({ + from: fromAddress, + to: toAddresses.join(", "), + subject, + html: htmlContent, + }); + } catch (err) { + console.log(err); + } }; export const sendDiscordNotification = async ( - connection: typeof discord.$inferInsert, - embed: any + connection: typeof discord.$inferInsert, + embed: any, ) => { - // try { - await fetch(connection.webhookUrl, { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ embeds: [embed] }), - }); - // } catch (err) { - // console.log(err); - // } + // try { + await fetch(connection.webhookUrl, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ embeds: [embed] }), + }); + // } catch (err) { + // console.log(err); + // } }; export const sendTelegramNotification = async ( - connection: typeof telegram.$inferInsert, - messageText: string, - inlineButton?: { - text: string; - url: string; - }[][] + connection: typeof telegram.$inferInsert, + messageText: string, + inlineButton?: { + text: string; + url: string; + }[][], ) => { - try { - const url = `https://api.telegram.org/bot${connection.botToken}/sendMessage`; - await fetch(url, { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ - chat_id: connection.chatId, - message_thread_id: connection.messageThreadId, - text: messageText, - parse_mode: "HTML", - disable_web_page_preview: true, - reply_markup: { - inline_keyboard: inlineButton, - }, - }), - }); - } catch (err) { - console.log(err); - } + try { + const url = `https://api.telegram.org/bot${connection.botToken}/sendMessage`; + await fetch(url, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + chat_id: connection.chatId, + message_thread_id: connection.messageThreadId, + text: messageText, + parse_mode: "HTML", + disable_web_page_preview: true, + reply_markup: { + inline_keyboard: inlineButton, + }, + }), + }); + } catch (err) { + console.log(err); + } }; export const sendSlackNotification = async ( - connection: typeof slack.$inferInsert, - message: any + connection: typeof slack.$inferInsert, + message: any, ) => { - try { - await fetch(connection.webhookUrl, { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify(message), - }); - } catch (err) { - console.log(err); - } + try { + await fetch(connection.webhookUrl, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(message), + }); + } catch (err) { + console.log(err); + } }; export const sendGotifyNotification = async ( - connection: typeof gotify.$inferInsert, - title: string, - message: string + connection: typeof gotify.$inferInsert, + title: string, + message: string, ) => { - const response = await fetch(`${connection.serverUrl}/message`, { - method: "POST", - headers: { - "Content-Type": "application/json", - "X-Gotify-Key": connection.appToken, - }, - body: JSON.stringify({ - title: title, - message: message, - priority: connection.priority, - extras: { - "client::display": { - contentType: "text/plain", - }, - }, - }), - }); + const response = await fetch(`${connection.serverUrl}/message`, { + method: "POST", + headers: { + "Content-Type": "application/json", + "X-Gotify-Key": connection.appToken, + }, + body: JSON.stringify({ + title: title, + message: message, + priority: connection.priority, + extras: { + "client::display": { + contentType: "text/plain", + }, + }, + }), + }); - if (!response.ok) { - throw new Error( - `Failed to send Gotify notification: ${response.statusText}` - ); - } + if (!response.ok) { + throw new Error( + `Failed to send Gotify notification: ${response.statusText}`, + ); + } }; export const sendNtfyNotification = async ( - connection: typeof ntfy.$inferInsert, - title: string, - tags: string, - actions: string, - message: string + connection: typeof ntfy.$inferInsert, + title: string, + tags: string, + actions: string, + message: string, ) => { - const response = await fetch(`${connection.serverUrl}/${connection.topic}`, { - method: "POST", - headers: { - Authorization: `Bearer ${connection.accessToken}`, - "X-Priority": connection.priority?.toString() || "3", - "X-Title": title, - "X-Tags": tags, - "X-Actions": actions, - }, - body: message, - }); + const response = await fetch(`${connection.serverUrl}/${connection.topic}`, { + method: "POST", + headers: { + Authorization: `Bearer ${connection.accessToken}`, + "X-Priority": connection.priority?.toString() || "3", + "X-Title": title, + "X-Tags": tags, + "X-Actions": actions, + }, + body: message, + }); - if (!response.ok) { - throw new Error(`Failed to send ntfy notification: ${response.statusText}`); - } + if (!response.ok) { + throw new Error(`Failed to send ntfy notification: ${response.statusText}`); + } }; export const sendCustomNotification = async ( - connection: typeof custom.$inferInsert, - payload: Record + connection: typeof custom.$inferInsert, + payload: Record, ) => { - try { - // Parse headers if provided - let headers: Record = { - "Content-Type": "application/json", - }; - if (connection.headers) { - try { - headers = { ...headers, ...JSON.parse(connection.headers) }; - } catch (error) { - console.error("Error parsing headers:", error); - } - } + try { + // Parse headers if provided + let headers: Record = { + "Content-Type": "application/json", + }; + if (connection.headers) { + try { + headers = { ...headers, ...JSON.parse(connection.headers) }; + } catch (error) { + console.error("Error parsing headers:", error); + } + } - // Default body with payload - const body = JSON.stringify(payload); + // Default body with payload + const body = JSON.stringify(payload); - const response = await fetch(connection.endpoint, { - method: "POST", - headers, - body, - }); + const response = await fetch(connection.endpoint, { + method: "POST", + headers, + body, + }); - if (!response.ok) { - throw new Error( - `Failed to send custom notification: ${response.statusText}` - ); - } + if (!response.ok) { + throw new Error( + `Failed to send custom notification: ${response.statusText}`, + ); + } - return response; - } catch (error) { - console.error("Error sending custom notification:", error); - throw error; - } + return response; + } catch (error) { + console.error("Error sending custom notification:", error); + throw error; + } };