Skip to content
Closed
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
7 changes: 6 additions & 1 deletion app/pipedrive/_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,12 @@ async def handle_duplicate_hermes_ids(hermes_ids: str, object_type: str) -> int:
await pipedrive_request(f'deals/{main_object.pd_deal_id}', method='PUT', data=hermes_deal_data)
app_logger.info(f'Updated deal {main_object.pd_deal_id} from deal {main_object.id} by deal.pd_deal_id')

return main_object.id


# we should actually break here, as if we continue, we will be updating with the incorrect data, and we have just updated, which should in turn trigger the webhook again

else:
raise ValueError(f'Unknown object type {object_type}')


class PipedriveEvent(HermesBaseModel):
Expand Down
17 changes: 11 additions & 6 deletions app/pipedrive/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
_process_pd_pipeline,
_process_pd_stage,
)
from app.pipedrive._schema import PDObjectNames, PDStatus, PipedriveEvent
from app.pipedrive._schema import PDObjectNames, PDStatus, PipedriveEvent, handle_duplicate_hermes_ids
from app.pipedrive._utils import app_logger
from app.tc2.tasks import update_client_from_company

Expand All @@ -28,21 +28,26 @@ async def prepare_event_data(event_data: dict) -> dict:
different. If they are, it sets the current value back to the previous value.
"""

async def handle_custom_field(data: dict, field_name: str, handle_func: Union[Callable, str] = None):
async def handle_custom_field(data: dict, field_name: str, handle_func: str) -> Union[dict, None]:
cf_fields = await CustomField.filter(machine_name=field_name).values_list('pd_field_id', flat=True)
for pd_field_id in cf_fields:
for state in [PDStatus.PREVIOUS, PDStatus.CURRENT]:
# check if the field exists in the data and if the value is a string
if data.get(state) and pd_field_id in data[state] and isinstance(data[state][pd_field_id], str):
if handle_func == 'revert changes':
if state == PDStatus.PREVIOUS:
data[PDStatus.CURRENT][pd_field_id] = data[PDStatus.PREVIOUS][pd_field_id]

if callable(handle_func):
data[state][pd_field_id] = await handle_func(data[state][pd_field_id], data['meta']['object'])
if handle_func == 'handle_duplicate_hermes_ids':
# if current value is a string, handle duplicate hermes_id
if state == PDStatus.CURRENT:
data[state][pd_field_id] = await handle_duplicate_hermes_ids(data[state][pd_field_id], data['meta']['object'])

# overwrite the previous value with the current value to avoid duplicate hermes_id validation error later
data[PDStatus.PREVIOUS][pd_field_id] = data[state][pd_field_id]
return data

## TODO: Re-enable in #282
# event_data = await handle_custom_field(event_data, 'hermes_id', handle_duplicate_hermes_ids)
event_data = await handle_custom_field(event_data, 'hermes_id', 'handle_duplicate_hermes_ids')

event_data = await handle_custom_field(event_data, 'signup_questionnaire', 'revert changes')

Expand Down
Loading
Loading