Skip to content

Latest commit

 

History

History
392 lines (323 loc) · 13.9 KB

File metadata and controls

392 lines (323 loc) · 13.9 KB

Organization Management System - Implementation Summary

Overview

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.

Architecture

Database Models

1. Organization Model (lib/models/Organization.ts)

- 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

2. User Model (Extended) (lib/models/User.ts)

- role: "individual" | "org-admin" | "member"
- organizationId: ObjectId (optional)
- joinedAt: Date (when joined organization)

3. SearchHistory Model (Extended) (lib/models/SearchHistory.ts)

- organizationId: ObjectId (optional)
- userName: string (for org-admin visibility)
- userEmail: string (for org-admin visibility)

Authentication & Authorization

Role-Based Access Control (lib/auth-helpers.ts)

  • requireAuth() - Base authentication check
  • requireOrgAdmin() - Requires org-admin role
  • requireOrgMember() - Requires member or admin role
  • isOrgAdmin() - Check if user is org-admin
  • canAccessOrgData() - Verify organization access
  • verifyOrgMembership() - Verify user belongs to specific organization

NextAuth Configuration (auth.ts)

  • JWT includes: role, organizationId
  • Session includes: user.role, user.organizationId
  • Auto-refreshes user role from database on each request

API Endpoints

Organization Management

  • 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)

Member Management

  • 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)

History & Analytics

  • GET /api/organization/history - Get all member search history (admin-only)
  • GET /api/organization/analytics - Get organization analytics (admin-only)

Authentication

  • POST /api/auth/join - Join organization via invite code
  • POST /api/auth/signup - Signup with optional org creation

UI Components

1. Organization Dashboard (app/dashboard/organization/page.tsx)

Features:

  • Organization overview (name, member count, total searches)
  • Member management section
  • Recent organization-wide activity
  • Quick stats cards
  • Navigation to settings and analytics

2. Member Management (components/member-management.tsx)

Features:

  • Invite member with email
  • Generate & copy invite links
  • View all members with roles
  • Remove members (admin-only)
  • See invite creation date and creator

3. Search History (components/search-history.tsx)

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

4. Organization Analytics (components/org-analytics.tsx)

Features:

  • Total searches metric
  • Active members count
  • Top contributors (members by search count)
  • Recent activity graph (last 7 days)
  • Real-time data updates

5. Organization Settings (app/dashboard/organization/settings/page.tsx)

Features:

  • Update organization name
  • View organization details (ID, creation date)
  • Danger zone: Delete organization
  • Only accessible by org-admin

6. Member Profile (app/dashboard/organization/members/[memberId]/page.tsx)

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

Organization Context in Research Operations

All research operations now include organization context:

Updated Files:

  1. app/api/research/route.ts

    • Captures userId, organizationId, userName, userEmail from session
    • Passes context to Inngest workflow
    • Tracks analytics with org context
  2. app/api/planner/route.ts

    • Includes organization metadata in responses
    • Enables future org-level plan tracking
  3. lib/inngest/functions.ts

    • Job finalization includes user/org context
    • Enables proper history attribution
  4. lib/inngest/extended-research.ts

    • Deep research includes org context
    • Maintains data lineage
  5. lib/store.ts

    • Job interface extended with userId, organizationId, userName, userEmail
    • Enables tracking of who initiated research
  6. app/api/history/route.ts

    • Automatically saves org context when creating history records
    • Supports org-wide history queries

User Flows

1. Organization Creation Flow

  1. User signs up at /auth/signup
  2. Selects "Create Organization" option
  3. Provides organization name
  4. Automatically becomes org-admin
  5. Redirected to organization dashboard

2. Member Invitation Flow

  1. Org-admin goes to organization dashboard
  2. Clicks "Invite Member"
  3. Enters member email
  4. System generates unique invite code
  5. Admin copies & shares invite link
  6. New user clicks link → redirected to /auth/join?code=xxx
  7. New user signs up
  8. Automatically added to organization as member

3. Organization Admin Flow

  1. Login → Dashboard shows organization overview
  2. View all members and their roles
  3. Access organization-wide search history with member labels
  4. View analytics dashboard
  5. Click any member → See detailed member profile
  6. Remove members if needed
  7. Access settings to update org name or delete

4. Organization Member Flow

  1. Login → Standard dashboard
  2. Can see organization name in header
  3. Can view org members list
  4. Cannot access admin features (invite, remove, settings)
  5. All searches automatically tagged with organization context

5. Individual User Flow

  1. Signup without creating organization
  2. Use platform normally with personal account
  3. No organization features visible
  4. Option to create organization later (future enhancement)

Data Isolation & Security

Multi-Tenancy Implementation

  • 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

Security Features

  • 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

Features Summary

✅ Implemented Features

  • 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

🔄 Future Enhancements

  • 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

Testing Checklist

To verify the system works correctly:

  1. Create Organization

    • Sign up and create organization
    • Verify org-admin role assigned
    • Check organization appears in dashboard
  2. Invite Members

    • Generate invite link
    • Copy and use invite code
    • Verify member added successfully
    • Check member has "member" role
  3. 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
  4. Test Member Experience

    • Login as member
    • Verify limited access (no admin features)
    • Perform research
    • Check history saved with org context
  5. Test Data Isolation

    • Create second organization
    • Verify cannot access first org's data
    • Check queries properly filtered
    • Ensure no cross-org leaks
  6. 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

Configuration

Environment Variables

No new environment variables required. Uses existing:

  • MONGODB_URI - Database connection
  • NEXTAUTH_SECRET - JWT signing
  • NEXTAUTH_URL - Auth callback URL

Database Indexes

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 })

File Structure

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)

Key Technical Decisions

  1. JWT Session Strategy: Chose JWT over database sessions for scalability, but refresh user role on each request to ensure up-to-date permissions

  2. Invite Code System: Used crypto.randomBytes for secure invite codes instead of predictable sequential IDs

  3. History Attribution: Store userName/userEmail in history records for efficient querying without joins (denormalization trade-off)

  4. Organization Context: Pass org context through entire research pipeline (API → Inngest → Job Store → History) for complete data lineage

  5. Soft Member Removal: When removing members, convert to "individual" role rather than deleting account, preserving user data

  6. Organization Deletion: On org deletion, update all members to individual and unset organizationId from history records (preserve history but remove org link)

Known Limitations

  1. Single Organization: Users can only belong to one organization at a time
  2. No Role Hierarchy: Only two roles (org-admin and member), no intermediate permissions
  3. File-Based Job Store: Job tracking still uses JSON file, should migrate to MongoDB for multi-instance support
  4. No Email Notifications: Invite links must be manually shared, no automatic email sending
  5. No Activity Logs: No audit trail of admin actions (who removed whom, when)

Conclusion

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:

  1. Run comprehensive manual testing (see Testing Checklist)
  2. Fix remaining minor Tailwind CSS warnings
  3. Consider migrating job store from file to MongoDB
  4. Add email notification system for invites
  5. Implement additional analytics and reporting features