Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix: filter issues and calendar events by user ID

Gusd76967e8cd14
diff --git a/src/shared/src/db/queries/calendar-event.ts b/src/shared/src/db/queries/calendar-event.ts
index e284c374d..755d8b32c 100644
--- a/src/shared/src/db/queries/calendar-event.ts
+++ b/src/shared/src/db/queries/calendar-event.ts
@@ -1,5 +1,5 @@
import { eq, and, asc, gte, lte, or, isNull, isNotNull, lt } from "drizzle-orm";
-import { calendarEvent } from "../schema";
+import { agent, calendarEvent } from "../schema";
import type { Database } from "../index";
export async function createCalendarEvent(
@@ -61,24 +61,34 @@ export async function updateCalendarEvent(
export async function getCalendarEvent(
db: Database,
id: string,
- workspaceId: string
+ workspaceId: string,
+ userId?: string
) {
+ const conditions = [eq(calendarEvent.id, id), eq(calendarEvent.workspaceId, workspaceId)];
+ if (userId) {
+ conditions.push(eq(agent.ownerId, userId));
+ const rows = await db
+ .select({ calendarEvent })
+ .from(calendarEvent)
+ .innerJoin(agent, eq(calendarEvent.agentId, agent.id))
+ .where(and(...conditions));
+ return rows[0]?.calendarEvent ?? null;
+ }
const rows = await db
.select()
.from(calendarEvent)
- .where(
- and(eq(calendarEvent.id, id), eq(calendarEvent.workspaceId, workspaceId))
- );
+ .where(and(...conditions));
return rows[0] ?? null;
}
export async function listCalendarEvents(
db: Database,
workspaceId: string,
- opts: { agentId?: string; from?: string; to?: string } = {}
+ opts: { userId?: string; agentId?: string; from?: string; to?: string } = {}
) {
const conditions = [eq(calendarEvent.workspaceId, workspaceId)];
if (opts.agentId) conditions.push(eq(calendarEvent.agentId, opts.agentId));
+ if (opts.userId) conditions.push(eq(agent.ownerId, opts.userId));
// Exclude completed one-off events: non-recurring events where
// lastTriggeredAt is set and >= scheduledAt are "done".
@@ -117,6 +127,16 @@ export async function listCalendarEvents(
if (opts.from) conditions.push(gte(calendarEvent.scheduledAt, opts.from));
if (opts.to) conditions.push(lte(calendarEvent.scheduledAt, opts.to));
}
+
+ if (opts.userId) {
+ const rows = await db
+ .select({ calendarEvent })
+ .from(calendarEvent)
+ .innerJoin(agent, eq(calendarEvent.agentId, agent.id))
+ .where(and(...conditions))
+ .orderBy(asc(calendarEvent.scheduledAt));
+ return rows.map(r => r.calendarEvent);
+ }
return db
.select()
.from(calendarEvent)
diff --git a/src/shared/src/db/queries/issue.ts b/src/shared/src/db/queries/issue.ts
index f86cd2383..81dfcec08 100644
--- a/src/shared/src/db/queries/issue.ts
+++ b/src/shared/src/db/queries/issue.ts
@@ -35,11 +35,13 @@ export async function createIssue(
return rows[0]!;
}
-export async function getIssue(db: Database, id: string, workspaceId: string) {
+export async function getIssue(db: Database, id: string, workspaceId: string, userId?: string) {
+ const conditions = [eq(issue.id, id), eq(issue.workspaceId, workspaceId)];
+ if (userId) conditions.push(eq(issue.creatorUserId, userId));
const rows = await db
.select()
.from(issue)
- .where(and(eq(issue.id, id), eq(issue.workspaceId, workspaceId)));
+ .where(and(...conditions));
return rows[0] ?? null;
}
@@ -54,9 +56,9 @@ export async function getIssueByConversation(db: Database, conversationId: strin
export async function listIssues(
db: Database,
workspaceId: string,
- opts: { agentId?: string; status?: IssueStatusType; terminal?: boolean } = {}
+ opts: { userId: string; agentId?: string; status?: IssueStatusType; terminal?: boolean }
) {
- const conditions = [eq(issue.workspaceId, workspaceId)];
+ const conditions = [eq(issue.workspaceId, workspaceId), eq(issue.creatorUserId, opts.userId)];
if (opts.agentId) conditions.push(eq(issue.agentId, opts.agentId));
if (opts.status) conditions.push(eq(issue.status, opts.status));
if (opts.terminal !== undefined) {
diff --git a/src/shared/test/queries/calendar-event.test.ts b/src/shared/test/queries/calendar-event.test.ts
index b906399e4..4cad718fd 100644
--- a/src/shared/test/queries/calendar-event.test.ts
+++ b/src/shared/test/queries/calendar-event.test.ts
@@ -264,6 +264,7 @@ describe("listCalendarEvents — completed one-off exclusion", () => {
chain.from = vi.fn(() => chain);
chain.where = vi.fn(() => chain);
chain.orderBy = vi.fn(() => Promise.resolve(rows));
+ chain.innerJoin = vi.fn(() => chain);
return chain;
}
@@ -288,6 +289,53 @@ describe("listCalendarEvents — completed one-off exclusion", () => {
await calendarQueries.listCalendarEvents(db, "ws_1", { agentId: "ag_1" });
expect(db.where).toHaveBeenCalledOnce();
});
+
+ it("uses innerJoin when userId is provided", async () => {
+ const ce = { id: "ce_1", agentId: "ag_1", workspaceId: "ws_1", title: "T", description: null, scheduledAt: "2026-01-01T09:00:00.000Z", repeatInterval: null, repeatStopAt: null, lastTriggeredAt: null, exceptions: [], createdAt: "2026-01-01", updatedAt: "2026-01-01" };
+ const db = createMockDb([{ calendarEvent: ce }]);
+ const result = await calendarQueries.listCalendarEvents(db, "ws_1", { userId: "u1" });
+ expect(db.innerJoin).toHaveBeenCalledOnce();
+ expect(result).toEqual([ce]);
+ });
+
+ it("does not use innerJoin when userId is not provided", async () => {
+ const db = createMockDb([]);
+ await calendarQueries.listCalendarEvents(db, "ws_1");
+ expect(db.innerJoin).not.toHaveBeenCalled();
+ });
+});
+
+describe("getCalendarEvent — user isolation", () => {
+ function createMockDb(rows: any[]) {
+ const chain: any = {};
+ chain.select = vi.fn(() => chain);
+ chain.from = vi.fn(() => chain);
+ chain.where = vi.fn(() => Promise.resolve(rows));
+ chain.innerJoin = vi.fn(() => chain);
+ return chain;
+ }
+
+ it("returns event without userId (backwards compat)", async () => {
+ const ce = { id: "ce_1" };
+ const db = createMockDb([ce]);
+ const result = await calendarQueries.getCalendarEvent(db, "ce_1", "ws_1");
+ expect(result).toEqual(ce);
+ expect(db.innerJoin).not.toHaveBeenCalled();
+ });
+
+ it("uses innerJoin when userId is provided", async () => {
+ const ce = { id: "ce_1" };
+ const db = createMockDb([{ calendarEvent: ce }]);
+ const result = await calendarQueries.getCalendarEvent(db, "ce_1", "ws_1", "u1");
+ expect(db.innerJoin).toHaveBeenCalledOnce();
+ expect(result).toEqual(ce);
+ });
+
+ it("returns null when userId does not match (empty rows from join)", async () => {
+ const db = createMockDb([]);
+ const result = await calendarQueries.getCalendarEvent(db, "ce_1", "ws_1", "wrong_user");
+ expect(result).toBeNull();
+ });
});
describe("expandOccurrences", () => {
diff --git a/src/shared/test/queries/issue.test.ts b/src/shared/test/queries/issue.test.ts
index 7ee631cae..63aa879c1 100644
--- a/src/shared/test/queries/issue.test.ts
+++ b/src/shared/test/queries/issue.test.ts
@@ -42,6 +42,13 @@ describe("createIssue", () => {
describe("getIssue", () => {
it("returns null when not found", async () => { expect(await issueQueries.getIssue(createSelectMock([]), "x", "w")).toBeNull(); });
it("returns issue", async () => { const i = { id: "iss_1" }; expect(await issueQueries.getIssue(createSelectMock([i]), "iss_1", "w")).toEqual(i); });
+ it("returns null when userId does not match (where adds creatorUserId condition)", async () => {
+ expect(await issueQueries.getIssue(createSelectMock([]), "iss_1", "w", "other_user")).toBeNull();
+ });
+ it("returns issue without userId (backwards compat for daemon)", async () => {
+ const i = { id: "iss_1" };
+ expect(await issueQueries.getIssue(createSelectMock([i]), "iss_1", "w")).toEqual(i);
+ });
});
describe("getIssueByConversation", () => {
@@ -56,15 +63,15 @@ describe("listIssues", () => {
chain.where = vi.fn(() => chain); chain.orderBy = vi.fn(() => Promise.resolve([]));
return chain;
}
- it("lists with default options", async () => {
+ it("lists with userId filter", async () => {
const mockDb = createListMock();
- await issueQueries.listIssues(mockDb, "w");
+ await issueQueries.listIssues(mockDb, "w", { userId: "u1" });
expect(mockDb.orderBy).toHaveBeenCalled();
});
- it("filters by agentId", async () => { await issueQueries.listIssues(createListMock(), "w", { agentId: "a" }); });
- it("filters by status", async () => { await issueQueries.listIssues(createListMock(), "w", { status: "todo" }); });
- it("filters terminal=true", async () => { await issueQueries.listIssues(createListMock(), "w", { terminal: true }); });
- it("filters terminal=false", async () => { await issueQueries.listIssues(createListMock(), "w", { terminal: false }); });
+ it("filters by agentId", async () => { await issueQueries.listIssues(createListMock(), "w", { userId: "u1", agentId: "a" }); });
+ it("filters by status", async () => { await issueQueries.listIssues(createListMock(), "w", { userId: "u1", status: "todo" }); });
+ it("filters terminal=true", async () => { await issueQueries.listIssues(createListMock(), "w", { userId: "u1", terminal: true }); });
+ it("filters terminal=false", async () => { await issueQueries.listIssues(createListMock(), "w", { userId: "u1", terminal: false }); });
});
describe("updateIssue", () => {
diff --git a/src/web/src/app/api/calendar/[id]/route.test.ts b/src/web/src/app/api/calendar/[id]/route.test.ts
index 82c6e9eaa..2c3732d4e 100644
--- a/src/web/src/app/api/calendar/[id]/route.test.ts
+++ b/src/web/src/app/api/calendar/[id]/route.test.ts
@@ -74,7 +74,7 @@ describe("GET /api/calendar/[id]", () => {
const res = await GET(r, { params: { id: "ce_1" } } as any);
expect(res.status).toBe(200);
expect((await res.json()).id).toBe("ce_1");
- expect(mockGet).toHaveBeenCalledWith({}, "ce_1", "ws1");
+ expect(mockGet).toHaveBeenCalledWith({}, "ce_1", "ws1", "u1");
});
it("returns 404 when the event is not in the caller's workspace", async () => {
diff --git a/src/web/src/app/api/calendar/[id]/route.ts b/src/web/src/app/api/calendar/[id]/route.ts
index e0a544f66..74fe551fb 100644
--- a/src/web/src/app/api/calendar/[id]/route.ts
+++ b/src/web/src/app/api/calendar/[id]/route.ts
@@ -26,7 +26,8 @@ export const GET = withAuth(async (req, ctx) => {
const event = await queries.calendarEvent.getCalendarEvent(
db,
id,
- ws.workspaceId
+ ws.workspaceId,
+ ctx.userId
);
if (!event) return writeError("calendar event not found", 404);
return writeJSON(calendarEventToResponse(event));
@@ -48,7 +49,8 @@ export const PATCH = withAuth(async (req, ctx) => {
const source = await queries.calendarEvent.getCalendarEvent(
db,
id,
- ws.workspaceId
+ ws.workspaceId,
+ ctx.userId
);
if (!source) return writeError("calendar event not found", 404);
@@ -193,7 +195,8 @@ export const DELETE = withAuth(async (req, ctx) => {
const source = await queries.calendarEvent.getCalendarEvent(
db,
id,
- ws.workspaceId
+ ws.workspaceId,
+ ctx.userId
);
if (!source) return writeError("calendar event not found", 404);
diff --git a/src/web/src/app/api/calendar/route.test.ts b/src/web/src/app/api/calendar/route.test.ts
index b699555dc..9647425f5 100644
--- a/src/web/src/app/api/calendar/route.test.ts
+++ b/src/web/src/app/api/calendar/route.test.ts
@@ -17,7 +17,10 @@ vi.mock("@alook/shared", async () => {
...actual,
createDb: vi.fn(() => ({})),
queries: {
- agent: { getAgent: (...a: unknown[]) => mockGetAgent(...a) },
+ agent: {
+ getAgent: (...a: unknown[]) => mockGetAgent(...a),
+ listAgents: vi.fn().mockResolvedValue([]),
+ },
calendarEvent: {
listCalendarEvents: (...a: unknown[]) => mockList(...a),
createCalendarEvent: (...a: unknown[]) => mockCreate(...a),
@@ -75,17 +78,19 @@ describe("GET /api/calendar", () => {
expect(body).toHaveLength(1);
expect(body[0].id).toBe("ce_1");
expect(mockList).toHaveBeenCalledWith({}, "ws1", {
+ userId: "u1",
agentId: "ag_1",
from: "2026-04-01T00:00:00.000Z",
to: "2026-04-30T23:59:59.999Z",
});
});
- it("returns 404 when filter agent is not in the workspace", async () => {
- mockGetAgent.mockResolvedValue(null);
+ it("returns empty when filter agent has no events", async () => {
+ mockList.mockResolvedValue([]);
const req = new NextRequest("http://localhost/api/calendar?agentId=ag_nope");
const res = await GET(req, {} as any);
- expect(res.status).toBe(404);
+ expect(res.status).toBe(200);
+ expect(await res.json()).toEqual([]);
});
it("expands daily recurring events into one row per occurrence", async () => {
diff --git a/src/web/src/app/api/calendar/route.ts b/src/web/src/app/api/calendar/route.ts
index 39f026026..908c3bef8 100644
--- a/src/web/src/app/api/calendar/route.ts
+++ b/src/web/src/app/api/calendar/route.ts
@@ -26,15 +26,10 @@ export const GET = withAuth(async (req: NextRequest, ctx) => {
const from = req.nextUrl.searchParams.get("from") ?? undefined;
const to = req.nextUrl.searchParams.get("to") ?? undefined;
- if (agentId) {
- const agent = await queries.agent.getAgent(db, agentId, ws.workspaceId, ctx.userId);
- if (!agent) return writeError("agent not found in workspace", 404);
- }
-
const rows = await queries.calendarEvent.listCalendarEvents(
db,
ws.workspaceId,
- { agentId, from, to }
+ { userId: ctx.userId, agentId, from, to }
);
// Expand recurring rows into per-occurrence virtual rows within [from, to].
@@ -99,10 +94,13 @@ export const GET = withAuth(async (req: NextRequest, ctx) => {
}
}
- // Also include meetings with scheduled_at in range
+ // Also include meetings with scheduled_at in range, filtered to user's agents
+ const userAgents = await queries.agent.listAgents(db, ws.workspaceId, ctx.userId);
+ const userAgentIds = new Set(userAgents.map(a => a.id));
const meetingRows = await queries.meetingSession.listMeetingsWithSchedule(db, ws.workspaceId);
for (const m of meetingRows) {
if (!m.scheduledAt) continue;
+ if (!userAgentIds.has(m.agentId)) continue;
if (from && m.scheduledAt < from) continue;
if (to && m.scheduledAt > to) continue;
if (agentId && m.agentId !== agentId) continue;
diff --git a/src/web/src/app/api/issues/[id]/comments/route.ts b/src/web/src/app/api/issues/[id]/comments/route.ts
index 8b33d7485..11aa5204e 100644
--- a/src/web/src/app/api/issues/[id]/comments/route.ts
+++ b/src/web/src/app/api/issues/[id]/comments/route.ts
@@ -23,7 +23,7 @@ export const GET = withAuth(async (req: NextRequest, ctx) => {
const id = ctx.params?.id;
if (!id) return writeError("issue id is required", 400);
- const issue = await queries.issue.getIssue(db, id, ws.workspaceId);
+ const issue = await queries.issue.getIssue(db, id, ws.workspaceId, ctx.userId);
if (!issue) return writeError("issue not found", 404);
const comments = await queries.issueComment.listComments(db, id, ws.workspaceId);
@@ -39,7 +39,7 @@ export const POST = withAuth(async (req: NextRequest, ctx) => {
const id = ctx.params?.id;
if (!id) return writeError("issue id is required", 400);
- const issue = await queries.issue.getIssue(db, id, ws.workspaceId);
+ const issue = await queries.issue.getIssue(db, id, ws.workspaceId, ctx.userId);
if (!issue) return writeError("issue not found", 404);
const agentId = req.nextUrl.searchParams.get("agentId");
diff --git a/src/web/src/app/api/issues/[id]/route.ts b/src/web/src/app/api/issues/[id]/route.ts
index e9531aaed..5d1a12196 100644
--- a/src/web/src/app/api/issues/[id]/route.ts
+++ b/src/web/src/app/api/issues/[id]/route.ts
@@ -26,7 +26,7 @@ export const GET = withAuth(async (req: NextRequest, ctx) => {
const id = ctx.params?.id;
if (!id) return writeError("issue id is required", 400);
- const issue = await queries.issue.getIssue(db, id, ws.workspaceId);
+ const issue = await queries.issue.getIssue(db, id, ws.workspaceId, ctx.userId);
if (!issue) return writeError("issue not found", 404);
const agentId = req.nextUrl.searchParams.get("agentId");
if (agentId && issue.agentId !== agentId) return writeError("issue does not belong to agent", 403);
@@ -59,7 +59,7 @@ export const PATCH = withAuth(async (req: NextRequest, ctx) => {
const id = ctx.params?.id;
if (!id) return writeError("issue id is required", 400);
- const existing = await queries.issue.getIssue(db, id, ws.workspaceId);
+ const existing = await queries.issue.getIssue(db, id, ws.workspaceId, ctx.userId);
if (!existing) return writeError("issue not found", 404);
const agentId = req.nextUrl.searchParams.get("agentId");
if (agentId && existing.agentId !== agentId) return writeError("issue does not belong to agent", 403);
@@ -171,7 +171,7 @@ export const DELETE = withAuth(async (req: NextRequest, ctx) => {
const id = ctx.params?.id;
if (!id) return writeError("issue id is required", 400);
- const issue = await queries.issue.getIssue(db, id, ws.workspaceId);
+ const issue = await queries.issue.getIssue(db, id, ws.workspaceId, ctx.userId);
if (!issue) return writeError("issue not found", 404);
if (issue.conversationId) {
@@ -205,7 +205,7 @@ export const POST = withAuth(async (req: NextRequest, ctx) => {
const id = ctx.params?.id;
if (!id) return writeError("issue id is required", 400);
- const issue = await queries.issue.getIssue(db, id, ws.workspaceId);
+ const issue = await queries.issue.getIssue(db, id, ws.workspaceId, ctx.userId);
if (!issue) return writeError("issue not found", 404);
const agentId = req.nextUrl.searchParams.get("agentId");
if (agentId && issue.agentId !== agentId) return writeError("issue does not belong to agent", 403);
diff --git a/src/web/src/app/api/issues/route.test.ts b/src/web/src/app/api/issues/route.test.ts
index 8663194e5..d4246decd 100644
--- a/src/web/src/app/api/issues/route.test.ts
+++ b/src/web/src/app/api/issues/route.test.ts
@@ -79,7 +79,7 @@ describe("GET /api/issues", () => {
const res = await GET(new NextRequest("http://localhost/api/issues?agentId=ag_1&terminal=false"), {} as any);
expect(res.status).toBe(200);
expect(await res.json()).toEqual([{ id: "iss_1", agent_id: "ag_1", title: "Fix", status: "todo", thread_agent_ids: [] }]);
- expect(mockListIssues).toHaveBeenCalledWith({}, "w1", { agentId: "ag_1", status: undefined, terminal: false });
+ expect(mockListIssues).toHaveBeenCalledWith({}, "w1", { userId: "u1", agentId: "ag_1", status: undefined, terminal: false });
});
it("rejects invalid status", async () => {
diff --git a/src/web/src/app/api/issues/route.ts b/src/web/src/app/api/issues/route.ts
index 906babd62..f1c7876fe 100644
--- a/src/web/src/app/api/issues/route.ts
+++ b/src/web/src/app/api/issues/route.ts
@@ -42,12 +42,8 @@ export const GET = withAuth(async (req: NextRequest, ctx) => {
parsedStatus = parsed.data;
}
- if (agentId) {
- const agent = await queries.agent.getAgent(db, agentId, ws.workspaceId, ctx.userId);
- if (!agent) return writeError("agent not found in workspace", 404);
- }
-
const rows = await queries.issue.listIssues(db, ws.workspaceId, {
+ userId: ctx.userId,
agentId,
status: parsedStatus,
terminal: terminalParam === null ? undefined : terminalParam === "true",
diff --git a/src/web/src/app/api/workspaces/[id]/overview/route.ts b/src/web/src/app/api/workspaces/[id]/overview/route.ts
index f946d1410..8835e2b12 100644
--- a/src/web/src/app/api/workspaces/[id]/overview/route.ts
+++ b/src/web/src/app/api/workspaces/[id]/overview/route.ts
@@ -49,6 +49,7 @@ export const GET = withAuth(async (req, ctx) => {
cached(cacheKeys.allMembers(ws.workspaceId), 600, () => queries.member.listMembers(db, ws.workspaceId)),
queries.workspaceInvite.listActiveInvites(db, ws.workspaceId),
queries.calendarEvent.listCalendarEvents(db, ws.workspaceId, {
+ userId: ctx.userId,
from: todayISO,
to: weekEndISO,
}),