Multi-Tenant Architecture
Overviewβ
The AttuneLogic API implements a robust multi-tenant architecture designed to serve multiple business types, primarily focusing on trucking and service/repair industries. The system uses a hybrid tenancy model that combines database-level isolation with shared infrastructure.
Core Componentsβ
1. Tenant Model (Customer)β
CustomerSchema = {
// Tenant Identifier
slug: { type: String, unique: true },
// Business Type
appType: { type: String, enum: ["trucking", "serviceRepair"] },
// Configuration
metadata: Schema.Types.Mixed,
// Status
active: Boolean,
paymentStatus: String,
};
2. Tenant Contextβ
The system maintains tenant context through:
- URL-based identification (
slug) - Authentication tokens
- Request middleware
- Parent company verification
Architectural Patternsβ
1. Data Isolationβ
// Example of tenant scoping in queries
async function findForTenant(model, query, tenant) {
return model.find({
...query,
parentCompany: tenant._id,
});
}
2. Feature Segregationβ
// Example of industry-specific feature toggle
if (customer.appType === "trucking") {
// Enable trucking-specific features
// - Route optimization
// - Fleet management
// - Driver tracking
} else if (customer.appType === "serviceRepair") {
// Enable service industry features
// - Service area management
// - Technician scheduling
// - Work order tracking
}
Security Implementationβ
1. Tenant Isolation Middlewareβ
const verifyParent = async (req, res, next) => {
// Verify user belongs to the correct tenant
// Prevent cross-tenant data access
// Validate tenant-specific permissions
};
2. Access Controlβ
- Tenant-level administrators
- Role-based access within tenants
- Industry-specific permissions
Configuration Managementβ
1. Default Configurationsβ
- Located in
@services/config/default-configs/ - Industry-specific defaults
- Customizable per tenant
2. Custom Configurationsβ
- Stored in tenant's metadata
- Override capability
- Feature flags
Database Strategyβ
1. Collection Structureβ
- All collections include
parentCompanyreference - Indexes on tenant identifiers
- Industry-specific collections when needed
2. Query Patternsβ
// Example of tenant-scoped query
const jobs = await Job.find({
parentCompany: req.user.parentCompany,
// other query parameters
}).lean();
API Designβ
1. Routingβ
- Version-based (
/api/v1/) - Tenant identification
- Industry-specific endpoints
2. Request Flowβ
Request β Tenant Resolution β Authentication β Parent Verification β Handler
Industry-Specific Implementationsβ
1. Trucking Industryβ
- Fleet management module
- Route optimization
- Driver management
- Load tracking
- Maintenance scheduling
2. Service/Repair Industryβ
- Service area management
- Technician dispatching
- Work order system
- Customer management
- Inventory tracking
Scalability Considerationsβ
1. Databaseβ
- Indexed tenant queries
- Efficient data partitioning
- Query optimization
2. Cachingβ
- Tenant-aware caching
- Configuration caching
- Session management
Best Practicesβ
1. Development Guidelinesβ
- Always include tenant context
- Validate cross-tenant operations
- Use industry-specific feature flags
- Implement proper error boundaries
2. Security Guidelinesβ
- Strict tenant isolation
- Role-based access control
- Data validation per tenant
- Audit logging
Common Patternsβ
1. Controller Patternβ
async function listResources(req, res) {
const { parentCompany } = req.user;
const resources = await Resource.find({ parentCompany });
return res.json({ resources });
}
2. Service Patternβ
class TenantService {
constructor(tenant) {
this.tenant = tenant;
}
async getConfiguration() {
return {
...defaultConfig,
...this.tenant.metadata,
};
}
}
Testing Strategyβ
1. Tenant Isolation Testsβ
- Verify data isolation
- Test cross-tenant access prevention
- Validate tenant-specific features
2. Industry-Specific Testsβ
- Test industry features
- Validate business rules
- Check configuration management
Monitoring and Debuggingβ
1. Loggingβ
- Tenant-aware logging
- Industry-specific metrics
- Performance monitoring
2. Error Handlingβ
- Tenant-specific error messages
- Industry-context preservation
- Proper error categorization
Future Considerationsβ
- Enhanced Tenant Analytics
- Dynamic Feature Management
- Improved Configuration System
- Advanced Industry Customization
- Cross-Tenant Collaboration Features
Last Updated: [Current Date] Version: 1.0