Skip to content

Commit 8da0c61

Browse files
renzonrenzon
authored andcommitted
Fixed bug of removing user's id on ActiveCampaign
Inserted user on Usuários Ativos List close #2467
1 parent e3212ce commit 8da0c61

File tree

3 files changed

+53
-21
lines changed

3 files changed

+53
-21
lines changed

Pipfile.lock

Lines changed: 13 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pythonpro/email_marketing/facade.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,13 @@ def _create_or_update(name: str, email: str, role: str, *tags, id='0', phone=Non
6666
if settings.ACTIVE_CAMPAIGN_TURNED_ON is False:
6767
return
6868
prospect_list_id = _get_lists()['Prospects']
69-
id = _normalise_id(id)
7069
tags = list(tags)
70+
id = _normalise_id(id)
7171

7272
data = {
7373
'email': email,
7474
'first_name': name.split()[0],
7575
'tags': ','.join(tags),
76-
'field[%PYTHONPRO_ID%]': id,
7776
f'p[{prospect_list_id}]': prospect_list_id,
7877
'status': '1',
7978
}
@@ -82,6 +81,7 @@ def _create_or_update(name: str, email: str, role: str, *tags, id='0', phone=Non
8281
if id == _normalise_id('0'):
8382
contact = _client.contacts.create_contact(data)
8483
else:
84+
data['field[%PYTHONPRO_ID%]'] = id
8585
try:
8686
contact_id = _find_active_campaign_contact_id(id)
8787
except ActiveCampaignError:
@@ -91,6 +91,8 @@ def _create_or_update(name: str, email: str, role: str, *tags, id='0', phone=Non
9191
contact = _client.contacts.edit_contact(data)
9292
if role:
9393
grant_role(email, id, role)
94+
usuarios_ativos_list_id = _get_lists()['Usuários Ativos']
95+
_client.contacts.update_list_status(usuarios_ativos_list_id, contact['subscriber_id'], 1)
9496
return contact
9597

9698

@@ -141,6 +143,10 @@ def tag_as(email: str, id: int, *tags):
141143
data['id'] = _find_active_campaign_contact_id(id)
142144
except ActiveCampaignError:
143145
data['email'] = email
146+
else:
147+
# Activating users all the time she is tagged
148+
usuarios_ativos_list_id = _get_lists()['Usuários Ativos']
149+
_client.contacts.update_list_status(usuarios_ativos_list_id, data['id'], 1)
144150
return _client.contacts.add_tag(data)
145151

146152

pythonpro/email_marketing/tests/test_user_creation.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def test_lists(resps):
3636
lists = facade._get_lists()
3737
assert lists == {
3838
'Python Pro': '1', 'Python Birds': '2', 'Pytools': '3', 'Prospects': '4', 'Plantão de Dúvidas': '5',
39+
'Usuários Ativos': '12'
3940
}
4041

4142

@@ -69,15 +70,22 @@ def resps_user_not_found(setup_active_settings):
6970
),
7071
json=_user_editon_ok, status=200
7172
)
73+
r.add(
74+
r.POST,
75+
(
76+
'https://foo.pythonhosted.com/api/3/contactLists'
77+
),
78+
json=_user_editon_ok, status=200
79+
)
7280

7381
yield r
7482

7583

7684
def test_client_creation_user_not_found(resps_user_not_found, grant_role_mock):
7785
facade.create_or_update_client('Renzo Nuccitelli', 'renzo@python.pro.br', 'turma', id=1)
7886
body_payload = (
79-
'email=renzo%40python.pro.br&first_name=Renzo&tags=turma&field%5B%25'
80-
'PYTHONPRO_ID%25%5D=0000000001&p%5B4%5D=4&status=1'
87+
'email=renzo%40python.pro.br&first_name=Renzo&tags=turma&p%5B4%5D=4&status=1'
88+
'&field%5B%25PYTHONPRO_ID%25%5D=0000000001'
8189
)
8290
assert resps_user_not_found.calls[2].request.body == body_payload
8391

@@ -120,15 +128,22 @@ def resps_user_found(setup_active_settings):
120128
),
121129
json=_user_editon_ok, status=200
122130
)
131+
r.add(
132+
r.POST,
133+
(
134+
'https://foo.pythonhosted.com/api/3/contactLists'
135+
),
136+
json=_user_editon_ok, status=200
137+
)
123138

124139
yield r
125140

126141

127142
def test_client_creation_user_found(resps_user_found, grant_role_mock):
128143
facade.create_or_update_client('Renzo Nuccitelli', 'renzo@python.pro.br', 'turma', id=1)
129144
body_payload = (
130-
'email=renzo%40python.pro.br&first_name=Renzo&tags=turma&field%5B%25'
131-
'PYTHONPRO_ID%25%5D=0000000001&p%5B4%5D=4&status=1&id=1'
145+
'email=renzo%40python.pro.br&first_name=Renzo&tags=turma&p%5B4%5D=4&status=1'
146+
'&field%5B%25PYTHONPRO_ID%25%5D=0000000001&id=1'
132147
)
133148
assert resps_user_found.calls[2].request.body == body_payload
134149

@@ -157,15 +172,22 @@ def resps_two_users_found(setup_active_settings):
157172
),
158173
json=_user_editon_ok, status=200
159174
)
175+
r.add(
176+
r.POST,
177+
(
178+
'https://foo.pythonhosted.com/api/3/contactLists'
179+
),
180+
json=_user_editon_ok, status=200
181+
)
160182

161183
yield r
162184

163185

164186
def test_client_creation_two_users_found(resps_two_users_found, grant_role_mock):
165187
facade.create_or_update_client('Renzo Nuccitelli', 'renzo@python.pro.br', 'turma', id=1)
166188
body_payload = (
167-
'email=renzo%40python.pro.br&first_name=Renzo&tags=turma&field%5B%25'
168-
'PYTHONPRO_ID%25%5D=0000000001&p%5B4%5D=4&status=1'
189+
'email=renzo%40python.pro.br&first_name=Renzo&tags=turma&p%5B4%5D=4&status=1'
190+
'&field%5B%25PYTHONPRO_ID%25%5D=0000000001'
169191
)
170192
assert resps_two_users_found.calls[2].request.body == body_payload
171193

@@ -249,6 +271,10 @@ def test_granted_client_role(resps_user_found, grant_role_mock):
249271
'id': '5', 'name': 'Plantão de Dúvidas', 'cdate': '2019-12-06 15:28:51', 'private': '0', 'userid': '1',
250272
'subscriber_count': 0
251273
},
274+
'5': {
275+
'id': '12', 'name': 'Usuários Ativos', 'cdate': '2020-06-06 15:28:51', 'private': '0', 'userid': '1',
276+
'subscriber_count': 0
277+
},
252278
'result_code': 1,
253279
'result_message': 'Sucesso:algo retornou',
254280
'result_output': 'json'

0 commit comments

Comments
 (0)