Tenants Module Specification
1. Purpose & Overview
The module manages account tenancy, agency organization structures, encrypted credential storage (), tenant middleware resolution, and platform audit logging.
tenantsTenantConfig2. Responsibilities
- Maintain ,
Tenant,Agency, andTenantConfigdomain models.AuditLog - Enforce tenant request resolution via .
TenantMiddleware - Perform Fernet AES-256 symmetric encryption/decryption for Meta WhatsApp access tokens.
3. Directory Structure
text
services/api/tenants/
├── admin.py
├── apps.py
├── middleware.py
├── migrations/
├── models.py
├── serializers.py
└── views.py
4. Models & Database Schemas
python
class Agency(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4)
name = models.CharField(max_length=255)
slug = models.SlugField(unique=True)
class Tenant(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4)
agency = models.ForeignKey(Agency, on_delete=models.CASCADE, related_name='tenants')
name = models.CharField(max_length=255)
slug = models.SlugField(unique=True)
class TenantConfig(models.Model):
tenant = models.ForeignKey(Tenant, on_delete=models.CASCADE)
provider = models.CharField(max_length=50) # 'meta'
key_name = models.CharField(max_length=100) # 'wa_access_token'
encrypted_value = models.TextField()
class AuditLog(models.Model):
tenant = models.ForeignKey(Tenant, on_delete=models.SET_NULL, null=True)
user = models.ForeignKey('users.User', on_delete=models.SET_NULL, null=True)
action = models.CharField(max_length=100)
resource = models.CharField(max_length=100)
details = models.JSONField(default=dict)
created_at = models.DateTimeField(auto_now_add=True)
5. Services & Business Logic
- : Encrypts string using
TenantConfig.set_value(plain_value).Fernet(settings.FERNET_KEY) - : Decrypts
TenantConfig.get_value()back to plain string.encrypted_value - : Initializes Fernet instance with development fallback support.
TenantConfig.get_fernet()
6. Serializers & Data Transfer Objects
- : Handles client creation and automatically encrypts
TenantSerializer.wa_access_token - : Serializes system audit records for Super Admin inspection.
AuditLogSerializer
7. Views & API Endpoints
- : List tenants.
GET /api/clients/ - : Create tenant (encrypts access token).
POST /api/clients/ - : View tenant configurations.
GET /api/clients/{id}/configs/ - : View system audit logs.
GET /api/audit-logs/
8. Permissions & Role Rules
- : Can view and create all tenants and audit logs.
SUPER_ADMIN - : Can view clients linked to their agency.
AGENCY_ADMIN
9. Signals & Event Listeners
- on
post_save&Tenant: Automatically generatesUserrecords.AuditLog
10. Background Tasks & Celery Jobs
- None (Tenant configuration operations are synchronous).
11. Data Flow & External Dependencies
- Uses Python library for AES-256 secret encryption.
cryptography.fernet.Fernet
12. Business Rules & Validations
- must be a valid 32 url-safe base64-encoded string.
FERNET_KEY - In mode, fallback key
DEBUG=Trueis used if unconfigured.KHS39TFFYZFLCfUNv1NNbPRZN2C1w7FKYmsl4ZcJ-Ok=
13. Sequence Diagram (Mermaid)
Rendering diagram...
14. Known Limitations & Technical Debt
- Single primary Fernet key used for all tenants.
15. Future Improvements
- Support envelope encryption with per-tenant encryption keys stored in AWS KMS / Vault.