Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix-false-engaged-setter-on-activation #32

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions server/app/helpers/activation_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def update_prospecting_metadata(prospecting_effort, task, criteria_name):
def get_new_status(
activation: Activation,
task: Dict,
criteria: List[FilterContainer],
criterion: FilterContainer,
opportunities: List[Dict],
events: List[Dict],
):
Expand All @@ -111,9 +111,7 @@ def get_new_status(
and activation.status != StatusEnum.opportunity_created
):
return StatusEnum.meeting_set
elif activation.status == StatusEnum.activated and is_inbound_criteria(
task, criteria
):
elif activation.status == StatusEnum.activated and criterion.direction.lower() == "inbound":
return StatusEnum.engaged

return activation.status
Expand Down
11 changes: 4 additions & 7 deletions server/app/services/activation_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ def increment_existing_activations(activations: List[Activation], settings: Sett
activations_by_account_id: Dict[str, List[Activation]] = {
activation.account.id: activation for activation in activations
}

criterion_by_name = {criterion.name: criterion for criterion in settings.criteria}

for account_id, tasks_by_criteria in criteria_group_tasks_by_account_id.items():
activation = activations_by_account_id.get(account_id)
Expand Down Expand Up @@ -230,7 +232,7 @@ def increment_existing_activations(activations: List[Activation], settings: Sett
# Determine new status
new_status = get_new_status(
activation=activation,
criteria=settings.criteria,
criterion=criterion_by_name.get(criteria_name),
task=task,
opportunities=opportunities,
events=meetings,
Expand Down Expand Up @@ -284,9 +286,7 @@ def increment_existing_activations(activations: List[Activation], settings: Sett
)

# Update engagement date if necessary
if not activation.engaged_date and is_inbound_criteria(
task, settings.criteria
):
if not activation.engaged_date and criterion_by_name.get(criteria_name).direction.lower() == "inbound":
activation.engaged_date = task_created_datetime.date()

# Update activation status and dates
Expand Down Expand Up @@ -379,9 +379,6 @@ def fetch_account_data(settings, tasks_by_account_id, first_prospecting_activity
)
)

criteria_name_by_direction = {
criteria.name: criteria.direction for criteria in settings.criteria
}
task_ids_by_criteria_name = get_task_ids_by_criteria_name(tasks_by_account_id)
contact_by_id = {contact.id: contact for contact in contacts}
for account_id, tasks_by_criteria_by_who_id in tasks_by_account_id.items():
Expand Down
71 changes: 44 additions & 27 deletions server/app/tests/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@

MOCK_CONTACT_IDS = [f"mock_contact_id{i}" for i in range(10)]
MOCK_ACCOUNT_IDS = [f"mock_account_id{i}" for i in range(1, 6)]
MOCK_ACCOUNT_FIELD_DESCRIBE_RESULT = [
{
"fields": [
{"name": "Id", "label": "Account ID", "type": "id"},
MOCK_ACCOUNT_FIELD_DESCRIBE_RESULT = {
"fields": [
{"name": "Id", "label": "Account ID", "type": "id"},
{"name": "Name", "label": "Account Name", "type": "string"},
{"name": "BillingStreet", "label": "Billing Street", "type": "textarea"},
{"name": "Phone", "label": "Phone", "type": "phone"},
{"name": "Industry", "label": "Industry", "type": "picklist"},
]
}
]
]
}


today = datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000+0000")
Expand All @@ -36,10 +34,13 @@
"fetch_contacts_by_account_ids": [],
"fetch_accounts_not_in_ids": [],
"fetch_logged_in_salesforce_user": [],
"fetch_sobject_fields__account": MOCK_ACCOUNT_FIELD_DESCRIBE_RESULT,
"fetch_salesforce_users": []
"fetch_salesforce_users": [],
}

# Add this new dictionary for permanent mock responses
permanent_mock_responses = {
"fetch_sobject_fields__account": MOCK_ACCOUNT_FIELD_DESCRIBE_RESULT
}

def add_mock_response(request_key, response):
"""
Expand All @@ -61,11 +62,14 @@ def clear_mocks():
for key in sobject_api_mock_response_by_request_key.keys():
sobject_api_mock_response_by_request_key[key] = []


import re


def mock_fetch_sobjects_async(soql_query, credentials, session):
return response_based_on_query(soql_query, return_raw_data=True)


def response_based_on_query(url, **kwargs):
"""
Mocks the response of a GET request to Salesforce's SObject API
Expand All @@ -75,12 +79,10 @@ def response_based_on_query(url, **kwargs):
print(f"Return raw data: {kwargs.get('return_raw_data', False)}")
try:
query_param = kwargs.get("params", {}).get("q", "") or url
is_valid_query = "/describe" or is_valid_salesforce_query(query_param) in url
return_raw_data = kwargs.get('return_raw_data', False)
is_valid_query = "/describe" in url or is_valid_salesforce_query(query_param)
return_raw_data = kwargs.get("return_raw_data", False)
if not is_valid_query:
return MagicMock(
status_code=400, json=lambda: {"error": f"Invalid query {query_param}"}
)
raise ValueError(f"Invalid query {query_param}")

# Mapping query characteristics to the corresponding key in the mock response dictionary
query_to_key_map = {
Expand Down Expand Up @@ -111,34 +113,46 @@ def response_based_on_query(url, **kwargs):
(
"AccountId IN" in query_param and "FROM Contact" in query_param
): "fetch_contacts_by_account_ids",
("SELECT Id,Name,Industry,AnnualRevenue,NumberOfEmployees,CreatedDate FROM Account" in query_param): "fetch_accounts_not_in_ids",
(
"SELECT Id,Name,Industry,AnnualRevenue,NumberOfEmployees,CreatedDate FROM Account"
in query_param
): "fetch_accounts_not_in_ids",
("Account" in url and "describe" in url): "fetch_sobject_fields__account",
("FROM User" in query_param): "fetch_salesforce_users",
}

# Determine which mock response to use based on the query characteristics
for condition, key in query_to_key_map.items():
if condition:
if not condition:
continue

mock_response = None
if key in permanent_mock_responses:
mock_response = permanent_mock_responses[key]
else:
mock_responses = sobject_api_mock_response_by_request_key.get(key)
mock_response = mock_responses.pop(0) if mock_responses else None
if mock_response and not return_raw_data:
return MagicMock(status_code=200, json=lambda: mock_response)
elif mock_response and return_raw_data:
return mock_response["records"]
else:
return MagicMock(
status_code=404,
json=lambda: {
"error": f"No more mock data available for endpoint {key}"
},
)

if mock_response and not return_raw_data:
return MagicMock(status_code=200, json=lambda: mock_response)
elif mock_response and return_raw_data:
return mock_response["records"]
else:
return MagicMock(
status_code=404,
json=lambda: {
"error": f"No more mock data available for endpoint {key}"
},
)

return MagicMock(status_code=404, json=lambda: {"error": "Not found"})
except Exception as e:
raise Exception(f"An error occurred while processing the query: {str(e)}")


# mock data


def get_n_mock_tasks_for_contacts_for_unique_values_content_criteria_query(
n, contacts, assignee_id
):
Expand All @@ -156,6 +170,7 @@ def get_n_mock_tasks_for_contacts_for_unique_values_content_criteria_query(
cloned_tasks.append(task_copy)
return cloned_tasks


def get_n_mock_tasks_per_contact_for_contains_content_crieria_query(
n, contacts, assignee_id
):
Expand All @@ -173,6 +188,7 @@ def get_n_mock_tasks_per_contact_for_contains_content_crieria_query(
cloned_tasks.append(task_copy)
return cloned_tasks


def get_n_mock_contacts_for_account(n, account_id):
contacts = []
for range_index in range(n):
Expand All @@ -186,6 +202,7 @@ def get_n_mock_contacts_for_account(n, account_id):
contacts.append(contact)
return contacts


def get_two_mock_contacts_per_account(accounts):
mock_contacts = []
for account in accounts:
Expand Down
9 changes: 7 additions & 2 deletions server/app/tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,17 +246,22 @@ def validate_prospecting_effort(


def assert_and_return_payload(response):
assert response.status_code == 200
assert response.status_code == 200, f"Unexpected status code: {response.status_code}, Response data: {response.data}"
data = response.get_json()
return data["data"][0]["raw_data"]


async def assert_and_return_payload_async(response_future):
response = await response_future
assert response.status_code == 200
response_data = response.data.decode('utf-8')
response_json = json.loads(response_data)
assert response.status_code == 200, f"Unexpected status code: {response.status_code}, Response message: {response_json.get('message', 'No message')}"
data = response.get_json()
return data["data"][0]["raw_data"]

def get_salesforce_compatible_datetime_now():
now = datetime.now()
return now.strftime("%Y-%m-%dT%H:%M:%S.000+0000")

def setup_one_activity_per_contact_with_staggered_created_dates_and_one_event_under_a_single_account_and_one_opportunity_for_a_different_account(
mock_user_id,
Expand Down
Loading
Loading