This document summarizes the complete organization management system implementation for VectorMindAI. The system enables organizations to use the platform collaboratively with role-based access control and comprehensive admin features.
- name: string
- createdBy: string (userId of creator)
- members: Array<{ userId, role, joinedAt, invitedBy }>
- invites: Array<{ email, inviteCode, expiresAt, createdBy }>
- settings: { features, billing, notifications }
- createdAt, updatedAt: timestamps- role: "individual" | "org-admin" | "member"
- organizationId: ObjectId (optional)
- joinedAt: Date (when joined organization)- organizationId: ObjectId (optional)
- userName: string (for org-admin visibility)
- userEmail: string (for org-admin visibility)requireAuth()- Base authentication checkrequireOrgAdmin()- Requires org-admin rolerequireOrgMember()- Requires member or admin roleisOrgAdmin()- Check if user is org-admincanAccessOrgData()- Verify organization accessverifyOrgMembership()- Verify user belongs to specific organization
- JWT includes: role, organizationId
- Session includes: user.role, user.organizationId
- Auto-refreshes user role from database on each request
POST /api/organization/create- Create new organization (during signup)GET /api/organization/info- Get organization details (admin-only)PATCH /api/organization/update- Update organization name (admin-only)DELETE /api/organization/delete- Delete organization (creator-only)
POST /api/organization/invite- Generate invite link (admin-only)GET /api/organization/invite- List active invites (admin-only)DELETE /api/organization/invite- Remove invite (admin-only)GET /api/organization/members- List all members (member-access)DELETE /api/organization/members- Remove member (admin-only)GET /api/organization/members/[memberId]- Get member profile (admin-only)
GET /api/organization/history- Get all member search history (admin-only)GET /api/organization/analytics- Get organization analytics (admin-only)
POST /api/auth/join- Join organization via invite codePOST /api/auth/signup- Signup with optional org creation
Features:
- Organization overview (name, member count, total searches)
- Member management section
- Recent organization-wide activity
- Quick stats cards
- Navigation to settings and analytics
Features:
- Invite member with email
- Generate & copy invite links
- View all members with roles
- Remove members (admin-only)
- See invite creation date and creator
Features:
- Show all organization searches (for org-admins)
- Display member name badge on each search
- Filter by member (future enhancement)
- Same functionality as individual history for non-org users
Features:
- Total searches metric
- Active members count
- Top contributors (members by search count)
- Recent activity graph (last 7 days)
- Real-time data updates
Features:
- Update organization name
- View organization details (ID, creation date)
- Danger zone: Delete organization
- Only accessible by org-admin
Features:
- Member information (name, email, role, join date)
- Activity statistics (total searches, recent activity)
- Last active timestamp
- Recent search history (last 10)
- Search status indicators
All research operations now include organization context:
-
app/api/research/route.ts- Captures userId, organizationId, userName, userEmail from session
- Passes context to Inngest workflow
- Tracks analytics with org context
-
app/api/planner/route.ts- Includes organization metadata in responses
- Enables future org-level plan tracking
-
lib/inngest/functions.ts- Job finalization includes user/org context
- Enables proper history attribution
-
lib/inngest/extended-research.ts- Deep research includes org context
- Maintains data lineage
-
lib/store.ts- Job interface extended with userId, organizationId, userName, userEmail
- Enables tracking of who initiated research
-
app/api/history/route.ts- Automatically saves org context when creating history records
- Supports org-wide history queries
- User signs up at
/auth/signup - Selects "Create Organization" option
- Provides organization name
- Automatically becomes org-admin
- Redirected to organization dashboard
- Org-admin goes to organization dashboard
- Clicks "Invite Member"
- Enters member email
- System generates unique invite code
- Admin copies & shares invite link
- New user clicks link → redirected to
/auth/join?code=xxx - New user signs up
- Automatically added to organization as member
- Login → Dashboard shows organization overview
- View all members and their roles
- Access organization-wide search history with member labels
- View analytics dashboard
- Click any member → See detailed member profile
- Remove members if needed
- Access settings to update org name or delete
- Login → Standard dashboard
- Can see organization name in header
- Can view org members list
- Cannot access admin features (invite, remove, settings)
- All searches automatically tagged with organization context
- Signup without creating organization
- Use platform normally with personal account
- No organization features visible
- Option to create organization later (future enhancement)
- Query Filtering: All organization-related queries include organizationId filter
- Authorization Checks: All admin routes verify org-admin role
- Session Validation: Organization membership verified on each request
- Cross-Org Prevention: Cannot access other organization's data even with valid IDs
- Invite codes are cryptographically random (32 characters)
- Invite expiration (default 7 days, configurable)
- Creator cannot be removed from organization
- Only creator can delete organization
- Cascade deletion: Removing org updates all members to individual
- History preservation: Search history kept but org reference removed
- Organization creation during signup
- Role-based access control (individual, org-admin, member)
- Member invitation system with unique codes
- Organization admin dashboard
- Member management (invite, view, remove)
- Organization-wide search history with member labels
- Organization analytics dashboard
- Member profile pages with detailed stats
- Organization settings page
- Organization context in all research operations
- Data isolation and security
- Conditional UI rendering based on roles
- Organization deletion with cleanup
- Member role management (promote/demote)
- Team workspaces within organizations
- Shared research templates
- Collaborative research sessions
- Usage quotas and billing per organization
- Activity audit logs
- Email notifications for invites
- Member permissions (read-only, contributor, admin)
- Export organization data
- Organization transfer ownership
To verify the system works correctly:
-
Create Organization
- Sign up and create organization
- Verify org-admin role assigned
- Check organization appears in dashboard
-
Invite Members
- Generate invite link
- Copy and use invite code
- Verify member added successfully
- Check member has "member" role
-
Test Admin Features
- View all members list
- Access organization-wide history
- View analytics dashboard
- Click member profile
- Remove a member
- Update organization name
- Verify all actions work
-
Test Member Experience
- Login as member
- Verify limited access (no admin features)
- Perform research
- Check history saved with org context
-
Test Data Isolation
- Create second organization
- Verify cannot access first org's data
- Check queries properly filtered
- Ensure no cross-org leaks
-
Test Security
- Try accessing admin routes as member (should fail)
- Try removing creator (should fail)
- Verify invite expiration works
- Check auth middleware on all routes
No new environment variables required. Uses existing:
MONGODB_URI- Database connectionNEXTAUTH_SECRET- JWT signingNEXTAUTH_URL- Auth callback URL
Recommended indexes for performance:
// User collection
db.users.createIndex({ organizationId: 1 })
db.users.createIndex({ email: 1 }, { unique: true })
// SearchHistory collection
db.searchhistories.createIndex({ organizationId: 1, timestamp: -1 })
db.searchhistories.createIndex({ userId: 1, timestamp: -1 })
// Organization collection
db.organizations.createIndex({ createdBy: 1 })
db.organizations.createIndex({ "invites.inviteCode": 1 })vectorMindAI-org/
├── app/
│ ├── api/
│ │ ├── organization/
│ │ │ ├── create/route.ts
│ │ │ ├── info/route.ts
│ │ │ ├── update/route.ts
│ │ │ ├── delete/route.ts
│ │ │ ├── invite/route.ts
│ │ │ ├── members/
│ │ │ │ ├── route.ts
│ │ │ │ └── [memberId]/route.ts
│ │ │ ├── history/route.ts
│ │ │ └── analytics/route.ts
│ │ ├── auth/
│ │ │ ├── signup/route.ts
│ │ │ └── join/route.ts
│ │ ├── research/route.ts (updated)
│ │ ├── planner/route.ts (updated)
│ │ └── history/route.ts (updated)
│ ├── dashboard/
│ │ └── organization/
│ │ ├── page.tsx
│ │ ├── settings/page.tsx
│ │ └── members/[memberId]/page.tsx
│ └── auth/
│ ├── signup/page.tsx (updated)
│ └── join/page.tsx
├── components/
│ ├── member-management.tsx
│ ├── org-analytics.tsx
│ └── search-history.tsx (updated)
├── lib/
│ ├── models/
│ │ ├── Organization.ts
│ │ ├── User.ts (updated)
│ │ └── SearchHistory.ts (updated)
│ ├── auth-helpers.ts
│ ├── store.ts (updated)
│ └── inngest/
│ ├── functions.ts (updated)
│ └── extended-research.ts (updated)
├── auth.ts (updated)
└── types/
└── next-auth.d.ts (updated)
-
JWT Session Strategy: Chose JWT over database sessions for scalability, but refresh user role on each request to ensure up-to-date permissions
-
Invite Code System: Used crypto.randomBytes for secure invite codes instead of predictable sequential IDs
-
History Attribution: Store userName/userEmail in history records for efficient querying without joins (denormalization trade-off)
-
Organization Context: Pass org context through entire research pipeline (API → Inngest → Job Store → History) for complete data lineage
-
Soft Member Removal: When removing members, convert to "individual" role rather than deleting account, preserving user data
-
Organization Deletion: On org deletion, update all members to individual and unset organizationId from history records (preserve history but remove org link)
- Single Organization: Users can only belong to one organization at a time
- No Role Hierarchy: Only two roles (org-admin and member), no intermediate permissions
- File-Based Job Store: Job tracking still uses JSON file, should migrate to MongoDB for multi-instance support
- No Email Notifications: Invite links must be manually shared, no automatic email sending
- No Activity Logs: No audit trail of admin actions (who removed whom, when)
The organization management system is fully implemented and ready for testing. All 20 implementation todos have been completed. The system provides:
- Complete role-based access control
- Secure multi-tenancy with data isolation
- Comprehensive admin dashboard and analytics
- Member invitation and management
- Organization-wide visibility for admins
- Privacy for individual users
- Extensible architecture for future enhancements
Next Steps:
- Run comprehensive manual testing (see Testing Checklist)
- Fix remaining minor Tailwind CSS warnings
- Consider migrating job store from file to MongoDB
- Add email notification system for invites
- Implement additional analytics and reporting features