File: //home/arjun/projects/buyercall/buyercall/blueprints/workflows/utils/workflow_validator.py
import collections
from flask_login import current_user
from buyercall.blueprints.agents.models import Agent, Team
from buyercall.blueprints.contacts.models import ContactTags
from buyercall.blueprints.sources.models import Source
class WorkflowValidator:
conditions = None
options = None
actions = None
condition_actions = None
SCORE = [
"cold",
"warm",
"hot"
]
LEAD_TYPE = [
"Customer",
"Prospect",
"Lead"
]
CHANNEL = [
"Phone call",
"Chat",
"SMS",
"Web form",
"Email",
"Integration",
"Import",
"API"
]
BIRTHDAY = [
"TODAY",
"5 days after"
"5 days before"
]
CREATED_DATE = [
"1 day later",
"2 day later",
"5 day later",
"10 day later",
"20 day later"
]
option_types = ["SCORE", "LEAD_TYPE", "CHANNEL", "BIRTHDAY", "CREATED_DATE"]
option_types_model = ["SOURCE", "CONTACT_STATUS_TAG"]
option_types_strings = ["CREDIT_SCORE", "INCOME", "INTERACTION_COUNT"]
action_types = ["AGENT", "TAGS", "INTENT_SCORE"]
SOURCE = None
CONTACT_STATUS_TAG = []
AGENT = None
TAGS = None
TEAMS = None
def __init__(self, conditions_actions) -> None:
self.condition_actions = conditions_actions
self.conditions = conditions_actions.get('conditions', {})
self.options = self.conditions.get('options', [])
self.actions = conditions_actions.get('actions', [])
def validate(self):
# options check
if self.options or self.options != []:
for option in self.options:
if option.get('type') in self.option_types:
try:
values = eval("self." + option.get('type'))
check = list(set(option.get('value')) - set(values))
# check = any(item not in values for item in option.get('value'))
if check != []:
raise ValueError("invalid Input " + option.get('type') +
" and value " + str(option.get('value')) + ".")
except Exception as e:
raise ValueError("! invalid Input " + option.get('type') +
" and value " + str(option.get('value')) + ".")
elif option.get('type') in self.option_types_strings:
if option.get('type') == "CREDIT_SCORE" and (int(option.get('value')) > 100):
raise ValueError("invalid value ! credit score must be less than 100")
elif (option.get('type') == "INCOME" or option.get('type') == "INTERACTION_COUNT") and (
int(option.get('value')) < 0):
raise ValueError("invalid value ! income must be a positive number")
elif option.get('type') in self.option_types_model:
self.SOURCE = option.get('value') if option.get('type') == "SOURCE" else None
tags = option.get('tags') if option.get('type') == "CONTACT_STATUS_TAG" else []
self.CONTACT_STATUS_TAG = self.CONTACT_STATUS_TAG + tags
else:
raise ValueError("invalid input condition")
# actions check
if self.actions or self.actions != []:
for action in self.actions:
if action.get('type') in self.action_types:
if action.get('type') == "INTENT_SCORE" and \
(list(set(option.get('value')) - set(values)) != []):
# any(item not in self.SCORE for item in action.get('value')):
raise ValueError(
"! invalid Input " + action.get('type') + " and value " + str(action.get('value')))
elif action.get('type') == "TAGS":
self.CONTACT_STATUS_TAG = self.CONTACT_STATUS_TAG + action.tags
elif action.get('type') == "AGENT":
self.AGENT = action.get('agentIds')
self.TEAMS = action.get('teamIds')
else:
raise ValueError("invalid input action")
if self.SOURCE:
source_data = Source.query.filter(Source.sid.in_(self.SOURCE),
Source.partnership_account_id == current_user.partnership_account_id).with_entities(
Source.sid).all()
if not (collections.compare(source_data) == collections.compare(self.SOURCE)):
raise ValueError("! invalid Source for This user")
if self.CONTACT_STATUS_TAG != []:
self.CONTACT_STATUS_TAG = list(collections.OrderedDict.fromkeys(self.CONTACT_STATUS_TAG))
tag_data = ContactTags.query.filter(ContactTags.sid.in_(self.CONTACT_STATUS_TAG),
ContactTags.is_active == True).with_entities(ContactTags.sid).all()
if not (collections.compare(tag_data) == collections.compare(self.CONTACT_STATUS_TAG)):
raise ValueError("! invalid tag for This user")
if self.AGENT:
agent_data = Agent.query.filter(Agent.sid.in_(self.AGENT),
Agent.partnership_account_id == current_user.partnership_account_id).with_entities(
Agent.sid).all()
if not (collections.compare(agent_data) == collections.compare(self.AGENT)):
raise ValueError("! invalid Agent for This user")
if self.TEAMS:
teams_data = Team.query.filter(Team.sid.in_(self.TEAMS),
Team.partnership_account_id == current_user.partnership_account_id). \
with_entities(Team.sid).all()
if not (collections.compare(teams_data)) == collections.compare(self.TEAMS):
raise ValueError("! invalid Team For the user")
return True