refactor: extract admin business logic to services
- Created TenantAdminService, FormAdminService, and UserAdminService - Separated business operations (CRUD, audit logs, generation logic) from routes
This commit is contained in:
1 parent
1038b4ff05
commit
e0081e6d46
3 files changed
+183
No files matched your search
@@ -0,0 +1,76 @@
|
||||
from typing import Optional, List
|
||||
from models import db, Tenant, FormConfig, Submission, UserRole
|
||||
from models import AuditLog
|
||||
import uuid
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class FormAdminService:
|
||||
@staticmethod
|
||||
def get_forms(user, tenant):
|
||||
if user.role == UserRole.ADMIN:
|
||||
return FormConfig.query.join(Tenant).order_by(FormConfig.created_at.desc()).all()
|
||||
elif tenant:
|
||||
return FormConfig.query.filter_by(tenant_id=tenant.id).order_by(FormConfig.created_at.desc()).all()
|
||||
return []
|
||||
|
||||
@staticmethod
|
||||
def create_form(tenant, name, description, form_structure, settings, is_active, user):
|
||||
form_id = f'form_{name.lower().replace(" ", "_")}_{str(uuid.uuid4())[:8]}'
|
||||
form = FormConfig(
|
||||
id=form_id,
|
||||
tenant_id=tenant.id,
|
||||
name=name,
|
||||
description=description,
|
||||
form_structure=form_structure,
|
||||
settings=settings,
|
||||
is_active=is_active
|
||||
)
|
||||
db.session.add(form)
|
||||
db.session.commit()
|
||||
|
||||
AuditLog.log_action(
|
||||
action='create',
|
||||
resource_type='form',
|
||||
user=user,
|
||||
tenant=tenant,
|
||||
resource_id=form.id,
|
||||
details={'name': name, 'tenant_id': tenant.id}
|
||||
)
|
||||
return form
|
||||
|
||||
@staticmethod
|
||||
def update_form(form, name, description, is_active, form_structure, settings, user, tenant):
|
||||
form.name = name if name else form.name
|
||||
form.description = description if description else form.description
|
||||
form.is_active = is_active
|
||||
if form_structure is not None:
|
||||
form.form_structure = form_structure
|
||||
if settings is not None:
|
||||
form.settings = settings
|
||||
|
||||
db.session.commit()
|
||||
|
||||
AuditLog.log_action(
|
||||
action='update',
|
||||
resource_type='form',
|
||||
user=user,
|
||||
tenant=tenant,
|
||||
resource_id=form.id,
|
||||
details={'name': form.name, 'is_active': form.is_active}
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def delete_form(form, user, tenant):
|
||||
db.session.delete(form)
|
||||
db.session.commit()
|
||||
|
||||
AuditLog.log_action(
|
||||
action='delete',
|
||||
resource_type='form',
|
||||
user=user,
|
||||
tenant=tenant,
|
||||
resource_id=form.id,
|
||||
details={'name': form.name}
|
||||
)
|
||||
@@ -0,0 +1,66 @@
|
||||
from typing import Optional, List
|
||||
from models import db, Tenant, FormConfig, Submission, UserRole, TenantStatus
|
||||
from models import AuditLog
|
||||
import uuid
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class TenantAdminService:
|
||||
@staticmethod
|
||||
def get_tenants(user):
|
||||
if user.role == UserRole.ADMIN:
|
||||
return Tenant.query.order_by(Tenant.created_at.desc()).all()
|
||||
return user.tenants if user else []
|
||||
|
||||
@staticmethod
|
||||
def create_tenant(name, public_key, allowed_origins, status, user):
|
||||
tenant_id = f'tenant_{name.lower().replace(" ", "_")}_{str(uuid.uuid4())[:8]}'
|
||||
tenant = Tenant(
|
||||
id=tenant_id,
|
||||
name=name,
|
||||
public_key=public_key or str(uuid.uuid4()).hex[:32],
|
||||
allowed_origins=allowed_origins,
|
||||
status=TenantStatus(status) if status in [s.value for s in TenantStatus] else TenantStatus.ACTIVE
|
||||
)
|
||||
db.session.add(tenant)
|
||||
db.session.commit()
|
||||
|
||||
AuditLog.log_action(
|
||||
action='create',
|
||||
resource_type='tenant',
|
||||
user=user,
|
||||
resource_id=tenant.id,
|
||||
details={'name': name, 'public_key': public_key}
|
||||
)
|
||||
return tenant
|
||||
|
||||
@staticmethod
|
||||
def update_tenant(tenant, name, public_key, allowed_origins, status, user):
|
||||
tenant.name = name if name else tenant.name
|
||||
tenant.public_key = public_key if public_key else tenant.public_key
|
||||
tenant.allowed_origins = allowed_origins if allowed_origins else tenant.allowed_origins
|
||||
if status in [s.value for s in TenantStatus]:
|
||||
tenant.status = TenantStatus(status)
|
||||
db.session.commit()
|
||||
|
||||
AuditLog.log_action(
|
||||
action='update',
|
||||
resource_type='tenant',
|
||||
user=user,
|
||||
resource_id=tenant.id,
|
||||
details={'name': tenant.name, 'status': tenant.status.value}
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def delete_tenant(tenant, user):
|
||||
db.session.delete(tenant)
|
||||
db.session.commit()
|
||||
|
||||
AuditLog.log_action(
|
||||
action='delete',
|
||||
resource_type='tenant',
|
||||
user=user,
|
||||
resource_id=tenant.id,
|
||||
details={'name': tenant.name}
|
||||
)
|
||||
@@ -0,0 +1,41 @@
|
||||
from typing import Optional, List
|
||||
from models import db, User, UserRole
|
||||
from models import AuditLog
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class UserAdminService:
|
||||
@staticmethod
|
||||
def get_users():
|
||||
return User.query.order_by(User.created_at.desc()).all()
|
||||
|
||||
@staticmethod
|
||||
def update_user(target_user, email, name, role, admin_user):
|
||||
target_user.email = email if email else target_user.email
|
||||
target_user.name = name if name else target_user.name
|
||||
if role in [r.value for r in UserRole]:
|
||||
target_user.role = UserRole(role)
|
||||
|
||||
db.session.commit()
|
||||
|
||||
AuditLog.log_action(
|
||||
action='update',
|
||||
resource_type='user',
|
||||
user=admin_user,
|
||||
resource_id=target_user.id,
|
||||
details={'email': target_user.email, 'role': target_user.role.value}
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def delete_user(target_user, admin_user):
|
||||
db.session.delete(target_user)
|
||||
db.session.commit()
|
||||
|
||||
AuditLog.log_action(
|
||||
action='delete',
|
||||
resource_type='user',
|
||||
user=admin_user,
|
||||
resource_id=target_user.id,
|
||||
details={'email': target_user.email}
|
||||
)
|
||||
Reference in new issue
Block a user