-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add POST /api/workspaces endpoint with centralized auth validation #141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
- Add POST /api/workspaces endpoint for workspace creation - Create validateAuthContext utility as single source of truth for auth/org validation - Fix personal API keys unable to add workspaces to orgs they're members of - Add self-access check allowing personal keys to specify own account_id - Refactor validateCreateArtistBody to use centralized utility + add org validation - Add comprehensive tests for validateAuthContext (15 tests)
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
lib/auth/validateAuthContext.ts
Outdated
| * @param params - The validation parameters | ||
| * @returns NextResponse with error or the validated result | ||
| */ | ||
| async function validateAccountIdOverride( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SRP
- actual: validateAccountIdOverride defined in file named lib/auth/validateAuthContext.ts
- required: new lib for validateAccountIdOverride
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
| const linkId = await insertAccountWorkspaceId(accountId, account.id); | ||
| if (!linkId) return null; | ||
|
|
||
| if (organizationId) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Workspace accounts are being added to artist_organization_ids table instead of account_organization_ids table, causing workspace visibility and database constraint issues in organizations.
View Details
📝 Patch Details
diff --git a/lib/workspaces/createWorkspaceInDb.ts b/lib/workspaces/createWorkspaceInDb.ts
index d7684c5..ebd591e 100644
--- a/lib/workspaces/createWorkspaceInDb.ts
+++ b/lib/workspaces/createWorkspaceInDb.ts
@@ -5,7 +5,7 @@ import {
type AccountWithSocials,
} from "@/lib/supabase/accounts/selectAccountWithSocials";
import { insertAccountWorkspaceId } from "@/lib/supabase/account_workspace_ids/insertAccountWorkspaceId";
-import { addArtistToOrganization } from "@/lib/supabase/artist_organization_ids/addArtistToOrganization";
+import { addAccountToOrganization } from "@/lib/supabase/account_organization_ids/addAccountToOrganization";
/**
* Result of creating a workspace in the database.
@@ -41,7 +41,7 @@ export async function createWorkspaceInDb(
if (!linkId) return null;
if (organizationId) {
- await addArtistToOrganization(account.id, organizationId);
+ await addAccountToOrganization(account.id, organizationId);
}
return {
Analysis
Bug Explanation
The workspace creation code incorrectly uses addArtistToOrganization() which inserts workspace records into the artist_organization_ids table. This is semantically wrong because:
-
Workspace semantics: Workspaces are workspace-type accounts that should be associated with organizations at the account level (using
account_organization_ids), not the artist level (usingartist_organization_ids). -
Pattern in codebase:
- Organizations are created as accounts and use
addAccountToOrganization()to insert intoaccount_organization_ids - Artists are created as accounts and use
addArtistToOrganization()to insert intoartist_organization_ids - Workspaces are created as accounts but incorrectly use
addArtistToOrganization()which is meant only for artist-type accounts
- Organizations are created as accounts and use
-
Database schema confirms the semantic difference:
artist_organization_idstable has columnartist_id(specific to artist accounts)account_organization_idstable has columnaccount_id(generic for any account type)
-
Impact:
- Workspaces are inserted into the wrong table (
artist_organization_idsinstead ofaccount_organization_ids) - This breaks workspace visibility in organization views (which query
account_organization_ids) - Violates semantic correctness - workspaces are account-level entities, not artist-level entities
- Workspaces are inserted into the wrong table (
Fix Explanation
The fix involved two changes to lib/workspaces/createWorkspaceInDb.ts:
-
Changed import: Replaced
import { addArtistToOrganization }withimport { addAccountToOrganization } -
Changed function call at line 43: Replaced
await addArtistToOrganization(account.id, organizationId)withawait addAccountToOrganization(account.id, organizationId)
This ensures that when a workspace is created with an organization_id, it is correctly inserted into the account_organization_ids table, aligning with how other account-level entities (like organizations themselves) are associated with organizations. This restores proper workspace visibility within organizations and prevents database constraint issues.
Add setupConversation mock to validateChatRequest.test.ts and handleChatGenerate.test.ts to break the import chain that was reaching the Supabase server client and throwing errors due to missing SUPABASE_URL and SUPABASE_KEY environment variables. Co-Authored-By: Claude Opus 4.5 <[email protected]>
|
You have run out of free Bugbot PR reviews for this billing cycle. This will reset on February 17. To receive reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial. |
Summary
Adds the POST /api/workspaces endpoint and fixes the bug where personal API keys couldn't add workspaces to organizations they're members of.
Changes
New Files
lib/auth/validateAuthContext.ts- Centralized auth/org validation utility (single source of truth)lib/auth/__tests__/validateAuthContext.test.ts- 15 tests for auth context validationapp/api/workspaces/route.ts- POST /api/workspaces endpointlib/workspaces/createWorkspaceInDb.ts- Database helper for workspace creationlib/workspaces/createWorkspacePostHandler.ts- Request handlerlib/workspaces/validateCreateWorkspaceBody.ts- Request validationlib/supabase/account_workspace_ids/insertAccountWorkspaceId.ts- Owner linkingModified Files
lib/artists/validateCreateArtistBody.ts- Refactored to use centralized auth + added missing org validationlib/artists/__tests__/*.test.ts- Updated tests for refactored validationBug Fix
Problem: Personal API keys couldn't add workspaces to organizations they're members of.
Root Cause:
canAccessAccountreturnedfalsewhenorgIdwasnull(personal API keys), even for self-access.Solution: Created
validateAuthContextutility that:Tests
All 43 tests passing.