⚡ 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(api): count members and projects per workspace
Andrej7e3ade041c20
diff --git a/apps/api/src/admin/controllers/list-workspaces.ts b/apps/api/src/admin/controllers/list-workspaces.tsindex 0fec0d497..b90e7e6cc 100644--- a/apps/api/src/admin/controllers/list-workspaces.ts+++ b/apps/api/src/admin/controllers/list-workspaces.ts@@ -31,8 +31,6 @@ async function listWorkspaces({ search, page, limit }: ListWorkspacesInput) {name: workspaceTable.name,slug: workspaceTable.slug,createdAt: workspaceTable.createdAt,- memberCount: sql<number>`(select count(*)::int from ${workspaceUserTable} where ${workspaceUserTable.workspaceId} = ${workspaceTable.id})`,- projectCount: sql<number>`(select count(*)::int from ${projectTable} where ${projectTable.workspaceId} = ${workspaceTable.id})`,}).from(workspaceTable).where(where)@@ -42,32 +40,55 @@ async function listWorkspaces({ search, page, limit }: ListWorkspacesInput) {db.select({ value: count() }).from(workspaceTable).where(where),]);- const owners =- rows.length > 0- ? await db- .select({- workspaceId: workspaceUserTable.workspaceId,- id: userTable.id,- name: userTable.name,- email: userTable.email,- })- .from(workspaceUserTable)- .innerJoin(userTable, eq(workspaceUserTable.userId, userTable.id))- .where(- and(- inArray(- workspaceUserTable.workspaceId,- rows.map((row) => row.id),- ),- sql`'owner' = any(string_to_array(${workspaceUserTable.role}, ','))`,- ),- )- : [];+ if (rows.length === 0) {+ return { workspaces: [], total: totalRow?.value ?? 0 };+ }++ const workspaceIds = rows.map((row) => row.id);+ const [owners, memberCounts, projectCounts] = await Promise.all([+ db+ .select({+ workspaceId: workspaceUserTable.workspaceId,+ id: userTable.id,+ name: userTable.name,+ email: userTable.email,+ })+ .from(workspaceUserTable)+ .innerJoin(userTable, eq(workspaceUserTable.userId, userTable.id))+ .where(+ and(+ inArray(workspaceUserTable.workspaceId, workspaceIds),+ sql`'owner' = any(string_to_array(${workspaceUserTable.role}, ','))`,+ ),+ ),+ db+ .select({+ workspaceId: workspaceUserTable.workspaceId,+ value: count(),+ })+ .from(workspaceUserTable)+ .where(inArray(workspaceUserTable.workspaceId, workspaceIds))+ .groupBy(workspaceUserTable.workspaceId),+ db+ .select({ workspaceId: projectTable.workspaceId, value: count() })+ .from(projectTable)+ .where(inArray(projectTable.workspaceId, workspaceIds))+ .groupBy(projectTable.workspaceId),+ ]);++ const members = new Map(+ memberCounts.map((row) => [row.workspaceId, row.value]),+ );+ const projects = new Map(+ projectCounts.map((row) => [row.workspaceId, row.value]),+ );return {workspaces: rows.map((row) => ({...row,createdAt: row.createdAt.toISOString(),+ memberCount: members.get(row.id) ?? 0,+ projectCount: projects.get(row.id) ?? 0,owners: owners.filter((owner) => owner.workspaceId === row.id).map(({ id, name, email }) => ({ id, name, email })),