diff --git a/app/api/notifications/add/route.ts b/app/api/notifications/add/route.ts index ea3b3d6..5568545 100644 --- a/app/api/notifications/add/route.ts +++ b/app/api/notifications/add/route.ts @@ -14,12 +14,20 @@ export async function POST(request: NextRequest) { const body = await request.json(); const { name, type, config } = schema.parse(body); - if(type !== "TELEGRAM") { + if(type !== "TELEGRAM" && type !== "NTFY" && type !== "SMTP") { return NextResponse.json({ error: "Invalid notification type" }, { status: 400 }); } const parsedConfig = JSON.parse(config); - if(parsedConfig.token === "" || parsedConfig.chat_id === "") { + if(type === "TELEGRAM" && (parsedConfig.token === "" || parsedConfig.chat_id === "")) { + return NextResponse.json({ error: "Invalid config" }, { status: 400 }); + } + + if(type === "NTFY" && (parsedConfig.url === "" || parsedConfig.token === "")) { + return NextResponse.json({ error: "Invalid config" }, { status: 400 }); + } + + if(type === "SMTP" && (parsedConfig.host === "" || parsedConfig.port === "" || parsedConfig.username === "" || parsedConfig.password === "" || parsedConfig.from === "" || parsedConfig.to === "")) { return NextResponse.json({ error: "Invalid config" }, { status: 400 }); } diff --git a/components/dialogues/AddNotification.tsx b/components/dialogues/AddNotification.tsx index ca09f2b..f3bc0f6 100644 --- a/components/dialogues/AddNotification.tsx +++ b/components/dialogues/AddNotification.tsx @@ -15,6 +15,15 @@ export default function AddNotification({ onNotificationAdded, onError, onSucces const [type, setType] = useState("Select a type"); const [telegramBotToken, setTelegramBotToken] = useState(""); const [telegramChatId, setTelegramChatId] = useState(""); + const [nftyUrl, setNftyUrl] = useState(""); + const [ntfyToken, setNftyToken] = useState(""); + const [smtpHost, setSmtpHost] = useState(""); + const [smtpPort, setSmtpPort] = useState(""); + const [smtpUsername, setSmtpUsername] = useState(""); + const [smtpPassword, setSmtpPassword] = useState(""); + const [smtpFrom, setSmtpFrom] = useState(""); + const [smtpTo, setSmtpTo] = useState(""); + const [smtpSecure, setSmtpSecure] = useState(false); const { addNotification } = useNotification(); @@ -23,10 +32,26 @@ export default function AddNotification({ onNotificationAdded, onError, onSucces setType("Select a type") setTelegramBotToken("") setTelegramChatId("") + setNftyUrl("") + setNftyToken("") + setSmtpHost("") + setSmtpPort("") + setSmtpUsername("") + setSmtpPassword("") + setSmtpFrom("") + setSmtpTo("") + setSmtpSecure(false) } const addNotificationHandler = async () => { - const config = type === "TELEGRAM" ? `{ "token": "${telegramBotToken}", "chat_id": "${telegramChatId}" }` : ""; + let config = ""; + if (type === "TELEGRAM") { + config = `{ "token": "${telegramBotToken}", "chat_id": "${telegramChatId}" }`; + } else if (type === "NTFY") { + config = `{ "url": "${nftyUrl}", "token": "${ntfyToken}" }`; + } else if (type === "SMTP") { + config = `{ "host": "${smtpHost}", "port": "${smtpPort}", "username": "${smtpUsername}", "password": "${smtpPassword}", "from": "${smtpFrom}", "to": "${smtpTo}", "secure": "${smtpSecure}" }`; + } const response = addNotification(name, type, config); if (typeof response === "string") { onError && onError(response) @@ -49,7 +74,7 @@ export default function AddNotification({ onNotificationAdded, onError, onSucces return (