121 lines
4.1 KiB
Python
121 lines
4.1 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Script to create the Auto Settlement Letter workflow
|
|
This workflow automatically generates a settlement letter when a file status changes to "CLOSED"
|
|
"""
|
|
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
|
|
)
|
|
from app.models.templates import DocumentTemplate
|
|
|
|
|
|
def create_settlement_workflow():
|
|
"""Create the Auto Settlement Letter workflow"""
|
|
|
|
# Get database session
|
|
db = next(get_db())
|
|
|
|
try:
|
|
# Check if workflow already exists
|
|
existing = db.query(DocumentWorkflow).filter(
|
|
DocumentWorkflow.name == "Auto Settlement Letter"
|
|
).first()
|
|
|
|
if existing:
|
|
print(f"Workflow 'Auto Settlement Letter' already exists with ID {existing.id}")
|
|
return existing
|
|
|
|
# Find or create a settlement letter template
|
|
template = db.query(DocumentTemplate).filter(
|
|
DocumentTemplate.name.ilike("%settlement%")
|
|
).first()
|
|
|
|
if not template:
|
|
# Create a basic settlement letter template
|
|
template = DocumentTemplate(
|
|
name="Settlement Letter Template",
|
|
description="Template for automatic settlement letter generation",
|
|
category="SETTLEMENT",
|
|
active=True,
|
|
created_by="system"
|
|
)
|
|
db.add(template)
|
|
db.flush() # Get the ID
|
|
print(f"Created settlement letter template with ID {template.id}")
|
|
else:
|
|
print(f"Using existing template: {template.name} (ID: {template.id})")
|
|
|
|
# Create the workflow
|
|
workflow = DocumentWorkflow(
|
|
name="Auto Settlement Letter",
|
|
description="Automatically generate a settlement letter when file status changes to CLOSED",
|
|
trigger_type=WorkflowTriggerType.FILE_STATUS_CHANGE,
|
|
trigger_conditions={
|
|
"type": "simple",
|
|
"field": "new_state.status",
|
|
"operator": "equals",
|
|
"value": "CLOSED"
|
|
},
|
|
delay_minutes=0, # Execute immediately
|
|
max_retries=3,
|
|
retry_delay_minutes=30,
|
|
timeout_minutes=60,
|
|
priority=8, # High priority
|
|
category="DOCUMENT_GENERATION",
|
|
tags=["settlement", "closure", "automated"],
|
|
status=WorkflowStatus.ACTIVE,
|
|
created_by="system"
|
|
)
|
|
|
|
db.add(workflow)
|
|
db.flush() # Get the workflow ID
|
|
|
|
# Create the document generation action
|
|
action = WorkflowAction(
|
|
workflow_id=workflow.id,
|
|
action_type=WorkflowActionType.GENERATE_DOCUMENT,
|
|
action_order=1,
|
|
action_name="Generate Settlement Letter",
|
|
template_id=template.id,
|
|
output_format="PDF",
|
|
custom_filename_template="Settlement_Letter_{{FILE_NO}}_{{CLOSED_DATE}}.pdf",
|
|
continue_on_failure=False,
|
|
parameters={
|
|
"auto_save": True,
|
|
"notification": "Generate settlement letter for closed file"
|
|
}
|
|
)
|
|
|
|
db.add(action)
|
|
db.commit()
|
|
|
|
print(f"✅ Successfully created 'Auto Settlement Letter' workflow:")
|
|
print(f" - Workflow ID: {workflow.id}")
|
|
print(f" - Action ID: {action.id}")
|
|
print(f" - Template ID: {template.id}")
|
|
print(f" - Trigger: File status change to 'CLOSED'")
|
|
print(f" - Action: Generate PDF settlement letter")
|
|
|
|
return workflow
|
|
|
|
except Exception as e:
|
|
db.rollback()
|
|
print(f"❌ Error creating settlement workflow: {str(e)}")
|
|
raise
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
workflow = create_settlement_workflow()
|