From 6b7712e35fc2ac6ccc611dc242b3faf94d3cb29a Mon Sep 17 00:00:00 2001 From: Mauricio Siu <47042324+Siumauricio@users.noreply.github.com> Date: Mon, 4 Nov 2024 10:10:52 -0600 Subject: [PATCH] fix(dokploy): filter notifications by admin --- apps/dokploy/server/api/routers/settings.ts | 4 ++-- packages/server/src/services/application.ts | 4 ++++ packages/server/src/services/compose.ts | 4 ++++ packages/server/src/utils/backups/mariadb.ts | 2 ++ packages/server/src/utils/backups/mongo.ts | 2 ++ packages/server/src/utils/backups/mysql.ts | 2 ++ packages/server/src/utils/backups/postgres.ts | 2 ++ packages/server/src/utils/notifications/build-error.ts | 9 +++++++-- packages/server/src/utils/notifications/build-success.ts | 9 +++++++-- .../server/src/utils/notifications/database-backup.ts | 9 +++++++-- .../server/src/utils/notifications/docker-cleanup.ts | 8 ++++++-- 11 files changed, 45 insertions(+), 10 deletions(-) diff --git a/apps/dokploy/server/api/routers/settings.ts b/apps/dokploy/server/api/routers/settings.ts index 485e8c73..e5c34b11 100644 --- a/apps/dokploy/server/api/routers/settings.ts +++ b/apps/dokploy/server/api/routers/settings.ts @@ -242,7 +242,7 @@ export const settingsRouter = createTRPCRouter({ await cleanUpUnusedImages(server.serverId); await cleanUpDockerBuilder(server.serverId); await cleanUpSystemPrune(server.serverId); - await sendDockerCleanupNotifications(); + await sendDockerCleanupNotifications(server.adminId); }); } } else { @@ -278,7 +278,7 @@ export const settingsRouter = createTRPCRouter({ await cleanUpUnusedImages(); await cleanUpDockerBuilder(); await cleanUpSystemPrune(); - await sendDockerCleanupNotifications(); + await sendDockerCleanupNotifications(admin.adminId); }); } else { const currentJob = scheduledJobs["docker-cleanup"]; diff --git a/packages/server/src/services/application.ts b/packages/server/src/services/application.ts index ec0c0c2f..5885671b 100644 --- a/packages/server/src/services/application.ts +++ b/packages/server/src/services/application.ts @@ -193,6 +193,7 @@ export const deployApplication = async ({ applicationName: application.name, applicationType: "application", buildLink, + adminId: application.project.adminId, }); } catch (error) { await updateDeploymentStatus(deployment.deploymentId, "error"); @@ -204,6 +205,7 @@ export const deployApplication = async ({ // @ts-ignore errorMessage: error?.message || "Error to build", buildLink, + adminId: application.project.adminId, }); console.log( @@ -314,6 +316,7 @@ export const deployRemoteApplication = async ({ applicationName: application.name, applicationType: "application", buildLink, + adminId: application.project.adminId, }); } catch (error) { // @ts-ignore @@ -336,6 +339,7 @@ export const deployRemoteApplication = async ({ // @ts-ignore errorMessage: error?.message || "Error to build", buildLink, + adminId: application.project.adminId, }); console.log( diff --git a/packages/server/src/services/compose.ts b/packages/server/src/services/compose.ts index 61d7e5fc..63d29539 100644 --- a/packages/server/src/services/compose.ts +++ b/packages/server/src/services/compose.ts @@ -235,6 +235,7 @@ export const deployCompose = async ({ applicationName: compose.name, applicationType: "compose", buildLink, + adminId: compose.project.adminId, }); } catch (error) { await updateDeploymentStatus(deployment.deploymentId, "error"); @@ -248,6 +249,7 @@ export const deployCompose = async ({ // @ts-ignore errorMessage: error?.message || "Error to build", buildLink, + adminId: compose.project.adminId, }); throw error; } @@ -353,6 +355,7 @@ export const deployRemoteCompose = async ({ applicationName: compose.name, applicationType: "compose", buildLink, + adminId: compose.project.adminId, }); } catch (error) { // @ts-ignore @@ -376,6 +379,7 @@ export const deployRemoteCompose = async ({ // @ts-ignore errorMessage: error?.message || "Error to build", buildLink, + adminId: compose.project.adminId, }); throw error; } diff --git a/packages/server/src/utils/backups/mariadb.ts b/packages/server/src/utils/backups/mariadb.ts index b8621b28..79cba9c5 100644 --- a/packages/server/src/utils/backups/mariadb.ts +++ b/packages/server/src/utils/backups/mariadb.ts @@ -49,6 +49,7 @@ export const runMariadbBackup = async ( projectName: project.name, databaseType: "mariadb", type: "success", + adminId: project.adminId, }); } catch (error) { console.log(error); @@ -59,6 +60,7 @@ export const runMariadbBackup = async ( type: "error", // @ts-ignore errorMessage: error?.message || "Error message not provided", + adminId: project.adminId, }); throw error; } diff --git a/packages/server/src/utils/backups/mongo.ts b/packages/server/src/utils/backups/mongo.ts index 1a30fe14..ddd1b889 100644 --- a/packages/server/src/utils/backups/mongo.ts +++ b/packages/server/src/utils/backups/mongo.ts @@ -46,6 +46,7 @@ export const runMongoBackup = async (mongo: Mongo, backup: BackupSchedule) => { projectName: project.name, databaseType: "mongodb", type: "success", + adminId: project.adminId, }); } catch (error) { console.log(error); @@ -56,6 +57,7 @@ export const runMongoBackup = async (mongo: Mongo, backup: BackupSchedule) => { type: "error", // @ts-ignore errorMessage: error?.message || "Error message not provided", + adminId: project.adminId, }); throw error; } diff --git a/packages/server/src/utils/backups/mysql.ts b/packages/server/src/utils/backups/mysql.ts index ea92ee84..b505204c 100644 --- a/packages/server/src/utils/backups/mysql.ts +++ b/packages/server/src/utils/backups/mysql.ts @@ -46,6 +46,7 @@ export const runMySqlBackup = async (mysql: MySql, backup: BackupSchedule) => { projectName: project.name, databaseType: "mysql", type: "success", + adminId: project.adminId, }); } catch (error) { console.log(error); @@ -56,6 +57,7 @@ export const runMySqlBackup = async (mysql: MySql, backup: BackupSchedule) => { type: "error", // @ts-ignore errorMessage: error?.message || "Error message not provided", + adminId: project.adminId, }); throw error; } diff --git a/packages/server/src/utils/backups/postgres.ts b/packages/server/src/utils/backups/postgres.ts index fe3171d6..e9609fc8 100644 --- a/packages/server/src/utils/backups/postgres.ts +++ b/packages/server/src/utils/backups/postgres.ts @@ -49,6 +49,7 @@ export const runPostgresBackup = async ( projectName: project.name, databaseType: "postgres", type: "success", + adminId: project.adminId, }); } catch (error) { await sendDatabaseBackupNotifications({ @@ -58,6 +59,7 @@ export const runPostgresBackup = async ( type: "error", // @ts-ignore errorMessage: error?.message || "Error message not provided", + adminId: project.adminId, }); throw error; diff --git a/packages/server/src/utils/notifications/build-error.ts b/packages/server/src/utils/notifications/build-error.ts index 703367ec..9fcb0641 100644 --- a/packages/server/src/utils/notifications/build-error.ts +++ b/packages/server/src/utils/notifications/build-error.ts @@ -2,7 +2,7 @@ import { db } from "@dokploy/server/db"; import { notifications } from "@dokploy/server/db/schema"; import BuildFailedEmail from "@dokploy/server/emails/emails/build-failed"; import { renderAsync } from "@react-email/components"; -import { eq } from "drizzle-orm"; +import { and, eq } from "drizzle-orm"; import { sendDiscordNotification, sendEmailNotification, @@ -16,6 +16,7 @@ interface Props { applicationType: string; errorMessage: string; buildLink: string; + adminId: string; } export const sendBuildErrorNotifications = async ({ @@ -24,10 +25,14 @@ export const sendBuildErrorNotifications = async ({ applicationType, errorMessage, buildLink, + adminId, }: Props) => { const date = new Date(); const notificationList = await db.query.notifications.findMany({ - where: eq(notifications.appBuildError, true), + where: and( + eq(notifications.appBuildError, true), + eq(notifications.adminId, adminId), + ), with: { email: true, discord: true, diff --git a/packages/server/src/utils/notifications/build-success.ts b/packages/server/src/utils/notifications/build-success.ts index 55cbb33d..9210eca2 100644 --- a/packages/server/src/utils/notifications/build-success.ts +++ b/packages/server/src/utils/notifications/build-success.ts @@ -2,7 +2,7 @@ import { db } from "@dokploy/server/db"; import { notifications } from "@dokploy/server/db/schema"; import BuildSuccessEmail from "@dokploy/server/emails/emails/build-success"; import { renderAsync } from "@react-email/components"; -import { eq } from "drizzle-orm"; +import { and, eq } from "drizzle-orm"; import { sendDiscordNotification, sendEmailNotification, @@ -15,6 +15,7 @@ interface Props { applicationName: string; applicationType: string; buildLink: string; + adminId: string; } export const sendBuildSuccessNotifications = async ({ @@ -22,10 +23,14 @@ export const sendBuildSuccessNotifications = async ({ applicationName, applicationType, buildLink, + adminId, }: Props) => { const date = new Date(); const notificationList = await db.query.notifications.findMany({ - where: eq(notifications.appDeploy, true), + where: and( + eq(notifications.appDeploy, true), + eq(notifications.adminId, adminId), + ), with: { email: true, discord: true, diff --git a/packages/server/src/utils/notifications/database-backup.ts b/packages/server/src/utils/notifications/database-backup.ts index 1b2a6909..c97afa03 100644 --- a/packages/server/src/utils/notifications/database-backup.ts +++ b/packages/server/src/utils/notifications/database-backup.ts @@ -2,7 +2,7 @@ import { db } from "@dokploy/server/db"; import { notifications } from "@dokploy/server/db/schema"; import DatabaseBackupEmail from "@dokploy/server/emails/emails/database-backup"; import { renderAsync } from "@react-email/components"; -import { eq } from "drizzle-orm"; +import { and, eq } from "drizzle-orm"; import { sendDiscordNotification, sendEmailNotification, @@ -16,16 +16,21 @@ export const sendDatabaseBackupNotifications = async ({ databaseType, type, errorMessage, + adminId, }: { projectName: string; applicationName: string; databaseType: "postgres" | "mysql" | "mongodb" | "mariadb"; type: "error" | "success"; + adminId: string; errorMessage?: string; }) => { const date = new Date(); const notificationList = await db.query.notifications.findMany({ - where: eq(notifications.databaseBackup, true), + where: and( + eq(notifications.databaseBackup, true), + eq(notifications.adminId, adminId), + ), with: { email: true, discord: true, diff --git a/packages/server/src/utils/notifications/docker-cleanup.ts b/packages/server/src/utils/notifications/docker-cleanup.ts index a42657bc..a2e09139 100644 --- a/packages/server/src/utils/notifications/docker-cleanup.ts +++ b/packages/server/src/utils/notifications/docker-cleanup.ts @@ -2,7 +2,7 @@ import { db } from "@dokploy/server/db"; import { notifications } from "@dokploy/server/db/schema"; import DockerCleanupEmail from "@dokploy/server/emails/emails/docker-cleanup"; import { renderAsync } from "@react-email/components"; -import { eq } from "drizzle-orm"; +import { and, eq } from "drizzle-orm"; import { sendDiscordNotification, sendEmailNotification, @@ -11,11 +11,15 @@ import { } from "./utils"; export const sendDockerCleanupNotifications = async ( + adminId: string, message = "Docker cleanup for dokploy", ) => { const date = new Date(); const notificationList = await db.query.notifications.findMany({ - where: eq(notifications.dockerCleanup, true), + where: and( + eq(notifications.dockerCleanup, true), + eq(notifications.adminId, adminId), + ), with: { email: true, discord: true,