#!/usr/bin/env python3 """ Script to create the Deadline Reminder workflow This workflow sends reminder emails when deadlines are approaching (within 7 days) """ import asyncio import sys import os # Add the project root to Python path sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) from sqlalchemy.orm import Session from app.database.base import get_db from app.models.document_workflows import ( DocumentWorkflow, WorkflowAction, WorkflowTriggerType, WorkflowActionType, WorkflowStatus ) def create_deadline_reminder_workflow(): """Create the Deadline Reminder workflow""" # Get database session db = next(get_db()) try: # Check if workflow already exists existing = db.query(DocumentWorkflow).filter( DocumentWorkflow.name == "Deadline Reminder" ).first() if existing: print(f"Workflow 'Deadline Reminder' already exists with ID {existing.id}") return existing # Create the workflow workflow = DocumentWorkflow( name="Deadline Reminder", description="Send reminder email when deadline approaches (within 7 days)", trigger_type=WorkflowTriggerType.DEADLINE_APPROACHING, trigger_conditions={ "type": "simple", "field": "data.days_until_deadline", "operator": "less_equal", "value": 7 }, delay_minutes=0, # Execute immediately max_retries=2, retry_delay_minutes=60, timeout_minutes=30, priority=7, # High priority for deadlines category="DEADLINE_MANAGEMENT", tags=["deadline", "reminder", "email", "notification"], status=WorkflowStatus.ACTIVE, created_by="system" ) db.add(workflow) db.flush() # Get the workflow ID # Create the email action action = WorkflowAction( workflow_id=workflow.id, action_type=WorkflowActionType.SEND_EMAIL, action_order=1, action_name="Send Deadline Reminder Email", email_recipients=["attorney", "client"], email_subject_template="Reminder: {{DEADLINE_TITLE}} due in {{DAYS_REMAINING}} days", continue_on_failure=False, parameters={ "email_template": "deadline_reminder", "include_attachments": False, "priority": "high", "email_body_template": """ Dear {{CLIENT_FULL}}, This is a friendly reminder that the following deadline is approaching: Deadline: {{DEADLINE_TITLE}} Due Date: {{DEADLINE_DATE}} Days Remaining: {{DAYS_REMAINING}} File Number: {{FILE_NO}} Matter: {{MATTER}} Please contact our office if you have any questions or need assistance. Best regards, {{ATTORNEY_NAME}} {{FIRM_NAME}} """.strip() } ) db.add(action) db.commit() print(f"✅ Successfully created 'Deadline Reminder' workflow:") print(f" - Workflow ID: {workflow.id}") print(f" - Action ID: {action.id}") print(f" - Trigger: Deadline approaching (≤ 7 days)") print(f" - Action: Send email to attorney and client") print(f" - Recipients: attorney, client") print(f" - Subject: Reminder: {{DEADLINE_TITLE}} due in {{DAYS_REMAINING}} days") return workflow except Exception as e: db.rollback() print(f"❌ Error creating deadline reminder workflow: {str(e)}") raise finally: db.close() if __name__ == "__main__": workflow = create_deadline_reminder_workflow()