Compare commits
1 Commits
353da0039f
...
frontendau
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1d610f0d2b |
1
.env.api
1
.env.api
@@ -2,6 +2,7 @@ NODE_ENV=development
|
|||||||
PORT=3000
|
PORT=3000
|
||||||
|
|
||||||
DATABASE_URL="mysql://platform:platform@db:3306/platform"
|
DATABASE_URL="mysql://platform:platform@db:3306/platform"
|
||||||
|
CENTRAL_DATABASE_URL="mysql://root:asjdnfqTash37faggT@db:3306/central_platform"
|
||||||
REDIS_URL="redis://redis:6379"
|
REDIS_URL="redis://redis:6379"
|
||||||
|
|
||||||
# JWT, multi-tenant hints, etc.
|
# JWT, multi-tenant hints, etc.
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ Use this checklist to ensure proper implementation of the field type system in y
|
|||||||
### Database
|
### Database
|
||||||
- [ ] Run migration: `npm run migrate:tenant` to add `ui_metadata` column
|
- [ ] Run migration: `npm run migrate:tenant` to add `ui_metadata` column
|
||||||
- [ ] Verify migration succeeded: Check `field_definitions` table has `ui_metadata` column
|
- [ ] Verify migration succeeded: Check `field_definitions` table has `ui_metadata` column
|
||||||
- [ ] (Optional) Run seed: `knex seed:run --specific=example_account_fields_with_ui_metadata.js`
|
- [ ] (Optional) Run seed: `knex seed:run --specific=example_contact_fields_with_ui_metadata.js`
|
||||||
- [ ] Test database access with sample queries
|
- [ ] Test database access with sample queries
|
||||||
|
|
||||||
### Services
|
### Services
|
||||||
|
|||||||
@@ -1,302 +0,0 @@
|
|||||||
# Tenant Migration Guide
|
|
||||||
|
|
||||||
## Quick Start
|
|
||||||
|
|
||||||
### Create a New Migration
|
|
||||||
```bash
|
|
||||||
cd backend
|
|
||||||
npm run migrate:make add_your_feature_name
|
|
||||||
```
|
|
||||||
|
|
||||||
Edit the generated file in `backend/migrations/tenant/`
|
|
||||||
|
|
||||||
### Test on Single Tenant
|
|
||||||
```bash
|
|
||||||
npm run migrate:tenant acme-corp
|
|
||||||
```
|
|
||||||
|
|
||||||
### Apply to All Tenants
|
|
||||||
```bash
|
|
||||||
npm run migrate:all-tenants
|
|
||||||
```
|
|
||||||
|
|
||||||
## Available Commands
|
|
||||||
|
|
||||||
| Command | Description |
|
|
||||||
|---------|-------------|
|
|
||||||
| `npm run migrate:make <name>` | Create a new migration file |
|
|
||||||
| `npm run migrate:tenant <slug>` | Run migrations for a specific tenant |
|
|
||||||
| `npm run migrate:all-tenants` | Run migrations for all active tenants |
|
|
||||||
| `npm run migrate:latest` | Run migrations (default DB - rarely used) |
|
|
||||||
| `npm run migrate:rollback` | Rollback last migration (default DB) |
|
|
||||||
|
|
||||||
## Architecture
|
|
||||||
|
|
||||||
### Multi-Tenant Database Structure
|
|
||||||
|
|
||||||
```
|
|
||||||
┌─────────────────────────┐
|
|
||||||
│ Central Database │
|
|
||||||
│ │
|
|
||||||
│ - tenants table │
|
|
||||||
│ - users table │
|
|
||||||
│ - (encrypted creds) │
|
|
||||||
└─────────────────────────┘
|
|
||||||
│
|
|
||||||
│ manages
|
|
||||||
│
|
|
||||||
┌───────┴────────┐
|
|
||||||
│ │
|
|
||||||
┌───▼────┐ ┌───▼────┐
|
|
||||||
│ Tenant │ │ Tenant │
|
|
||||||
│ DB1 │ ... │ DBN │
|
|
||||||
│ │ │ │
|
|
||||||
│ - users│ │ - users│
|
|
||||||
│ - roles│ │ - roles│
|
|
||||||
│ - apps │ │ - apps │
|
|
||||||
│ - ... │ │ - ... │
|
|
||||||
└────────┘ └────────┘
|
|
||||||
```
|
|
||||||
|
|
||||||
### How Migrations Work
|
|
||||||
|
|
||||||
1. **New Tenant Provisioning** (Automatic)
|
|
||||||
- User creates tenant via API
|
|
||||||
- `TenantProvisioningService.provisionTenant()` is called
|
|
||||||
- Database is created
|
|
||||||
- All migrations in `migrations/tenant/` are automatically run
|
|
||||||
- Tenant status set to ACTIVE
|
|
||||||
|
|
||||||
2. **Existing Tenants** (Manual)
|
|
||||||
- Developer creates new migration file
|
|
||||||
- Tests on single tenant: `npm run migrate:tenant test-tenant`
|
|
||||||
- Applies to all: `npm run migrate:all-tenants`
|
|
||||||
- Each tenant database is updated independently
|
|
||||||
|
|
||||||
### Migration Scripts
|
|
||||||
|
|
||||||
#### `migrate-tenant.ts`
|
|
||||||
- Accepts tenant slug or ID as argument
|
|
||||||
- Fetches tenant from central database
|
|
||||||
- Decrypts database password
|
|
||||||
- Creates Knex connection to tenant DB
|
|
||||||
- Runs pending migrations
|
|
||||||
- Reports success/failure
|
|
||||||
|
|
||||||
#### `migrate-all-tenants.ts`
|
|
||||||
- Fetches all ACTIVE tenants from central DB
|
|
||||||
- Iterates through each tenant
|
|
||||||
- Runs migrations sequentially
|
|
||||||
- Collects success/failure results
|
|
||||||
- Provides comprehensive summary
|
|
||||||
- Exits with error if any tenant fails
|
|
||||||
|
|
||||||
## Security
|
|
||||||
|
|
||||||
### Password Encryption
|
|
||||||
|
|
||||||
Tenant database passwords are encrypted using **AES-256-CBC** and stored in the central database.
|
|
||||||
|
|
||||||
**Required Environment Variable:**
|
|
||||||
```bash
|
|
||||||
DB_ENCRYPTION_KEY=your-32-character-secret-key!!
|
|
||||||
```
|
|
||||||
|
|
||||||
This key must:
|
|
||||||
- Be exactly 32 characters (256 bits)
|
|
||||||
- Match the key used by backend services
|
|
||||||
- Be kept secure (never commit to git)
|
|
||||||
- Be the same across all environments accessing tenant DBs
|
|
||||||
|
|
||||||
### Encryption Flow
|
|
||||||
|
|
||||||
```
|
|
||||||
Tenant Creation:
|
|
||||||
Plain Password → Encrypt → Store in Central DB
|
|
||||||
|
|
||||||
Migration Time:
|
|
||||||
Encrypted Password → Decrypt → Connect to Tenant DB → Run Migrations
|
|
||||||
```
|
|
||||||
|
|
||||||
## Example Workflow
|
|
||||||
|
|
||||||
### Adding a New Field to All Tenants
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# 1. Create migration
|
|
||||||
cd backend
|
|
||||||
npm run migrate:make add_priority_to_tasks
|
|
||||||
|
|
||||||
# 2. Edit the migration file
|
|
||||||
# migrations/tenant/20250127120000_add_priority_to_tasks.js
|
|
||||||
|
|
||||||
# 3. Test on staging tenant
|
|
||||||
npm run migrate:tenant staging-company
|
|
||||||
|
|
||||||
# 4. Verify it worked
|
|
||||||
# Connect to staging DB and check schema
|
|
||||||
|
|
||||||
# 5. Apply to all tenants
|
|
||||||
npm run migrate:all-tenants
|
|
||||||
```
|
|
||||||
|
|
||||||
Expected output:
|
|
||||||
```
|
|
||||||
🚀 Starting migration for all tenants...
|
|
||||||
|
|
||||||
📋 Found 5 active tenant(s)
|
|
||||||
|
|
||||||
🔄 Migrating tenant: Acme Corp (acme_corp_db)
|
|
||||||
✅ Acme Corp: Ran 1 migrations:
|
|
||||||
- 20250127120000_add_priority_to_tasks.js
|
|
||||||
|
|
||||||
🔄 Migrating tenant: TechStart (techstart_db)
|
|
||||||
✅ TechStart: Ran 1 migrations:
|
|
||||||
- 20250127120000_add_priority_to_tasks.js
|
|
||||||
|
|
||||||
...
|
|
||||||
|
|
||||||
============================================================
|
|
||||||
📊 Migration Summary
|
|
||||||
============================================================
|
|
||||||
✅ Successful: 5
|
|
||||||
❌ Failed: 0
|
|
||||||
|
|
||||||
🎉 All tenant migrations completed successfully!
|
|
||||||
```
|
|
||||||
|
|
||||||
## Troubleshooting
|
|
||||||
|
|
||||||
### Error: "Cannot find module '../prisma/generated-central/client'"
|
|
||||||
|
|
||||||
**Solution:** Generate Prisma client
|
|
||||||
```bash
|
|
||||||
cd backend
|
|
||||||
npx prisma generate --schema=prisma/schema-central.prisma
|
|
||||||
```
|
|
||||||
|
|
||||||
### Error: "Invalid encrypted password format"
|
|
||||||
|
|
||||||
**Solution:** Check `DB_ENCRYPTION_KEY` environment variable matches the one used for encryption.
|
|
||||||
|
|
||||||
### Error: "Migration failed: Table already exists"
|
|
||||||
|
|
||||||
**Cause:** Migration was partially applied or run manually
|
|
||||||
|
|
||||||
**Solution:**
|
|
||||||
```bash
|
|
||||||
# Check migration status in tenant DB
|
|
||||||
mysql -h <host> -u <user> -p<pass> <dbname> -e "SELECT * FROM knex_migrations"
|
|
||||||
|
|
||||||
# If migration is listed, it's already applied
|
|
||||||
# If not, investigate why table exists and fix manually
|
|
||||||
```
|
|
||||||
|
|
||||||
### Migration Hangs
|
|
||||||
|
|
||||||
**Possible causes:**
|
|
||||||
- Network connection to database lost
|
|
||||||
- Database server down
|
|
||||||
- Migration has long-running query
|
|
||||||
|
|
||||||
**Solution:** Add timeout to migration and check database connectivity
|
|
||||||
|
|
||||||
## Best Practices
|
|
||||||
|
|
||||||
1. ✅ **Test first**: Always test migrations on a single tenant before applying to all
|
|
||||||
2. ✅ **Rollback ready**: Write `down()` functions for every migration
|
|
||||||
3. ✅ **Idempotent**: Use `IF NOT EXISTS` clauses where possible
|
|
||||||
4. ✅ **Backup**: Take database backups before major migrations
|
|
||||||
5. ✅ **Monitor**: Watch the output of `migrate:all-tenants` carefully
|
|
||||||
6. ✅ **Version control**: Commit migration files to git
|
|
||||||
7. ✅ **Document**: Add comments explaining complex migrations
|
|
||||||
|
|
||||||
8. ❌ **Don't skip testing**: Never run untested migrations on production
|
|
||||||
9. ❌ **Don't modify**: Never modify existing migration files after they're deployed
|
|
||||||
10. ❌ **Don't forget down()**: Always implement rollback logic
|
|
||||||
|
|
||||||
## Integration with TenantProvisioningService
|
|
||||||
|
|
||||||
The migrations are also used during tenant provisioning:
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
// src/tenant/tenant-provisioning.service.ts
|
|
||||||
|
|
||||||
async provisionTenant(tenantId: string): Promise<void> {
|
|
||||||
// ... create database ...
|
|
||||||
|
|
||||||
// Run migrations automatically
|
|
||||||
await this.runTenantMigrations(tenant);
|
|
||||||
|
|
||||||
// ... update tenant status ...
|
|
||||||
}
|
|
||||||
|
|
||||||
async runTenantMigrations(tenant: any): Promise<void> {
|
|
||||||
const knexConfig = {
|
|
||||||
client: 'mysql2',
|
|
||||||
connection: {
|
|
||||||
host: tenant.dbHost,
|
|
||||||
port: tenant.dbPort,
|
|
||||||
user: tenant.dbUser,
|
|
||||||
password: decryptedPassword,
|
|
||||||
database: tenant.dbName,
|
|
||||||
},
|
|
||||||
migrations: {
|
|
||||||
directory: './migrations/tenant',
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const knexInstance = knex(knexConfig);
|
|
||||||
await knexInstance.migrate.latest();
|
|
||||||
await knexInstance.destroy();
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
This ensures every new tenant starts with the complete schema.
|
|
||||||
|
|
||||||
## CI/CD Integration
|
|
||||||
|
|
||||||
### Docker Compose
|
|
||||||
```yaml
|
|
||||||
services:
|
|
||||||
backend:
|
|
||||||
image: your-backend:latest
|
|
||||||
command: sh -c "npm run migrate:all-tenants && npm run start:prod"
|
|
||||||
environment:
|
|
||||||
- DB_ENCRYPTION_KEY=${DB_ENCRYPTION_KEY}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Kubernetes Job
|
|
||||||
```yaml
|
|
||||||
apiVersion: batch/v1
|
|
||||||
kind: Job
|
|
||||||
metadata:
|
|
||||||
name: tenant-migrations
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- name: migrate
|
|
||||||
image: your-backend:latest
|
|
||||||
command: ["npm", "run", "migrate:all-tenants"]
|
|
||||||
env:
|
|
||||||
- name: DB_ENCRYPTION_KEY
|
|
||||||
valueFrom:
|
|
||||||
secretKeyRef:
|
|
||||||
name: db-secrets
|
|
||||||
key: encryption-key
|
|
||||||
restartPolicy: OnFailure
|
|
||||||
```
|
|
||||||
|
|
||||||
## Further Documentation
|
|
||||||
|
|
||||||
- [Backend Scripts README](backend/scripts/README.md) - Detailed script documentation
|
|
||||||
- [Multi-Tenant Implementation](MULTI_TENANT_IMPLEMENTATION.md) - Architecture overview
|
|
||||||
- [Multi-Tenant Migration](MULTI_TENANT_MIGRATION.md) - Migration strategy
|
|
||||||
|
|
||||||
## Support
|
|
||||||
|
|
||||||
For questions or issues:
|
|
||||||
1. Check the [Backend Scripts README](backend/scripts/README.md)
|
|
||||||
2. Review existing migration files in `backend/migrations/tenant/`
|
|
||||||
3. Check Knex documentation: https://knexjs.org/guide/migrations.html
|
|
||||||
@@ -1,374 +0,0 @@
|
|||||||
# Tenant Migration Implementation - Complete
|
|
||||||
|
|
||||||
## ✅ Implementation Summary
|
|
||||||
|
|
||||||
All tenant migration functionality has been successfully added to the backend. This implementation provides comprehensive tools for managing database schema changes across all tenants in the multi-tenant platform.
|
|
||||||
|
|
||||||
## 📁 Files Created
|
|
||||||
|
|
||||||
### Scripts Directory: `/root/neo/backend/scripts/`
|
|
||||||
|
|
||||||
1. **`migrate-tenant.ts`** (167 lines)
|
|
||||||
- Migrates a single tenant by slug or ID
|
|
||||||
- Handles password decryption
|
|
||||||
- Provides detailed progress output
|
|
||||||
- Usage: `npm run migrate:tenant <slug-or-id>`
|
|
||||||
|
|
||||||
2. **`migrate-all-tenants.ts`** (170 lines)
|
|
||||||
- Migrates all active tenants in sequence
|
|
||||||
- Collects success/failure statistics
|
|
||||||
- Provides comprehensive summary
|
|
||||||
- Exits with error code if any tenant fails
|
|
||||||
- Usage: `npm run migrate:all-tenants`
|
|
||||||
|
|
||||||
3. **`check-migration-status.ts`** (181 lines)
|
|
||||||
- Checks migration status across all tenants
|
|
||||||
- Shows completed and pending migrations
|
|
||||||
- Identifies which tenants need updates
|
|
||||||
- Usage: `npm run migrate:status`
|
|
||||||
|
|
||||||
4. **`README.md`** (Comprehensive documentation)
|
|
||||||
- Detailed usage instructions
|
|
||||||
- Security notes on password encryption
|
|
||||||
- Troubleshooting guide
|
|
||||||
- Best practices
|
|
||||||
- Example workflows
|
|
||||||
|
|
||||||
### Documentation Files
|
|
||||||
|
|
||||||
5. **`/root/neo/TENANT_MIGRATION_GUIDE.md`** (Root level guide)
|
|
||||||
- Quick start guide
|
|
||||||
- Architecture diagrams
|
|
||||||
- Complete workflow examples
|
|
||||||
- CI/CD integration examples
|
|
||||||
- Security documentation
|
|
||||||
|
|
||||||
### Updated Files
|
|
||||||
|
|
||||||
6. **`/root/neo/backend/package.json`**
|
|
||||||
- Added 6 new migration scripts to the `scripts` section
|
|
||||||
|
|
||||||
## 🚀 Available Commands
|
|
||||||
|
|
||||||
| Command | Description |
|
|
||||||
|---------|-------------|
|
|
||||||
| `npm run migrate:make <name>` | Create a new migration file in `migrations/tenant/` |
|
|
||||||
| `npm run migrate:status` | Check migration status for all tenants |
|
|
||||||
| `npm run migrate:tenant <slug>` | Run pending migrations for a specific tenant |
|
|
||||||
| `npm run migrate:all-tenants` | Run pending migrations for all active tenants |
|
|
||||||
| `npm run migrate:latest` | Run migrations on default database (rarely used) |
|
|
||||||
| `npm run migrate:rollback` | Rollback last migration on default database |
|
|
||||||
|
|
||||||
## 🔧 How It Works
|
|
||||||
|
|
||||||
### Architecture
|
|
||||||
|
|
||||||
```
|
|
||||||
┌─────────────────────────────────────────────────────────┐
|
|
||||||
│ Central Database │
|
|
||||||
│ │
|
|
||||||
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
|
|
||||||
│ │ Tenant │ │ Tenant │ │ Tenant │ │
|
|
||||||
│ │ 1 │ │ 2 │ │ N │ │
|
|
||||||
│ └──────────┘ └──────────┘ └──────────┘ │
|
|
||||||
│ │ │ │ │
|
|
||||||
│ │ (encrypted │ │ │
|
|
||||||
│ │ password) │ │ │
|
|
||||||
└───────┼──────────────┼──────────────┼───────────────────┘
|
|
||||||
│ │ │
|
|
||||||
▼ ▼ ▼
|
|
||||||
┌───────────┐ ┌───────────┐ ┌───────────┐
|
|
||||||
│ Tenant │ │ Tenant │ │ Tenant │
|
|
||||||
│ DB 1 │ │ DB 2 │ │ DB N │
|
|
||||||
│ │ │ │ │ │
|
|
||||||
│ Migrations│ │ Migrations│ │ Migrations│
|
|
||||||
│ Applied │ │ Applied │ │ Applied │
|
|
||||||
└───────────┘ └───────────┘ └───────────┘
|
|
||||||
```
|
|
||||||
|
|
||||||
### Migration Flow
|
|
||||||
|
|
||||||
1. **Creating a Migration**
|
|
||||||
```bash
|
|
||||||
npm run migrate:make add_custom_fields
|
|
||||||
# Creates: migrations/tenant/20250127123456_add_custom_fields.js
|
|
||||||
```
|
|
||||||
|
|
||||||
2. **Testing on Single Tenant**
|
|
||||||
```bash
|
|
||||||
npm run migrate:tenant acme-corp
|
|
||||||
# Output:
|
|
||||||
# 📋 Tenant: Acme Corp (acme-corp)
|
|
||||||
# 📊 Database: acme_corp_db
|
|
||||||
# 🔄 Running migrations...
|
|
||||||
# ✅ Ran 1 migration(s):
|
|
||||||
# - 20250127123456_add_custom_fields.js
|
|
||||||
```
|
|
||||||
|
|
||||||
3. **Checking Status**
|
|
||||||
```bash
|
|
||||||
npm run migrate:status
|
|
||||||
# Shows which tenants have pending migrations
|
|
||||||
```
|
|
||||||
|
|
||||||
4. **Applying to All Tenants**
|
|
||||||
```bash
|
|
||||||
npm run migrate:all-tenants
|
|
||||||
# Migrates all active tenants sequentially
|
|
||||||
# Provides summary of successes/failures
|
|
||||||
```
|
|
||||||
|
|
||||||
## 🔐 Security Features
|
|
||||||
|
|
||||||
### Password Encryption
|
|
||||||
- Tenant database passwords are encrypted using **AES-256-CBC**
|
|
||||||
- Stored encrypted in central database
|
|
||||||
- Automatically decrypted during migration
|
|
||||||
- Requires `DB_ENCRYPTION_KEY` environment variable
|
|
||||||
|
|
||||||
### Environment Setup
|
|
||||||
```bash
|
|
||||||
# Required for migration scripts
|
|
||||||
export DB_ENCRYPTION_KEY="your-32-character-secret-key!!"
|
|
||||||
```
|
|
||||||
|
|
||||||
This key must match the key used by `TenantService` for encryption/decryption.
|
|
||||||
|
|
||||||
## 📋 Example Workflows
|
|
||||||
|
|
||||||
### Scenario 1: Adding a Field to All Tenants
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# 1. Create migration
|
|
||||||
npm run migrate:make add_priority_field
|
|
||||||
|
|
||||||
# 2. Edit the generated file
|
|
||||||
# migrations/tenant/20250127120000_add_priority_field.js
|
|
||||||
|
|
||||||
# 3. Test on one tenant
|
|
||||||
npm run migrate:tenant test-company
|
|
||||||
|
|
||||||
# 4. Check status
|
|
||||||
npm run migrate:status
|
|
||||||
|
|
||||||
# 5. Apply to all
|
|
||||||
npm run migrate:all-tenants
|
|
||||||
```
|
|
||||||
|
|
||||||
### Scenario 2: Checking Migration Status
|
|
||||||
|
|
||||||
```bash
|
|
||||||
npm run migrate:status
|
|
||||||
|
|
||||||
# Output:
|
|
||||||
# 📋 Found 3 active tenant(s)
|
|
||||||
#
|
|
||||||
# 📦 Acme Corp (acme-corp)
|
|
||||||
# Database: acme_corp_db
|
|
||||||
# Completed: 5 migration(s)
|
|
||||||
# ✅ Up to date
|
|
||||||
#
|
|
||||||
# 📦 TechStart (techstart)
|
|
||||||
# Database: techstart_db
|
|
||||||
# Completed: 4 migration(s)
|
|
||||||
# ⚠️ Pending: 1 migration(s)
|
|
||||||
# - 20250127120000_add_priority_field.js
|
|
||||||
#
|
|
||||||
# 💡 Run: npm run migrate:all-tenants
|
|
||||||
```
|
|
||||||
|
|
||||||
### Scenario 3: New Tenant Provisioning (Automatic)
|
|
||||||
|
|
||||||
When a new tenant is created via the API:
|
|
||||||
```typescript
|
|
||||||
// Happens automatically in TenantProvisioningService
|
|
||||||
POST /tenants
|
|
||||||
{
|
|
||||||
"name": "New Company",
|
|
||||||
"slug": "new-company"
|
|
||||||
}
|
|
||||||
|
|
||||||
// Backend automatically:
|
|
||||||
// 1. Creates database
|
|
||||||
// 2. Runs all migrations
|
|
||||||
// 3. Sets tenant status to ACTIVE
|
|
||||||
```
|
|
||||||
|
|
||||||
## 🛠️ Technical Implementation
|
|
||||||
|
|
||||||
### Script Structure
|
|
||||||
|
|
||||||
All scripts follow this pattern:
|
|
||||||
|
|
||||||
1. **Import Dependencies**
|
|
||||||
```typescript
|
|
||||||
import { PrismaClient as CentralPrismaClient } from '../prisma/generated-central/client';
|
|
||||||
import knex, { Knex } from 'knex';
|
|
||||||
import { createDecipheriv } from 'crypto';
|
|
||||||
```
|
|
||||||
|
|
||||||
2. **Decrypt Password**
|
|
||||||
```typescript
|
|
||||||
function decryptPassword(encryptedPassword: string): string {
|
|
||||||
// AES-256-CBC decryption
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
3. **Create Tenant Connection**
|
|
||||||
```typescript
|
|
||||||
function createTenantKnexConnection(tenant: any): Knex {
|
|
||||||
const decryptedPassword = decryptPassword(tenant.dbPassword);
|
|
||||||
return knex({ /* config */ });
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
4. **Run Migrations**
|
|
||||||
```typescript
|
|
||||||
const [batchNo, log] = await tenantKnex.migrate.latest();
|
|
||||||
```
|
|
||||||
|
|
||||||
5. **Report Results**
|
|
||||||
```typescript
|
|
||||||
console.log(`✅ Ran ${log.length} migrations`);
|
|
||||||
```
|
|
||||||
|
|
||||||
## 🧪 Testing the Implementation
|
|
||||||
|
|
||||||
### 1. Verify Scripts Are Available
|
|
||||||
```bash
|
|
||||||
cd /root/neo/backend
|
|
||||||
npm run | grep migrate
|
|
||||||
```
|
|
||||||
|
|
||||||
Expected output:
|
|
||||||
```
|
|
||||||
migrate:make
|
|
||||||
migrate:latest
|
|
||||||
migrate:rollback
|
|
||||||
migrate:status
|
|
||||||
migrate:tenant
|
|
||||||
migrate:all-tenants
|
|
||||||
```
|
|
||||||
|
|
||||||
### 2. Test Creating a Migration
|
|
||||||
```bash
|
|
||||||
npm run migrate:make test_migration
|
|
||||||
```
|
|
||||||
|
|
||||||
Should create a file in `migrations/tenant/`
|
|
||||||
|
|
||||||
### 3. Check Status (if tenants exist)
|
|
||||||
```bash
|
|
||||||
npm run migrate:status
|
|
||||||
```
|
|
||||||
|
|
||||||
### 4. Test Single Tenant Migration (if tenants exist)
|
|
||||||
```bash
|
|
||||||
npm run migrate:tenant <your-tenant-slug>
|
|
||||||
```
|
|
||||||
|
|
||||||
## 📚 Documentation Locations
|
|
||||||
|
|
||||||
- **Quick Reference**: `/root/neo/TENANT_MIGRATION_GUIDE.md`
|
|
||||||
- **Detailed Scripts Docs**: `/root/neo/backend/scripts/README.md`
|
|
||||||
- **Architecture Overview**: `/root/neo/MULTI_TENANT_IMPLEMENTATION.md`
|
|
||||||
|
|
||||||
## 🎯 Key Features
|
|
||||||
|
|
||||||
✅ **Single Tenant Migration** - Target specific tenants for testing
|
|
||||||
✅ **Bulk Migration** - Update all tenants at once
|
|
||||||
✅ **Status Checking** - See which tenants need updates
|
|
||||||
✅ **Progress Tracking** - Detailed output for each operation
|
|
||||||
✅ **Error Handling** - Graceful failure with detailed error messages
|
|
||||||
✅ **Security** - Encrypted password storage and decryption
|
|
||||||
✅ **Comprehensive Docs** - Multiple levels of documentation
|
|
||||||
|
|
||||||
## 🔄 Integration Points
|
|
||||||
|
|
||||||
### With Existing Code
|
|
||||||
|
|
||||||
1. **TenantProvisioningService**
|
|
||||||
- Already uses `runTenantMigrations()` method
|
|
||||||
- New scripts complement automatic provisioning
|
|
||||||
- Same migration directory: `migrations/tenant/`
|
|
||||||
|
|
||||||
2. **Knex Configuration**
|
|
||||||
- Uses existing `knexfile.js`
|
|
||||||
- Same migration table: `knex_migrations`
|
|
||||||
- Compatible with existing migrations
|
|
||||||
|
|
||||||
3. **Prisma Central Client**
|
|
||||||
- Scripts use central DB to fetch tenant list
|
|
||||||
- Same encryption/decryption logic as backend services
|
|
||||||
|
|
||||||
## 🚦 Next Steps
|
|
||||||
|
|
||||||
### To Use This Implementation:
|
|
||||||
|
|
||||||
1. **Ensure Environment Variables**
|
|
||||||
```bash
|
|
||||||
export DB_ENCRYPTION_KEY="your-32-character-secret-key!!"
|
|
||||||
```
|
|
||||||
|
|
||||||
2. **Generate Prisma Client** (if not already done)
|
|
||||||
```bash
|
|
||||||
cd /root/neo/backend
|
|
||||||
npx prisma generate --schema=prisma/schema-central.prisma
|
|
||||||
```
|
|
||||||
|
|
||||||
3. **Check Current Status**
|
|
||||||
```bash
|
|
||||||
npm run migrate:status
|
|
||||||
```
|
|
||||||
|
|
||||||
4. **Create Your First Migration**
|
|
||||||
```bash
|
|
||||||
npm run migrate:make add_my_feature
|
|
||||||
```
|
|
||||||
|
|
||||||
5. **Test and Apply**
|
|
||||||
```bash
|
|
||||||
# Test on one tenant
|
|
||||||
npm run migrate:tenant <slug>
|
|
||||||
|
|
||||||
# Apply to all
|
|
||||||
npm run migrate:all-tenants
|
|
||||||
```
|
|
||||||
|
|
||||||
## 📊 Complete File List
|
|
||||||
|
|
||||||
```
|
|
||||||
/root/neo/
|
|
||||||
├── TENANT_MIGRATION_GUIDE.md (new)
|
|
||||||
└── backend/
|
|
||||||
├── package.json (updated - 6 new scripts)
|
|
||||||
├── knexfile.js (existing)
|
|
||||||
├── migrations/
|
|
||||||
│ └── tenant/ (existing)
|
|
||||||
│ ├── 20250126000001_create_users_and_rbac.js
|
|
||||||
│ ├── 20250126000002_create_object_definitions.js
|
|
||||||
│ ├── 20250126000003_create_apps.js
|
|
||||||
│ ├── 20250126000004_create_standard_objects.js
|
|
||||||
│ └── 20250126000005_add_ui_metadata_to_fields.js
|
|
||||||
├── scripts/ (new directory)
|
|
||||||
│ ├── README.md (new)
|
|
||||||
│ ├── migrate-tenant.ts (new)
|
|
||||||
│ ├── migrate-all-tenants.ts (new)
|
|
||||||
│ └── check-migration-status.ts (new)
|
|
||||||
└── src/
|
|
||||||
└── tenant/
|
|
||||||
└── tenant-provisioning.service.ts (existing - uses migrations)
|
|
||||||
```
|
|
||||||
|
|
||||||
## ✨ Summary
|
|
||||||
|
|
||||||
The tenant migration system is now fully implemented with:
|
|
||||||
- ✅ 3 TypeScript migration scripts
|
|
||||||
- ✅ 6 npm commands
|
|
||||||
- ✅ 2 comprehensive documentation files
|
|
||||||
- ✅ Full integration with existing architecture
|
|
||||||
- ✅ Security features (password encryption)
|
|
||||||
- ✅ Error handling and progress reporting
|
|
||||||
- ✅ Status checking capabilities
|
|
||||||
|
|
||||||
You can now manage database migrations across all tenants efficiently and safely! 🎉
|
|
||||||
@@ -1,91 +0,0 @@
|
|||||||
╔══════════════════════════════════════════════════════════════════════╗
|
|
||||||
║ TENANT MIGRATION - QUICK REFERENCE ║
|
|
||||||
╚══════════════════════════════════════════════════════════════════════╝
|
|
||||||
|
|
||||||
📍 LOCATION: /root/neo/backend
|
|
||||||
|
|
||||||
┌─────────────────────────────────────────────────────────────────────┐
|
|
||||||
│ COMMON COMMANDS │
|
|
||||||
└─────────────────────────────────────────────────────────────────────┘
|
|
||||||
|
|
||||||
Create Migration:
|
|
||||||
$ npm run migrate:make add_my_feature
|
|
||||||
|
|
||||||
Check Status:
|
|
||||||
$ npm run migrate:status
|
|
||||||
|
|
||||||
Test on One Tenant:
|
|
||||||
$ npm run migrate:tenant acme-corp
|
|
||||||
|
|
||||||
Apply to All Tenants:
|
|
||||||
$ npm run migrate:all-tenants
|
|
||||||
|
|
||||||
|
|
||||||
┌─────────────────────────────────────────────────────────────────────┐
|
|
||||||
│ ALL AVAILABLE COMMANDS │
|
|
||||||
└─────────────────────────────────────────────────────────────────────┘
|
|
||||||
|
|
||||||
npm run migrate:make <name> Create new migration file
|
|
||||||
npm run migrate:status Check status across all tenants
|
|
||||||
npm run migrate:tenant <slug> Migrate specific tenant
|
|
||||||
npm run migrate:all-tenants Migrate all active tenants
|
|
||||||
npm run migrate:latest Migrate default DB (rarely used)
|
|
||||||
npm run migrate:rollback Rollback default DB (rarely used)
|
|
||||||
|
|
||||||
|
|
||||||
┌─────────────────────────────────────────────────────────────────────┐
|
|
||||||
│ TYPICAL WORKFLOW │
|
|
||||||
└─────────────────────────────────────────────────────────────────────┘
|
|
||||||
|
|
||||||
1. Create: npm run migrate:make add_priority_field
|
|
||||||
2. Edit: vim migrations/tenant/20250127_*.js
|
|
||||||
3. Test: npm run migrate:tenant test-company
|
|
||||||
4. Status: npm run migrate:status
|
|
||||||
5. Deploy: npm run migrate:all-tenants
|
|
||||||
|
|
||||||
|
|
||||||
┌─────────────────────────────────────────────────────────────────────┐
|
|
||||||
│ ENVIRONMENT REQUIRED │
|
|
||||||
└─────────────────────────────────────────────────────────────────────┘
|
|
||||||
|
|
||||||
export DB_ENCRYPTION_KEY="your-32-character-secret-key!!"
|
|
||||||
|
|
||||||
|
|
||||||
┌─────────────────────────────────────────────────────────────────────┐
|
|
||||||
│ FILE LOCATIONS │
|
|
||||||
└─────────────────────────────────────────────────────────────────────┘
|
|
||||||
|
|
||||||
Scripts: backend/scripts/migrate-*.ts
|
|
||||||
Migrations: backend/migrations/tenant/
|
|
||||||
Config: backend/knexfile.js
|
|
||||||
Docs: TENANT_MIGRATION_GUIDE.md
|
|
||||||
|
|
||||||
|
|
||||||
┌─────────────────────────────────────────────────────────────────────┐
|
|
||||||
│ DOCUMENTATION │
|
|
||||||
└─────────────────────────────────────────────────────────────────────┘
|
|
||||||
|
|
||||||
Quick Guide: cat TENANT_MIGRATION_GUIDE.md
|
|
||||||
Script Docs: cat backend/scripts/README.md
|
|
||||||
Complete: cat TENANT_MIGRATION_IMPLEMENTATION_COMPLETE.md
|
|
||||||
|
|
||||||
|
|
||||||
┌─────────────────────────────────────────────────────────────────────┐
|
|
||||||
│ TROUBLESHOOTING │
|
|
||||||
└─────────────────────────────────────────────────────────────────────┘
|
|
||||||
|
|
||||||
Missing Prisma Client:
|
|
||||||
$ npx prisma generate --schema=prisma/schema-central.prisma
|
|
||||||
|
|
||||||
Check Scripts Available:
|
|
||||||
$ npm run | grep migrate
|
|
||||||
|
|
||||||
Connection Error:
|
|
||||||
- Check DB_ENCRYPTION_KEY matches encryption key
|
|
||||||
- Verify central database is accessible
|
|
||||||
- Ensure tenant databases are online
|
|
||||||
|
|
||||||
|
|
||||||
╔══════════════════════════════════════════════════════════════════════╗
|
|
||||||
║ For detailed help: cat TENANT_MIGRATION_GUIDE.md ║
|
|
||||||
╚══════════════════════════════════════════════════════════════════════╝
|
|
||||||
@@ -17,13 +17,7 @@
|
|||||||
"test:watch": "jest --watch",
|
"test:watch": "jest --watch",
|
||||||
"test:cov": "jest --coverage",
|
"test:cov": "jest --coverage",
|
||||||
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
|
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
|
||||||
"test:e2e": "jest --config ./test/jest-e2e.json",
|
"test:e2e": "jest --config ./test/jest-e2e.json"
|
||||||
"migrate:make": "knex migrate:make --knexfile=knexfile.js",
|
|
||||||
"migrate:latest": "knex migrate:latest --knexfile=knexfile.js",
|
|
||||||
"migrate:rollback": "knex migrate:rollback --knexfile=knexfile.js",
|
|
||||||
"migrate:status": "ts-node -r tsconfig-paths/register scripts/check-migration-status.ts",
|
|
||||||
"migrate:tenant": "ts-node -r tsconfig-paths/register scripts/migrate-tenant.ts",
|
|
||||||
"migrate:all-tenants": "ts-node -r tsconfig-paths/register scripts/migrate-all-tenants.ts"
|
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@nestjs/bullmq": "^10.1.0",
|
"@nestjs/bullmq": "^10.1.0",
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
-- CreateTable
|
||||||
|
CREATE TABLE `users` (
|
||||||
|
`id` VARCHAR(191) NOT NULL,
|
||||||
|
`email` VARCHAR(191) NOT NULL,
|
||||||
|
`password` VARCHAR(191) NOT NULL,
|
||||||
|
`firstName` VARCHAR(191) NULL,
|
||||||
|
`lastName` VARCHAR(191) NULL,
|
||||||
|
`role` VARCHAR(191) NOT NULL DEFAULT 'admin',
|
||||||
|
`isActive` BOOLEAN NOT NULL DEFAULT true,
|
||||||
|
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||||
|
`updatedAt` DATETIME(3) NOT NULL,
|
||||||
|
|
||||||
|
UNIQUE INDEX `users_email_key`(`email`),
|
||||||
|
PRIMARY KEY (`id`)
|
||||||
|
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||||
@@ -8,6 +8,20 @@ datasource db {
|
|||||||
url = env("CENTRAL_DATABASE_URL")
|
url = env("CENTRAL_DATABASE_URL")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
model User {
|
||||||
|
id String @id @default(cuid())
|
||||||
|
email String @unique
|
||||||
|
password String
|
||||||
|
firstName String?
|
||||||
|
lastName String?
|
||||||
|
role String @default("admin") // admin, superadmin
|
||||||
|
isActive Boolean @default(true)
|
||||||
|
createdAt DateTime @default(now())
|
||||||
|
updatedAt DateTime @updatedAt
|
||||||
|
|
||||||
|
@@map("users")
|
||||||
|
}
|
||||||
|
|
||||||
model Tenant {
|
model Tenant {
|
||||||
id String @id @default(cuid())
|
id String @id @default(cuid())
|
||||||
name String
|
name String
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
// Tenant-specific database schema
|
// Tenant-specific database schema
|
||||||
// This schema is applied to each tenant's database
|
// This schema is applied to each tenant's database
|
||||||
// NOTE: Each tenant has its own database, so there is NO tenantId column in these tables
|
|
||||||
|
|
||||||
generator client {
|
generator client {
|
||||||
provider = "prisma-client-js"
|
provider = "prisma-client-js"
|
||||||
@@ -12,10 +11,30 @@ datasource db {
|
|||||||
url = env("TENANT_DATABASE_URL")
|
url = env("TENANT_DATABASE_URL")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Multi-tenancy
|
||||||
|
model Tenant {
|
||||||
|
id String @id @default(uuid())
|
||||||
|
name String
|
||||||
|
slug String @unique
|
||||||
|
isActive Boolean @default(true)
|
||||||
|
createdAt DateTime @default(now())
|
||||||
|
updatedAt DateTime @updatedAt
|
||||||
|
|
||||||
|
users User[]
|
||||||
|
objectDefinitions ObjectDefinition[]
|
||||||
|
accounts Account[]
|
||||||
|
apps App[]
|
||||||
|
roles Role[]
|
||||||
|
permissions Permission[]
|
||||||
|
|
||||||
|
@@map("tenants")
|
||||||
|
}
|
||||||
|
|
||||||
// User & Auth
|
// User & Auth
|
||||||
model User {
|
model User {
|
||||||
id String @id @default(uuid())
|
id String @id @default(uuid())
|
||||||
email String @unique
|
tenantId String
|
||||||
|
email String
|
||||||
password String
|
password String
|
||||||
firstName String?
|
firstName String?
|
||||||
lastName String?
|
lastName String?
|
||||||
@@ -23,39 +42,48 @@ model User {
|
|||||||
createdAt DateTime @default(now())
|
createdAt DateTime @default(now())
|
||||||
updatedAt DateTime @updatedAt
|
updatedAt DateTime @updatedAt
|
||||||
|
|
||||||
|
tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
|
||||||
userRoles UserRole[]
|
userRoles UserRole[]
|
||||||
accounts Account[]
|
accounts Account[]
|
||||||
|
|
||||||
|
@@unique([tenantId, email])
|
||||||
|
@@index([tenantId])
|
||||||
@@map("users")
|
@@map("users")
|
||||||
}
|
}
|
||||||
|
|
||||||
// RBAC - Spatie-like
|
// RBAC - Spatie-like
|
||||||
model Role {
|
model Role {
|
||||||
id String @id @default(uuid())
|
id String @id @default(uuid())
|
||||||
|
tenantId String
|
||||||
name String
|
name String
|
||||||
guardName String @default("api")
|
guardName String @default("api")
|
||||||
description String?
|
description String?
|
||||||
createdAt DateTime @default(now())
|
createdAt DateTime @default(now())
|
||||||
updatedAt DateTime @updatedAt
|
updatedAt DateTime @updatedAt
|
||||||
|
|
||||||
|
tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
|
||||||
userRoles UserRole[]
|
userRoles UserRole[]
|
||||||
rolePermissions RolePermission[]
|
rolePermissions RolePermission[]
|
||||||
|
|
||||||
@@unique([name, guardName])
|
@@unique([tenantId, name, guardName])
|
||||||
|
@@index([tenantId])
|
||||||
@@map("roles")
|
@@map("roles")
|
||||||
}
|
}
|
||||||
|
|
||||||
model Permission {
|
model Permission {
|
||||||
id String @id @default(uuid())
|
id String @id @default(uuid())
|
||||||
|
tenantId String
|
||||||
name String
|
name String
|
||||||
guardName String @default("api")
|
guardName String @default("api")
|
||||||
description String?
|
description String?
|
||||||
createdAt DateTime @default(now())
|
createdAt DateTime @default(now())
|
||||||
updatedAt DateTime @updatedAt
|
updatedAt DateTime @updatedAt
|
||||||
|
|
||||||
|
tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
|
||||||
rolePermissions RolePermission[]
|
rolePermissions RolePermission[]
|
||||||
|
|
||||||
@@unique([name, guardName])
|
@@unique([tenantId, name, guardName])
|
||||||
|
@@index([tenantId])
|
||||||
@@map("permissions")
|
@@map("permissions")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,59 +120,66 @@ model RolePermission {
|
|||||||
// Object Definition (Metadata)
|
// Object Definition (Metadata)
|
||||||
model ObjectDefinition {
|
model ObjectDefinition {
|
||||||
id String @id @default(uuid())
|
id String @id @default(uuid())
|
||||||
apiName String @unique
|
tenantId String
|
||||||
|
apiName String
|
||||||
label String
|
label String
|
||||||
pluralLabel String?
|
pluralLabel String?
|
||||||
description String? @db.Text
|
description String? @db.Text
|
||||||
isSystem Boolean @default(false)
|
isSystem Boolean @default(false)
|
||||||
isCustom Boolean @default(true)
|
tableName String?
|
||||||
createdAt DateTime @default(now()) @map("created_at")
|
isActive Boolean @default(true)
|
||||||
updatedAt DateTime @updatedAt @map("updated_at")
|
createdAt DateTime @default(now())
|
||||||
|
updatedAt DateTime @updatedAt
|
||||||
|
|
||||||
|
tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
|
||||||
fields FieldDefinition[]
|
fields FieldDefinition[]
|
||||||
pages AppPage[]
|
pages AppPage[]
|
||||||
|
|
||||||
|
@@unique([tenantId, apiName])
|
||||||
|
@@index([tenantId])
|
||||||
@@map("object_definitions")
|
@@map("object_definitions")
|
||||||
}
|
}
|
||||||
|
|
||||||
model FieldDefinition {
|
model FieldDefinition {
|
||||||
id String @id @default(uuid())
|
id String @id @default(uuid())
|
||||||
objectDefinitionId String
|
objectId String
|
||||||
apiName String
|
apiName String
|
||||||
label String
|
label String
|
||||||
type String // String, Number, Date, Boolean, Reference, etc.
|
type String // text, number, boolean, date, datetime, lookup, picklist, etc.
|
||||||
length Int?
|
description String? @db.Text
|
||||||
precision Int?
|
isRequired Boolean @default(false)
|
||||||
scale Int?
|
isUnique Boolean @default(false)
|
||||||
referenceObject String?
|
isReadonly Boolean @default(false)
|
||||||
defaultValue String? @db.Text
|
isLookup Boolean @default(false)
|
||||||
description String? @db.Text
|
referenceTo String? // objectApiName for lookup fields
|
||||||
isRequired Boolean @default(false)
|
defaultValue String?
|
||||||
isUnique Boolean @default(false)
|
options Json? // for picklist fields
|
||||||
isSystem Boolean @default(false)
|
validationRules Json? // custom validation rules
|
||||||
isCustom Boolean @default(true)
|
isActive Boolean @default(true)
|
||||||
displayOrder Int @default(0)
|
createdAt DateTime @default(now())
|
||||||
createdAt DateTime @default(now()) @map("created_at")
|
updatedAt DateTime @updatedAt
|
||||||
updatedAt DateTime @updatedAt @map("updated_at")
|
|
||||||
|
|
||||||
object ObjectDefinition @relation(fields: [objectDefinitionId], references: [id], onDelete: Cascade)
|
object ObjectDefinition @relation(fields: [objectId], references: [id], onDelete: Cascade)
|
||||||
|
|
||||||
@@unique([objectDefinitionId, apiName])
|
@@unique([objectId, apiName])
|
||||||
@@index([objectDefinitionId])
|
@@index([objectId])
|
||||||
@@map("field_definitions")
|
@@map("field_definitions")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Example static object: Account
|
// Example static object: Account
|
||||||
model Account {
|
model Account {
|
||||||
id String @id @default(uuid())
|
id String @id @default(uuid())
|
||||||
|
tenantId String
|
||||||
name String
|
name String
|
||||||
status String @default("active")
|
status String @default("active")
|
||||||
ownerId String
|
ownerId String
|
||||||
createdAt DateTime @default(now())
|
createdAt DateTime @default(now())
|
||||||
updatedAt DateTime @updatedAt
|
updatedAt DateTime @updatedAt
|
||||||
|
|
||||||
|
tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
|
||||||
owner User @relation(fields: [ownerId], references: [id])
|
owner User @relation(fields: [ownerId], references: [id])
|
||||||
|
|
||||||
|
@@index([tenantId])
|
||||||
@@index([ownerId])
|
@@index([ownerId])
|
||||||
@@map("accounts")
|
@@map("accounts")
|
||||||
}
|
}
|
||||||
@@ -152,7 +187,8 @@ model Account {
|
|||||||
// Application Builder
|
// Application Builder
|
||||||
model App {
|
model App {
|
||||||
id String @id @default(uuid())
|
id String @id @default(uuid())
|
||||||
slug String @unique
|
tenantId String
|
||||||
|
slug String
|
||||||
label String
|
label String
|
||||||
description String? @db.Text
|
description String? @db.Text
|
||||||
icon String?
|
icon String?
|
||||||
@@ -160,8 +196,11 @@ model App {
|
|||||||
createdAt DateTime @default(now())
|
createdAt DateTime @default(now())
|
||||||
updatedAt DateTime @updatedAt
|
updatedAt DateTime @updatedAt
|
||||||
|
|
||||||
|
tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
|
||||||
pages AppPage[]
|
pages AppPage[]
|
||||||
|
|
||||||
|
@@unique([tenantId, slug])
|
||||||
|
@@index([tenantId])
|
||||||
@@map("apps")
|
@@map("apps")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,194 +0,0 @@
|
|||||||
# Tenant Migration Scripts
|
|
||||||
|
|
||||||
This directory contains scripts for managing database migrations across all tenants in the multi-tenant platform.
|
|
||||||
|
|
||||||
## Available Scripts
|
|
||||||
|
|
||||||
### 1. Create a New Migration
|
|
||||||
|
|
||||||
```bash
|
|
||||||
npm run migrate:make <migration_name>
|
|
||||||
```
|
|
||||||
|
|
||||||
Creates a new migration file in `migrations/tenant/` directory.
|
|
||||||
|
|
||||||
**Example:**
|
|
||||||
```bash
|
|
||||||
npm run migrate:make add_status_field_to_contacts
|
|
||||||
```
|
|
||||||
|
|
||||||
### 2. Migrate a Single Tenant
|
|
||||||
|
|
||||||
```bash
|
|
||||||
npm run migrate:tenant <tenant-slug-or-id>
|
|
||||||
```
|
|
||||||
|
|
||||||
Runs all pending migrations for a specific tenant. You can identify the tenant by its slug or ID.
|
|
||||||
|
|
||||||
**Example:**
|
|
||||||
```bash
|
|
||||||
npm run migrate:tenant acme-corp
|
|
||||||
npm run migrate:tenant cm5a1b2c3d4e5f6g7h8i9j0k
|
|
||||||
```
|
|
||||||
|
|
||||||
### 3. Migrate All Tenants
|
|
||||||
|
|
||||||
```bash
|
|
||||||
npm run migrate:all-tenants
|
|
||||||
```
|
|
||||||
|
|
||||||
Runs all pending migrations for **all active tenants** in the system. This is useful when:
|
|
||||||
- You've created a new migration that needs to be applied to all tenants
|
|
||||||
- You're updating the schema across the entire platform
|
|
||||||
- You need to ensure all tenants are up to date
|
|
||||||
|
|
||||||
**Output:**
|
|
||||||
- Shows progress for each tenant
|
|
||||||
- Lists which migrations were applied
|
|
||||||
- Provides a summary at the end
|
|
||||||
- Exits with error code if any tenant fails
|
|
||||||
|
|
||||||
### 4. Rollback Migration (Manual)
|
|
||||||
|
|
||||||
```bash
|
|
||||||
npm run migrate:rollback
|
|
||||||
```
|
|
||||||
|
|
||||||
⚠️ **Warning:** This runs a rollback on the **default database** configured in `knexfile.js`. For tenant-specific rollbacks, you'll need to manually configure the connection.
|
|
||||||
|
|
||||||
## Migration Flow
|
|
||||||
|
|
||||||
### During New Tenant Provisioning
|
|
||||||
|
|
||||||
When a new tenant is created via the API, migrations are automatically run as part of the provisioning process:
|
|
||||||
|
|
||||||
1. Tenant database is created
|
|
||||||
2. `TenantProvisioningService.runTenantMigrations()` is called
|
|
||||||
3. All migrations in `migrations/tenant/` are executed
|
|
||||||
|
|
||||||
### For Existing Tenants
|
|
||||||
|
|
||||||
When you add a new migration file and need to apply it to existing tenants:
|
|
||||||
|
|
||||||
1. Create the migration:
|
|
||||||
```bash
|
|
||||||
npm run migrate:make add_new_feature
|
|
||||||
```
|
|
||||||
|
|
||||||
2. Edit the generated migration file in `migrations/tenant/`
|
|
||||||
|
|
||||||
3. Test on a single tenant first:
|
|
||||||
```bash
|
|
||||||
npm run migrate:tenant test-tenant
|
|
||||||
```
|
|
||||||
|
|
||||||
4. If successful, apply to all tenants:
|
|
||||||
```bash
|
|
||||||
npm run migrate:all-tenants
|
|
||||||
```
|
|
||||||
|
|
||||||
## Migration Directory Structure
|
|
||||||
|
|
||||||
```
|
|
||||||
backend/
|
|
||||||
├── migrations/
|
|
||||||
│ └── tenant/ # Tenant-specific migrations
|
|
||||||
│ ├── 20250126000001_create_users_and_rbac.js
|
|
||||||
│ ├── 20250126000002_create_object_definitions.js
|
|
||||||
│ └── ...
|
|
||||||
├── scripts/
|
|
||||||
│ ├── migrate-tenant.ts # Single tenant migration
|
|
||||||
│ └── migrate-all-tenants.ts # All tenants migration
|
|
||||||
└── knexfile.js # Knex configuration
|
|
||||||
```
|
|
||||||
|
|
||||||
## Security Notes
|
|
||||||
|
|
||||||
### Database Password Encryption
|
|
||||||
|
|
||||||
Tenant database passwords are encrypted in the central database using AES-256-CBC encryption. The migration scripts automatically:
|
|
||||||
|
|
||||||
1. Fetch tenant connection details from the central database
|
|
||||||
2. Decrypt the database password using the `DB_ENCRYPTION_KEY` environment variable
|
|
||||||
3. Connect to the tenant database
|
|
||||||
4. Run migrations
|
|
||||||
5. Close the connection
|
|
||||||
|
|
||||||
**Required Environment Variable:**
|
|
||||||
```bash
|
|
||||||
DB_ENCRYPTION_KEY=your-32-character-secret-key!!
|
|
||||||
```
|
|
||||||
|
|
||||||
This key must match the key used by `TenantService` for encryption.
|
|
||||||
|
|
||||||
## Troubleshooting
|
|
||||||
|
|
||||||
### Migration Fails for One Tenant
|
|
||||||
|
|
||||||
If `migrate:all-tenants` fails for a specific tenant:
|
|
||||||
|
|
||||||
1. Check the error message in the output
|
|
||||||
2. Investigate the tenant's database directly
|
|
||||||
3. Fix the issue (manual SQL, data cleanup, etc.)
|
|
||||||
4. Re-run migrations for that tenant: `npm run migrate:tenant <slug>`
|
|
||||||
5. Once fixed, run `migrate:all-tenants` again to ensure others are updated
|
|
||||||
|
|
||||||
### Migration Already Exists
|
|
||||||
|
|
||||||
Knex tracks which migrations have been run in the `knex_migrations` table in each tenant database. If a migration was already applied, it will be skipped automatically.
|
|
||||||
|
|
||||||
### Connection Issues
|
|
||||||
|
|
||||||
If you see connection errors:
|
|
||||||
|
|
||||||
1. Verify the central database is accessible
|
|
||||||
2. Check that tenant database credentials are correct
|
|
||||||
3. Ensure `DB_ENCRYPTION_KEY` matches the one used for encryption
|
|
||||||
4. Verify the tenant's database server is running and accessible
|
|
||||||
|
|
||||||
## Example Migration File
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
// migrations/tenant/20250126000006_add_custom_fields.js
|
|
||||||
|
|
||||||
exports.up = async function(knex) {
|
|
||||||
await knex.schema.table('field_definitions', (table) => {
|
|
||||||
table.boolean('is_custom').defaultTo(false);
|
|
||||||
table.string('custom_type', 50).nullable();
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.down = async function(knex) {
|
|
||||||
await knex.schema.table('field_definitions', (table) => {
|
|
||||||
table.dropColumn('is_custom');
|
|
||||||
table.dropColumn('custom_type');
|
|
||||||
});
|
|
||||||
};
|
|
||||||
```
|
|
||||||
|
|
||||||
## Best Practices
|
|
||||||
|
|
||||||
1. **Always test on a single tenant first** before running migrations on all tenants
|
|
||||||
2. **Include rollback logic** in your `down()` function
|
|
||||||
3. **Use transactions** for complex multi-step migrations
|
|
||||||
4. **Backup production databases** before running migrations
|
|
||||||
5. **Monitor the output** when running `migrate:all-tenants` to catch any failures
|
|
||||||
6. **Version control** your migration files
|
|
||||||
7. **Document breaking changes** in migration comments
|
|
||||||
8. **Consider data migrations** separately from schema migrations when dealing with large datasets
|
|
||||||
|
|
||||||
## CI/CD Integration
|
|
||||||
|
|
||||||
In your deployment pipeline, you can automatically migrate all tenants:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# After deploying new code
|
|
||||||
npm run migrate:all-tenants
|
|
||||||
```
|
|
||||||
|
|
||||||
Or integrate it into your Docker deployment:
|
|
||||||
|
|
||||||
```dockerfile
|
|
||||||
# In your Dockerfile or docker-compose.yml
|
|
||||||
CMD npm run migrate:all-tenants && npm run start:prod
|
|
||||||
```
|
|
||||||
@@ -1,181 +0,0 @@
|
|||||||
import { PrismaClient as CentralPrismaClient } from '.prisma/central';
|
|
||||||
import knex, { Knex } from 'knex';
|
|
||||||
import { createDecipheriv } from 'crypto';
|
|
||||||
|
|
||||||
// Encryption configuration
|
|
||||||
const ALGORITHM = 'aes-256-cbc';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Decrypt a tenant's database password
|
|
||||||
*/
|
|
||||||
function decryptPassword(encryptedPassword: string): string {
|
|
||||||
try {
|
|
||||||
// Check if password is already plaintext (for legacy/development)
|
|
||||||
if (!encryptedPassword.includes(':')) {
|
|
||||||
return encryptedPassword;
|
|
||||||
}
|
|
||||||
|
|
||||||
const key = Buffer.from(process.env.ENCRYPTION_KEY, 'hex');
|
|
||||||
const parts = encryptedPassword.split(':');
|
|
||||||
if (parts.length !== 2) {
|
|
||||||
throw new Error('Invalid encrypted password format');
|
|
||||||
}
|
|
||||||
|
|
||||||
const iv = Buffer.from(parts[0], 'hex');
|
|
||||||
const encrypted = parts[1];
|
|
||||||
|
|
||||||
const decipher = createDecipheriv(ALGORITHM, key, iv);
|
|
||||||
|
|
||||||
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
|
|
||||||
decrypted += decipher.final('utf8');
|
|
||||||
|
|
||||||
return decrypted;
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error decrypting password:', error);
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a Knex connection for a specific tenant
|
|
||||||
*/
|
|
||||||
function createTenantKnexConnection(tenant: any): Knex {
|
|
||||||
const decryptedPassword = decryptPassword(tenant.dbPassword);
|
|
||||||
|
|
||||||
return knex({
|
|
||||||
client: 'mysql2',
|
|
||||||
connection: {
|
|
||||||
host: tenant.dbHost,
|
|
||||||
port: tenant.dbPort,
|
|
||||||
user: tenant.dbUsername,
|
|
||||||
password: decryptedPassword,
|
|
||||||
database: tenant.dbName,
|
|
||||||
},
|
|
||||||
migrations: {
|
|
||||||
tableName: 'knex_migrations',
|
|
||||||
directory: './migrations/tenant',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get migration status for a specific tenant
|
|
||||||
*/
|
|
||||||
async function getTenantMigrationStatus(tenant: any): Promise<{
|
|
||||||
completed: string[];
|
|
||||||
pending: string[];
|
|
||||||
}> {
|
|
||||||
const tenantKnex = createTenantKnexConnection(tenant);
|
|
||||||
|
|
||||||
try {
|
|
||||||
const [completed, pending] = await tenantKnex.migrate.list();
|
|
||||||
return {
|
|
||||||
completed: completed[1] || [],
|
|
||||||
pending: pending || [],
|
|
||||||
};
|
|
||||||
} catch (error) {
|
|
||||||
throw error;
|
|
||||||
} finally {
|
|
||||||
await tenantKnex.destroy();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check migration status across all tenants
|
|
||||||
*/
|
|
||||||
async function checkMigrationStatus() {
|
|
||||||
console.log('🔍 Checking migration status for all tenants...\n');
|
|
||||||
|
|
||||||
const centralPrisma = new CentralPrismaClient();
|
|
||||||
|
|
||||||
try {
|
|
||||||
// Fetch all active tenants
|
|
||||||
const tenants = await centralPrisma.tenant.findMany({
|
|
||||||
where: {
|
|
||||||
status: 'ACTIVE',
|
|
||||||
},
|
|
||||||
orderBy: {
|
|
||||||
name: 'asc',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (tenants.length === 0) {
|
|
||||||
console.log('⚠️ No active tenants found.');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(`📋 Found ${tenants.length} active tenant(s)\n`);
|
|
||||||
console.log('='.repeat(80));
|
|
||||||
|
|
||||||
let allUpToDate = true;
|
|
||||||
const tenantsWithPending: { name: string; pending: string[] }[] = [];
|
|
||||||
|
|
||||||
// Check each tenant
|
|
||||||
for (const tenant of tenants) {
|
|
||||||
try {
|
|
||||||
const status = await getTenantMigrationStatus(tenant);
|
|
||||||
|
|
||||||
console.log(`\n📦 ${tenant.name} (${tenant.slug})`);
|
|
||||||
console.log(` Database: ${tenant.dbName}`);
|
|
||||||
console.log(` Completed: ${status.completed.length} migration(s)`);
|
|
||||||
|
|
||||||
if (status.pending.length > 0) {
|
|
||||||
allUpToDate = false;
|
|
||||||
console.log(` ⚠️ Pending: ${status.pending.length} migration(s)`);
|
|
||||||
status.pending.forEach((migration) => {
|
|
||||||
console.log(` - ${migration}`);
|
|
||||||
});
|
|
||||||
tenantsWithPending.push({
|
|
||||||
name: tenant.name,
|
|
||||||
pending: status.pending,
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
console.log(` ✅ Up to date`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Show last 3 completed migrations
|
|
||||||
if (status.completed.length > 0) {
|
|
||||||
const recent = status.completed.slice(-3);
|
|
||||||
console.log(` Recent migrations:`);
|
|
||||||
recent.forEach((migration) => {
|
|
||||||
console.log(` - ${migration}`);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.log(`\n❌ ${tenant.name}: Failed to check status`);
|
|
||||||
console.log(` Error: ${error.message}`);
|
|
||||||
allUpToDate = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print summary
|
|
||||||
console.log('\n' + '='.repeat(80));
|
|
||||||
console.log('📊 Summary');
|
|
||||||
console.log('='.repeat(80));
|
|
||||||
|
|
||||||
if (allUpToDate) {
|
|
||||||
console.log('✅ All tenants are up to date!');
|
|
||||||
} else {
|
|
||||||
console.log(`⚠️ ${tenantsWithPending.length} tenant(s) have pending migrations:\n`);
|
|
||||||
tenantsWithPending.forEach(({ name, pending }) => {
|
|
||||||
console.log(` ${name}: ${pending.length} pending`);
|
|
||||||
});
|
|
||||||
console.log('\n💡 Run: npm run migrate:all-tenants');
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error('❌ Fatal error:', error);
|
|
||||||
process.exit(1);
|
|
||||||
} finally {
|
|
||||||
await centralPrisma.$disconnect();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run the status check
|
|
||||||
checkMigrationStatus()
|
|
||||||
.then(() => {
|
|
||||||
process.exit(0);
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
console.error('Unhandled error:', error);
|
|
||||||
process.exit(1);
|
|
||||||
});
|
|
||||||
50
backend/scripts/create-admin-user.ts
Normal file
50
backend/scripts/create-admin-user.ts
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
import { PrismaClient as CentralPrismaClient } from '../node_modules/.prisma/central';
|
||||||
|
import * as bcrypt from 'bcrypt';
|
||||||
|
|
||||||
|
// Central database client
|
||||||
|
const centralPrisma = new CentralPrismaClient();
|
||||||
|
|
||||||
|
async function createAdminUser() {
|
||||||
|
const email = 'admin@example.com';
|
||||||
|
const password = 'admin123';
|
||||||
|
const firstName = 'Admin';
|
||||||
|
const lastName = 'User';
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Check if admin user already exists
|
||||||
|
const existingUser = await centralPrisma.user.findUnique({
|
||||||
|
where: { email },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (existingUser) {
|
||||||
|
console.log(`User ${email} already exists`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hash password
|
||||||
|
const hashedPassword = await bcrypt.hash(password, 10);
|
||||||
|
|
||||||
|
// Create admin user in central database
|
||||||
|
const user = await centralPrisma.user.create({
|
||||||
|
data: {
|
||||||
|
email,
|
||||||
|
password: hashedPassword,
|
||||||
|
firstName,
|
||||||
|
lastName,
|
||||||
|
role: 'superadmin',
|
||||||
|
isActive: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('\nAdmin user created successfully!');
|
||||||
|
console.log('Email:', email);
|
||||||
|
console.log('Password:', password);
|
||||||
|
console.log('User ID:', user.id);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error creating admin user:', error);
|
||||||
|
} finally {
|
||||||
|
await centralPrisma.$disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
createAdminUser();
|
||||||
138
backend/scripts/create-tenant-user.ts
Normal file
138
backend/scripts/create-tenant-user.ts
Normal file
@@ -0,0 +1,138 @@
|
|||||||
|
import { PrismaClient as CentralPrismaClient } from '../node_modules/.prisma/central';
|
||||||
|
import * as bcrypt from 'bcrypt';
|
||||||
|
import { Knex, knex } from 'knex';
|
||||||
|
|
||||||
|
// Central database client
|
||||||
|
const centralPrisma = new CentralPrismaClient();
|
||||||
|
|
||||||
|
async function createTenantUser() {
|
||||||
|
const tenantSlug = 'tenant1';
|
||||||
|
const email = 'user@example.com';
|
||||||
|
const password = 'user123';
|
||||||
|
const firstName = 'Test';
|
||||||
|
const lastName = 'User';
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Get tenant database connection info
|
||||||
|
const tenant = await centralPrisma.tenant.findFirst({
|
||||||
|
where: { slug: tenantSlug },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!tenant) {
|
||||||
|
console.log(`Tenant ${tenantSlug} not found. Creating tenant...`);
|
||||||
|
|
||||||
|
// Create tenant in central database
|
||||||
|
const newTenant = await centralPrisma.tenant.create({
|
||||||
|
data: {
|
||||||
|
name: 'Default Tenant',
|
||||||
|
slug: tenantSlug,
|
||||||
|
dbHost: 'db',
|
||||||
|
dbPort: 3306,
|
||||||
|
dbName: 'platform',
|
||||||
|
dbUsername: 'platform',
|
||||||
|
dbPassword: 'platform',
|
||||||
|
status: 'active',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('Tenant created:', newTenant.slug);
|
||||||
|
} else {
|
||||||
|
console.log('Tenant found:', tenant.slug);
|
||||||
|
}
|
||||||
|
|
||||||
|
const tenantInfo = tenant || {
|
||||||
|
dbHost: 'db',
|
||||||
|
dbPort: 3306,
|
||||||
|
dbName: 'platform',
|
||||||
|
dbUsername: 'platform',
|
||||||
|
dbPassword: 'platform',
|
||||||
|
};
|
||||||
|
|
||||||
|
// Connect to tenant database (using root for now since tenant password is encrypted)
|
||||||
|
const tenantDb: Knex = knex({
|
||||||
|
client: 'mysql2',
|
||||||
|
connection: {
|
||||||
|
host: tenantInfo.dbHost,
|
||||||
|
port: tenantInfo.dbPort,
|
||||||
|
database: tenantInfo.dbName,
|
||||||
|
user: 'root',
|
||||||
|
password: 'asjdnfqTash37faggT',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Check if user already exists
|
||||||
|
const existingUser = await tenantDb('users')
|
||||||
|
.where({ email })
|
||||||
|
.first();
|
||||||
|
|
||||||
|
if (existingUser) {
|
||||||
|
console.log(`User ${email} already exists in tenant ${tenantSlug}`);
|
||||||
|
await tenantDb.destroy();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hash password
|
||||||
|
const hashedPassword = await bcrypt.hash(password, 10);
|
||||||
|
|
||||||
|
// Create user
|
||||||
|
await tenantDb('users').insert({
|
||||||
|
email,
|
||||||
|
password: hashedPassword,
|
||||||
|
firstName,
|
||||||
|
lastName,
|
||||||
|
isActive: true,
|
||||||
|
created_at: new Date(),
|
||||||
|
updated_at: new Date(),
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(`\nUser created successfully in tenant ${tenantSlug}!`);
|
||||||
|
console.log('Email:', email);
|
||||||
|
console.log('Password:', password);
|
||||||
|
|
||||||
|
// Create admin role if it doesn't exist
|
||||||
|
let adminRole = await tenantDb('roles')
|
||||||
|
.where({ name: 'admin' })
|
||||||
|
.first();
|
||||||
|
|
||||||
|
if (!adminRole) {
|
||||||
|
await tenantDb('roles').insert({
|
||||||
|
name: 'admin',
|
||||||
|
guardName: 'api',
|
||||||
|
description: 'Administrator role with full access',
|
||||||
|
created_at: new Date(),
|
||||||
|
updated_at: new Date(),
|
||||||
|
});
|
||||||
|
|
||||||
|
adminRole = await tenantDb('roles')
|
||||||
|
.where({ name: 'admin' })
|
||||||
|
.first();
|
||||||
|
|
||||||
|
console.log('Admin role created');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the created user
|
||||||
|
const user = await tenantDb('users')
|
||||||
|
.where({ email })
|
||||||
|
.first();
|
||||||
|
|
||||||
|
// Assign admin role to user
|
||||||
|
if (adminRole && user) {
|
||||||
|
await tenantDb('user_roles').insert({
|
||||||
|
userId: user.id,
|
||||||
|
roleId: adminRole.id,
|
||||||
|
created_at: new Date(),
|
||||||
|
updated_at: new Date(),
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('Admin role assigned to user');
|
||||||
|
}
|
||||||
|
|
||||||
|
await tenantDb.destroy();
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error creating tenant user:', error);
|
||||||
|
} finally {
|
||||||
|
await centralPrisma.$disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
createTenantUser();
|
||||||
@@ -1,165 +0,0 @@
|
|||||||
import { PrismaClient as CentralPrismaClient } from '.prisma/central';
|
|
||||||
import knex, { Knex } from 'knex';
|
|
||||||
import { createDecipheriv } from 'crypto';
|
|
||||||
|
|
||||||
// Encryption configuration - must match the one used in tenant service
|
|
||||||
const ALGORITHM = 'aes-256-cbc';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Decrypt a tenant's database password
|
|
||||||
*/
|
|
||||||
function decryptPassword(encryptedPassword: string): string {
|
|
||||||
try {
|
|
||||||
// Check if password is already plaintext (for legacy/development)
|
|
||||||
if (!encryptedPassword.includes(':')) {
|
|
||||||
console.warn('⚠️ Password appears to be unencrypted, using as-is');
|
|
||||||
return encryptedPassword;
|
|
||||||
}
|
|
||||||
|
|
||||||
const key = Buffer.from(process.env.ENCRYPTION_KEY, 'hex');
|
|
||||||
const parts = encryptedPassword.split(':');
|
|
||||||
if (parts.length !== 2) {
|
|
||||||
throw new Error('Invalid encrypted password format');
|
|
||||||
}
|
|
||||||
|
|
||||||
const iv = Buffer.from(parts[0], 'hex');
|
|
||||||
const encrypted = parts[1];
|
|
||||||
|
|
||||||
const decipher = createDecipheriv(ALGORITHM, key, iv);
|
|
||||||
|
|
||||||
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
|
|
||||||
decrypted += decipher.final('utf8');
|
|
||||||
|
|
||||||
return decrypted;
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error decrypting password:', error);
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a Knex connection for a specific tenant
|
|
||||||
*/
|
|
||||||
function createTenantKnexConnection(tenant: any): Knex {
|
|
||||||
const decryptedPassword = decryptPassword(tenant.dbPassword);
|
|
||||||
|
|
||||||
return knex({
|
|
||||||
client: 'mysql2',
|
|
||||||
connection: {
|
|
||||||
host: tenant.dbHost,
|
|
||||||
port: tenant.dbPort,
|
|
||||||
user: tenant.dbUsername,
|
|
||||||
password: decryptedPassword,
|
|
||||||
database: tenant.dbName,
|
|
||||||
},
|
|
||||||
migrations: {
|
|
||||||
tableName: 'knex_migrations',
|
|
||||||
directory: './migrations/tenant',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Run migrations for a specific tenant
|
|
||||||
*/
|
|
||||||
async function migrateTenant(tenant: any): Promise<void> {
|
|
||||||
console.log(`\n🔄 Migrating tenant: ${tenant.name} (${tenant.dbName})`);
|
|
||||||
|
|
||||||
const tenantKnex = createTenantKnexConnection(tenant);
|
|
||||||
|
|
||||||
try {
|
|
||||||
const [batchNo, log] = await tenantKnex.migrate.latest();
|
|
||||||
|
|
||||||
if (log.length === 0) {
|
|
||||||
console.log(`✅ ${tenant.name}: Already up to date`);
|
|
||||||
} else {
|
|
||||||
console.log(`✅ ${tenant.name}: Ran ${log.length} migrations:`);
|
|
||||||
log.forEach((migration) => {
|
|
||||||
console.log(` - ${migration}`);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error(`❌ ${tenant.name}: Migration failed:`, error.message);
|
|
||||||
throw error;
|
|
||||||
} finally {
|
|
||||||
await tenantKnex.destroy();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Main function to migrate all active tenants
|
|
||||||
*/
|
|
||||||
async function migrateAllTenants() {
|
|
||||||
console.log('🚀 Starting migration for all tenants...\n');
|
|
||||||
|
|
||||||
const centralPrisma = new CentralPrismaClient();
|
|
||||||
|
|
||||||
try {
|
|
||||||
// Fetch all active tenants
|
|
||||||
const tenants = await centralPrisma.tenant.findMany({
|
|
||||||
where: {
|
|
||||||
status: 'ACTIVE',
|
|
||||||
},
|
|
||||||
orderBy: {
|
|
||||||
name: 'asc',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (tenants.length === 0) {
|
|
||||||
console.log('⚠️ No active tenants found.');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(`📋 Found ${tenants.length} active tenant(s)\n`);
|
|
||||||
|
|
||||||
let successCount = 0;
|
|
||||||
let failureCount = 0;
|
|
||||||
const failures: { tenant: string; error: string }[] = [];
|
|
||||||
|
|
||||||
// Migrate each tenant sequentially
|
|
||||||
for (const tenant of tenants) {
|
|
||||||
try {
|
|
||||||
await migrateTenant(tenant);
|
|
||||||
successCount++;
|
|
||||||
} catch (error) {
|
|
||||||
failureCount++;
|
|
||||||
failures.push({
|
|
||||||
tenant: tenant.name,
|
|
||||||
error: error.message,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print summary
|
|
||||||
console.log('\n' + '='.repeat(60));
|
|
||||||
console.log('📊 Migration Summary');
|
|
||||||
console.log('='.repeat(60));
|
|
||||||
console.log(`✅ Successful: ${successCount}`);
|
|
||||||
console.log(`❌ Failed: ${failureCount}`);
|
|
||||||
|
|
||||||
if (failures.length > 0) {
|
|
||||||
console.log('\n❌ Failed Tenants:');
|
|
||||||
failures.forEach(({ tenant, error }) => {
|
|
||||||
console.log(` - ${tenant}: ${error}`);
|
|
||||||
});
|
|
||||||
process.exit(1);
|
|
||||||
} else {
|
|
||||||
console.log('\n🎉 All tenant migrations completed successfully!');
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error('❌ Fatal error:', error);
|
|
||||||
process.exit(1);
|
|
||||||
} finally {
|
|
||||||
await centralPrisma.$disconnect();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run the migration
|
|
||||||
migrateAllTenants()
|
|
||||||
.then(() => {
|
|
||||||
process.exit(0);
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
console.error('Unhandled error:', error);
|
|
||||||
process.exit(1);
|
|
||||||
});
|
|
||||||
@@ -1,134 +0,0 @@
|
|||||||
import { PrismaClient as CentralPrismaClient } from '.prisma/central';
|
|
||||||
import knex, { Knex } from 'knex';
|
|
||||||
import { createDecipheriv } from 'crypto';
|
|
||||||
|
|
||||||
// Encryption configuration
|
|
||||||
const ALGORITHM = 'aes-256-cbc';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Decrypt a tenant's database password
|
|
||||||
*/
|
|
||||||
function decryptPassword(encryptedPassword: string): string {
|
|
||||||
try {
|
|
||||||
// Check if password is already plaintext (for legacy/development)
|
|
||||||
if (!encryptedPassword.includes(':')) {
|
|
||||||
console.warn('⚠️ Password appears to be unencrypted, using as-is');
|
|
||||||
return encryptedPassword;
|
|
||||||
}
|
|
||||||
|
|
||||||
const key = Buffer.from(process.env.ENCRYPTION_KEY, 'hex');
|
|
||||||
const parts = encryptedPassword.split(':');
|
|
||||||
if (parts.length !== 2) {
|
|
||||||
throw new Error('Invalid encrypted password format');
|
|
||||||
}
|
|
||||||
|
|
||||||
const iv = Buffer.from(parts[0], 'hex');
|
|
||||||
const encrypted = parts[1];
|
|
||||||
|
|
||||||
const decipher = createDecipheriv(ALGORITHM, key, iv);
|
|
||||||
|
|
||||||
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
|
|
||||||
decrypted += decipher.final('utf8');
|
|
||||||
|
|
||||||
return decrypted;
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error decrypting password:', error);
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a Knex connection for a specific tenant
|
|
||||||
*/
|
|
||||||
function createTenantKnexConnection(tenant: any): Knex {
|
|
||||||
const decryptedPassword = decryptPassword(tenant.dbPassword);
|
|
||||||
|
|
||||||
return knex({
|
|
||||||
client: 'mysql2',
|
|
||||||
connection: {
|
|
||||||
host: tenant.dbHost,
|
|
||||||
port: tenant.dbPort,
|
|
||||||
user: tenant.dbUsername,
|
|
||||||
password: decryptedPassword,
|
|
||||||
database: tenant.dbName,
|
|
||||||
},
|
|
||||||
migrations: {
|
|
||||||
tableName: 'knex_migrations',
|
|
||||||
directory: './migrations/tenant',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Migrate a specific tenant by slug or ID
|
|
||||||
*/
|
|
||||||
async function migrateTenant() {
|
|
||||||
const tenantIdentifier = process.argv[2];
|
|
||||||
|
|
||||||
if (!tenantIdentifier) {
|
|
||||||
console.error('❌ Usage: npm run migrate:tenant <tenant-slug-or-id>');
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(`🔍 Looking for tenant: ${tenantIdentifier}\n`);
|
|
||||||
|
|
||||||
const centralPrisma = new CentralPrismaClient();
|
|
||||||
|
|
||||||
try {
|
|
||||||
// Find tenant by slug or ID
|
|
||||||
const tenant = await centralPrisma.tenant.findFirst({
|
|
||||||
where: {
|
|
||||||
OR: [
|
|
||||||
{ slug: tenantIdentifier },
|
|
||||||
{ id: tenantIdentifier },
|
|
||||||
],
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!tenant) {
|
|
||||||
console.error(`❌ Tenant not found: ${tenantIdentifier}`);
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(`📋 Tenant: ${tenant.name} (${tenant.slug})`);
|
|
||||||
console.log(`📊 Database: ${tenant.dbName}`);
|
|
||||||
console.log(`🔄 Running migrations...\n`);
|
|
||||||
|
|
||||||
const tenantKnex = createTenantKnexConnection(tenant);
|
|
||||||
|
|
||||||
try {
|
|
||||||
const [batchNo, log] = await tenantKnex.migrate.latest();
|
|
||||||
|
|
||||||
if (log.length === 0) {
|
|
||||||
console.log(`✅ Already up to date (batch ${batchNo})`);
|
|
||||||
} else {
|
|
||||||
console.log(`✅ Ran ${log.length} migration(s) (batch ${batchNo}):`);
|
|
||||||
log.forEach((migration) => {
|
|
||||||
console.log(` - ${migration}`);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log('\n🎉 Migration completed successfully!');
|
|
||||||
} catch (error) {
|
|
||||||
console.error('❌ Migration failed:', error.message);
|
|
||||||
throw error;
|
|
||||||
} finally {
|
|
||||||
await tenantKnex.destroy();
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error('❌ Fatal error:', error);
|
|
||||||
process.exit(1);
|
|
||||||
} finally {
|
|
||||||
await centralPrisma.$disconnect();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run the migration
|
|
||||||
migrateTenant()
|
|
||||||
.then(() => {
|
|
||||||
process.exit(0);
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
console.error('Unhandled error:', error);
|
|
||||||
process.exit(1);
|
|
||||||
});
|
|
||||||
@@ -1,147 +0,0 @@
|
|||||||
/**
|
|
||||||
* Example seed data for Account object with UI metadata
|
|
||||||
* Run this after migrations to add UI metadata to existing Account fields
|
|
||||||
*/
|
|
||||||
|
|
||||||
exports.seed = async function(knex) {
|
|
||||||
// Get the Account object
|
|
||||||
const accountObj = await knex('object_definitions')
|
|
||||||
.where({ apiName: 'Account' })
|
|
||||||
.first();
|
|
||||||
|
|
||||||
if (!accountObj) {
|
|
||||||
console.log('Account object not found. Please run migrations first.');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(`Found Account object with ID: ${accountObj.id}`);
|
|
||||||
|
|
||||||
// Update existing Account fields with UI metadata
|
|
||||||
const fieldsToUpdate = [
|
|
||||||
{
|
|
||||||
apiName: 'name',
|
|
||||||
ui_metadata: JSON.stringify({
|
|
||||||
fieldType: 'TEXT',
|
|
||||||
placeholder: 'Enter account name',
|
|
||||||
helpText: 'The name of the organization or company',
|
|
||||||
showOnList: true,
|
|
||||||
showOnDetail: true,
|
|
||||||
showOnEdit: true,
|
|
||||||
sortable: true,
|
|
||||||
section: 'basic',
|
|
||||||
sectionLabel: 'Basic Information',
|
|
||||||
sectionOrder: 1,
|
|
||||||
validationRules: [
|
|
||||||
{ type: 'required', message: 'Account name is required' },
|
|
||||||
{ type: 'minLength', value: 2, message: 'Account name must be at least 2 characters' },
|
|
||||||
{ type: 'maxLength', value: 255, message: 'Account name cannot exceed 255 characters' }
|
|
||||||
]
|
|
||||||
})
|
|
||||||
},
|
|
||||||
{
|
|
||||||
apiName: 'website',
|
|
||||||
ui_metadata: JSON.stringify({
|
|
||||||
fieldType: 'URL',
|
|
||||||
placeholder: 'https://www.example.com',
|
|
||||||
helpText: 'Company website URL',
|
|
||||||
showOnList: true,
|
|
||||||
showOnDetail: true,
|
|
||||||
showOnEdit: true,
|
|
||||||
sortable: true,
|
|
||||||
section: 'basic',
|
|
||||||
sectionLabel: 'Basic Information',
|
|
||||||
sectionOrder: 1,
|
|
||||||
validationRules: [
|
|
||||||
{ type: 'url', message: 'Please enter a valid URL' }
|
|
||||||
]
|
|
||||||
})
|
|
||||||
},
|
|
||||||
{
|
|
||||||
apiName: 'phone',
|
|
||||||
ui_metadata: JSON.stringify({
|
|
||||||
fieldType: 'TEXT',
|
|
||||||
placeholder: '+1 (555) 000-0000',
|
|
||||||
helpText: 'Primary phone number',
|
|
||||||
showOnList: true,
|
|
||||||
showOnDetail: true,
|
|
||||||
showOnEdit: true,
|
|
||||||
sortable: false,
|
|
||||||
section: 'contact',
|
|
||||||
sectionLabel: 'Contact Information',
|
|
||||||
sectionOrder: 2,
|
|
||||||
validationRules: [
|
|
||||||
{ type: 'pattern', value: '^\\+?[0-9\\s\\-\\(\\)]+$', message: 'Please enter a valid phone number' }
|
|
||||||
]
|
|
||||||
})
|
|
||||||
},
|
|
||||||
{
|
|
||||||
apiName: 'industry',
|
|
||||||
ui_metadata: JSON.stringify({
|
|
||||||
fieldType: 'SELECT',
|
|
||||||
placeholder: 'Select industry',
|
|
||||||
helpText: 'The primary industry this account operates in',
|
|
||||||
showOnList: true,
|
|
||||||
showOnDetail: true,
|
|
||||||
showOnEdit: true,
|
|
||||||
sortable: true,
|
|
||||||
section: 'details',
|
|
||||||
sectionLabel: 'Account Details',
|
|
||||||
sectionOrder: 3,
|
|
||||||
options: [
|
|
||||||
{ value: 'technology', label: 'Technology' },
|
|
||||||
{ value: 'finance', label: 'Finance' },
|
|
||||||
{ value: 'healthcare', label: 'Healthcare' },
|
|
||||||
{ value: 'manufacturing', label: 'Manufacturing' },
|
|
||||||
{ value: 'retail', label: 'Retail' },
|
|
||||||
{ value: 'education', label: 'Education' },
|
|
||||||
{ value: 'government', label: 'Government' },
|
|
||||||
{ value: 'nonprofit', label: 'Non-Profit' },
|
|
||||||
{ value: 'other', label: 'Other' }
|
|
||||||
]
|
|
||||||
})
|
|
||||||
},
|
|
||||||
{
|
|
||||||
apiName: 'ownerId',
|
|
||||||
ui_metadata: JSON.stringify({
|
|
||||||
fieldType: 'SELECT',
|
|
||||||
placeholder: 'Select owner',
|
|
||||||
helpText: 'The user who owns this account',
|
|
||||||
showOnList: true,
|
|
||||||
showOnDetail: true,
|
|
||||||
showOnEdit: true,
|
|
||||||
sortable: true,
|
|
||||||
section: 'system',
|
|
||||||
sectionLabel: 'System Information',
|
|
||||||
sectionOrder: 4,
|
|
||||||
// This would be dynamically populated from the users table
|
|
||||||
// For now, providing static structure
|
|
||||||
isReference: true,
|
|
||||||
referenceObject: 'User',
|
|
||||||
referenceDisplayField: 'name'
|
|
||||||
})
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
// Update each field with UI metadata
|
|
||||||
for (const fieldUpdate of fieldsToUpdate) {
|
|
||||||
const result = await knex('field_definitions')
|
|
||||||
.where({
|
|
||||||
objectDefinitionId: accountObj.id,
|
|
||||||
apiName: fieldUpdate.apiName
|
|
||||||
})
|
|
||||||
.update({
|
|
||||||
ui_metadata: fieldUpdate.ui_metadata,
|
|
||||||
updated_at: knex.fn.now()
|
|
||||||
});
|
|
||||||
|
|
||||||
if (result > 0) {
|
|
||||||
console.log(`✓ Updated ${fieldUpdate.apiName} with UI metadata`);
|
|
||||||
} else {
|
|
||||||
console.log(`✗ Field ${fieldUpdate.apiName} not found`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log('\n✅ Account fields UI metadata seed completed successfully!');
|
|
||||||
console.log('You can now fetch the Account object UI config via:');
|
|
||||||
console.log('GET /api/setup/objects/Account/ui-config');
|
|
||||||
};
|
|
||||||
@@ -42,8 +42,13 @@ export class AuthController {
|
|||||||
|
|
||||||
@HttpCode(HttpStatus.OK)
|
@HttpCode(HttpStatus.OK)
|
||||||
@Post('login')
|
@Post('login')
|
||||||
async login(@Body() loginDto: LoginDto) {
|
async login(@TenantId() tenantId: string, @Body() loginDto: LoginDto) {
|
||||||
|
if (!tenantId) {
|
||||||
|
throw new UnauthorizedException('Tenant ID is required');
|
||||||
|
}
|
||||||
|
|
||||||
const user = await this.authService.validateUser(
|
const user = await this.authService.validateUser(
|
||||||
|
tenantId,
|
||||||
loginDto.email,
|
loginDto.email,
|
||||||
loginDto.password,
|
loginDto.password,
|
||||||
);
|
);
|
||||||
@@ -57,9 +62,15 @@ export class AuthController {
|
|||||||
|
|
||||||
@Post('register')
|
@Post('register')
|
||||||
async register(
|
async register(
|
||||||
|
@TenantId() tenantId: string,
|
||||||
@Body() registerDto: RegisterDto,
|
@Body() registerDto: RegisterDto,
|
||||||
) {
|
) {
|
||||||
|
if (!tenantId) {
|
||||||
|
throw new UnauthorizedException('Tenant ID is required');
|
||||||
|
}
|
||||||
|
|
||||||
const user = await this.authService.register(
|
const user = await this.authService.register(
|
||||||
|
tenantId,
|
||||||
registerDto.email,
|
registerDto.email,
|
||||||
registerDto.password,
|
registerDto.password,
|
||||||
registerDto.firstName,
|
registerDto.firstName,
|
||||||
|
|||||||
@@ -5,10 +5,12 @@ import { ConfigService } from '@nestjs/config';
|
|||||||
import { AuthService } from './auth.service';
|
import { AuthService } from './auth.service';
|
||||||
import { AuthController } from './auth.controller';
|
import { AuthController } from './auth.controller';
|
||||||
import { JwtStrategy } from './jwt.strategy';
|
import { JwtStrategy } from './jwt.strategy';
|
||||||
|
import { TenantModule } from '../tenant/tenant.module';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
PassportModule,
|
PassportModule,
|
||||||
|
TenantModule,
|
||||||
JwtModule.registerAsync({
|
JwtModule.registerAsync({
|
||||||
inject: [ConfigService],
|
inject: [ConfigService],
|
||||||
useFactory: (config: ConfigService) => ({
|
useFactory: (config: ConfigService) => ({
|
||||||
|
|||||||
@@ -1,43 +1,43 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { JwtService } from '@nestjs/jwt';
|
import { JwtService } from '@nestjs/jwt';
|
||||||
import { PrismaService } from '../prisma/prisma.service';
|
import { TenantDatabaseService } from '../tenant/tenant-database.service';
|
||||||
import * as bcrypt from 'bcrypt';
|
import * as bcrypt from 'bcrypt';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AuthService {
|
export class AuthService {
|
||||||
constructor(
|
constructor(
|
||||||
private prisma: PrismaService,
|
private tenantDbService: TenantDatabaseService,
|
||||||
private jwtService: JwtService,
|
private jwtService: JwtService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async validateUser(
|
async validateUser(
|
||||||
|
tenantId: string,
|
||||||
email: string,
|
email: string,
|
||||||
password: string,
|
password: string,
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
const user = await this.prisma.user.findUnique({
|
const tenantDb = await this.tenantDbService.getTenantKnex(tenantId);
|
||||||
where: {
|
|
||||||
email,
|
const user = await tenantDb('users')
|
||||||
},
|
.where({ email })
|
||||||
include: {
|
.first();
|
||||||
userRoles: {
|
|
||||||
include: {
|
|
||||||
role: {
|
|
||||||
include: {
|
|
||||||
rolePermissions: {
|
|
||||||
include: {
|
|
||||||
permission: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (user && (await bcrypt.compare(password, user.password))) {
|
if (!user) {
|
||||||
const { password, ...result } = user;
|
return null;
|
||||||
return result;
|
}
|
||||||
|
|
||||||
|
if (await bcrypt.compare(password, user.password)) {
|
||||||
|
// Load user roles and permissions
|
||||||
|
const userRoles = await tenantDb('user_roles')
|
||||||
|
.where({ userId: user.id })
|
||||||
|
.join('roles', 'user_roles.roleId', 'roles.id')
|
||||||
|
.select('roles.*');
|
||||||
|
|
||||||
|
const { password: _, ...result } = user;
|
||||||
|
return {
|
||||||
|
...result,
|
||||||
|
tenantId,
|
||||||
|
userRoles,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
@@ -47,6 +47,7 @@ export class AuthService {
|
|||||||
const payload = {
|
const payload = {
|
||||||
sub: user.id,
|
sub: user.id,
|
||||||
email: user.email,
|
email: user.email,
|
||||||
|
tenantId: user.tenantId,
|
||||||
};
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -56,27 +57,36 @@ export class AuthService {
|
|||||||
email: user.email,
|
email: user.email,
|
||||||
firstName: user.firstName,
|
firstName: user.firstName,
|
||||||
lastName: user.lastName,
|
lastName: user.lastName,
|
||||||
|
tenantId: user.tenantId,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async register(
|
async register(
|
||||||
|
tenantId: string,
|
||||||
email: string,
|
email: string,
|
||||||
password: string,
|
password: string,
|
||||||
firstName?: string,
|
firstName?: string,
|
||||||
lastName?: string,
|
lastName?: string,
|
||||||
) {
|
) {
|
||||||
|
const tenantDb = await this.tenantDbService.getTenantKnex(tenantId);
|
||||||
|
|
||||||
const hashedPassword = await bcrypt.hash(password, 10);
|
const hashedPassword = await bcrypt.hash(password, 10);
|
||||||
|
|
||||||
const user = await this.prisma.user.create({
|
const [userId] = await tenantDb('users').insert({
|
||||||
data: {
|
email,
|
||||||
email,
|
password: hashedPassword,
|
||||||
password: hashedPassword,
|
firstName,
|
||||||
firstName,
|
lastName,
|
||||||
lastName,
|
isActive: true,
|
||||||
},
|
created_at: new Date(),
|
||||||
|
updated_at: new Date(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const user = await tenantDb('users')
|
||||||
|
.where({ id: userId })
|
||||||
|
.first();
|
||||||
|
|
||||||
const { password: _, ...result } = user;
|
const { password: _, ...result } = user;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,38 +1,42 @@
|
|||||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||||
import { TenantDatabaseService } from '../tenant/tenant-database.service';
|
import { PrismaService } from '../prisma/prisma.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ObjectService {
|
export class ObjectService {
|
||||||
constructor(private tenantDbService: TenantDatabaseService) {}
|
constructor(private prisma: PrismaService) {}
|
||||||
|
|
||||||
// Setup endpoints - Object metadata management
|
// Setup endpoints - Object metadata management
|
||||||
async getObjectDefinitions(tenantId: string) {
|
async getObjectDefinitions(tenantId: string) {
|
||||||
const knex = await this.tenantDbService.getTenantKnex(tenantId);
|
return this.prisma.objectDefinition.findMany({
|
||||||
return knex('object_definitions')
|
where: { tenantId },
|
||||||
.select('*')
|
include: {
|
||||||
.orderBy('label', 'asc');
|
fields: true,
|
||||||
|
},
|
||||||
|
orderBy: { label: 'asc' },
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async getObjectDefinition(tenantId: string, apiName: string) {
|
async getObjectDefinition(tenantId: string, apiName: string) {
|
||||||
const knex = await this.tenantDbService.getTenantKnex(tenantId);
|
const obj = await this.prisma.objectDefinition.findUnique({
|
||||||
|
where: {
|
||||||
const obj = await knex('object_definitions')
|
tenantId_apiName: {
|
||||||
.where({ apiName })
|
tenantId,
|
||||||
.first();
|
apiName,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
fields: {
|
||||||
|
where: { isActive: true },
|
||||||
|
orderBy: { label: 'asc' },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
if (!obj) {
|
if (!obj) {
|
||||||
throw new NotFoundException(`Object ${apiName} not found`);
|
throw new NotFoundException(`Object ${apiName} not found`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get fields for this object
|
return obj;
|
||||||
const fields = await knex('field_definitions')
|
|
||||||
.where({ objectDefinitionId: obj.id })
|
|
||||||
.orderBy('label', 'asc');
|
|
||||||
|
|
||||||
return {
|
|
||||||
...obj,
|
|
||||||
fields,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async createObjectDefinition(
|
async createObjectDefinition(
|
||||||
@@ -45,15 +49,13 @@ export class ObjectService {
|
|||||||
isSystem?: boolean;
|
isSystem?: boolean;
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
const knex = await this.tenantDbService.getTenantKnex(tenantId);
|
return this.prisma.objectDefinition.create({
|
||||||
const [id] = await knex('object_definitions').insert({
|
data: {
|
||||||
id: knex.raw('(UUID())'),
|
tenantId,
|
||||||
...data,
|
...data,
|
||||||
created_at: knex.fn.now(),
|
tableName: `custom_${data.apiName.toLowerCase()}`,
|
||||||
updated_at: knex.fn.now(),
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return knex('object_definitions').where({ id }).first();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async createFieldDefinition(
|
async createFieldDefinition(
|
||||||
@@ -66,22 +68,20 @@ export class ObjectService {
|
|||||||
description?: string;
|
description?: string;
|
||||||
isRequired?: boolean;
|
isRequired?: boolean;
|
||||||
isUnique?: boolean;
|
isUnique?: boolean;
|
||||||
referenceObject?: string;
|
isLookup?: boolean;
|
||||||
|
referenceTo?: string;
|
||||||
defaultValue?: string;
|
defaultValue?: string;
|
||||||
|
options?: any;
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
const knex = await this.tenantDbService.getTenantKnex(tenantId);
|
|
||||||
const obj = await this.getObjectDefinition(tenantId, objectApiName);
|
const obj = await this.getObjectDefinition(tenantId, objectApiName);
|
||||||
|
|
||||||
const [id] = await knex('field_definitions').insert({
|
return this.prisma.fieldDefinition.create({
|
||||||
id: knex.raw('(UUID())'),
|
data: {
|
||||||
objectDefinitionId: obj.id,
|
objectId: obj.id,
|
||||||
...data,
|
...data,
|
||||||
created_at: knex.fn.now(),
|
},
|
||||||
updated_at: knex.fn.now(),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return knex('field_definitions').where({ id }).first();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Runtime endpoints - CRUD operations
|
// Runtime endpoints - CRUD operations
|
||||||
@@ -91,16 +91,19 @@ export class ObjectService {
|
|||||||
userId: string,
|
userId: string,
|
||||||
filters?: any,
|
filters?: any,
|
||||||
) {
|
) {
|
||||||
const knex = await this.tenantDbService.getTenantKnex(tenantId);
|
|
||||||
|
|
||||||
// For demonstration, using Account as example static object
|
// For demonstration, using Account as example static object
|
||||||
if (objectApiName === 'Account') {
|
if (objectApiName === 'Account') {
|
||||||
return knex('accounts')
|
return this.prisma.account.findMany({
|
||||||
.where({ ownerId: userId })
|
where: {
|
||||||
.where(filters || {});
|
tenantId,
|
||||||
|
ownerId: userId, // Basic sharing rule
|
||||||
|
...filters,
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// For custom objects, you'd need dynamic query building
|
// For custom objects, you'd need dynamic query building
|
||||||
|
// This is a simplified version
|
||||||
throw new Error(`Runtime queries for ${objectApiName} not yet implemented`);
|
throw new Error(`Runtime queries for ${objectApiName} not yet implemented`);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,12 +113,14 @@ export class ObjectService {
|
|||||||
recordId: string,
|
recordId: string,
|
||||||
userId: string,
|
userId: string,
|
||||||
) {
|
) {
|
||||||
const knex = await this.tenantDbService.getTenantKnex(tenantId);
|
|
||||||
|
|
||||||
if (objectApiName === 'Account') {
|
if (objectApiName === 'Account') {
|
||||||
const record = await knex('accounts')
|
const record = await this.prisma.account.findFirst({
|
||||||
.where({ id: recordId, ownerId: userId })
|
where: {
|
||||||
.first();
|
id: recordId,
|
||||||
|
tenantId,
|
||||||
|
ownerId: userId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
if (!record) {
|
if (!record) {
|
||||||
throw new NotFoundException('Record not found');
|
throw new NotFoundException('Record not found');
|
||||||
@@ -133,18 +138,14 @@ export class ObjectService {
|
|||||||
data: any,
|
data: any,
|
||||||
userId: string,
|
userId: string,
|
||||||
) {
|
) {
|
||||||
const knex = await this.tenantDbService.getTenantKnex(tenantId);
|
|
||||||
|
|
||||||
if (objectApiName === 'Account') {
|
if (objectApiName === 'Account') {
|
||||||
const [id] = await knex('accounts').insert({
|
return this.prisma.account.create({
|
||||||
id: knex.raw('(UUID())'),
|
data: {
|
||||||
ownerId: userId,
|
tenantId,
|
||||||
...data,
|
ownerId: userId,
|
||||||
created_at: knex.fn.now(),
|
...data,
|
||||||
updated_at: knex.fn.now(),
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return knex('accounts').where({ id }).first();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error(`Runtime queries for ${objectApiName} not yet implemented`);
|
throw new Error(`Runtime queries for ${objectApiName} not yet implemented`);
|
||||||
@@ -157,17 +158,14 @@ export class ObjectService {
|
|||||||
data: any,
|
data: any,
|
||||||
userId: string,
|
userId: string,
|
||||||
) {
|
) {
|
||||||
const knex = await this.tenantDbService.getTenantKnex(tenantId);
|
|
||||||
|
|
||||||
if (objectApiName === 'Account') {
|
if (objectApiName === 'Account') {
|
||||||
// Verify ownership
|
// Verify ownership
|
||||||
await this.getRecord(tenantId, objectApiName, recordId, userId);
|
await this.getRecord(tenantId, objectApiName, recordId, userId);
|
||||||
|
|
||||||
await knex('accounts')
|
return this.prisma.account.update({
|
||||||
.where({ id: recordId })
|
where: { id: recordId },
|
||||||
.update({ ...data, updated_at: knex.fn.now() });
|
data,
|
||||||
|
});
|
||||||
return knex('accounts').where({ id: recordId }).first();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error(`Runtime queries for ${objectApiName} not yet implemented`);
|
throw new Error(`Runtime queries for ${objectApiName} not yet implemented`);
|
||||||
@@ -179,15 +177,13 @@ export class ObjectService {
|
|||||||
recordId: string,
|
recordId: string,
|
||||||
userId: string,
|
userId: string,
|
||||||
) {
|
) {
|
||||||
const knex = await this.tenantDbService.getTenantKnex(tenantId);
|
|
||||||
|
|
||||||
if (objectApiName === 'Account') {
|
if (objectApiName === 'Account') {
|
||||||
// Verify ownership
|
// Verify ownership
|
||||||
await this.getRecord(tenantId, objectApiName, recordId, userId);
|
await this.getRecord(tenantId, objectApiName, recordId, userId);
|
||||||
|
|
||||||
await knex('accounts').where({ id: recordId }).delete();
|
return this.prisma.account.delete({
|
||||||
|
where: { id: recordId },
|
||||||
return { success: true };
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error(`Runtime queries for ${objectApiName} not yet implemented`);
|
throw new Error(`Runtime queries for ${objectApiName} not yet implemented`);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Injectable, OnModuleInit, OnModuleDestroy } from '@nestjs/common';
|
import { Injectable, OnModuleInit, OnModuleDestroy } from '@nestjs/common';
|
||||||
import { PrismaClient } from '.prisma/tenant';
|
import { PrismaClient } from '@prisma/client';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class PrismaService
|
export class PrismaService
|
||||||
|
|||||||
@@ -8,22 +8,30 @@ export class TenantDatabaseService {
|
|||||||
private readonly logger = new Logger(TenantDatabaseService.name);
|
private readonly logger = new Logger(TenantDatabaseService.name);
|
||||||
private tenantConnections: Map<string, Knex> = new Map();
|
private tenantConnections: Map<string, Knex> = new Map();
|
||||||
|
|
||||||
async getTenantKnex(tenantId: string): Promise<Knex> {
|
async getTenantKnex(tenantIdOrSlug: string): Promise<Knex> {
|
||||||
if (this.tenantConnections.has(tenantId)) {
|
if (this.tenantConnections.has(tenantIdOrSlug)) {
|
||||||
return this.tenantConnections.get(tenantId);
|
return this.tenantConnections.get(tenantIdOrSlug);
|
||||||
}
|
}
|
||||||
|
|
||||||
const centralPrisma = getCentralPrisma();
|
const centralPrisma = getCentralPrisma();
|
||||||
const tenant = await centralPrisma.tenant.findUnique({
|
|
||||||
where: { id: tenantId },
|
// Try to find tenant by ID first, then by slug
|
||||||
|
let tenant = await centralPrisma.tenant.findUnique({
|
||||||
|
where: { id: tenantIdOrSlug },
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!tenant) {
|
||||||
|
tenant = await centralPrisma.tenant.findUnique({
|
||||||
|
where: { slug: tenantIdOrSlug },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (!tenant) {
|
if (!tenant) {
|
||||||
throw new Error(`Tenant ${tenantId} not found`);
|
throw new Error(`Tenant ${tenantIdOrSlug} not found`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tenant.status !== 'active') {
|
if (tenant.status !== 'active') {
|
||||||
throw new Error(`Tenant ${tenantId} is not active`);
|
throw new Error(`Tenant ${tenantIdOrSlug} is not active`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decrypt password
|
// Decrypt password
|
||||||
@@ -56,7 +64,7 @@ export class TenantDatabaseService {
|
|||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.tenantConnections.set(tenantId, tenantKnex);
|
this.tenantConnections.set(tenantIdOrSlug, tenantKnex);
|
||||||
return tenantKnex;
|
return tenantKnex;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,29 +19,53 @@ export class TenantMiddleware implements NestMiddleware {
|
|||||||
const hostname = host.split(':')[0]; // Remove port if present
|
const hostname = host.split(':')[0]; // Remove port if present
|
||||||
const parts = hostname.split('.');
|
const parts = hostname.split('.');
|
||||||
|
|
||||||
// For local development, accept x-tenant-id header as fallback
|
this.logger.log(`Host header: ${host}, hostname: ${hostname}, parts: ${JSON.stringify(parts)}`);
|
||||||
|
|
||||||
|
// For local development, accept x-tenant-id header
|
||||||
let tenantId = req.headers['x-tenant-id'] as string;
|
let tenantId = req.headers['x-tenant-id'] as string;
|
||||||
let subdomain: string | null = null;
|
let subdomain: string | null = null;
|
||||||
|
|
||||||
// Extract subdomain (e.g., "acme" from "acme.routebox.co")
|
this.logger.log(`Host header: ${host}, hostname: ${hostname}, parts: ${JSON.stringify(parts)}, x-tenant-id: ${tenantId}`);
|
||||||
if (parts.length > 2) {
|
|
||||||
|
// If x-tenant-id is explicitly provided, use it directly
|
||||||
|
if (tenantId) {
|
||||||
|
this.logger.log(`Using explicit x-tenant-id: ${tenantId}`);
|
||||||
|
(req as any).tenantId = tenantId;
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract subdomain (e.g., "tenant1" from "tenant1.routebox.co")
|
||||||
|
// For production domains with 3+ parts, extract first part as subdomain
|
||||||
|
if (parts.length >= 3) {
|
||||||
subdomain = parts[0];
|
subdomain = parts[0];
|
||||||
// Ignore www subdomain
|
// Ignore www subdomain
|
||||||
if (subdomain === 'www') {
|
if (subdomain === 'www') {
|
||||||
subdomain = null;
|
subdomain = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// For development (e.g., tenant1.localhost), also check 2 parts
|
||||||
|
else if (parts.length === 2 && parts[1] === 'localhost') {
|
||||||
|
subdomain = parts[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
this.logger.log(`Extracted subdomain: ${subdomain}`);
|
||||||
|
|
||||||
// Get tenant by subdomain if available
|
// Get tenant by subdomain if available
|
||||||
if (subdomain) {
|
if (subdomain) {
|
||||||
const tenant = await this.tenantDbService.getTenantByDomain(subdomain);
|
try {
|
||||||
if (tenant) {
|
const tenant = await this.tenantDbService.getTenantByDomain(subdomain);
|
||||||
tenantId = tenant.id;
|
if (tenant) {
|
||||||
this.logger.log(
|
tenantId = tenant.id;
|
||||||
`Tenant identified: ${tenant.name} (${tenant.id}) from subdomain: ${subdomain}`,
|
this.logger.log(
|
||||||
);
|
`Tenant identified: ${tenant.name} (${tenant.id}) from subdomain: ${subdomain}`,
|
||||||
} else {
|
);
|
||||||
this.logger.warn(`No tenant found for subdomain: ${subdomain}`);
|
}
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.warn(`No tenant found for subdomain: ${subdomain}`, error.message);
|
||||||
|
// Fall back to using subdomain as tenantId directly if domain lookup fails
|
||||||
|
tenantId = subdomain;
|
||||||
|
this.logger.log(`Using subdomain as tenantId fallback: ${tenantId}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,10 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import { Toaster } from 'vue-sonner'
|
||||||
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div>
|
<div>
|
||||||
|
<Toaster position="top-right" :duration="4000" richColors />
|
||||||
<NuxtPage />
|
<NuxtPage />
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -22,8 +22,6 @@
|
|||||||
--input: 214.3 31.8% 91.4%;
|
--input: 214.3 31.8% 91.4%;
|
||||||
--ring: 222.2 84% 4.9%;
|
--ring: 222.2 84% 4.9%;
|
||||||
--radius: 0.5rem;
|
--radius: 0.5rem;
|
||||||
--popover: 0 0% 100%;
|
|
||||||
--popover-foreground: 222.2 84% 4.9%;
|
|
||||||
--sidebar-background: 0 0% 98%;
|
--sidebar-background: 0 0% 98%;
|
||||||
--sidebar-foreground: 240 5.3% 26.1%;
|
--sidebar-foreground: 240 5.3% 26.1%;
|
||||||
--sidebar-primary: 240 5.9% 10%;
|
--sidebar-primary: 240 5.9% 10%;
|
||||||
@@ -52,8 +50,6 @@
|
|||||||
--border: 217.2 32.6% 17.5%;
|
--border: 217.2 32.6% 17.5%;
|
||||||
--input: 217.2 32.6% 17.5%;
|
--input: 217.2 32.6% 17.5%;
|
||||||
--ring: 212.7 26.8% 83.9%;
|
--ring: 212.7 26.8% 83.9%;
|
||||||
--popover: 222.2 84% 4.9%;
|
|
||||||
--popover-foreground: 210 40% 98%;
|
|
||||||
--sidebar-background: 240 5.9% 10%;
|
--sidebar-background: 240 5.9% 10%;
|
||||||
--sidebar-foreground: 240 4.8% 95.9%;
|
--sidebar-foreground: 240 4.8% 95.9%;
|
||||||
--sidebar-primary: 224.3 76.3% 48%;
|
--sidebar-primary: 224.3 76.3% 48%;
|
||||||
|
|||||||
@@ -5,8 +5,34 @@ import { Label } from '@/components/ui/label'
|
|||||||
|
|
||||||
const config = useRuntimeConfig()
|
const config = useRuntimeConfig()
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
|
const { toast } = useToast()
|
||||||
|
|
||||||
const tenantId = ref('123')
|
// Cookie for server-side auth check
|
||||||
|
const tokenCookie = useCookie('token')
|
||||||
|
|
||||||
|
// Extract subdomain from hostname (e.g., tenant1.localhost → tenant1)
|
||||||
|
const getSubdomain = () => {
|
||||||
|
if (!import.meta.client) return null
|
||||||
|
const hostname = window.location.hostname
|
||||||
|
const parts = hostname.split('.')
|
||||||
|
|
||||||
|
console.log('Extracting subdomain from:', hostname, 'parts:', parts)
|
||||||
|
|
||||||
|
// For localhost development: tenant1.localhost or localhost
|
||||||
|
if (hostname === 'localhost' || hostname === '127.0.0.1') {
|
||||||
|
return null // Use default tenant for plain localhost
|
||||||
|
}
|
||||||
|
|
||||||
|
// For subdomains like tenant1.routebox.co or tenant1.localhost
|
||||||
|
if (parts.length >= 2 && parts[0] !== 'www') {
|
||||||
|
console.log('Using subdomain:', parts[0])
|
||||||
|
return parts[0] // Return subdomain
|
||||||
|
}
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
const subdomain = ref(getSubdomain())
|
||||||
const email = ref('')
|
const email = ref('')
|
||||||
const password = ref('')
|
const password = ref('')
|
||||||
const loading = ref(false)
|
const loading = ref(false)
|
||||||
@@ -17,12 +43,18 @@ const handleLogin = async () => {
|
|||||||
loading.value = true
|
loading.value = true
|
||||||
error.value = ''
|
error.value = ''
|
||||||
|
|
||||||
|
const headers: Record<string, string> = {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only send x-tenant-id if we have a subdomain
|
||||||
|
if (subdomain.value) {
|
||||||
|
headers['x-tenant-id'] = subdomain.value
|
||||||
|
}
|
||||||
|
|
||||||
const response = await fetch(`${config.public.apiBaseUrl}/api/auth/login`, {
|
const response = await fetch(`${config.public.apiBaseUrl}/api/auth/login`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers,
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'x-tenant-id': tenantId.value,
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
email: email.value,
|
email: email.value,
|
||||||
password: password.value,
|
password: password.value,
|
||||||
@@ -36,15 +68,23 @@ const handleLogin = async () => {
|
|||||||
|
|
||||||
const data = await response.json()
|
const data = await response.json()
|
||||||
|
|
||||||
// Store credentials
|
// Store credentials in localStorage
|
||||||
localStorage.setItem('tenantId', tenantId.value)
|
// Store the tenant ID that was used for login
|
||||||
|
const tenantToStore = subdomain.value || data.user?.tenantId || 'tenant1'
|
||||||
|
localStorage.setItem('tenantId', tenantToStore)
|
||||||
localStorage.setItem('token', data.access_token)
|
localStorage.setItem('token', data.access_token)
|
||||||
localStorage.setItem('user', JSON.stringify(data.user))
|
localStorage.setItem('user', JSON.stringify(data.user))
|
||||||
|
|
||||||
|
// Also store token in cookie for server-side auth check
|
||||||
|
tokenCookie.value = data.access_token
|
||||||
|
|
||||||
|
toast.success('Login successful!')
|
||||||
|
|
||||||
// Redirect to home
|
// Redirect to home
|
||||||
router.push('/')
|
router.push('/')
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
error.value = e.message || 'Login failed'
|
error.value = e.message || 'Login failed'
|
||||||
|
toast.error(e.message || 'Login failed')
|
||||||
} finally {
|
} finally {
|
||||||
loading.value = false
|
loading.value = false
|
||||||
}
|
}
|
||||||
@@ -65,10 +105,6 @@ const handleLogin = async () => {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="grid gap-6">
|
<div class="grid gap-6">
|
||||||
<div class="grid gap-2">
|
|
||||||
<Label for="tenantId">Tenant ID</Label>
|
|
||||||
<Input id="tenantId" v-model="tenantId" type="text" placeholder="123" required />
|
|
||||||
</div>
|
|
||||||
<div class="grid gap-2">
|
<div class="grid gap-2">
|
||||||
<Label for="email">Email</Label>
|
<Label for="email">Email</Label>
|
||||||
<Input id="email" v-model="email" type="email" placeholder="m@example.com" required />
|
<Input id="email" v-model="email" type="email" placeholder="m@example.com" required />
|
||||||
|
|||||||
@@ -1,19 +1,19 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import type { PrimitiveProps } from "reka-ui"
|
import type { PrimitiveProps } from 'reka-ui'
|
||||||
import type { HTMLAttributes } from "vue"
|
import type { HTMLAttributes } from 'vue'
|
||||||
import type { ButtonVariants } from "."
|
import type { ButtonVariants } from '.'
|
||||||
import { Primitive } from "reka-ui"
|
import { Primitive } from 'reka-ui'
|
||||||
import { cn } from "@/lib/utils"
|
import { cn } from '@/lib/utils'
|
||||||
import { buttonVariants } from "."
|
import { buttonVariants } from '.'
|
||||||
|
|
||||||
interface Props extends PrimitiveProps {
|
interface Props extends PrimitiveProps {
|
||||||
variant?: ButtonVariants["variant"]
|
variant?: ButtonVariants['variant']
|
||||||
size?: ButtonVariants["size"]
|
size?: ButtonVariants['size']
|
||||||
class?: HTMLAttributes["class"]
|
class?: HTMLAttributes['class']
|
||||||
}
|
}
|
||||||
|
|
||||||
const props = withDefaults(defineProps<Props>(), {
|
const props = withDefaults(defineProps<Props>(), {
|
||||||
as: "button",
|
as: 'button',
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
@@ -1,38 +1,36 @@
|
|||||||
import type { VariantProps } from "class-variance-authority"
|
import type { VariantProps } from 'class-variance-authority'
|
||||||
import { cva } from "class-variance-authority"
|
import { cva } from 'class-variance-authority'
|
||||||
|
|
||||||
export { default as Button } from "./Button.vue"
|
export { default as Button } from './Button.vue'
|
||||||
|
|
||||||
export const buttonVariants = cva(
|
export const buttonVariants = cva(
|
||||||
"inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-md text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0",
|
'inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-md text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0',
|
||||||
{
|
{
|
||||||
variants: {
|
variants: {
|
||||||
variant: {
|
variant: {
|
||||||
default: "bg-primary text-primary-foreground shadow hover:bg-primary/90",
|
default: 'bg-primary text-primary-foreground shadow hover:bg-primary/90',
|
||||||
destructive:
|
destructive: 'bg-destructive text-destructive-foreground shadow-sm hover:bg-destructive/90',
|
||||||
"bg-destructive text-destructive-foreground shadow-sm hover:bg-destructive/90",
|
|
||||||
outline:
|
outline:
|
||||||
"border border-input bg-background shadow-sm hover:bg-accent hover:text-accent-foreground",
|
'border border-input bg-background shadow-sm hover:bg-accent hover:text-accent-foreground',
|
||||||
secondary:
|
secondary: 'bg-secondary text-secondary-foreground shadow-sm hover:bg-secondary/80',
|
||||||
"bg-secondary text-secondary-foreground shadow-sm hover:bg-secondary/80",
|
ghost: 'hover:bg-accent hover:text-accent-foreground',
|
||||||
ghost: "hover:bg-accent hover:text-accent-foreground",
|
link: 'text-primary underline-offset-4 hover:underline',
|
||||||
link: "text-primary underline-offset-4 hover:underline",
|
|
||||||
},
|
},
|
||||||
size: {
|
size: {
|
||||||
"default": "h-9 px-4 py-2",
|
default: 'h-9 px-4 py-2',
|
||||||
"xs": "h-7 rounded px-2",
|
xs: 'h-7 rounded px-2',
|
||||||
"sm": "h-8 rounded-md px-3 text-xs",
|
sm: 'h-8 rounded-md px-3 text-xs',
|
||||||
"lg": "h-10 rounded-md px-8",
|
lg: 'h-10 rounded-md px-8',
|
||||||
"icon": "h-9 w-9",
|
icon: 'h-9 w-9',
|
||||||
"icon-sm": "size-8",
|
'icon-sm': 'size-8',
|
||||||
"icon-lg": "size-10",
|
'icon-lg': 'size-10',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
defaultVariants: {
|
defaultVariants: {
|
||||||
variant: "default",
|
variant: 'default',
|
||||||
size: "default",
|
size: 'default',
|
||||||
},
|
},
|
||||||
},
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
export type ButtonVariants = VariantProps<typeof buttonVariants>
|
export type ButtonVariants = VariantProps<typeof buttonVariants>
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import { Button } from '@/components/ui/button'
|
|||||||
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'
|
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'
|
||||||
import { CalendarIcon } from 'lucide-vue-next'
|
import { CalendarIcon } from 'lucide-vue-next'
|
||||||
import { cn } from '@/lib/utils'
|
import { cn } from '@/lib/utils'
|
||||||
import { CalendarDate, type DateValue } from '@internationalized/date'
|
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
modelValue?: Date | string | null
|
modelValue?: Date | string | null
|
||||||
@@ -23,27 +22,18 @@ const emit = defineEmits<{
|
|||||||
'update:modelValue': [value: Date | null]
|
'update:modelValue': [value: Date | null]
|
||||||
}>()
|
}>()
|
||||||
|
|
||||||
const placeholder = ref<DateValue>(new CalendarDate(new Date().getFullYear(), new Date().getMonth() + 1, new Date().getDate()))
|
const value = computed({
|
||||||
|
|
||||||
const value = computed<DateValue | undefined>({
|
|
||||||
get: () => {
|
get: () => {
|
||||||
if (!props.modelValue) return undefined
|
if (!props.modelValue) return undefined
|
||||||
const date = props.modelValue instanceof Date ? props.modelValue : new Date(props.modelValue)
|
return props.modelValue instanceof Date ? props.modelValue : new Date(props.modelValue)
|
||||||
return new CalendarDate(date.getFullYear(), date.getMonth() + 1, date.getDate())
|
|
||||||
},
|
},
|
||||||
set: (dateValue) => {
|
set: (date) => {
|
||||||
if (!dateValue) {
|
emit('update:modelValue', date || null)
|
||||||
emit('update:modelValue', null)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
const jsDate = new Date(dateValue.year, dateValue.month - 1, dateValue.day)
|
|
||||||
emit('update:modelValue', jsDate)
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
const formatDate = (dateValue: DateValue | undefined) => {
|
const formatDate = (date: Date | undefined) => {
|
||||||
if (!dateValue) return props.placeholder
|
if (!date) return props.placeholder
|
||||||
const date = new Date(dateValue.year, dateValue.month - 1, dateValue.day)
|
|
||||||
return date.toLocaleDateString('en-US', {
|
return date.toLocaleDateString('en-US', {
|
||||||
year: 'numeric',
|
year: 'numeric',
|
||||||
month: 'long',
|
month: 'long',
|
||||||
@@ -68,7 +58,7 @@ const formatDate = (dateValue: DateValue | undefined) => {
|
|||||||
</Button>
|
</Button>
|
||||||
</PopoverTrigger>
|
</PopoverTrigger>
|
||||||
<PopoverContent class="w-auto p-0">
|
<PopoverContent class="w-auto p-0">
|
||||||
<Calendar v-model="value" :placeholder="placeholder" />
|
<Calendar v-model="value" initial-focus />
|
||||||
</PopoverContent>
|
</PopoverContent>
|
||||||
</Popover>
|
</Popover>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import type { HTMLAttributes } from "vue"
|
|||||||
import { reactiveOmit } from "@vueuse/core"
|
import { reactiveOmit } from "@vueuse/core"
|
||||||
import {
|
import {
|
||||||
PopoverContent,
|
PopoverContent,
|
||||||
|
PopoverPortal,
|
||||||
useForwardPropsEmits,
|
useForwardPropsEmits,
|
||||||
} from "reka-ui"
|
} from "reka-ui"
|
||||||
import { cn } from "@/lib/utils"
|
import { cn } from "@/lib/utils"
|
||||||
@@ -27,15 +28,17 @@ const forwarded = useForwardPropsEmits(delegatedProps, emits)
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<PopoverContent
|
<PopoverPortal>
|
||||||
v-bind="{ ...forwarded, ...$attrs }"
|
<PopoverContent
|
||||||
:class="
|
v-bind="{ ...forwarded, ...$attrs }"
|
||||||
cn(
|
:class="
|
||||||
'z-50 w-72 rounded-md border bg-popover p-4 text-popover-foreground shadow-md outline-none data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2',
|
cn(
|
||||||
props.class,
|
'z-50 w-72 rounded-md border bg-popover p-4 text-popover-foreground shadow-md outline-none data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2',
|
||||||
)
|
props.class,
|
||||||
"
|
)
|
||||||
>
|
"
|
||||||
<slot />
|
>
|
||||||
</PopoverContent>
|
<slot />
|
||||||
|
</PopoverContent>
|
||||||
|
</PopoverPortal>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -1,18 +0,0 @@
|
|||||||
<script setup lang="ts">
|
|
||||||
import { TabsRoot, type TabsRootProps } from 'radix-vue'
|
|
||||||
import { computed, type HTMLAttributes } from 'vue'
|
|
||||||
import { cn } from '@/lib/utils'
|
|
||||||
|
|
||||||
const props = defineProps<TabsRootProps & { class?: HTMLAttributes['class'] }>()
|
|
||||||
|
|
||||||
const delegatedProps = computed(() => {
|
|
||||||
const { class: _, ...delegated } = props
|
|
||||||
return delegated
|
|
||||||
})
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<template>
|
|
||||||
<TabsRoot v-bind="delegatedProps" :class="cn('', props.class)">
|
|
||||||
<slot />
|
|
||||||
</TabsRoot>
|
|
||||||
</template>
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
<script setup lang="ts">
|
|
||||||
import { TabsContent, type TabsContentProps } from 'radix-vue'
|
|
||||||
import { computed, type HTMLAttributes } from 'vue'
|
|
||||||
import { cn } from '@/lib/utils'
|
|
||||||
|
|
||||||
const props = defineProps<TabsContentProps & { class?: HTMLAttributes['class'] }>()
|
|
||||||
|
|
||||||
const delegatedProps = computed(() => {
|
|
||||||
const { class: _, ...delegated } = props
|
|
||||||
return delegated
|
|
||||||
})
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<template>
|
|
||||||
<TabsContent
|
|
||||||
v-bind="delegatedProps"
|
|
||||||
:class="
|
|
||||||
cn(
|
|
||||||
'mt-2 ring-offset-background focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2',
|
|
||||||
props.class
|
|
||||||
)
|
|
||||||
"
|
|
||||||
>
|
|
||||||
<slot />
|
|
||||||
</TabsContent>
|
|
||||||
</template>
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
<script setup lang="ts">
|
|
||||||
import { TabsList, type TabsListProps } from 'radix-vue'
|
|
||||||
import { computed, type HTMLAttributes } from 'vue'
|
|
||||||
import { cn } from '@/lib/utils'
|
|
||||||
|
|
||||||
const props = defineProps<TabsListProps & { class?: HTMLAttributes['class'] }>()
|
|
||||||
|
|
||||||
const delegatedProps = computed(() => {
|
|
||||||
const { class: _, ...delegated } = props
|
|
||||||
return delegated
|
|
||||||
})
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<template>
|
|
||||||
<TabsList
|
|
||||||
v-bind="delegatedProps"
|
|
||||||
:class="
|
|
||||||
cn(
|
|
||||||
'inline-flex h-9 items-center justify-center rounded-lg bg-muted p-1 text-muted-foreground',
|
|
||||||
props.class
|
|
||||||
)
|
|
||||||
"
|
|
||||||
>
|
|
||||||
<slot />
|
|
||||||
</TabsList>
|
|
||||||
</template>
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
<script setup lang="ts">
|
|
||||||
import { TabsTrigger, type TabsTriggerProps } from 'radix-vue'
|
|
||||||
import { computed, type HTMLAttributes } from 'vue'
|
|
||||||
import { cn } from '@/lib/utils'
|
|
||||||
|
|
||||||
const props = defineProps<TabsTriggerProps & { class?: HTMLAttributes['class'] }>()
|
|
||||||
|
|
||||||
const delegatedProps = computed(() => {
|
|
||||||
const { class: _, ...delegated } = props
|
|
||||||
return delegated
|
|
||||||
})
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<template>
|
|
||||||
<TabsTrigger
|
|
||||||
v-bind="delegatedProps"
|
|
||||||
:class="
|
|
||||||
cn(
|
|
||||||
'inline-flex items-center justify-center whitespace-nowrap rounded-md px-3 py-1 text-sm font-medium ring-offset-background transition-all focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 data-[state=active]:bg-background data-[state=active]:text-foreground data-[state=active]:shadow',
|
|
||||||
props.class
|
|
||||||
)
|
|
||||||
"
|
|
||||||
>
|
|
||||||
<slot />
|
|
||||||
</TabsTrigger>
|
|
||||||
</template>
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
export { default as Tabs } from './Tabs.vue'
|
|
||||||
export { default as TabsContent } from './TabsContent.vue'
|
|
||||||
export { default as TabsList } from './TabsList.vue'
|
|
||||||
export { default as TabsTrigger } from './TabsTrigger.vue'
|
|
||||||
@@ -139,7 +139,7 @@ const getFieldsBySection = (section: FieldSection) => {
|
|||||||
<div class="grid gap-6 md:grid-cols-2">
|
<div class="grid gap-6 md:grid-cols-2">
|
||||||
<FieldRenderer
|
<FieldRenderer
|
||||||
v-for="field in getFieldsBySection(section)"
|
v-for="field in getFieldsBySection(section)"
|
||||||
:key="field?.id"
|
:key="field.id"
|
||||||
:field="field"
|
:field="field"
|
||||||
:model-value="data[field.apiName]"
|
:model-value="data[field.apiName]"
|
||||||
:mode="ViewMode.DETAIL"
|
:mode="ViewMode.DETAIL"
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
export const useApi = () => {
|
export const useApi = () => {
|
||||||
const config = useRuntimeConfig()
|
const config = useRuntimeConfig()
|
||||||
|
const router = useRouter()
|
||||||
|
const { toast } = useToast()
|
||||||
|
const { isLoggedIn, logout } = useAuth()
|
||||||
|
|
||||||
// Use current domain for API calls (same subdomain routing)
|
// Use current domain for API calls (same subdomain routing)
|
||||||
const getApiBaseUrl = () => {
|
const getApiBaseUrl = () => {
|
||||||
@@ -34,13 +37,44 @@ export const useApi = () => {
|
|||||||
return headers
|
return headers
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const handleResponse = async (response: Response) => {
|
||||||
|
if (response.status === 401) {
|
||||||
|
// Unauthorized - not authenticated
|
||||||
|
if (import.meta.client) {
|
||||||
|
logout()
|
||||||
|
toast.error('Your session has expired. Please login again.')
|
||||||
|
router.push('/login')
|
||||||
|
}
|
||||||
|
throw new Error('Unauthorized')
|
||||||
|
}
|
||||||
|
|
||||||
|
if (response.status === 403) {
|
||||||
|
// Forbidden - not authorized
|
||||||
|
if (import.meta.client) {
|
||||||
|
toast.error('You do not have permission to perform this action.')
|
||||||
|
// Redirect to home if logged in, otherwise to login
|
||||||
|
if (isLoggedIn()) {
|
||||||
|
router.push('/')
|
||||||
|
} else {
|
||||||
|
router.push('/login')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new Error('Forbidden')
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`HTTP error! status: ${response.status}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.json()
|
||||||
|
}
|
||||||
|
|
||||||
const api = {
|
const api = {
|
||||||
async get(path: string) {
|
async get(path: string) {
|
||||||
const response = await fetch(`${getApiBaseUrl()}/api${path}`, {
|
const response = await fetch(`${getApiBaseUrl()}/api${path}`, {
|
||||||
headers: getHeaders(),
|
headers: getHeaders(),
|
||||||
})
|
})
|
||||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`)
|
return handleResponse(response)
|
||||||
return response.json()
|
|
||||||
},
|
},
|
||||||
|
|
||||||
async post(path: string, data: any) {
|
async post(path: string, data: any) {
|
||||||
@@ -49,8 +83,7 @@ export const useApi = () => {
|
|||||||
headers: getHeaders(),
|
headers: getHeaders(),
|
||||||
body: JSON.stringify(data),
|
body: JSON.stringify(data),
|
||||||
})
|
})
|
||||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`)
|
return handleResponse(response)
|
||||||
return response.json()
|
|
||||||
},
|
},
|
||||||
|
|
||||||
async put(path: string, data: any) {
|
async put(path: string, data: any) {
|
||||||
@@ -59,8 +92,7 @@ export const useApi = () => {
|
|||||||
headers: getHeaders(),
|
headers: getHeaders(),
|
||||||
body: JSON.stringify(data),
|
body: JSON.stringify(data),
|
||||||
})
|
})
|
||||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`)
|
return handleResponse(response)
|
||||||
return response.json()
|
|
||||||
},
|
},
|
||||||
|
|
||||||
async delete(path: string) {
|
async delete(path: string) {
|
||||||
@@ -68,8 +100,7 @@ export const useApi = () => {
|
|||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
headers: getHeaders(),
|
headers: getHeaders(),
|
||||||
})
|
})
|
||||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`)
|
return handleResponse(response)
|
||||||
return response.json()
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
32
frontend/composables/useAuth.ts
Normal file
32
frontend/composables/useAuth.ts
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
export const useAuth = () => {
|
||||||
|
const tokenCookie = useCookie('token')
|
||||||
|
|
||||||
|
const isLoggedIn = () => {
|
||||||
|
if (!import.meta.client) return false
|
||||||
|
const token = localStorage.getItem('token')
|
||||||
|
const tenantId = localStorage.getItem('tenantId')
|
||||||
|
return !!(token && tenantId)
|
||||||
|
}
|
||||||
|
|
||||||
|
const logout = () => {
|
||||||
|
if (import.meta.client) {
|
||||||
|
localStorage.removeItem('token')
|
||||||
|
localStorage.removeItem('tenantId')
|
||||||
|
localStorage.removeItem('user')
|
||||||
|
}
|
||||||
|
// Clear cookie for server-side check
|
||||||
|
tokenCookie.value = null
|
||||||
|
}
|
||||||
|
|
||||||
|
const getUser = () => {
|
||||||
|
if (!import.meta.client) return null
|
||||||
|
const userStr = localStorage.getItem('user')
|
||||||
|
return userStr ? JSON.parse(userStr) : null
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
isLoggedIn,
|
||||||
|
logout,
|
||||||
|
getUser,
|
||||||
|
}
|
||||||
|
}
|
||||||
20
frontend/composables/useToast.ts
Normal file
20
frontend/composables/useToast.ts
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import { toast as sonnerToast } from 'vue-sonner'
|
||||||
|
|
||||||
|
export const useToast = () => {
|
||||||
|
const toast = {
|
||||||
|
success: (message: string) => {
|
||||||
|
sonnerToast.success(message)
|
||||||
|
},
|
||||||
|
error: (message: string) => {
|
||||||
|
sonnerToast.error(message)
|
||||||
|
},
|
||||||
|
info: (message: string) => {
|
||||||
|
sonnerToast.info(message)
|
||||||
|
},
|
||||||
|
warning: (message: string) => {
|
||||||
|
sonnerToast.warning(message)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
return { toast }
|
||||||
|
}
|
||||||
38
frontend/middleware/auth.global.ts
Normal file
38
frontend/middleware/auth.global.ts
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
export default defineNuxtRouteMiddleware((to, from) => {
|
||||||
|
// Allow pages to opt-out of auth with definePageMeta({ auth: false })
|
||||||
|
if (to.meta.auth === false) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Public routes that don't require authentication
|
||||||
|
const publicRoutes = ['/login', '/register']
|
||||||
|
|
||||||
|
if (publicRoutes.includes(to.path)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const token = useCookie('token')
|
||||||
|
const authMessage = useCookie('authMessage')
|
||||||
|
|
||||||
|
// Routes that don't need a toast message (user knows they need to login)
|
||||||
|
const silentRoutes = ['/']
|
||||||
|
|
||||||
|
// Check token cookie (works on both server and client)
|
||||||
|
if (!token.value) {
|
||||||
|
if (!silentRoutes.includes(to.path)) {
|
||||||
|
authMessage.value = 'Please login to access this page'
|
||||||
|
}
|
||||||
|
return navigateTo('/login')
|
||||||
|
}
|
||||||
|
|
||||||
|
// On client side, also verify localStorage is in sync
|
||||||
|
if (import.meta.client) {
|
||||||
|
const { isLoggedIn } = useAuth()
|
||||||
|
if (!isLoggedIn()) {
|
||||||
|
if (!silentRoutes.includes(to.path)) {
|
||||||
|
authMessage.value = 'Please login to access this page'
|
||||||
|
}
|
||||||
|
return navigateTo('/login')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
@@ -41,11 +41,6 @@ export default defineNuxtConfig({
|
|||||||
|
|
||||||
typescript: {
|
typescript: {
|
||||||
strict: true,
|
strict: true,
|
||||||
tsConfig: {
|
|
||||||
compilerOptions: {
|
|
||||||
verbatimModuleSyntax: false,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
|
|
||||||
features: {
|
features: {
|
||||||
@@ -53,14 +48,11 @@ export default defineNuxtConfig({
|
|||||||
},
|
},
|
||||||
|
|
||||||
vite: {
|
vite: {
|
||||||
optimizeDeps: {
|
|
||||||
include: ['@internationalized/date'],
|
|
||||||
},
|
|
||||||
server: {
|
server: {
|
||||||
hmr: {
|
hmr: {
|
||||||
clientPort: 3001,
|
clientPort: 3001,
|
||||||
},
|
},
|
||||||
allowedHosts: ['.routebox.co', 'localhost', '127.0.0.1'],
|
allowedHosts: ['.routebox.co', 'localhost', '127.0.0.1',],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
16
frontend/package-lock.json
generated
16
frontend/package-lock.json
generated
@@ -9,7 +9,6 @@
|
|||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"hasInstallScript": true,
|
"hasInstallScript": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@internationalized/date": "^3.10.1",
|
|
||||||
"@nuxtjs/tailwindcss": "^6.11.4",
|
"@nuxtjs/tailwindcss": "^6.11.4",
|
||||||
"@vueuse/core": "^10.11.1",
|
"@vueuse/core": "^10.11.1",
|
||||||
"class-variance-authority": "^0.7.0",
|
"class-variance-authority": "^0.7.0",
|
||||||
@@ -21,7 +20,8 @@
|
|||||||
"shadcn-nuxt": "^2.3.3",
|
"shadcn-nuxt": "^2.3.3",
|
||||||
"tailwind-merge": "^2.2.1",
|
"tailwind-merge": "^2.2.1",
|
||||||
"vue": "^3.4.15",
|
"vue": "^3.4.15",
|
||||||
"vue-router": "^4.2.5"
|
"vue-router": "^4.2.5",
|
||||||
|
"vue-sonner": "^1.3.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@nuxtjs/color-mode": "^3.3.2",
|
"@nuxtjs/color-mode": "^3.3.2",
|
||||||
@@ -1259,9 +1259,9 @@
|
|||||||
"peer": true
|
"peer": true
|
||||||
},
|
},
|
||||||
"node_modules/@internationalized/date": {
|
"node_modules/@internationalized/date": {
|
||||||
"version": "3.10.1",
|
"version": "3.10.0",
|
||||||
"resolved": "https://registry.npmjs.org/@internationalized/date/-/date-3.10.1.tgz",
|
"resolved": "https://registry.npmjs.org/@internationalized/date/-/date-3.10.0.tgz",
|
||||||
"integrity": "sha512-oJrXtQiAXLvT9clCf1K4kxp3eKsQhIaZqxEyowkBcsvZDdZkbWrVmnGknxs5flTD0VGsxrxKgBCZty1EzoiMzA==",
|
"integrity": "sha512-oxDR/NTEJ1k+UFVQElaNIk65E/Z83HK1z1WI3lQyhTtnNg4R5oVXaPzK3jcpKG8UHKDVuDQHzn+wsxSz8RP3aw==",
|
||||||
"license": "Apache-2.0",
|
"license": "Apache-2.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@swc/helpers": "^0.5.0"
|
"@swc/helpers": "^0.5.0"
|
||||||
@@ -16036,6 +16036,12 @@
|
|||||||
"vue": "^3.5.0"
|
"vue": "^3.5.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/vue-sonner": {
|
||||||
|
"version": "1.3.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/vue-sonner/-/vue-sonner-1.3.2.tgz",
|
||||||
|
"integrity": "sha512-UbZ48E9VIya3ToiRHAZUbodKute/z/M1iT8/3fU8zEbwBRE11AKuHikssv18LMk2gTTr6eMQT4qf6JoLHWuj/A==",
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
"node_modules/webidl-conversions": {
|
"node_modules/webidl-conversions": {
|
||||||
"version": "3.0.1",
|
"version": "3.0.1",
|
||||||
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz",
|
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz",
|
||||||
|
|||||||
@@ -15,7 +15,6 @@
|
|||||||
"format": "prettier --write \"**/*.{js,ts,vue,json,css,scss,md}\""
|
"format": "prettier --write \"**/*.{js,ts,vue,json,css,scss,md}\""
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@internationalized/date": "^3.10.1",
|
|
||||||
"@nuxtjs/tailwindcss": "^6.11.4",
|
"@nuxtjs/tailwindcss": "^6.11.4",
|
||||||
"@vueuse/core": "^10.11.1",
|
"@vueuse/core": "^10.11.1",
|
||||||
"class-variance-authority": "^0.7.0",
|
"class-variance-authority": "^0.7.0",
|
||||||
@@ -27,7 +26,8 @@
|
|||||||
"shadcn-nuxt": "^2.3.3",
|
"shadcn-nuxt": "^2.3.3",
|
||||||
"tailwind-merge": "^2.2.1",
|
"tailwind-merge": "^2.2.1",
|
||||||
"vue": "^3.4.15",
|
"vue": "^3.4.15",
|
||||||
"vue-router": "^4.2.5"
|
"vue-router": "^4.2.5",
|
||||||
|
"vue-sonner": "^1.3.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@nuxtjs/color-mode": "^3.3.2",
|
"@nuxtjs/color-mode": "^3.3.2",
|
||||||
|
|||||||
@@ -7,13 +7,11 @@ import EditView from '@/components/views/EditView.vue'
|
|||||||
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'
|
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'
|
||||||
import {
|
import {
|
||||||
FieldType,
|
FieldType,
|
||||||
ViewMode
|
ViewMode,
|
||||||
} from '@/types/field-types'
|
type ListViewConfig,
|
||||||
import type {
|
type DetailViewConfig,
|
||||||
ListViewConfig,
|
type EditViewConfig,
|
||||||
DetailViewConfig,
|
type FieldConfig
|
||||||
EditViewConfig,
|
|
||||||
FieldConfig
|
|
||||||
} from '@/types/field-types'
|
} from '@/types/field-types'
|
||||||
|
|
||||||
// Example: Contact Object
|
// Example: Contact Object
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<NuxtLayout name="default">
|
<NuxtLayout name="default">
|
||||||
<div class="text-center space-y-6">
|
<div class="text-center space-y-6">
|
||||||
|
|||||||
@@ -1,6 +1,25 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { LayoutGrid } from 'lucide-vue-next'
|
import { LayoutGrid } from 'lucide-vue-next'
|
||||||
import LoginForm from '@/components/LoginForm.vue'
|
import LoginForm from '@/components/LoginForm.vue'
|
||||||
|
|
||||||
|
// Skip auth middleware for login page
|
||||||
|
definePageMeta({
|
||||||
|
auth: false
|
||||||
|
})
|
||||||
|
|
||||||
|
const { toast } = useToast()
|
||||||
|
|
||||||
|
// Check for auth message from cookie
|
||||||
|
const authMessage = useCookie('authMessage')
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
if (authMessage.value) {
|
||||||
|
console.log('Displaying auth message: ' + authMessage.value)
|
||||||
|
toast.error(authMessage.value)
|
||||||
|
// Clear the message after displaying
|
||||||
|
authMessage.value = null
|
||||||
|
}
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
|||||||
@@ -17,11 +17,6 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<form @submit.prevent="handleRegister" class="space-y-4">
|
<form @submit.prevent="handleRegister" class="space-y-4">
|
||||||
<div class="space-y-2">
|
|
||||||
<Label for="tenantId">Tenant ID</Label>
|
|
||||||
<Input id="tenantId" v-model="tenantId" type="text" required placeholder="123" />
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="space-y-2">
|
<div class="space-y-2">
|
||||||
<Label for="email">Email</Label>
|
<Label for="email">Email</Label>
|
||||||
<Input
|
<Input
|
||||||
@@ -74,10 +69,29 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
|
// Skip auth middleware for register page
|
||||||
|
definePageMeta({
|
||||||
|
auth: false
|
||||||
|
})
|
||||||
|
|
||||||
const config = useRuntimeConfig()
|
const config = useRuntimeConfig()
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
|
|
||||||
const tenantId = ref('123')
|
// Extract subdomain from hostname
|
||||||
|
const getSubdomain = () => {
|
||||||
|
if (!import.meta.client) return null
|
||||||
|
const hostname = window.location.hostname
|
||||||
|
const parts = hostname.split('.')
|
||||||
|
if (hostname === 'localhost' || hostname === '127.0.0.1') {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
if (parts.length > 1 && parts[0] !== 'www') {
|
||||||
|
return parts[0]
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
const subdomain = ref(getSubdomain())
|
||||||
const email = ref('')
|
const email = ref('')
|
||||||
const password = ref('')
|
const password = ref('')
|
||||||
const firstName = ref('')
|
const firstName = ref('')
|
||||||
@@ -92,12 +106,17 @@ const handleRegister = async () => {
|
|||||||
error.value = ''
|
error.value = ''
|
||||||
success.value = false
|
success.value = false
|
||||||
|
|
||||||
|
const headers: Record<string, string> = {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
}
|
||||||
|
|
||||||
|
if (subdomain.value) {
|
||||||
|
headers['x-tenant-id'] = subdomain.value
|
||||||
|
}
|
||||||
|
|
||||||
const response = await fetch(`${config.public.apiBaseUrl}/api/auth/register`, {
|
const response = await fetch(`${config.public.apiBaseUrl}/api/auth/register`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers,
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'x-tenant-id': tenantId.value,
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
email: email.value,
|
email: email.value,
|
||||||
password: password.value,
|
password: password.value,
|
||||||
|
|||||||
@@ -40,10 +40,6 @@ export default {
|
|||||||
DEFAULT: 'hsl(var(--card))',
|
DEFAULT: 'hsl(var(--card))',
|
||||||
foreground: 'hsl(var(--card-foreground))',
|
foreground: 'hsl(var(--card-foreground))',
|
||||||
},
|
},
|
||||||
popover: {
|
|
||||||
DEFAULT: 'hsl(var(--popover))',
|
|
||||||
foreground: 'hsl(var(--popover-foreground))',
|
|
||||||
},
|
|
||||||
sidebar: {
|
sidebar: {
|
||||||
DEFAULT: 'hsl(var(--sidebar-background))',
|
DEFAULT: 'hsl(var(--sidebar-background))',
|
||||||
foreground: 'hsl(var(--sidebar-foreground))',
|
foreground: 'hsl(var(--sidebar-foreground))',
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
export * from './field-types'
|
|
||||||
6
package-lock.json
generated
Normal file
6
package-lock.json
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"name": "neo",
|
||||||
|
"lockfileVersion": 3,
|
||||||
|
"requires": true,
|
||||||
|
"packages": {}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user