Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(web): fall back to default org when desktop sends a stale orgId

Richie McIlroy258cf36b2142
diff --git a/apps/web/__tests__/unit/desktop-video-create.test.ts b/apps/web/__tests__/unit/desktop-video-create.test.ts
index f398346e5..135421d0d 100644
--- a/apps/web/__tests__/unit/desktop-video-create.test.ts
+++ b/apps/web/__tests__/unit/desktop-video-create.test.ts
@@ -247,4 +247,92 @@ describe("GET /create", () => {
orgId: orgValues?.id,
});
});
+
+ it("routes a stale explicit orgId to the provisioned org when the user has none", async () => {
+ mockGetCurrentUser.mockResolvedValue({
+ id: "user-1",
+ email: "[email protected]",
+ defaultOrgId: "org-stale",
+ activeOrganizationId: "",
+ });
+ mockDb.where
+ .mockResolvedValueOnce([])
+ .mockResolvedValueOnce([])
+ .mockResolvedValueOnce([])
+ .mockResolvedValueOnce([])
+ .mockResolvedValueOnce([{ count: 5 }]);
+
+ const response = await app.request(
+ "https://cap.test/create?orgId=org-stale",
+ );
+
+ expect(response.status).toBe(200);
+ expect(mockDb.transaction).toHaveBeenCalledTimes(1);
+
+ const orgValues = insertedValues(schema.organizations) as
+ | { id: string }
+ | undefined;
+
+ expect(insertedValues(schema.videos)).toMatchObject({
+ orgId: orgValues?.id,
+ });
+
+ expect(mockDb.set).toHaveBeenCalledWith(
+ expect.objectContaining({ defaultOrgId: orgValues?.id }),
+ );
+ });
+
+ it("falls back to the default org when a stale explicit orgId is sent and the user still has orgs", async () => {
+ mockGetCurrentUser.mockResolvedValue({
+ id: "user-1",
+ email: "[email protected]",
+ defaultOrgId: "org-1",
+ activeOrganizationId: "org-1",
+ });
+ mockDb.where
+ .mockResolvedValueOnce([
+ { id: "org-1", name: "Acme", createdAt: new Date() },
+ ])
+ .mockResolvedValueOnce([])
+ .mockResolvedValueOnce([{ count: 5 }]);
+
+ const response = await app.request(
+ "https://cap.test/create?orgId=org-stale",
+ );
+
+ expect(response.status).toBe(200);
+ expect(mockDb.transaction).not.toHaveBeenCalled();
+
+ expect(insertedValues(schema.videos)).toMatchObject({
+ orgId: "org-1",
+ ownerId: "user-1",
+ });
+ });
+
+ it("honours an explicit orgId the user belongs to", async () => {
+ mockGetCurrentUser.mockResolvedValue({
+ id: "user-1",
+ email: "[email protected]",
+ defaultOrgId: "org-1",
+ activeOrganizationId: "org-1",
+ });
+ mockDb.where
+ .mockResolvedValueOnce([
+ { id: "org-1", name: "Acme", createdAt: new Date() },
+ ])
+ .mockResolvedValueOnce([
+ { id: "org-2", name: "Team", createdAt: new Date() },
+ ])
+ .mockResolvedValueOnce([{ count: 5 }]);
+
+ const response = await app.request("https://cap.test/create?orgId=org-2");
+
+ expect(response.status).toBe(200);
+ expect(mockDb.transaction).not.toHaveBeenCalled();
+
+ expect(insertedValues(schema.videos)).toMatchObject({
+ orgId: "org-2",
+ ownerId: "user-1",
+ });
+ });
});
diff --git a/apps/web/app/api/desktop/[...route]/video.ts b/apps/web/app/api/desktop/[...route]/video.ts
index aeb97f303..63008fac5 100644
--- a/apps/web/app/api/desktop/[...route]/video.ts
+++ b/apps/web/app/api/desktop/[...route]/video.ts
@@ -215,10 +215,10 @@ app.get(
const userOrgIds = userOrganizations.map((org) => org.id);
let videoOrgId: Organisation.OrganisationId;
- if (orgId) {
- // Hard error if the user requested org is non-existent or they don't have access.
- if (!userOrgIds.includes(orgId))
- return c.json({ error: "forbidden_org" }, { status: 403 });
+ // Desktop persists orgId in settings and keeps sending it after the
+ // user leaves the org, so an unknown orgId falls back to the
+ // default/first org below instead of hard-failing every upload.
+ if (orgId && userOrgIds.includes(orgId)) {
videoOrgId = orgId;
} else if (user.defaultOrgId) {
// User's defaultOrgId is no longer valid, switch to first available org