File: //home/arjun/projects/buyercall/buyercall/blueprints/workflows/utils/workflow_identifier.py
from buyercall.blueprints.workflows.models import WorkflowConditionAction
class WorkflowIdentifier:
def __init__(self, workflows, sources, channels, channels_type_names) -> None:
self.workflows = workflows
self.sources = sources
self.channels = channels
self.channels_type_names = channels_type_names
def identify(self, **params):
source = params.get('source', None)
channel = params.get('channel', None)
channel_type = params.get('channel_type', None)
is_existing_contact = params.get('is_existing_contact', None)
results = []
print('all_workflows : ', self.workflows)
for workflow in self.workflows:
workflow_detail = WorkflowConditionAction.get_by_id(workflow.workflow_detail)
if workflow_detail:
print('workflow : ', workflow.name)
workflow_statuses = self._check(workflow_detail, source, channel, channel_type)
if any(workflow_statuses) and workflow_detail.conditions[
'type'] == 'OR' and workflow_detail.is_new_contact != is_existing_contact:
results.append(workflow)
if all(workflow_statuses) and workflow_detail.conditions[
'type'] == 'AND' and workflow_detail.is_new_contact != is_existing_contact:
results.append(workflow)
return results
def _check(self, workflow, source, channel, channel_type, **kwargs):
print('All sources : ', self.sources)
print('Current source : ', source)
condition_statuses = []
if workflow.conditions:
for condition in workflow.conditions['options']:
if condition['type'] == 'SOURCE':
if condition['operator'] == 'EQ' and source in condition['value'] and source in self.sources:
condition_statuses.append(True)
elif condition['operator'] == 'NEQ' and source not in condition['value'] and source in self.sources:
condition_statuses.append(True)
else:
condition_statuses.append(False)
elif condition['type'] == 'CHANNEL':
if condition['operator'] == 'EQ' and channel_type in condition[
'value'] and channel in self.channels:
condition_statuses.append(True)
elif condition['operator'] == 'NEQ' and channel_type not in condition[
'value'] and channel in self.channels:
condition_statuses.append(True)
else:
condition_statuses.append(False)
elif condition['type'] == 'SCORE':
pass
elif condition['type'] == 'CONTACT_STATUS_TAG':
pass
elif condition['type'] == 'BIRTHDAY':
pass
elif condition['type'] == 'CREDIT_SCORE':
pass
elif condition['type'] == 'INCOME':
pass
elif condition['type'] == 'INTERACTION_COUNT':
pass
elif condition['type'] == 'CREATED_DATE':
pass
else:
condition_statuses.append(False)
print('Condition statuses : ', condition_statuses)
return condition_statuses