Backend: - LOW-002: Add Query validation with max page size limits (100) - LOW-003: Replace magic strings with TaskStatus.is_done flag - LOW-004: Add 'creation' trigger type validation - Add action_executor.py with UpdateFieldAction and AutoAssignAction Frontend: - LOW-005: Replace TypeScript 'any' with 'unknown' + type guards - LOW-006: Add ConfirmModal component with A11Y support - LOW-007: Add ToastContext for user feedback notifications - LOW-009: Add Skeleton components (17 loading states replaced) - LOW-010: Setup Vitest with 21 tests for ConfirmModal and Skeleton Components updated: - App.tsx, ProtectedRoute.tsx, Spaces.tsx, Projects.tsx, Tasks.tsx - ProjectSettings.tsx, AuditPage.tsx, WorkloadPage.tsx, ProjectHealthPage.tsx - Comments.tsx, AttachmentList.tsx, TriggerList.tsx, TaskDetailModal.tsx - NotificationBell.tsx, BlockerDialog.tsx, CalendarView.tsx, WorkloadUserDetail.tsx 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
344 lines
12 KiB
Python
344 lines
12 KiB
Python
import uuid
|
|
from fastapi import APIRouter, Depends, HTTPException, status, Query
|
|
from sqlalchemy.orm import Session
|
|
from typing import Optional
|
|
|
|
from app.core.database import get_db
|
|
from app.models import User, Project, Trigger, TriggerLog
|
|
from app.schemas.trigger import (
|
|
TriggerCreate, TriggerUpdate, TriggerResponse, TriggerListResponse,
|
|
TriggerLogResponse, TriggerLogListResponse, TriggerUserInfo
|
|
)
|
|
from app.middleware.auth import get_current_user, check_project_access, check_project_edit_access
|
|
from app.services.trigger_scheduler import TriggerSchedulerService
|
|
from app.services.trigger_service import TriggerService
|
|
from app.services.action_executor import ActionValidationError
|
|
|
|
router = APIRouter(tags=["triggers"])
|
|
|
|
|
|
def trigger_to_response(trigger: Trigger) -> TriggerResponse:
|
|
"""Convert Trigger model to TriggerResponse."""
|
|
return TriggerResponse(
|
|
id=trigger.id,
|
|
project_id=trigger.project_id,
|
|
name=trigger.name,
|
|
description=trigger.description,
|
|
trigger_type=trigger.trigger_type,
|
|
conditions=trigger.conditions,
|
|
actions=trigger.actions if isinstance(trigger.actions, list) else [trigger.actions],
|
|
is_active=trigger.is_active,
|
|
created_by=trigger.created_by,
|
|
created_at=trigger.created_at,
|
|
updated_at=trigger.updated_at,
|
|
creator=TriggerUserInfo(
|
|
id=trigger.creator.id,
|
|
name=trigger.creator.name,
|
|
email=trigger.creator.email,
|
|
) if trigger.creator else None,
|
|
)
|
|
|
|
|
|
@router.post("/api/projects/{project_id}/triggers", response_model=TriggerResponse, status_code=status.HTTP_201_CREATED)
|
|
async def create_trigger(
|
|
project_id: str,
|
|
trigger_data: TriggerCreate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""Create a new trigger for a project."""
|
|
project = db.query(Project).filter(Project.id == project_id).first()
|
|
|
|
if not project:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Project not found",
|
|
)
|
|
|
|
if not check_project_edit_access(current_user, project):
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="Permission denied - only project owner can manage triggers",
|
|
)
|
|
|
|
# Validate trigger type
|
|
valid_trigger_types = ["field_change", "schedule", "creation"]
|
|
if trigger_data.trigger_type not in valid_trigger_types:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail=f"Invalid trigger type. Must be one of: {', '.join(valid_trigger_types)}",
|
|
)
|
|
|
|
# Validate conditions based on trigger type
|
|
if trigger_data.trigger_type == "field_change":
|
|
# Validate field_change conditions
|
|
if not trigger_data.conditions.field:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="Field is required for field_change triggers",
|
|
)
|
|
if trigger_data.conditions.field not in ["status_id", "assignee_id", "priority"]:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="Invalid condition field. Must be 'status_id', 'assignee_id', or 'priority'",
|
|
)
|
|
if not trigger_data.conditions.operator:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="Operator is required for field_change triggers",
|
|
)
|
|
if trigger_data.conditions.operator not in ["equals", "not_equals", "changed_to", "changed_from"]:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="Invalid operator. Must be 'equals', 'not_equals', 'changed_to', or 'changed_from'",
|
|
)
|
|
elif trigger_data.trigger_type == "schedule":
|
|
# Validate schedule conditions
|
|
has_cron = trigger_data.conditions.cron_expression is not None
|
|
has_deadline = trigger_data.conditions.deadline_reminder_days is not None
|
|
|
|
if not has_cron and not has_deadline:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="Schedule triggers require either cron_expression or deadline_reminder_days",
|
|
)
|
|
|
|
# Validate cron expression if provided
|
|
if has_cron:
|
|
is_valid, error_msg = TriggerSchedulerService.parse_cron_expression(
|
|
trigger_data.conditions.cron_expression
|
|
)
|
|
if not is_valid:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail=error_msg or "Invalid cron expression",
|
|
)
|
|
|
|
# Validate actions configuration (FEAT-014, FEAT-015)
|
|
try:
|
|
actions_dicts = [a.model_dump(exclude_none=True) for a in trigger_data.actions]
|
|
TriggerService.validate_actions(actions_dicts, db)
|
|
except ActionValidationError as e:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail=str(e),
|
|
)
|
|
|
|
# Create trigger
|
|
trigger = Trigger(
|
|
id=str(uuid.uuid4()),
|
|
project_id=project_id,
|
|
name=trigger_data.name,
|
|
description=trigger_data.description,
|
|
trigger_type=trigger_data.trigger_type,
|
|
conditions=trigger_data.conditions.model_dump(),
|
|
actions=[a.model_dump(exclude_none=True) for a in trigger_data.actions],
|
|
is_active=trigger_data.is_active,
|
|
created_by=current_user.id,
|
|
)
|
|
db.add(trigger)
|
|
db.commit()
|
|
db.refresh(trigger)
|
|
|
|
return trigger_to_response(trigger)
|
|
|
|
|
|
@router.get("/api/projects/{project_id}/triggers", response_model=TriggerListResponse)
|
|
async def list_triggers(
|
|
project_id: str,
|
|
is_active: Optional[bool] = None,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""List all triggers for a project."""
|
|
project = db.query(Project).filter(Project.id == project_id).first()
|
|
|
|
if not project:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Project not found",
|
|
)
|
|
|
|
if not check_project_access(current_user, project):
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="Access denied",
|
|
)
|
|
|
|
query = db.query(Trigger).filter(Trigger.project_id == project_id)
|
|
|
|
if is_active is not None:
|
|
query = query.filter(Trigger.is_active == is_active)
|
|
|
|
triggers = query.order_by(Trigger.created_at.desc()).all()
|
|
|
|
return TriggerListResponse(
|
|
triggers=[trigger_to_response(t) for t in triggers],
|
|
total=len(triggers),
|
|
)
|
|
|
|
|
|
@router.get("/api/triggers/{trigger_id}", response_model=TriggerResponse)
|
|
async def get_trigger(
|
|
trigger_id: str,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""Get a specific trigger by ID."""
|
|
trigger = db.query(Trigger).filter(Trigger.id == trigger_id).first()
|
|
|
|
if not trigger:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Trigger not found",
|
|
)
|
|
|
|
project = trigger.project
|
|
if not check_project_access(current_user, project):
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="Access denied",
|
|
)
|
|
|
|
return trigger_to_response(trigger)
|
|
|
|
|
|
@router.put("/api/triggers/{trigger_id}", response_model=TriggerResponse)
|
|
async def update_trigger(
|
|
trigger_id: str,
|
|
trigger_data: TriggerUpdate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""Update a trigger."""
|
|
trigger = db.query(Trigger).filter(Trigger.id == trigger_id).first()
|
|
|
|
if not trigger:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Trigger not found",
|
|
)
|
|
|
|
project = trigger.project
|
|
if not check_project_edit_access(current_user, project):
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="Permission denied",
|
|
)
|
|
|
|
# Update fields if provided
|
|
if trigger_data.name is not None:
|
|
trigger.name = trigger_data.name
|
|
if trigger_data.description is not None:
|
|
trigger.description = trigger_data.description
|
|
if trigger_data.conditions is not None:
|
|
# Validate conditions based on trigger type
|
|
if trigger.trigger_type == "field_change":
|
|
if trigger_data.conditions.field and trigger_data.conditions.field not in ["status_id", "assignee_id", "priority"]:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="Invalid condition field",
|
|
)
|
|
elif trigger.trigger_type == "schedule":
|
|
# Validate cron expression if provided
|
|
if trigger_data.conditions.cron_expression is not None:
|
|
is_valid, error_msg = TriggerSchedulerService.parse_cron_expression(
|
|
trigger_data.conditions.cron_expression
|
|
)
|
|
if not is_valid:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail=error_msg or "Invalid cron expression",
|
|
)
|
|
trigger.conditions = trigger_data.conditions.model_dump(exclude_none=True)
|
|
if trigger_data.actions is not None:
|
|
# Validate actions configuration (FEAT-014, FEAT-015)
|
|
try:
|
|
actions_dicts = [a.model_dump(exclude_none=True) for a in trigger_data.actions]
|
|
TriggerService.validate_actions(actions_dicts, db)
|
|
except ActionValidationError as e:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail=str(e),
|
|
)
|
|
trigger.actions = [a.model_dump(exclude_none=True) for a in trigger_data.actions]
|
|
if trigger_data.is_active is not None:
|
|
trigger.is_active = trigger_data.is_active
|
|
|
|
db.commit()
|
|
db.refresh(trigger)
|
|
|
|
return trigger_to_response(trigger)
|
|
|
|
|
|
@router.delete("/api/triggers/{trigger_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
async def delete_trigger(
|
|
trigger_id: str,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""Delete a trigger."""
|
|
trigger = db.query(Trigger).filter(Trigger.id == trigger_id).first()
|
|
|
|
if not trigger:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Trigger not found",
|
|
)
|
|
|
|
project = trigger.project
|
|
if not check_project_edit_access(current_user, project):
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="Permission denied",
|
|
)
|
|
|
|
db.delete(trigger)
|
|
db.commit()
|
|
|
|
|
|
@router.get("/api/triggers/{trigger_id}/logs", response_model=TriggerLogListResponse)
|
|
async def list_trigger_logs(
|
|
trigger_id: str,
|
|
limit: int = Query(50, ge=1, le=200, description="Number of logs to return"),
|
|
offset: int = Query(0, ge=0, description="Number of logs to skip"),
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""Get execution logs for a trigger."""
|
|
trigger = db.query(Trigger).filter(Trigger.id == trigger_id).first()
|
|
|
|
if not trigger:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Trigger not found",
|
|
)
|
|
|
|
project = trigger.project
|
|
if not check_project_access(current_user, project):
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="Access denied",
|
|
)
|
|
|
|
total = db.query(TriggerLog).filter(TriggerLog.trigger_id == trigger_id).count()
|
|
|
|
logs = db.query(TriggerLog).filter(
|
|
TriggerLog.trigger_id == trigger_id,
|
|
).order_by(TriggerLog.executed_at.desc()).offset(offset).limit(limit).all()
|
|
|
|
return TriggerLogListResponse(
|
|
logs=[
|
|
TriggerLogResponse(
|
|
id=log.id,
|
|
trigger_id=log.trigger_id,
|
|
task_id=log.task_id,
|
|
executed_at=log.executed_at,
|
|
status=log.status,
|
|
details=log.details,
|
|
error_message=log.error_message,
|
|
) for log in logs
|
|
],
|
|
total=total,
|
|
)
|