frontend admin refakctorálás
This commit is contained in:
@@ -1188,6 +1188,152 @@ async def review_contribution(
|
||||
}
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# VALIDATION RULES (Provider Validation Engine)
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class ValidationRuleUpdate(BaseModel):
|
||||
"""Update payload for a single validation rule."""
|
||||
rule_value: int = Field(..., ge=0, le=100, description="New integer value for this rule (0-100)")
|
||||
description: Optional[str] = Field(None, max_length=500, description="Updated description")
|
||||
|
||||
|
||||
class ValidationRuleBulkUpdate(BaseModel):
|
||||
"""Bulk update payload — dict of rule_key → new_value."""
|
||||
rules: Dict[str, int] = Field(
|
||||
..., description="Mapping of rule_key → new integer value (0-100)"
|
||||
)
|
||||
|
||||
|
||||
@router.get("/validation-rules", response_model=List[Dict[str, Any]])
|
||||
async def list_validation_rules(
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(deps.get_current_active_user),
|
||||
_: User = Depends(deps.RequirePermission("gamification:manage")),
|
||||
):
|
||||
"""
|
||||
Fetch all ValidationRule records from the database.
|
||||
|
||||
Returns a list of {id, rule_key, rule_value, description, updated_at}.
|
||||
These rules drive the ValidationEngine for provider scoring and
|
||||
auto-approval thresholds.
|
||||
"""
|
||||
from app.models.gamification.validation_rule import ValidationRule
|
||||
|
||||
stmt = select(ValidationRule).order_by(ValidationRule.rule_key)
|
||||
result = await db.execute(stmt)
|
||||
rules = result.scalars().all()
|
||||
|
||||
return [
|
||||
{
|
||||
"id": r.id,
|
||||
"rule_key": r.rule_key,
|
||||
"rule_value": r.rule_value,
|
||||
"description": r.description,
|
||||
"updated_at": r.updated_at.isoformat() if r.updated_at else None,
|
||||
}
|
||||
for r in rules
|
||||
]
|
||||
|
||||
|
||||
@router.put("/validation-rules", response_model=Dict[str, Any])
|
||||
async def bulk_update_validation_rules(
|
||||
data: ValidationRuleBulkUpdate,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(deps.get_current_active_user),
|
||||
_: User = Depends(deps.RequirePermission("gamification:manage")),
|
||||
):
|
||||
"""
|
||||
Bulk-update ValidationRule values.
|
||||
|
||||
Accepts a dict of rule_key → new_value. Iterates through each entry
|
||||
and updates rule_value in the database. Unknown keys are silently skipped.
|
||||
|
||||
Returns a summary of updated rules.
|
||||
"""
|
||||
from app.models.gamification.validation_rule import ValidationRule
|
||||
|
||||
updated: List[Dict[str, Any]] = []
|
||||
errors: List[Dict[str, Any]] = []
|
||||
|
||||
for rule_key, new_value in data.rules.items():
|
||||
stmt = select(ValidationRule).where(ValidationRule.rule_key == rule_key)
|
||||
rule = (await db.execute(stmt)).scalar_one_or_none()
|
||||
|
||||
if not rule:
|
||||
errors.append({"rule_key": rule_key, "error": "Rule not found"})
|
||||
logger.warning(f"Validation rule '{rule_key}' not found, skipping.")
|
||||
continue
|
||||
|
||||
if not (0 <= new_value <= 100):
|
||||
errors.append({"rule_key": rule_key, "error": "Value must be between 0 and 100"})
|
||||
continue
|
||||
|
||||
old_val = rule.rule_value
|
||||
rule.rule_value = new_value
|
||||
updated.append({
|
||||
"rule_key": rule_key,
|
||||
"old_value": old_val,
|
||||
"new_value": new_value,
|
||||
})
|
||||
|
||||
await db.commit()
|
||||
|
||||
return {
|
||||
"message": f"Updated {len(updated)} rule(s) successfully.",
|
||||
"updated": updated,
|
||||
"errors": errors if errors else None,
|
||||
}
|
||||
|
||||
|
||||
@router.put("/validation-rules/{rule_key}", response_model=Dict[str, Any])
|
||||
async def update_validation_rule(
|
||||
rule_key: str,
|
||||
data: ValidationRuleUpdate,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(deps.get_current_active_user),
|
||||
_: User = Depends(deps.RequirePermission("gamification:manage")),
|
||||
):
|
||||
"""
|
||||
Update a single ValidationRule by its rule_key.
|
||||
|
||||
Allows updating both rule_value and description.
|
||||
"""
|
||||
from app.models.gamification.validation_rule import ValidationRule
|
||||
|
||||
stmt = select(ValidationRule).where(ValidationRule.rule_key == rule_key)
|
||||
rule = (await db.execute(stmt)).scalar_one_or_none()
|
||||
|
||||
if not rule:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Validation rule '{rule_key}' not found.",
|
||||
)
|
||||
|
||||
old_value = rule.rule_value
|
||||
rule.rule_value = data.rule_value
|
||||
if data.description is not None:
|
||||
rule.description = data.description
|
||||
|
||||
await db.commit()
|
||||
await db.refresh(rule)
|
||||
|
||||
logger.info(
|
||||
f"Validation rule '{rule_key}' updated: {old_value} → {data.rule_value}"
|
||||
)
|
||||
|
||||
return {
|
||||
"id": rule.id,
|
||||
"rule_key": rule.rule_key,
|
||||
"rule_value": rule.rule_value,
|
||||
"description": rule.description,
|
||||
"old_value": old_value,
|
||||
"updated_at": rule.updated_at.isoformat() if rule.updated_at else None,
|
||||
"message": f"Rule '{rule_key}' updated successfully.",
|
||||
}
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# LEADERBOARD (Admin)
|
||||
# =============================================================================
|
||||
|
||||
Reference in New Issue
Block a user