from rest_framework.decorators import api_view from rest_framework.response import Response from rest_framework import status from app.workflow_engine import run_workflow from datetime import datetime # Thêm import @api_view(["POST"]) def execute_workflow(request): try: workflow_code = request.data.get("workflow_code") trigger = request.data.get("trigger") # Tạo bản sao của dữ liệu request để làm context cho workflow. context = dict(request.data) # FIX: Bổ sung biến hệ thống: ngày hiện tại để Serializer có thể lấy giá trị cho field 'date' context["current_date"] = datetime.now().strftime("%Y-%m-%d") if not workflow_code or not trigger: # Sử dụng status.HTTP_400_BAD_REQUEST hoặc 400 như trong code gốc return Response({"error": "workflow_code & trigger are required"}, status=400) result = run_workflow(workflow_code, trigger, context) return Response({"success": True, "result": result}) except Exception as e: # Trả về lỗi chi tiết hơn return Response({"error": str(e)}, status=400)