HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //proc/1233/root/home/arjun/projects/buyercall/buyercall/blueprints/workflows/endpoints.py
import logging as log

from flask import (
    request, Blueprint
)
from flask_login import current_user

from buyercall.blueprints.user.decorators import api_role_required
from buyercall.blueprints.workflows.models import WordkflowType, Workflow, WorkflowConditionAction
from buyercall.blueprints.workflows.serializers import WorkflowOutSchema, WorkflowTypeOutSchema, WorkflowDetailOutSchema
from buyercall.blueprints.workflows.utils.assigned_agents_collector import WorkflowAssignedAgents
from buyercall.lib.util_rest import api_jsonify

log = log.getLogger(__name__)
workflows_api = Blueprint('workflows_api', __name__, template_folder='templates', url_prefix='/api/workflows')


@api_role_required('admin')
def create_workflow_type():
    received = request.get_json()
    if received.get("name", None):
        message = "Workflow type created successfully"
        status_code = 201
        success = True
        if not WordkflowType.is_name_exists(received.get("name")):
            workflow_type = WordkflowType.create(name=received.get("name"))
        else:
            message = "Workflow type name already exists"
            status_code = 401
            success = False
    else:
        message = "Workflow type name is required"
        status_code = 401
        success = False
    return api_jsonify(status_code=status_code, success=success, message=message)


@api_role_required('admin')
def get_workflow_types():
    message = "Workflow types fetched successfully"
    workflow_types = WordkflowType.query.filter(WordkflowType.is_active == True).all()
    return api_jsonify(data=WorkflowTypeOutSchema(many=True).dump(workflow_types), message=message)


@api_role_required('admin')
def get_workflows():
    workflows = Workflow.query.filter(Workflow.partnership_account_id == current_user.partnership_account_id).all()
    message = "Workflows fetched successfully"
    return api_jsonify(data=WorkflowOutSchema(many=True).dump(workflows), message=message)


@api_role_required('admin')
def get_workflow_detail(wid):
    data = {}
    if wid:
        message = "Workflow fetched successfully"
        success = True
        status_code = 200
        workflow = Workflow.query.filter(Workflow.sid == wid).first()
        data = WorkflowDetailOutSchema().dump(workflow)
    else:
        message = "Invalid input data"
        status_code = 401
        success = False
    return api_jsonify(data=data, message=message, status_code=status_code, success=success)


@api_role_required('admin')
def create_workflow():
    message = "Workflow created successfully"
    status_code = 200
    success = True

    received = request.get_json() or {}
    is_new_contact = True if received.get('selectExecutionType', 1) == 1 else False
    assigned_agents = WorkflowAssignedAgents(received.get('actions', {})).get('AGENT')
    _workflow_condition_action_params = {
        "conditions": received.get('conditions', {}),
        "actions": received.get('actions', {}),
        "is_new_contact": is_new_contact,
        "meta_data": assigned_agents
    }
    workflow_type = received.get('type_id', None)
    validation_status = True
    if Workflow.is_name_exists(received.get("name", '')):
        status_code = 401
        success = False
        message = 'Workflow name already exists'
    elif validation_status:
        workflow_condition_action = WorkflowConditionAction.create(**_workflow_condition_action_params)
        _workflow_params = {
            "name": received.get("name", None),
            "description": received.get("description", None),
            "workflow_type": WordkflowType.get_id_from_sid(workflow_type),
            "workflow_detail": workflow_condition_action.id,
            "created_by": current_user.id,
            "is_active": received.get("isActive", False),
            "partnership_account_id": current_user.partnership_account_id or 1,
            "partnership_id": current_user.partnership_id or 1
        }
        workflow = Workflow.create(**_workflow_params)
    else:
        status_code = 401
        success = False
        message = 'Invalid input data'
    return api_jsonify(status_code=status_code, success=success, message=message)


@api_role_required('admin')
def update_workflow(wid):
    data = {}
    received = request.get_json()
    print('received : ', received)
    if wid:
        message = "Workflow updated successfully"
        success = True
        status_code = 200
        workflow = Workflow.query.filter(Workflow.sid == wid).first()
        if workflow:
            workflow.name = received.get("name", None)
            workflow.description = received.get("description", None)
            workflow_conditions = received.get("conditions", None)
            workflow_actions = received.get("actions", None)
            is_new_contact = True if received.get('selectExecutionType', 1) == 1 else False
            assigned_agents = WorkflowAssignedAgents(received.get('actions', {})).get('AGENT')
            _workflow_condition_action_params = {
                'conditions': workflow_conditions,
                'actions': workflow_actions,
                'is_new_contact': is_new_contact,
                'meta_data': assigned_agents
            }
            if workflow_conditions or workflow_actions:
                # validation_status = WorkflowValidator().validate(workflow_type, _workflow_condition_action_params)
                validation_status = True
                if validation_status:
                    workflow_detail = WorkflowConditionAction.get_by_id(workflow.workflow_detail)
                    if workflow_detail:
                        workflow_detail.conditions = workflow_conditions
                        workflow_detail.actions = workflow_actions
                        workflow_detail.is_new_contact = is_new_contact
                        workflow_detail.meta_data = assigned_agents
                        workflow_detail.save()
            workflow.updated_by = current_user.id
            workflow.save()
        else:
            message = "Workflow not found"
            status_code = 404
            success = False
    else:
        message = "Invalid input data"
        status_code = 401
        success = False

    return api_jsonify(data=data, message=message, status_code=status_code, success=success)