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(api): keep hidden projects in alert rules

Andreja4aade259cca
diff --git a/apps/api/src/notification-preferences/find-hidden-rule-project-ids.ts b/apps/api/src/notification-preferences/find-hidden-rule-project-ids.ts
new file mode 100644
index 000000000..923dfa2ee
--- /dev/null
+++ b/apps/api/src/notification-preferences/find-hidden-rule-project-ids.ts
@@ -0,0 +1,36 @@
+import { and, eq, not } from "drizzle-orm";
+import db from "../database";
+import {
+ userNotificationWorkspaceProjectTable,
+ userNotificationWorkspaceRuleTable,
+} from "../database/schema";
+import { projectAccessCondition } from "../project-access/project-access-condition";
+
+export async function findHiddenRuleProjectIds(
+ userId: string,
+ workspaceId: string,
+): Promise<string[]> {
+ const rows = await db
+ .select({ projectId: userNotificationWorkspaceProjectTable.projectId })
+ .from(userNotificationWorkspaceProjectTable)
+ .innerJoin(
+ userNotificationWorkspaceRuleTable,
+ eq(
+ userNotificationWorkspaceRuleTable.id,
+ userNotificationWorkspaceProjectTable.workspaceRuleId,
+ ),
+ )
+ .where(
+ and(
+ eq(userNotificationWorkspaceRuleTable.userId, userId),
+ eq(userNotificationWorkspaceRuleTable.workspaceId, workspaceId),
+ not(
+ projectAccessCondition(
+ userId,
+ userNotificationWorkspaceProjectTable.projectId,
+ ),
+ ),
+ ),
+ );
+ return rows.map((row) => row.projectId);
+}
diff --git a/apps/api/src/notification-preferences/service.ts b/apps/api/src/notification-preferences/service.ts
index 0bd6538f2..d5fe7e681 100644
--- a/apps/api/src/notification-preferences/service.ts
+++ b/apps/api/src/notification-preferences/service.ts
@@ -11,6 +11,7 @@ import {
import { assertPublicWebhookDestination } from "../plugins/generic-webhook/config";
import { findInaccessibleProjectIds } from "../project-access/find-inaccessible-project-ids";
import { projectAccessCondition } from "../project-access/project-access-condition";
+import { findHiddenRuleProjectIds } from "./find-hidden-rule-project-ids";
import { decryptSecret, encryptSecret } from "./secrets";
export type NotificationPreferenceProjectMode = "all" | "selected";
@@ -141,8 +142,9 @@ export async function validateProjectSelection(
userId: string,
workspaceId: string,
selectedProjectIds: string[],
+ hiddenProjectIds: string[] = [],
) {
- if (selectedProjectIds.length === 0) {
+ if (selectedProjectIds.length === 0 && hiddenProjectIds.length === 0) {
throw new HTTPException(400, {
message: "Select at least one project for selected project mode",
});
@@ -159,7 +161,7 @@ export async function validateProjectSelection(
),
);
- if (projects.length !== selectedProjectIds.length) {
+ if (projects.length !== new Set(selectedProjectIds).size) {
throw new HTTPException(400, {
message: "One or more selected projects are invalid",
});
@@ -524,11 +526,19 @@ export async function upsertWorkspaceRule(
): Promise<NotificationPreferenceResponse> {
await assertWorkspaceMembership(userId, workspaceId);
+ const hiddenProjectIds =
+ input.projectMode === "selected"
+ ? await findHiddenRuleProjectIds(userId, workspaceId)
+ : [];
+ const visibleProjectIds = (input.selectedProjectIds ?? []).filter(
+ (projectId) => !hiddenProjectIds.includes(projectId),
+ );
if (input.projectMode === "selected") {
await validateProjectSelection(
userId,
workspaceId,
- input.selectedProjectIds ?? [],
+ visibleProjectIds,
+ hiddenProjectIds,
);
}
@@ -631,11 +641,13 @@ export async function upsertWorkspaceRule(
if (input.projectMode === "selected") {
await db.insert(userNotificationWorkspaceProjectTable).values(
- (input.selectedProjectIds ?? []).map((projectId) => ({
- workspaceId,
- workspaceRuleId,
- projectId,
- })),
+ [...new Set([...visibleProjectIds, ...hiddenProjectIds])].map(
+ (projectId) => ({
+ workspaceId,
+ workspaceRuleId,
+ projectId,
+ }),
+ ),
);
}