Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

Copy link

Copilot AI commented Oct 19, 2025

Thanks for asking me to work on this. I will get started on it and keep this PR's description up to date as I form a plan and make progress.

Original prompt

Add a new enterprise (multi-tenant) feature to support path-isolated tenancy, enterprise management, employee sync via API, admin audit/review workflow, and frontend enterprise UI pieces. Do not change existing tables; add new Prisma models and migrations. Implement server modules, middleware, admin endpoints, and frontend components, and include environment variable controls.

Requirements (implement exactly):

  1. Prisma schema and migration
  • Append new Prisma models in prisma/schema.prisma: Enterprise, EnterpriseDomain, EnterpriseMember, EnterpriseTeam, Employee, enums EnterpriseStatus and EnterpriseRole. Keep relations to existing User model. Do not modify existing models.
  • Add a SQL migration file prisma/migrations/20251019_add_enterprise_tables.sql that creates corresponding tables and foreign keys where safe.
  1. Environment variables
  • Add .env.example.enterprise listing:
    • ENTERPRISE_ENABLED
    • ENTERPRISE_CHARGE_REQUIRED
    • ENTERPRISE_SAAS_MODE
    • ENTERPRISE_ADMIN_API_KEY
    • ENTERPRISE_EXTERNAL_SYNC_KEY
  1. Server: new module packages/server/src/modules/enterprise
  • Add enterprise.service.ts implementing: createEnterprise (creates PENDING enterprise, member for creator), approveEnterprise, rejectEnterprise, getBySlug, listUserEnterprises, addEmployeeAndUser (creates Employee and calls existing auth service to create user if missing), syncEmployeesFromExternal.
  • Add enterprise.controller.ts registering Express routes consistent with repository with endpoints:
    • POST /api/enterprises (requireAuth) -> create (PENDING)
    • GET /api/enterprises/me (requireAuth) -> list user's active enterprises
    • GET /api/enterprises/slug/:slug -> get by slug
    • POST /api/enterprises/:enterpriseId/employees/sync -> validateApiKey(ENTERPRISE_EXTERNAL_SYNC_KEY) -> sync
  • Ensure imports and registrations consistent with repo (if routes are registered via server bootstrap, add note to register route at server entrypoint).
  1. Middleware
  • Add tenant middleware packages/server/src/middleware/tenant.middleware.ts: path-based tenant resolver (/e/:slug/*), host domain mapping, attach req.enterprise and rewrite req.url to remove prefix.
  • Add packages/server/src/middleware/apikey.ts and adminAuth.ts for API key validation.
  1. Admin module
  • Add admin-enterprise.controller.ts under packages/server/src/modules/admin to allow admin operations:
    • GET /api/admin/enterprises -> list all enterprises (protected by ENTERPRISE_ADMIN_API_KEY)
    • POST /api/admin/enterprises/:enterpriseId/approve
    • POST /api/admin/enterprises/:enterpriseId/reject
  1. Frontend
  • Add components packages/web/src/components/EnterpriseSwitcher.tsx and EnterpriseCreateForm.tsx implementing listing /api/enterprises/me and posting /api/enterprises.
  1. Routing and path isolation guidance
  • Add middleware usage in server bootstrap: use tenantResolver before main app routes; document how to prefix routes with /e/:slug and how to rewrite URL in middleware.
  1. Tests / docs
  • Add docs/enterprise.md describing feature flags, env vars, migration steps, admin workflow, domain verification, OAuth2 notes, and external sync API contract.

Implementation notes for developer:

  • Reuse existing auth service API: authService.createUserIfNotExists({ email, firstName, lastName }) — if actual method differs, adapt accordingly.
  • Use PrismaClient from @prisma/client.
  • Protect admin endpoints with X-Enterprise-Admin-Key header checked against ENTERPRISE_ADMIN_API_KEY.
  • Protect external sync with X-Enterprise-API-Key header checked against ENTERPRISE_EXTERNAL_SYNC_KEY.
  • Do not modify existing tables; create new tables and use string references to existing Team ids where appropriate (EnterpriseTeam.teamId).
  • Add file comments where further integration points are needed (route registration, auth service import path adaptations).

Files to create (provide each file contents):

  • prisma/schema.prisma (append new models)
  • prisma/migrations/20251019_add_enterprise_tables.sql
  • .env.example.enterprise
  • packages/server/src/modules/enterprise/enterprise.service.ts
  • packages/server/src/modules/enterprise/enterprise.controller.ts
  • packages/server/src/modules/admin/admin-enterprise.controller.ts
  • packages/server/src/middleware/tenant.middleware.ts
  • packages/server/src/middleware/apikey.ts
  • packages/server/src/middleware/adminAuth.ts
  • packages/web/src/components/EnterpriseSwitcher.tsx
  • packages/web/src/components/EnterpriseCreateForm.tsx
  • docs/enterprise.md

Make sure code compiles under TypeScript; add // TODO comments where integration to existing codebase is required (e.g., importing existing authService, registering routes in server bootstrap). Keep code style consistent with TypeScript, using async/await and express Request/Response types. Return appropriate status codes and JSON responses.

Create a new branch feat/enterprise-multitenant off main, add these files with the contents prepared in earlier messages, and open a PR targeting main. Include a clear PR ...

This pull request was created as a result of the following prompt from Copilot chat.

Add a new enterprise (multi-tenant) feature to support path-isolated tenancy, enterprise management, employee sync via API, admin audit/review workflow, and frontend enterprise UI pieces. Do not change existing tables; add new Prisma models and migrations. Implement server modules, middleware, admin endpoints, and frontend components, and include environment variable controls.

Requirements (implement exactly):

  1. Prisma schema and migration
  • Append new Prisma models in prisma/schema.prisma: Enterprise, EnterpriseDomain, EnterpriseMember, EnterpriseTeam, Employee, enums EnterpriseStatus and EnterpriseRole. Keep relations to existing User model. Do not modify existing models.
  • Add a SQL migration file prisma/migrations/20251019_add_enterprise_tables.sql that creates corresponding tables and foreign keys where safe.
  1. Environment variables
  • Add .env.example.enterprise listing:
    • ENTERPRISE_ENABLED
    • ENTERPRISE_CHARGE_REQUIRED
    • ENTERPRISE_SAAS_MODE
    • ENTERPRISE_ADMIN_API_KEY
    • ENTERPRISE_EXTERNAL_SYNC_KEY
  1. Server: new module packages/server/src/modules/enterprise
  • Add enterprise.service.ts implementing: createEnterprise (creates PENDING enterprise, member for creator), approveEnterprise, rejectEnterprise, getBySlug, listUserEnterprises, addEmployeeAndUser (creates Employee and calls existing auth service to create user if missing), syncEmployeesFromExternal.
  • Add enterprise.controller.ts registering Express routes consistent with repository with endpoints:
    • POST /api/enterprises (requireAuth) -> create (PENDING)
    • GET /api/enterprises/me (requireAuth) -> list user's active enterprises
    • GET /api/enterprises/slug/:slug -> get by slug
    • POST /api/enterprises/:enterpriseId/employees/sync -> validateApiKey(ENTERPRISE_EXTERNAL_SYNC_KEY) -> sync
  • Ensure imports and registrations consistent with repo (if routes are registered via server bootstrap, add note to register route at server entrypoint).
  1. Middleware
  • Add tenant middleware packages/server/src/middleware/tenant.middleware.ts: path-based tenant resolver (/e/:slug/*), host domain mapping, attach req.enterprise and rewrite req.url to remove prefix.
  • Add packages/server/src/middleware/apikey.ts and adminAuth.ts for API key validation.
  1. Admin module
  • Add admin-enterprise.controller.ts under packages/server/src/modules/admin to allow admin operations:
    • GET /api/admin/enterprises -> list all enterprises (protected by ENTERPRISE_ADMIN_API_KEY)
    • POST /api/admin/enterprises/:enterpriseId/approve
    • POST /api/admin/enterprises/:enterpriseId/reject
  1. Frontend
  • Add components packages/web/src/components/EnterpriseSwitcher.tsx and EnterpriseCreateForm.tsx implementing listing /api/enterprises/me and posting /api/enterprises.
  1. Routing and path isolation guidance
  • Add middleware usage in server bootstrap: use tenantResolver before main app routes; document how to prefix routes with /e/:slug and how to rewrite URL in middleware.
  1. Tests / docs
  • Add docs/enterprise.md describing feature flags, env vars, migration steps, admin workflow, domain verification, OAuth2 notes, and external sync API contract.

Implementation notes for developer:

  • Reuse existing auth service API: authService.createUserIfNotExists({ email, firstName, lastName }) — if actual method differs, adapt accordingly.
  • Use PrismaClient from @prisma/client.
  • Protect admin endpoints with X-Enterprise-Admin-Key header checked against ENTERPRISE_ADMIN_API_KEY.
  • Protect external sync with X-Enterprise-API-Key header checked against ENTERPRISE_EXTERNAL_SYNC_KEY.
  • Do not modify existing tables; create new tables and use string references to existing Team ids where appropriate (EnterpriseTeam.teamId).
  • Add file comments where further integration points are needed (route registration, auth service import path adaptations).

Files to create (provide each file contents):

  • prisma/schema.prisma (append new models)
  • prisma/migrations/20251019_add_enterprise_tables.sql
  • .env.example.enterprise
  • packages/server/src/modules/enterprise/enterprise.service.ts
  • packages/server/src/modules/enterprise/enterprise.controller.ts
  • packages/server/src/modules/admin/admin-enterprise.controller.ts
  • packages/server/src/middleware/tenant.middleware.ts
  • packages/server/src/middleware/apikey.ts
  • packages/server/src/middleware/adminAuth.ts
  • packages/web/src/components/EnterpriseSwitcher.tsx
  • packages/web/src/components/EnterpriseCreateForm.tsx
  • docs/enterprise.md

Make sure code compiles under TypeScript; add // TODO comments where integration to existing codebase is required (e.g., importing existing authService, registering routes in server bootstrap). Keep code style consistent with TypeScript, using async/await and express Request/Response types. Return appropriate status codes and JSON responses.

Create a new branch feat/enterprise-multitenant off main, add these files with the contents prepared in earlier messages, and open a PR targeting main. Include a clear PR description summarizing changes and migration instructions. Do not open any issues.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@jefyisyous jefyisyous marked this pull request as ready for review October 19, 2025 07:43
@jefyisyous jefyisyous merged commit 094dd1c into main Oct 19, 2025
1 check failed
Copilot AI requested a review from jefyisyous October 19, 2025 07:44
@jefyisyous jefyisyous deleted the copilot/add-enterprise-multi-tenant-feature branch October 19, 2025 08:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants