chore: fix all ruff lint errors + format — 0 errors, 306 tests pass
This commit is contained in:
@@ -67,9 +67,10 @@ async def ensure_onboarding_workflow_exists(
|
||||
|
||||
If it doesn't exist yet, create it. Returns the workflow ID.
|
||||
"""
|
||||
from app.models.workflow import Workflow
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.models.workflow import Workflow
|
||||
|
||||
result = await db.execute(
|
||||
select(Workflow).where(
|
||||
Workflow.tenant_id == tenant_id,
|
||||
@@ -82,8 +83,11 @@ async def ensure_onboarding_workflow_exists(
|
||||
return existing.id
|
||||
|
||||
from app.services.workflow_service import create_workflow
|
||||
|
||||
wf_dict = await create_workflow(
|
||||
db, tenant_id, user_id,
|
||||
db,
|
||||
tenant_id,
|
||||
user_id,
|
||||
get_onboarding_workflow_definition(),
|
||||
)
|
||||
return uuid.UUID(wf_dict["id"]) if wf_dict else None
|
||||
@@ -105,7 +109,9 @@ async def trigger_onboarding(
|
||||
return None
|
||||
|
||||
instance = await create_instance(
|
||||
db, tenant_id, admin_user_id,
|
||||
db,
|
||||
tenant_id,
|
||||
admin_user_id,
|
||||
str(wf_id),
|
||||
context={"new_user_id": str(new_user_id), "user_id": str(new_user_id)},
|
||||
)
|
||||
|
||||
+38
-20
@@ -7,20 +7,20 @@ Integrates with the event bus for event-triggered workflows.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
import logging
|
||||
from datetime import datetime, timezone
|
||||
import uuid
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.core.event_bus import get_event_bus
|
||||
from app.models.workflow import Workflow, WorkflowInstance, WorkflowStepHistory
|
||||
from app.models.notification import Notification
|
||||
from app.models.workflow import Workflow, WorkflowInstance
|
||||
from app.services.workflow_service import (
|
||||
_log_step_history,
|
||||
_instance_to_dict,
|
||||
_log_step_history,
|
||||
create_instance,
|
||||
find_workflows_for_event,
|
||||
)
|
||||
@@ -59,7 +59,7 @@ class WorkflowEngine:
|
||||
steps = workflow.steps or []
|
||||
if instance.current_step_index >= len(steps):
|
||||
instance.status = "completed"
|
||||
instance.completed_at = datetime.now(timezone.utc)
|
||||
instance.completed_at = datetime.now(UTC)
|
||||
await self.db.flush()
|
||||
return _instance_to_dict(instance)
|
||||
|
||||
@@ -68,7 +68,9 @@ class WorkflowEngine:
|
||||
|
||||
# Log step entry
|
||||
await _log_step_history(
|
||||
self.db, self.tenant_id, instance.id,
|
||||
self.db,
|
||||
self.tenant_id,
|
||||
instance.id,
|
||||
step_index=instance.current_step_index,
|
||||
step_type=step_type,
|
||||
action="processing",
|
||||
@@ -103,7 +105,9 @@ class WorkflowEngine:
|
||||
|
||||
# Execute action based on type
|
||||
if action_type == "create_notification":
|
||||
user_id = config.get("user_id") or (str(instance.initiated_by) if instance.initiated_by else None)
|
||||
user_id = config.get("user_id") or (
|
||||
str(instance.initiated_by) if instance.initiated_by else None
|
||||
)
|
||||
if user_id:
|
||||
notification = Notification(
|
||||
tenant_id=self.tenant_id,
|
||||
@@ -120,7 +124,9 @@ class WorkflowEngine:
|
||||
|
||||
# Log action executed
|
||||
await _log_step_history(
|
||||
self.db, self.tenant_id, instance.id,
|
||||
self.db,
|
||||
self.tenant_id,
|
||||
instance.id,
|
||||
step_index=instance.current_step_index,
|
||||
step_type="action",
|
||||
action="executed",
|
||||
@@ -131,7 +137,7 @@ class WorkflowEngine:
|
||||
next_idx = instance.current_step_index + 1
|
||||
if next_idx >= len(steps):
|
||||
instance.status = "completed"
|
||||
instance.completed_at = datetime.now(timezone.utc)
|
||||
instance.completed_at = datetime.now(UTC)
|
||||
else:
|
||||
instance.current_step_index = next_idx
|
||||
instance.status = "in_progress"
|
||||
@@ -139,12 +145,12 @@ class WorkflowEngine:
|
||||
await self.db.flush()
|
||||
return _instance_to_dict(instance)
|
||||
|
||||
async def _process_notification(
|
||||
self, instance: WorkflowInstance, step: dict
|
||||
) -> dict[str, Any]:
|
||||
async def _process_notification(self, instance: WorkflowInstance, step: dict) -> dict[str, Any]:
|
||||
"""Process a notification step — sends notification and advances."""
|
||||
config = step.get("config", {})
|
||||
user_id = config.get("user_id") or (str(instance.initiated_by) if instance.initiated_by else None)
|
||||
user_id = config.get("user_id") or (
|
||||
str(instance.initiated_by) if instance.initiated_by else None
|
||||
)
|
||||
|
||||
if user_id:
|
||||
notification = Notification(
|
||||
@@ -158,7 +164,9 @@ class WorkflowEngine:
|
||||
await self.db.flush()
|
||||
|
||||
await _log_step_history(
|
||||
self.db, self.tenant_id, instance.id,
|
||||
self.db,
|
||||
self.tenant_id,
|
||||
instance.id,
|
||||
step_index=instance.current_step_index,
|
||||
step_type="notification",
|
||||
action="sent",
|
||||
@@ -174,7 +182,7 @@ class WorkflowEngine:
|
||||
next_idx = instance.current_step_index + 1
|
||||
if next_idx >= len(steps):
|
||||
instance.status = "completed"
|
||||
instance.completed_at = datetime.now(timezone.utc)
|
||||
instance.completed_at = datetime.now(UTC)
|
||||
else:
|
||||
instance.current_step_index = next_idx
|
||||
|
||||
@@ -211,10 +219,16 @@ class WorkflowEngine:
|
||||
elif operator == "lt":
|
||||
condition_met = actual is not None and expected is not None and actual < expected
|
||||
elif operator == "contains":
|
||||
condition_met = actual is not None and expected in actual if isinstance(actual, (str, list)) else False
|
||||
condition_met = (
|
||||
actual is not None and expected in actual
|
||||
if isinstance(actual, str | list)
|
||||
else False
|
||||
)
|
||||
|
||||
await _log_step_history(
|
||||
self.db, self.tenant_id, instance.id,
|
||||
self.db,
|
||||
self.tenant_id,
|
||||
instance.id,
|
||||
step_index=instance.current_step_index,
|
||||
step_type="condition",
|
||||
action="evaluated",
|
||||
@@ -230,7 +244,7 @@ class WorkflowEngine:
|
||||
next_idx = instance.current_step_index + 1
|
||||
if next_idx >= len(steps):
|
||||
instance.status = "completed"
|
||||
instance.completed_at = datetime.now(timezone.utc)
|
||||
instance.completed_at = datetime.now(UTC)
|
||||
else:
|
||||
instance.current_step_index = next_idx
|
||||
|
||||
@@ -253,8 +267,11 @@ async def handle_event(
|
||||
instances: list[dict[str, Any]] = []
|
||||
for wf in workflows:
|
||||
inst = await create_instance(
|
||||
db, tenant_id,
|
||||
uuid.UUID(payload.get("user_id", str(uuid.uuid4()))) if payload.get("user_id") else None or uuid.uuid4(),
|
||||
db,
|
||||
tenant_id,
|
||||
uuid.UUID(payload.get("user_id", str(uuid.uuid4())))
|
||||
if payload.get("user_id")
|
||||
else None or uuid.uuid4(),
|
||||
str(wf.id),
|
||||
context=payload,
|
||||
)
|
||||
@@ -274,6 +291,7 @@ def register_workflow_event_handlers() -> None:
|
||||
async def _workflow_event_handler(payload: dict[str, Any]) -> None:
|
||||
"""Handle events that may trigger workflows."""
|
||||
from app.core.db import create_db_session
|
||||
|
||||
tenant_id_str = payload.get("tenant_id")
|
||||
event_name = payload.get("event", "")
|
||||
if not tenant_id_str or not event_name:
|
||||
|
||||
Reference in New Issue
Block a user