Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix: enable all configured notification channels

Tin70afa9df7eb8
diff --git a/apps/api/src/notification-preferences/service.ts b/apps/api/src/notification-preferences/service.ts
index 554541646..15c1ece6e 100644
--- a/apps/api/src/notification-preferences/service.ts
+++ b/apps/api/src/notification-preferences/service.ts
@@ -1,4 +1,4 @@
-import { and, eq, inArray } from "drizzle-orm";
+import { and, eq, inArray, or } from "drizzle-orm";
import { HTTPException } from "hono/http-exception";
import db from "../database";
import {
@@ -74,6 +74,13 @@ export type UpsertWorkspaceRuleInput = {
selectedProjectIds?: string[];
};
+type WorkspaceRuleChannelState = {
+ emailEnabled: boolean;
+ ntfyEnabled: boolean;
+ gotifyEnabled: boolean;
+ webhookEnabled: boolean;
+};
+
function normalizeOptionalString(value: string | null | undefined) {
if (typeof value !== "string") {
return value === null ? null : undefined;
@@ -268,6 +275,13 @@ export async function updateNotificationPreferences(
const webhookEnabled =
input.webhookEnabled ?? decryptedExisting?.webhookEnabled ?? false;
+ const enabledRuleCascade: WorkspaceRuleChannelState = {
+ emailEnabled: false,
+ ntfyEnabled: false,
+ gotifyEnabled: false,
+ webhookEnabled: false,
+ };
+
const shouldValidateNtfy =
ntfyEnabled ||
input.ntfyServerUrl !== undefined ||
@@ -386,6 +400,11 @@ export async function updateNotificationPreferences(
webhookEnabled?: boolean;
} = {};
+ const hadEmailEnabled = decryptedExisting?.emailEnabled ?? false;
+ const hadNtfyEnabled = decryptedExisting?.ntfyEnabled ?? false;
+ const hadGotifyEnabled = decryptedExisting?.gotifyEnabled ?? false;
+ const hadWebhookEnabled = decryptedExisting?.webhookEnabled ?? false;
+
if (!emailEnabled) {
ruleCascade.emailEnabled = false;
}
@@ -402,14 +421,54 @@ export async function updateNotificationPreferences(
ruleCascade.webhookEnabled = false;
}
- if (Object.keys(ruleCascade).length > 0) {
+ if (emailEnabled && !hadEmailEnabled && emailAddress) {
+ enabledRuleCascade.emailEnabled = true;
+ }
+
+ if (ntfyEnabled && !hadNtfyEnabled && ntfyServerUrl && ntfyTopic) {
+ enabledRuleCascade.ntfyEnabled = true;
+ }
+
+ if (
+ gotifyEnabled &&
+ !hadGotifyEnabled &&
+ gotifyServerUrl &&
+ data.gotifyToken
+ ) {
+ enabledRuleCascade.gotifyEnabled = true;
+ }
+
+ if (webhookEnabled && !hadWebhookEnabled && webhookUrl) {
+ enabledRuleCascade.webhookEnabled = true;
+ }
+
+ const ruleEnableCascade = Object.fromEntries(
+ Object.entries(enabledRuleCascade).filter(([, value]) => value),
+ ) as Partial<WorkspaceRuleChannelState>;
+
+ if (
+ Object.keys(ruleCascade).length > 0 ||
+ Object.keys(ruleEnableCascade).length > 0
+ ) {
await db
.update(userNotificationWorkspaceRuleTable)
.set({
+ ...ruleEnableCascade,
...ruleCascade,
updatedAt: new Date(),
})
- .where(eq(userNotificationWorkspaceRuleTable.userId, userId));
+ .where(
+ and(
+ eq(userNotificationWorkspaceRuleTable.userId, userId),
+ eq(userNotificationWorkspaceRuleTable.isActive, true),
+ or(
+ eq(userNotificationWorkspaceRuleTable.emailEnabled, true),
+ eq(userNotificationWorkspaceRuleTable.ntfyEnabled, true),
+ eq(userNotificationWorkspaceRuleTable.gotifyEnabled, true),
+ eq(userNotificationWorkspaceRuleTable.webhookEnabled, true),
+ ),
+ ),
+ );
}
return getNotificationPreferences(userId, emailAddress);
diff --git a/apps/web/src/components/account/notification-preferences-settings.tsx b/apps/web/src/components/account/notification-preferences-settings.tsx
index 46d369450..ff20edee1 100644
--- a/apps/web/src/components/account/notification-preferences-settings.tsx
+++ b/apps/web/src/components/account/notification-preferences-settings.tsx
@@ -39,6 +39,36 @@ type GlobalChannelPrefsState = {
webhook: { enabled: boolean; url: string; secret: string };
};
+function createWorkspaceRuleState(input: {
+ hasEmailChannel: boolean;
+ hasGotifyChannel: boolean;
+ hasNtfyChannel: boolean;
+ hasWebhookChannel: boolean;
+ rule?: WorkspaceRuleState;
+}): WorkspaceRuleState {
+ if (input.rule) {
+ return {
+ isActive: input.rule.isActive,
+ emailEnabled: input.rule.emailEnabled,
+ ntfyEnabled: input.rule.ntfyEnabled,
+ gotifyEnabled: input.rule.gotifyEnabled,
+ webhookEnabled: input.rule.webhookEnabled,
+ projectMode: input.rule.projectMode,
+ selectedProjectIds: input.rule.selectedProjectIds,
+ };
+ }
+
+ return {
+ isActive: false,
+ emailEnabled: input.hasEmailChannel,
+ ntfyEnabled: input.hasNtfyChannel,
+ gotifyEnabled: input.hasGotifyChannel,
+ webhookEnabled: input.hasWebhookChannel,
+ projectMode: "all",
+ selectedProjectIds: [],
+ };
+}
+
function createDefaultGlobalChannelPrefs(): GlobalChannelPrefsState {
return {
emailEnabled: false,
@@ -137,15 +167,15 @@ function WorkspaceRuleCard({
workspace: WorkspaceSummary;
}) {
const { t } = useTranslation();
- const [state, setState] = React.useState<WorkspaceRuleState>({
- isActive: rule?.isActive ?? false,
- emailEnabled: rule?.emailEnabled ?? false,
- ntfyEnabled: rule?.ntfyEnabled ?? false,
- gotifyEnabled: rule?.gotifyEnabled ?? false,
- webhookEnabled: rule?.webhookEnabled ?? false,
- projectMode: rule?.projectMode ?? "all",
- selectedProjectIds: rule?.selectedProjectIds ?? [],
- });
+ const [state, setState] = React.useState<WorkspaceRuleState>(() =>
+ createWorkspaceRuleState({
+ hasEmailChannel,
+ hasGotifyChannel,
+ hasNtfyChannel,
+ hasWebhookChannel,
+ rule,
+ }),
+ );
const [isSaving, setIsSaving] = React.useState(false);
const [isDeleting, setIsDeleting] = React.useState(false);
const { data: projects } = useGetProjects({
@@ -157,16 +187,22 @@ function WorkspaceRuleCard({
});
React.useEffect(() => {
- setState({
- isActive: rule?.isActive ?? false,
- emailEnabled: rule?.emailEnabled ?? false,
- ntfyEnabled: rule?.ntfyEnabled ?? false,
- gotifyEnabled: rule?.gotifyEnabled ?? false,
- webhookEnabled: rule?.webhookEnabled ?? false,
- projectMode: rule?.projectMode ?? "all",
- selectedProjectIds: rule?.selectedProjectIds ?? [],
- });
- }, [rule]);
+ setState(
+ createWorkspaceRuleState({
+ hasEmailChannel,
+ hasGotifyChannel,
+ hasNtfyChannel,
+ hasWebhookChannel,
+ rule,
+ }),
+ );
+ }, [
+ hasEmailChannel,
+ hasGotifyChannel,
+ hasNtfyChannel,
+ hasWebhookChannel,
+ rule,
+ ]);
const isConnected = Boolean(rule);
const isBusy = isSaving || isDeleting;