Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(desktop-api): merge owned and member organizations for video create

Richie McIlroyd221a5b98d14
diff --git a/apps/web/app/api/desktop/[...route]/video.ts b/apps/web/app/api/desktop/[...route]/video.ts
index 7e7e95d3c..f06584633 100644
--- a/apps/web/app/api/desktop/[...route]/video.ts
+++ b/apps/web/app/api/desktop/[...route]/video.ts
@@ -16,7 +16,7 @@ import { dub, userIsPro } from "@cap/utils";
import { Storage } from "@cap/web-backend";
import { Organisation, Video } from "@cap/web-domain";
import { zValidator } from "@hono/zod-validator";
-import { and, count, eq, lte, or } from "drizzle-orm";
+import { and, count, eq, lte } from "drizzle-orm";
import { Effect, Option } from "effect";
import { Hono } from "hono";
import { z } from "zod";
@@ -34,6 +34,28 @@ import { withAuth } from "../../utils";
export const app = new Hono().use(withAuth);
+type UserOrganizationSelection = {
+ id: Organisation.OrganisationId;
+ name: string;
+ createdAt: Date;
+};
+
+function mergeUserOrganizationSelections(
+ ...rowSets: UserOrganizationSelection[][]
+) {
+ const organizationsById = new Map<string, UserOrganizationSelection>();
+
+ for (const rows of rowSets) {
+ for (const row of rows) {
+ organizationsById.set(row.id, row);
+ }
+ }
+
+ return Array.from(organizationsById.values())
+ .sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime())
+ .map(({ createdAt, ...organization }) => organization);
+}
+
app.get(
"/create",
zValidator(
@@ -114,27 +136,32 @@ app.get(
});
}
- const userOrganizations = await db()
- .select({
- id: organizations.id,
- name: organizations.name,
- })
- .from(organizations)
- .leftJoin(
- organizationMembers,
- eq(organizations.id, organizationMembers.organizationId),
- )
- .where(
- or(
- // User owns the organization
- eq(organizations.ownerId, user.id),
- // User is a member of the organization
- eq(organizationMembers.userId, user.id),
- ),
- )
- // Remove duplicates if user is both owner and member
- .groupBy(organizations.id, organizations.name)
- .orderBy(organizations.createdAt);
+ const [ownedOrganizations, memberOrganizations] = await Promise.all([
+ db()
+ .select({
+ id: organizations.id,
+ name: organizations.name,
+ createdAt: organizations.createdAt,
+ })
+ .from(organizations)
+ .where(eq(organizations.ownerId, user.id)),
+ db()
+ .select({
+ id: organizations.id,
+ name: organizations.name,
+ createdAt: organizations.createdAt,
+ })
+ .from(organizationMembers)
+ .innerJoin(
+ organizations,
+ eq(organizations.id, organizationMembers.organizationId),
+ )
+ .where(eq(organizationMembers.userId, user.id)),
+ ]);
+ const userOrganizations = mergeUserOrganizationSelections(
+ ownedOrganizations,
+ memberOrganizations,
+ );
const userOrgIds = userOrganizations.map((org) => org.id);
let videoOrgId: Organisation.OrganisationId;