- Created TenantAdminService, FormAdminService, and UserAdminService - Separated business operations (CRUD, audit logs, generation logic) from routes
77 lines
2.3 KiB
Python
77 lines
2.3 KiB
Python
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}
|
|
)
|