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

Pipedrive qol tweaks #1953

Merged
merged 6 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Add logic to create a person and associate with a lead
  • Loading branch information
matthewelwell committed Mar 1, 2023
commit 49d969ed917987bebc42e724fddcfafb98f6c947
25 changes: 20 additions & 5 deletions api/integrations/lead_tracking/pipedrive/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
PipedriveLead,
PipedriveOrganization,
PipedriveOrganizationField,
PipedrivePerson,
)


Expand Down Expand Up @@ -72,19 +73,33 @@ def create_lead(
self,
title: str,
organization_id: int,
person_id: int = None,
custom_fields: typing.Dict[str, typing.Any] = None,
) -> PipedriveLead:
data = {
"title": title,
"organization_id": organization_id,
**(custom_fields or {}),
}
if person_id:
data["person_id"] = person_id

api_response_data = self._make_request(
resource="leads", http_method="post", data=data, expected_status_code=201
)
return PipedriveLead.from_response_data(api_response_data)

def create_person(self, name: str, email: str) -> PipedrivePerson:
api_response_data = self._make_request(
resource="leads",
resource="persons",
http_method="post",
data={
"title": title,
"organization_id": organization_id,
**(custom_fields or {}),
"name": name,
"email": email,
},
expected_status_code=201,
)
return PipedriveLead.from_response_data(api_response_data)
return PipedrivePerson.from_response_data(api_response_data)

def _make_request(
self,
Expand Down
3 changes: 3 additions & 0 deletions api/integrations/lead_tracking/pipedrive/lead_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,12 @@ def create_lead(self, user: FFAdminUser) -> PipedriveLead:
)
raise e

person = self.client.create_person(name=user.full_name, email=user.email)

create_lead_kwargs = {
"title": user.email,
"organization_id": organization.id,
"person_id": person.id,
"custom_fields": {},
}
if user.sign_up_type:
Expand Down
9 changes: 9 additions & 0 deletions api/integrations/lead_tracking/pipedrive/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
BasePipedriveCustomFieldSchema,
PipedriveLeadSchema,
PipedriveOrganizationSchema,
PipedrivePersonSchema,
PipedriveValueSchema,
)

Expand Down Expand Up @@ -111,3 +112,11 @@ class PipedriveOrganizationField(BasePipedriveCustomField):

class PipedriveDealField(BasePipedriveCustomField):
pass


class PipedrivePerson(BasePipedriveModel):
schema = PipedrivePersonSchema()

def __init__(self, name: str, id: int = None):
self.name = name
self.id = id
5 changes: 5 additions & 0 deletions api/integrations/lead_tracking/pipedrive/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ class BasePipedriveCustomFieldSchema(BaseSchema):
name = fields.Str()
field_type = fields.Str()
add_visible_flag = fields.Bool()


class PipedrivePersonSchema(BaseSchema):
name = fields.Str()
id = fields.Int()
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"success": true,
"data": {
"id": 1,
"company_id": 1,
"owner_id": {
"id": 1,
"name": "Johnny Bravo",
"email": "johnny.bravo@testing.com",
"has_pic": 0,
"pic_hash": null,
"active_flag": true,
"value": 1
},
"org_id": null,
"name": "Yogi Bear",
"first_name": "Yogi",
"last_name": "Bear",
"open_deals_count": 0,
"related_open_deals_count": 0,
"closed_deals_count": 0,
"related_closed_deals_count": 0,
"participant_open_deals_count": 0,
"participant_closed_deals_count": 0,
"email_messages_count": 0,
"activities_count": 0,
"done_activities_count": 0,
"undone_activities_count": 0,
"files_count": 0,
"notes_count": 0,
"followers_count": 0,
"won_deals_count": 0,
"related_won_deals_count": 0,
"lost_deals_count": 0,
"related_lost_deals_count": 0,
"active_flag": true,
"phone": [
{
"value": "",
"primary": true
}
],
"email": [
{
"label": "",
"value": "yogi.bear@testing.com",
"primary": true
}
],
"first_char": "y",
"update_time": "2023-03-01 17:01:28",
"delete_time": null,
"add_time": "2023-03-01 17:01:28",
"visible_to": "3",
"picture_id": null,
"next_activity_date": null,
"next_activity_time": null,
"next_activity_id": null,
"last_activity_id": null,
"last_activity_date": null,
"last_incoming_mail_time": null,
"last_outgoing_mail_time": null,
"label": null,
"org_name": null,
"cc_email": "johnnybravo@pipedrivemail.com",
"primary_email": "yogi.bear@testing.com",
"owner_name": "Johnny Bravo",
"48ce4c9eda91f7dd0686311da7d9de760f36412c": null,
"c969dcba47bfbf0d1ebdc7725585fa5923d86289": null,
"139c0597b60ff87b43fbf8a161c711fe271d840b": null,
"59b8dfd809153b4c8d907a43d4267382af527b0d": null,
"42ed554baed97917838f615a89d0bb1b95f92664": null,
"719adb6116ae3940aabb2cc27c856b36c7ea20f7": null,
"3b8ae6152f631d9097f20bfccf84ace0c0aa30dd": null,
"adb724817541a261deb317be9713a27cd05c731f": null,
"5f8b3dab1eb64632d5f53f6dd6722603133854b9": null,
"7480d49272ba86fe23623761907fb61a493ff55d": null,
"04acbb84aca70fcf3197d34872347dbd261971bd": null,
"44093b7190dc9fab58420b2aae010d968f87669d": null,
"2f9d60ffac95d6416cfac3481ec6319fe36a7413": null,
"7b273efc901d979329db2529438a5ab11d28c3df": null,
"aada9a1b320aae2a0c3acfec9b76f4903a81233a": null,
"54f35043438c589847bef00866adcb6a427b2fc2": null,
"76289b0ef12853c348615ce1fc2c5c06db23d842": null
},
"related_objects": {
"user": {
"13603501": {
"id": 13603501,
"name": "Johnny Bravo",
"email": "johnny.bravo@testing.com",
"has_pic": 0,
"pic_hash": null,
"active_flag": true
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,41 @@ def test_pipedrive_api_client_create_deal_field(

assert organization_field.key == deal_field_key
assert organization_field.name == deal_field_name


@responses.activate
def test_pipedrive_api_client_create_person(
pipedrive_api_client, pipedrive_base_url, pipedrive_api_token
):
# Given
example_response_file_name = join(
dirname(abspath(__file__)), "example_api_responses/create_person.json"
)

# obtained from file above, duplicated here to simplfiy test
person_name = "Yogi Bear"
person_email = "yogi.bear@testing.com"
person_id = 1

with open(example_response_file_name) as f:
responses.add(
method=responses.POST,
url=f"{pipedrive_base_url}/persons",
json=json.load(f),
status=201,
)

# When
person = pipedrive_api_client.create_person(name=person_name, email=person_email)

# Then
assert len(responses.calls) == 1
call = responses.calls[0]
assert call.request.params["api_token"] == pipedrive_api_token

json_request_body = json.loads(call.request.body)
assert json_request_body["name"] == person_name
assert json_request_body["email"] == person_email

assert person.name == person_name
assert person.id == person_id
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
from integrations.lead_tracking.pipedrive.lead_tracker import (
PipedriveLeadTracker,
)
from integrations.lead_tracking.pipedrive.models import PipedriveOrganization
from integrations.lead_tracking.pipedrive.models import (
PipedriveOrganization,
PipedrivePerson,
)
from users.models import FFAdminUser, SignUpType


Expand All @@ -21,6 +24,9 @@ def test_create_lead_adds_to_existing_organization_if_exists(db, mocker, setting
mock_pipedrive_client = mocker.MagicMock()
mock_pipedrive_client.search_organizations.return_value = [organization]

person = PipedrivePerson(id=1, name="Elmer Fudd")
mock_pipedrive_client.create_person.return_value = person

lead_tracker = PipedriveLeadTracker(client=mock_pipedrive_client)

# When
Expand All @@ -33,6 +39,7 @@ def test_create_lead_adds_to_existing_organization_if_exists(db, mocker, setting
"custom_fields": {
settings.PIPEDRIVE_SIGN_UP_TYPE_DEAL_FIELD_KEY: SignUpType.NO_INVITE.value
},
"person_id": person.id,
}
mock_pipedrive_client.create_lead.assert_called_once_with(
**expected_create_lead_kwargs
Expand All @@ -55,6 +62,9 @@ def test_create_lead_creates_new_organization_if_not_exists(db, settings, mocker
mock_pipedrive_client.search_organizations.return_value = []
mock_pipedrive_client.create_organization.return_value = organization

person = PipedrivePerson(id=1, name="Elmer Fudd")
mock_pipedrive_client.create_person.return_value = person

lead_tracker = PipedriveLeadTracker(client=mock_pipedrive_client)

# When
Expand All @@ -67,6 +77,7 @@ def test_create_lead_creates_new_organization_if_not_exists(db, settings, mocker
"custom_fields": {
settings.PIPEDRIVE_SIGN_UP_TYPE_DEAL_FIELD_KEY: SignUpType.NO_INVITE.value
},
"person_id": person.id,
}
mock_pipedrive_client.create_lead.assert_called_once_with(
**expected_create_lead_kwargs
Expand Down