feat(notifications): add custom notification type and enhance notification schema with custom handling

This commit is contained in:
ChristoferMendes
2025-09-26 17:07:52 -03:00
parent b5bc384664
commit 7f3fe52b53
6 changed files with 13612 additions and 275 deletions
+12
View File
@@ -0,0 +1,12 @@
ALTER TYPE "public"."notificationType" ADD VALUE 'custom';--> statement-breakpoint
CREATE TABLE "custom" (
"customId" text PRIMARY KEY NOT NULL,
"endpoint" text NOT NULL,
"httpMethod" text DEFAULT 'POST' NOT NULL,
"headers" text,
"body" text,
"queryParams" text
);
--> statement-breakpoint
ALTER TABLE "notification" ADD COLUMN "customId" text;--> statement-breakpoint
ALTER TABLE "notification" ADD CONSTRAINT "notification_customId_custom_customId_fk" FOREIGN KEY ("customId") REFERENCES "public"."custom"("customId") ON DELETE cascade ON UPDATE no action;
@@ -0,0 +1,3 @@
ALTER TABLE "custom" DROP COLUMN "httpMethod";--> statement-breakpoint
ALTER TABLE "custom" DROP COLUMN "body";--> statement-breakpoint
ALTER TABLE "custom" DROP COLUMN "queryParams";
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+14
View File
@@ -792,6 +792,20 @@
"when": 1758483520214, "when": 1758483520214,
"tag": "0112_freezing_skrulls", "tag": "0112_freezing_skrulls",
"breakpoints": true "breakpoints": true
},
{
"idx": 113,
"version": "7",
"when": 1758913506515,
"tag": "0113_keen_wasp",
"breakpoints": true
},
{
"idx": 114,
"version": "7",
"when": 1758914193619,
"tag": "0114_smart_shape",
"breakpoints": true
} }
] ]
} }
+320 -275
View File
@@ -6,355 +6,400 @@ import { z } from "zod";
import { organization } from "./account"; import { organization } from "./account";
export const notificationType = pgEnum("notificationType", [ export const notificationType = pgEnum("notificationType", [
"slack", "slack",
"telegram", "telegram",
"discord", "discord",
"email", "email",
"gotify", "gotify",
"ntfy", "ntfy",
"custom",
]); ]);
export const notifications = pgTable("notification", { export const notifications = pgTable("notification", {
notificationId: text("notificationId") notificationId: text("notificationId")
.notNull() .notNull()
.primaryKey() .primaryKey()
.$defaultFn(() => nanoid()), .$defaultFn(() => nanoid()),
name: text("name").notNull(), name: text("name").notNull(),
appDeploy: boolean("appDeploy").notNull().default(false), appDeploy: boolean("appDeploy").notNull().default(false),
appBuildError: boolean("appBuildError").notNull().default(false), appBuildError: boolean("appBuildError").notNull().default(false),
databaseBackup: boolean("databaseBackup").notNull().default(false), databaseBackup: boolean("databaseBackup").notNull().default(false),
dokployRestart: boolean("dokployRestart").notNull().default(false), dokployRestart: boolean("dokployRestart").notNull().default(false),
dockerCleanup: boolean("dockerCleanup").notNull().default(false), dockerCleanup: boolean("dockerCleanup").notNull().default(false),
serverThreshold: boolean("serverThreshold").notNull().default(false), serverThreshold: boolean("serverThreshold").notNull().default(false),
notificationType: notificationType("notificationType").notNull(), notificationType: notificationType("notificationType").notNull(),
createdAt: text("createdAt") createdAt: text("createdAt")
.notNull() .notNull()
.$defaultFn(() => new Date().toISOString()), .$defaultFn(() => new Date().toISOString()),
slackId: text("slackId").references(() => slack.slackId, { slackId: text("slackId").references(() => slack.slackId, {
onDelete: "cascade", onDelete: "cascade",
}), }),
telegramId: text("telegramId").references(() => telegram.telegramId, { telegramId: text("telegramId").references(() => telegram.telegramId, {
onDelete: "cascade", onDelete: "cascade",
}), }),
discordId: text("discordId").references(() => discord.discordId, { discordId: text("discordId").references(() => discord.discordId, {
onDelete: "cascade", onDelete: "cascade",
}), }),
emailId: text("emailId").references(() => email.emailId, { emailId: text("emailId").references(() => email.emailId, {
onDelete: "cascade", onDelete: "cascade",
}), }),
gotifyId: text("gotifyId").references(() => gotify.gotifyId, { gotifyId: text("gotifyId").references(() => gotify.gotifyId, {
onDelete: "cascade", onDelete: "cascade",
}), }),
ntfyId: text("ntfyId").references(() => ntfy.ntfyId, { ntfyId: text("ntfyId").references(() => ntfy.ntfyId, {
onDelete: "cascade", onDelete: "cascade",
}), }),
organizationId: text("organizationId") customId: text("customId").references(() => custom.customId, {
.notNull() onDelete: "cascade",
.references(() => organization.id, { onDelete: "cascade" }), }),
organizationId: text("organizationId")
.notNull()
.references(() => organization.id, { onDelete: "cascade" }),
}); });
export const slack = pgTable("slack", { export const slack = pgTable("slack", {
slackId: text("slackId") slackId: text("slackId")
.notNull() .notNull()
.primaryKey() .primaryKey()
.$defaultFn(() => nanoid()), .$defaultFn(() => nanoid()),
webhookUrl: text("webhookUrl").notNull(), webhookUrl: text("webhookUrl").notNull(),
channel: text("channel"), channel: text("channel"),
}); });
export const telegram = pgTable("telegram", { export const telegram = pgTable("telegram", {
telegramId: text("telegramId") telegramId: text("telegramId")
.notNull() .notNull()
.primaryKey() .primaryKey()
.$defaultFn(() => nanoid()), .$defaultFn(() => nanoid()),
botToken: text("botToken").notNull(), botToken: text("botToken").notNull(),
chatId: text("chatId").notNull(), chatId: text("chatId").notNull(),
messageThreadId: text("messageThreadId"), messageThreadId: text("messageThreadId"),
}); });
export const discord = pgTable("discord", { export const discord = pgTable("discord", {
discordId: text("discordId") discordId: text("discordId")
.notNull() .notNull()
.primaryKey() .primaryKey()
.$defaultFn(() => nanoid()), .$defaultFn(() => nanoid()),
webhookUrl: text("webhookUrl").notNull(), webhookUrl: text("webhookUrl").notNull(),
decoration: boolean("decoration"), decoration: boolean("decoration"),
}); });
export const email = pgTable("email", { export const email = pgTable("email", {
emailId: text("emailId") emailId: text("emailId")
.notNull() .notNull()
.primaryKey() .primaryKey()
.$defaultFn(() => nanoid()), .$defaultFn(() => nanoid()),
smtpServer: text("smtpServer").notNull(), smtpServer: text("smtpServer").notNull(),
smtpPort: integer("smtpPort").notNull(), smtpPort: integer("smtpPort").notNull(),
username: text("username").notNull(), username: text("username").notNull(),
password: text("password").notNull(), password: text("password").notNull(),
fromAddress: text("fromAddress").notNull(), fromAddress: text("fromAddress").notNull(),
toAddresses: text("toAddress").array().notNull(), toAddresses: text("toAddress").array().notNull(),
}); });
export const gotify = pgTable("gotify", { export const gotify = pgTable("gotify", {
gotifyId: text("gotifyId") gotifyId: text("gotifyId")
.notNull() .notNull()
.primaryKey() .primaryKey()
.$defaultFn(() => nanoid()), .$defaultFn(() => nanoid()),
serverUrl: text("serverUrl").notNull(), serverUrl: text("serverUrl").notNull(),
appToken: text("appToken").notNull(), appToken: text("appToken").notNull(),
priority: integer("priority").notNull().default(5), priority: integer("priority").notNull().default(5),
decoration: boolean("decoration"), decoration: boolean("decoration"),
}); });
export const ntfy = pgTable("ntfy", { export const ntfy = pgTable("ntfy", {
ntfyId: text("ntfyId") ntfyId: text("ntfyId")
.notNull() .notNull()
.primaryKey() .primaryKey()
.$defaultFn(() => nanoid()), .$defaultFn(() => nanoid()),
serverUrl: text("serverUrl").notNull(), serverUrl: text("serverUrl").notNull(),
topic: text("topic").notNull(), topic: text("topic").notNull(),
accessToken: text("accessToken").notNull(), accessToken: text("accessToken").notNull(),
priority: integer("priority").notNull().default(3), 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
}); });
export const notificationsRelations = relations(notifications, ({ one }) => ({ export const notificationsRelations = relations(notifications, ({ one }) => ({
slack: one(slack, { slack: one(slack, {
fields: [notifications.slackId], fields: [notifications.slackId],
references: [slack.slackId], references: [slack.slackId],
}), }),
telegram: one(telegram, { telegram: one(telegram, {
fields: [notifications.telegramId], fields: [notifications.telegramId],
references: [telegram.telegramId], references: [telegram.telegramId],
}), }),
discord: one(discord, { discord: one(discord, {
fields: [notifications.discordId], fields: [notifications.discordId],
references: [discord.discordId], references: [discord.discordId],
}), }),
email: one(email, { email: one(email, {
fields: [notifications.emailId], fields: [notifications.emailId],
references: [email.emailId], references: [email.emailId],
}), }),
gotify: one(gotify, { gotify: one(gotify, {
fields: [notifications.gotifyId], fields: [notifications.gotifyId],
references: [gotify.gotifyId], references: [gotify.gotifyId],
}), }),
ntfy: one(ntfy, { ntfy: one(ntfy, {
fields: [notifications.ntfyId], fields: [notifications.ntfyId],
references: [ntfy.ntfyId], references: [ntfy.ntfyId],
}), }),
organization: one(organization, { custom: one(custom, {
fields: [notifications.organizationId], fields: [notifications.customId],
references: [organization.id], references: [custom.customId],
}), }),
organization: one(organization, {
fields: [notifications.organizationId],
references: [organization.id],
}),
})); }));
export const notificationsSchema = createInsertSchema(notifications); export const notificationsSchema = createInsertSchema(notifications);
export const apiCreateSlack = notificationsSchema export const apiCreateSlack = notificationsSchema
.pick({ .pick({
appBuildError: true, appBuildError: true,
databaseBackup: true, databaseBackup: true,
dokployRestart: true, dokployRestart: true,
name: true, name: true,
appDeploy: true, appDeploy: true,
dockerCleanup: true, dockerCleanup: true,
serverThreshold: true, serverThreshold: true,
}) })
.extend({ .extend({
webhookUrl: z.string().min(1), webhookUrl: z.string().min(1),
channel: z.string(), channel: z.string(),
}) })
.required(); .required();
export const apiUpdateSlack = apiCreateSlack.partial().extend({ export const apiUpdateSlack = apiCreateSlack.partial().extend({
notificationId: z.string().min(1), notificationId: z.string().min(1),
slackId: z.string(), slackId: z.string(),
organizationId: z.string().optional(), organizationId: z.string().optional(),
}); });
export const apiTestSlackConnection = apiCreateSlack.pick({ export const apiTestSlackConnection = apiCreateSlack.pick({
webhookUrl: true, webhookUrl: true,
channel: true, channel: true,
}); });
export const apiCreateTelegram = notificationsSchema export const apiCreateTelegram = notificationsSchema
.pick({ .pick({
appBuildError: true, appBuildError: true,
databaseBackup: true, databaseBackup: true,
dokployRestart: true, dokployRestart: true,
name: true, name: true,
appDeploy: true, appDeploy: true,
dockerCleanup: true, dockerCleanup: true,
serverThreshold: true, serverThreshold: true,
}) })
.extend({ .extend({
botToken: z.string().min(1), botToken: z.string().min(1),
chatId: z.string().min(1), chatId: z.string().min(1),
messageThreadId: z.string(), messageThreadId: z.string(),
}) })
.required(); .required();
export const apiUpdateTelegram = apiCreateTelegram.partial().extend({ export const apiUpdateTelegram = apiCreateTelegram.partial().extend({
notificationId: z.string().min(1), notificationId: z.string().min(1),
telegramId: z.string().min(1), telegramId: z.string().min(1),
organizationId: z.string().optional(), organizationId: z.string().optional(),
}); });
export const apiTestTelegramConnection = apiCreateTelegram.pick({ export const apiTestTelegramConnection = apiCreateTelegram.pick({
botToken: true, botToken: true,
chatId: true, chatId: true,
messageThreadId: true, messageThreadId: true,
}); });
export const apiCreateDiscord = notificationsSchema export const apiCreateDiscord = notificationsSchema
.pick({ .pick({
appBuildError: true, appBuildError: true,
databaseBackup: true, databaseBackup: true,
dokployRestart: true, dokployRestart: true,
name: true, name: true,
appDeploy: true, appDeploy: true,
dockerCleanup: true, dockerCleanup: true,
serverThreshold: true, serverThreshold: true,
}) })
.extend({ .extend({
webhookUrl: z.string().min(1), webhookUrl: z.string().min(1),
decoration: z.boolean(), decoration: z.boolean(),
}) })
.required(); .required();
export const apiUpdateDiscord = apiCreateDiscord.partial().extend({ export const apiUpdateDiscord = apiCreateDiscord.partial().extend({
notificationId: z.string().min(1), notificationId: z.string().min(1),
discordId: z.string().min(1), discordId: z.string().min(1),
organizationId: z.string().optional(), organizationId: z.string().optional(),
}); });
export const apiTestDiscordConnection = apiCreateDiscord export const apiTestDiscordConnection = apiCreateDiscord
.pick({ .pick({
webhookUrl: true, webhookUrl: true,
}) })
.extend({ .extend({
decoration: z.boolean().optional(), decoration: z.boolean().optional(),
}); });
export const apiCreateEmail = notificationsSchema export const apiCreateEmail = notificationsSchema
.pick({ .pick({
appBuildError: true, appBuildError: true,
databaseBackup: true, databaseBackup: true,
dokployRestart: true, dokployRestart: true,
name: true, name: true,
appDeploy: true, appDeploy: true,
dockerCleanup: true, dockerCleanup: true,
serverThreshold: true, serverThreshold: true,
}) })
.extend({ .extend({
smtpServer: z.string().min(1), smtpServer: z.string().min(1),
smtpPort: z.number().min(1), smtpPort: z.number().min(1),
username: z.string().min(1), username: z.string().min(1),
password: z.string().min(1), password: z.string().min(1),
fromAddress: z.string().min(1), fromAddress: z.string().min(1),
toAddresses: z.array(z.string()).min(1), toAddresses: z.array(z.string()).min(1),
}) })
.required(); .required();
export const apiUpdateEmail = apiCreateEmail.partial().extend({ export const apiUpdateEmail = apiCreateEmail.partial().extend({
notificationId: z.string().min(1), notificationId: z.string().min(1),
emailId: z.string().min(1), emailId: z.string().min(1),
organizationId: z.string().optional(), organizationId: z.string().optional(),
}); });
export const apiTestEmailConnection = apiCreateEmail.pick({ export const apiTestEmailConnection = apiCreateEmail.pick({
smtpServer: true, smtpServer: true,
smtpPort: true, smtpPort: true,
username: true, username: true,
password: true, password: true,
toAddresses: true, toAddresses: true,
fromAddress: true, fromAddress: true,
}); });
export const apiCreateGotify = notificationsSchema export const apiCreateGotify = notificationsSchema
.pick({ .pick({
appBuildError: true, appBuildError: true,
databaseBackup: true, databaseBackup: true,
dokployRestart: true, dokployRestart: true,
name: true, name: true,
appDeploy: true, appDeploy: true,
dockerCleanup: true, dockerCleanup: true,
}) })
.extend({ .extend({
serverUrl: z.string().min(1), serverUrl: z.string().min(1),
appToken: z.string().min(1), appToken: z.string().min(1),
priority: z.number().min(1), priority: z.number().min(1),
decoration: z.boolean(), decoration: z.boolean(),
}) })
.required(); .required();
export const apiUpdateGotify = apiCreateGotify.partial().extend({ export const apiUpdateGotify = apiCreateGotify.partial().extend({
notificationId: z.string().min(1), notificationId: z.string().min(1),
gotifyId: z.string().min(1), gotifyId: z.string().min(1),
organizationId: z.string().optional(), organizationId: z.string().optional(),
}); });
export const apiTestGotifyConnection = apiCreateGotify export const apiTestGotifyConnection = apiCreateGotify
.pick({ .pick({
serverUrl: true, serverUrl: true,
appToken: true, appToken: true,
priority: true, priority: true,
}) })
.extend({ .extend({
decoration: z.boolean().optional(), decoration: z.boolean().optional(),
}); });
export const apiCreateNtfy = notificationsSchema export const apiCreateNtfy = notificationsSchema
.pick({ .pick({
appBuildError: true, appBuildError: true,
databaseBackup: true, databaseBackup: true,
dokployRestart: true, dokployRestart: true,
name: true, name: true,
appDeploy: true, appDeploy: true,
dockerCleanup: true, dockerCleanup: true,
}) })
.extend({ .extend({
serverUrl: z.string().min(1), serverUrl: z.string().min(1),
topic: z.string().min(1), topic: z.string().min(1),
accessToken: z.string().min(1), accessToken: z.string().min(1),
priority: z.number().min(1), priority: z.number().min(1),
}) })
.required(); .required();
export const apiUpdateNtfy = apiCreateNtfy.partial().extend({ export const apiUpdateNtfy = apiCreateNtfy.partial().extend({
notificationId: z.string().min(1), notificationId: z.string().min(1),
ntfyId: z.string().min(1), ntfyId: z.string().min(1),
organizationId: z.string().optional(), organizationId: z.string().optional(),
}); });
export const apiTestNtfyConnection = apiCreateNtfy.pick({ export const apiTestNtfyConnection = apiCreateNtfy.pick({
serverUrl: true, serverUrl: true,
topic: true, topic: true,
accessToken: true, accessToken: true,
priority: true, priority: true,
}); });
export const apiFindOneNotification = notificationsSchema export const apiFindOneNotification = notificationsSchema
.pick({ .pick({
notificationId: true, notificationId: true,
}) })
.required(); .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(),
});
export const apiUpdateCustom = apiCreateCustom.partial().extend({
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(),
});
export const apiSendTest = notificationsSchema export const apiSendTest = notificationsSchema
.extend({ .extend({
botToken: z.string(), botToken: z.string(),
chatId: z.string(), chatId: z.string(),
webhookUrl: z.string(), webhookUrl: z.string(),
channel: z.string(), channel: z.string(),
smtpServer: z.string(), smtpServer: z.string(),
smtpPort: z.number(), smtpPort: z.number(),
fromAddress: z.string(), fromAddress: z.string(),
username: z.string(), username: z.string(),
password: z.string(), password: z.string(),
toAddresses: z.array(z.string()), toAddresses: z.array(z.string()),
serverUrl: z.string(), serverUrl: z.string(),
topic: z.string(), topic: z.string(),
appToken: z.string(), appToken: z.string(),
accessToken: z.string(), accessToken: z.string(),
priority: z.number(), priority: z.number(),
}) endpoint: z.string(),
.partial(); headers: z.string(),
})
.partial();