⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.
TypeScript honoissue-managementissue-trackerjira-alternativekanbanlinear-alternativemcpproject-managementreactself-hostedtypescript
fix(calendar): retain feeds when task label assignments change
Tin8a9ae7288e75
diff --git a/apps/api/src/calendar-feed/service.ts b/apps/api/src/calendar-feed/service.tsindex de72b2646..db837fbe3 100644--- a/apps/api/src/calendar-feed/service.ts+++ b/apps/api/src/calendar-feed/service.ts@@ -1,5 +1,5 @@import { randomBytes } from "node:crypto";-import { and, asc, eq, inArray, isNotNull, or } from "drizzle-orm";+import { and, asc, eq, inArray, isNotNull, isNull, or } from "drizzle-orm";import { HTTPException } from "hono/http-exception";import db from "../database";import {@@ -16,28 +16,77 @@ export async function createCalendarFeed(labelIds: string[],timeZone: string,) {- const ids = [...new Set(labelIds)];- const labels = await db- .select({ id: labelTable.id })- .from(labelTable)- .where(- and(eq(labelTable.workspaceId, workspaceId), inArray(labelTable.id, ids)),- );- if (labels.length !== ids.length) {- throw new HTTPException(400, {- message: "Select labels from this workspace",- });- }- const [feed] = await db- .insert(calendarFeedTable)- .values({- projectId,- labelIds: ids,- timeZone,- token: randomBytes(32).toString("hex"),- })- .returning();- return feed;+ return db.transaction(async (tx) => {+ const ids = [...new Set(labelIds)];+ const labels = await tx+ .select({+ id: labelTable.id,+ name: labelTable.name,+ color: labelTable.color,+ })+ .from(labelTable)+ .where(+ and(+ eq(labelTable.workspaceId, workspaceId),+ inArray(labelTable.id, ids),+ isNull(labelTable.deletionStartedAt),+ ),+ );+ if (labels.length !== ids.length) {+ throw new HTTPException(400, {+ message: "Select labels from this workspace",+ });+ }+ // Task assignments are disposable. Older workspaces may have no definition,+ // so materialize one and keep subscriptions tied to that persistent row.+ const definitions = [+ ...new Map(labels.map((label) => [label.name, label])).values(),+ ].sort((a, b) => a.name.localeCompare(b.name));+ await tx+ .insert(labelTable)+ .values(+ definitions.map(({ name, color }) => ({+ name,+ color,+ workspaceId,+ taskId: null,+ })),+ )+ .onConflictDoNothing({+ target: [labelTable.workspaceId, labelTable.name],+ where: isNull(labelTable.taskId),+ });+ const roots = await tx+ .select({ id: labelTable.id })+ .from(labelTable)+ .where(+ and(+ eq(labelTable.workspaceId, workspaceId),+ inArray(+ labelTable.name,+ definitions.map((label) => label.name),+ ),+ isNull(labelTable.taskId),+ isNull(labelTable.deletionStartedAt),+ ),+ )+ .orderBy(asc(labelTable.name));+ if (roots.length !== definitions.length) {+ throw new HTTPException(400, {+ message: "Select labels that are not being deleted",+ });+ }+ const [feed] = await tx+ .insert(calendarFeedTable)+ .values({+ projectId,+ labelIds: roots.map((label) => label.id),+ timeZone,+ token: randomBytes(32).toString("hex"),+ })+ .returning();+ return feed;+ });}export function listCalendarFeeds(projectId: string) {diff --git a/apps/web/src/components/project/calendar-feed-settings.tsx b/apps/web/src/components/project/calendar-feed-settings.tsxindex c70d0e26b..b921305ee 100644--- a/apps/web/src/components/project/calendar-feed-settings.tsx+++ b/apps/web/src/components/project/calendar-feed-settings.tsx@@ -61,6 +61,9 @@ export function CalendarFeedSettings({ projectId }: { projectId: string }) {),onSuccess: async () => {setSelected([]);+ await queryClient.invalidateQueries({+ queryKey: ["labels", workspace?.id],+ });await queryClient.invalidateQueries({ queryKey });toast.success(t("settings:calendarFeeds.created"));},diff --git a/tests/api-integration/calendar-feed.test.ts b/tests/api-integration/calendar-feed.test.tsindex 695d1f3a7..695d9d0bc 100644--- a/tests/api-integration/calendar-feed.test.ts+++ b/tests/api-integration/calendar-feed.test.ts@@ -181,6 +181,69 @@ describe("API integration: calendar feeds", () => {expect(result).not.toContain("BEGIN:VEVENT");});+ it.each([true, false])(+ "keeps feeds stable after a task label is removed (workspace definition: %s)",+ async (hasDefinition) => {+ const { member, project, labels, app, create } = await setup();+ const tasks = await db+ .insert(schema.taskTable)+ .values([+ {+ title: "Original assignment",+ projectId: project.id,+ number: 1,+ dueDate: new Date(),+ },+ {+ title: "Remaining assignment",+ projectId: project.id,+ number: 2,+ dueDate: new Date(),+ },+ ])+ .returning();+ const copies = await db+ .insert(schema.labelTable)+ .values(+ tasks.map((task) => ({+ name: labels[0].name,+ color: "gray",+ workspaceId: member.workspace.id,+ taskId: task.id,+ })),+ )+ .returning();+ if (!hasDefinition)+ await db+ .delete(schema.labelTable)+ .where(eq(schema.labelTable.id, labels[0].id));+ const response = await create({ labelIds: [copies[0].id, copies[1].id] });+ expect(response.status).toBe(201);+ const feed = (await response.json()) as Feed;+ const root = await db.query.labelTable.findFirst({+ where: eq(schema.labelTable.id, feed.labelIds[0]),+ });+ expect(root?.taskId).toBeNull();+ expect(root?.name).toBe(labels[0].name);+ expect(feed.labelIds).toHaveLength(1);+ if (hasDefinition) expect(root?.id).toBe(labels[0].id);+ await updateLabel(copies[0].id, "Unrelated", "gray");+ expect(await (await app.request(feedPath(feed))).text()).toContain(+ "SUMMARY:Remaining assignment",+ );+ await db+ .delete(schema.labelTable)+ .where(eq(schema.labelTable.id, copies[0].id));+ expect(await (await app.request(feedPath(feed))).text()).toContain(+ "SUMMARY:Remaining assignment",+ );+ await updateLabel(feed.labelIds[0], "Renamed workspace label", "gray");+ expect(await (await app.request(feedPath(feed))).text()).toContain(+ "SUMMARY:Remaining assignment",+ );+ },+ );+it("revokes one link without affecting others and cascades project deletion", async () => {const { app, project, endpoint, create } = await setup();const first = (await (await create()).json()) as Feed;