mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
265 lines
5.8 KiB
TypeScript
265 lines
5.8 KiB
TypeScript
import { db } from "@/server/db";
|
|
import {
|
|
type apiCreateDiscord,
|
|
type apiCreateEmail,
|
|
type apiCreateSlack,
|
|
type apiCreateTelegram,
|
|
discord,
|
|
email,
|
|
notifications,
|
|
slack,
|
|
telegram,
|
|
} from "@/server/db/schema";
|
|
import { TRPCError } from "@trpc/server";
|
|
import { eq } from "drizzle-orm";
|
|
|
|
export type Notification = typeof notifications.$inferSelect;
|
|
|
|
export const createSlackNotification = async (
|
|
input: typeof apiCreateSlack._type,
|
|
) => {
|
|
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",
|
|
});
|
|
}
|
|
|
|
const newDestination = await tx
|
|
.insert(notifications)
|
|
.values({
|
|
slackId: newSlack.slackId,
|
|
name: input.name,
|
|
appDeploy: input.appDeploy,
|
|
userJoin: input.userJoin,
|
|
appBuildError: input.appBuildError,
|
|
databaseBackup: input.databaseBackup,
|
|
dokployRestart: input.dokployRestart,
|
|
notificationType: "slack",
|
|
})
|
|
.returning()
|
|
.then((value) => value[0]);
|
|
|
|
if (!newDestination) {
|
|
throw new TRPCError({
|
|
code: "BAD_REQUEST",
|
|
message: "Error input: Inserting notification",
|
|
});
|
|
}
|
|
|
|
return newDestination;
|
|
});
|
|
};
|
|
|
|
export const createTelegramNotification = async (
|
|
input: typeof apiCreateTelegram._type,
|
|
) => {
|
|
await db.transaction(async (tx) => {
|
|
const newTelegram = await tx
|
|
.insert(telegram)
|
|
.values({
|
|
botToken: input.botToken,
|
|
chatId: input.chatId,
|
|
})
|
|
.returning()
|
|
.then((value) => value[0]);
|
|
|
|
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,
|
|
userJoin: input.userJoin,
|
|
appBuildError: input.appBuildError,
|
|
databaseBackup: input.databaseBackup,
|
|
dokployRestart: input.dokployRestart,
|
|
notificationType: "telegram",
|
|
})
|
|
.returning()
|
|
.then((value) => value[0]);
|
|
|
|
if (!newDestination) {
|
|
throw new TRPCError({
|
|
code: "BAD_REQUEST",
|
|
message: "Error input: Inserting notification",
|
|
});
|
|
}
|
|
|
|
return newDestination;
|
|
});
|
|
};
|
|
|
|
export const createDiscordNotification = async (
|
|
input: typeof apiCreateDiscord._type,
|
|
) => {
|
|
await db.transaction(async (tx) => {
|
|
const newDiscord = await tx
|
|
.insert(discord)
|
|
.values({
|
|
webhookUrl: input.webhookUrl,
|
|
})
|
|
.returning()
|
|
.then((value) => value[0]);
|
|
|
|
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,
|
|
userJoin: input.userJoin,
|
|
appBuildError: input.appBuildError,
|
|
databaseBackup: input.databaseBackup,
|
|
dokployRestart: input.dokployRestart,
|
|
notificationType: "discord",
|
|
})
|
|
.returning()
|
|
.then((value) => value[0]);
|
|
|
|
if (!newDestination) {
|
|
throw new TRPCError({
|
|
code: "BAD_REQUEST",
|
|
message: "Error input: Inserting notification",
|
|
});
|
|
}
|
|
|
|
return newDestination;
|
|
});
|
|
};
|
|
|
|
export const createEmailNotification = async (
|
|
input: typeof apiCreateEmail._type,
|
|
) => {
|
|
await db.transaction(async (tx) => {
|
|
const newEmail = await tx
|
|
.insert(email)
|
|
.values({
|
|
smtpServer: input.smtpServer,
|
|
smtpPort: input.smtpPort,
|
|
username: input.username,
|
|
password: input.password,
|
|
toAddresses: input.toAddresses,
|
|
})
|
|
.returning()
|
|
.then((value) => value[0]);
|
|
|
|
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,
|
|
userJoin: input.userJoin,
|
|
appBuildError: input.appBuildError,
|
|
databaseBackup: input.databaseBackup,
|
|
dokployRestart: input.dokployRestart,
|
|
notificationType: "email",
|
|
})
|
|
.returning()
|
|
.then((value) => value[0]);
|
|
|
|
if (!newDestination) {
|
|
throw new TRPCError({
|
|
code: "BAD_REQUEST",
|
|
message: "Error input: Inserting notification",
|
|
});
|
|
}
|
|
|
|
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,
|
|
},
|
|
});
|
|
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();
|
|
|
|
return result[0];
|
|
};
|
|
|
|
export const updateDestinationById = async (
|
|
notificationId: string,
|
|
notificationData: Partial<Notification>,
|
|
) => {
|
|
const result = await db
|
|
.update(notifications)
|
|
.set({
|
|
...notificationData,
|
|
})
|
|
.where(eq(notifications.notificationId, notificationId))
|
|
.returning();
|
|
|
|
return result[0];
|
|
};
|
|
|
|
export const sendNotification = async (
|
|
notificationData: Partial<Notification>,
|
|
) => {
|
|
// if(notificationData.notificationType === "slack"){
|
|
// const { webhookUrl, channel } = notificationData;
|
|
// try {
|
|
// const response = await fetch(webhookUrl, {
|
|
// method: "POST",
|
|
// headers: {
|
|
// "Content-Type": "application/json",
|
|
// },
|
|
// body: JSON.stringify({ text: "Test notification", channel }),
|
|
// });
|
|
// } catch (err) {
|
|
// console.log(err);
|
|
// }
|
|
// }
|
|
};
|