Skip to content

Commit

Permalink
fix engaged date before first prospecting activity date bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Nickz22 committed Oct 15, 2024
1 parent 146eb49 commit f13db82
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 34 deletions.
18 changes: 9 additions & 9 deletions server/app/helpers/activation_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,12 @@ def create_activation(
qualifying_event,
task_ids_by_criteria_name,
settings,
all_tasks_under_account,
outbound_tasks_under_account,
engaged_date,
):
today = date.today()
last_prospecting_activity = parse_datetime_string_with_timezone(
all_tasks_under_account[-1]["CreatedDate"]
outbound_tasks_under_account[-1]["CreatedDate"]
).date()
# Determine the activated_date based on the activation condition
if qualifying_opportunity:
Expand All @@ -335,12 +335,12 @@ def create_activation(
activating_task = next(
(
task
for task in reversed(all_tasks_under_account)
for task in reversed(outbound_tasks_under_account)
if task["Id"] in outbound_task_ids
and len(
[
t
for t in all_tasks_under_account
for t in outbound_tasks_under_account
if t["CreatedDate"] <= task["CreatedDate"]
and t["Id"] in outbound_task_ids
]
Expand All @@ -367,13 +367,13 @@ def create_activation(

contact_by_id = {
task["Contact"].id: task["Contact"]
for task in all_tasks_under_account
for task in outbound_tasks_under_account
if task["Contact"]
}

activation = Activation(
id=generate_unique_id(),
account=all_tasks_under_account[0]["Account"],
account=outbound_tasks_under_account[0]["Account"],
# activated_date=activated_date,
activated_date=account_first_prospecting_activity,
days_activated=(today - account_first_prospecting_activity).days,
Expand All @@ -395,12 +395,12 @@ def create_activation(
),
event_ids=[qualifying_event["Id"]] if qualifying_event else None,
task_ids=outbound_task_ids,
tasks=all_tasks_under_account, # Add this line
tasks=outbound_tasks_under_account, # Add this line
status=activation_status,
prospecting_metadata=create_prospecting_metadata(
task_ids=outbound_task_ids,
task_ids_by_criteria_name=task_ids_by_criteria_name,
all_tasks_under_account=all_tasks_under_account,
all_tasks_under_account=outbound_tasks_under_account,
),
)

Expand All @@ -412,7 +412,7 @@ def create_activation(

activation.prospecting_effort = create_prospecting_efforts(
activation,
all_tasks_under_account,
outbound_tasks_under_account,
outbound_task_ids,
task_ids_by_criteria_name,
qualifying_opportunity,
Expand Down
44 changes: 19 additions & 25 deletions server/app/services/activation_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ async def fetch_account_data(
period_days=settings.tracking_period,
)

# if the task is not in the tracking period, but was created before the start of the tracking period,
# if the task is not in the tracking period, but was created before the start of the tracking period,
# let's proceed until we find a task that is in the tracking period
if (
not is_task_in_tracking_period
Expand All @@ -612,11 +612,6 @@ async def fetch_account_data(
if is_task_in_tracking_period:
if task.get("WhoId") not in valid_task_ids_by_who_id:
valid_task_ids_by_who_id[task.get("WhoId")] = []
account_first_prospecting_activity = (
parse_datetime_string_with_timezone(
task.get("CreatedDate")
).date()
)
valid_task_ids_by_who_id[task.get("WhoId")].append(task.get("Id"))
last_valid_task_assignee_id = task.get("OwnerId")
task_ids.append(task.get("Id"))
Expand Down Expand Up @@ -654,19 +649,13 @@ async def fetch_account_data(
)
)
valid_task_ids_by_who_id.clear()
account_first_prospecting_activity = None
task_ids.clear()
if is_model_date_field_within_window(
task, start_tracking_period, settings.tracking_period
):
valid_task_ids_by_who_id[task.get("WhoId")] = [
task.get("Id")
]
account_first_prospecting_activity = (
parse_datetime_string_with_timezone(
task.get("CreatedDate")
).date()
)
task_ids = [task.get("Id")]
last_valid_task_assignee_id = task.get("OwnerId")
continue
Expand Down Expand Up @@ -704,8 +693,15 @@ async def fetch_account_data(
f"Warning: No valid Salesforce user found for account {account_id}"
)
if last_valid_task_assignee_id in salesforce_user_by_id:
outbound_tasks_in_tracking_period = [
task
for task in all_outbound_tasks_under_account
if task["Id"] in task_ids
]
activation = create_activation(
account_first_prospecting_activity=account_first_prospecting_activity,
account_first_prospecting_activity=parse_datetime_string_with_timezone(
outbound_tasks_in_tracking_period[0]["CreatedDate"]
).date(),
active_contact_ids=active_contact_ids,
last_valid_task_creator=salesforce_user_by_id.get(
last_valid_task_assignee_id
Expand All @@ -715,11 +711,7 @@ async def fetch_account_data(
qualifying_event=qualifying_event,
task_ids_by_criteria_name=task_ids_by_criteria_name,
settings=settings,
all_tasks_under_account=[
task
for task in all_tasks_under_account
if task["Id"] in task_ids
],
outbound_tasks_under_account=outbound_tasks_in_tracking_period,
engaged_date=engaged_date,
)
activations.append(activation)
Expand All @@ -730,7 +722,6 @@ async def fetch_account_data(
+ settings.inactivity_threshold,
)
valid_task_ids_by_who_id.clear()
account_first_prospecting_activity = None
task_ids.clear()
except Exception as e:
set_context(
Expand Down Expand Up @@ -790,8 +781,15 @@ async def fetch_account_data(
else None
)
try:
outbound_tasks_in_tracking_period = [
task
for task in all_outbound_tasks_under_account
if task["Id"] in task_ids
]
activation = create_activation(
account_first_prospecting_activity=account_first_prospecting_activity,
account_first_prospecting_activity=parse_datetime_string_with_timezone(
outbound_tasks_in_tracking_period[0]["CreatedDate"]
).date(),
active_contact_ids=active_contact_ids,
last_valid_task_creator=salesforce_user_by_id.get(
last_valid_task_assignee_id
Expand All @@ -801,11 +799,7 @@ async def fetch_account_data(
qualifying_event=qualifying_event,
task_ids_by_criteria_name=task_ids_by_criteria_name,
settings=settings,
all_tasks_under_account=[
task
for task in all_tasks_under_account
if task["Id"] in task_ids
],
outbound_tasks_under_account=outbound_tasks_in_tracking_period,
engaged_date=engaged_date,
)
activations.append(activation)
Expand Down

0 comments on commit f13db82

Please sign in to comment.