import { db } from "@dokploy/server/db"; import { notifications } from "@dokploy/server/db/schema"; import { renderAsync } from "@react-email/components"; import { format } from "date-fns"; import { and, eq } from "drizzle-orm"; import { sendDiscordNotification, sendEmailNotification, sendGotifyNotification, sendNtfyNotification, sendSlackNotification, sendTelegramNotification, } from "./utils"; export const sendVolumeBackupNotifications = async ({ projectName, applicationName, volumeName, serviceType, type, errorMessage, organizationId, backupSize, }: { projectName: string; applicationName: string; volumeName: string; serviceType: | "application" | "postgres" | "mysql" | "mongodb" | "mariadb" | "redis" | "compose"; type: "error" | "success"; organizationId: string; errorMessage?: string; backupSize?: string; }) => { const date = new Date(); const unixDate = ~~(Number(date) / 1000); const notificationList = await db.query.notifications.findMany({ where: and( eq(notifications.volumeBackup, true), eq(notifications.organizationId, organizationId), ), with: { email: true, discord: true, telegram: true, slack: true, gotify: true, ntfy: true, }, }); for (const notification of notificationList) { const { email, discord, telegram, slack, gotify, ntfy } = notification; if (email) { const subject = `Volume Backup ${type === "success" ? "Successful" : "Failed"} - ${applicationName}`; const htmlContent = `
Project: ${projectName}
Application: ${applicationName}
Volume Name: ${volumeName}
Service Type: ${serviceType}
${backupSize ? `Backup Size: ${backupSize}
` : ""}Date: ${date.toLocaleString()}
${type === "error" && errorMessage ? `Error: ${errorMessage}
This notification was sent by Dokploy Volume Backup System.
${errorMessage}`
: "";
const sizeInfo = backupSize ? `\nBackup Size: ${backupSize}` : "";
const messageText = `${statusEmoji} Volume Backup ${typeStatus}\n\nProject: ${projectName}\nApplication: ${applicationName}\nVolume Name: ${volumeName}\nService Type: ${serviceType}${sizeInfo}\nDate: ${format(date, "PP")}\nTime: ${format(date, "pp")}${isError ? errorMsg : ""}`;
await sendTelegramNotification(telegram, messageText);
}
if (slack) {
const { channel } = slack;
await sendSlackNotification(slack, {
channel: channel,
attachments: [
{
color: type === "success" ? "#00FF00" : "#FF0000",
pretext:
type === "success"
? ":white_check_mark: *Volume Backup Successful*"
: ":x: *Volume Backup Failed*",
fields: [
...(type === "error" && errorMessage
? [
{
title: "Error Message",
value: errorMessage,
short: false,
},
]
: []),
{
title: "Project",
value: projectName,
short: true,
},
{
title: "Application",
value: applicationName,
short: true,
},
{
title: "Volume Name",
value: volumeName,
short: true,
},
{
title: "Service Type",
value: serviceType,
short: true,
},
...(backupSize
? [
{
title: "Backup Size",
value: backupSize,
short: true,
},
]
: []),
{
title: "Time",
value: date.toLocaleString(),
short: true,
},
{
title: "Type",
value: type,
short: true,
},
{
title: "Status",
value: type === "success" ? "Successful" : "Failed",
short: true,
},
],
},
],
});
}
}
};