Skip to content

Commit e46cd8f

Browse files
authored
fix: always create an alias for existing profiles (#17)
2 parents eb67c9c + 53cbb3c commit e46cd8f

File tree

4 files changed

+40
-14
lines changed

4 files changed

+40
-14
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ Change Log
1414
Unreleased
1515
~~~~~~~~~~
1616

17+
[0.1.8]
18+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19+
fix: always create an alias for existing profiles
20+
1721
[0.1.7]
1822
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1923
feat: add retrieve_unsubscribed_emails method

braze/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
Python client for interacting with Braze APIs.
33
"""
44

5-
__version__ = '0.1.7'
5+
__version__ = '0.1.8'

braze/client.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -222,17 +222,22 @@ def create_braze_alias(self, emails, alias_label, attributes=None):
222222
attributes = attributes or []
223223

224224
for email in emails:
225-
if not self.get_braze_external_id(email):
226-
user_alias = {
227-
'alias_label': alias_label,
228-
'alias_name': email,
229-
}
230-
user_aliases.append(user_alias)
231-
attribute = {
232-
'user_alias': user_alias,
233-
'email': email,
234-
}
235-
attributes.append(attribute)
225+
user_alias = {
226+
'alias_label': alias_label,
227+
'alias_name': email,
228+
}
229+
braze_external_id = self.get_braze_external_id(email)
230+
# Adding a user alias for an existing user requires an external_id to be
231+
# included in the new user alias object.
232+
# http://web.archive.org/web/20231005191135/https://www.braze.com/docs/api/endpoints/user_data/post_user_alias#response
233+
if braze_external_id:
234+
user_alias['external_id'] = braze_external_id
235+
user_aliases.append(user_alias)
236+
attribute = {
237+
'user_alias': user_alias,
238+
'email': email,
239+
}
240+
attributes.append(attribute)
236241

237242
# Each request can support up to 50 aliases.
238243
for user_alias_chunk in self._chunks(user_aliases, USER_ALIAS_CHUNK_SIZE):

tests/braze/test_client.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,23 @@ def test_create_braze_alias_user_exists(self):
247247
Tests that calls to to /users/alias/new and /users/track are not made
248248
if a Braze user already exists for the given email.
249249
"""
250+
existing_enternal_id = '1'
250251
responses.add(
251252
responses.POST,
252253
self.EXPORT_ID_URL,
253-
json={'users': [{'external_id': '1'}], 'message': 'success'},
254+
json={'users': [{'external_id': existing_enternal_id}], 'message': 'success'},
255+
status=201
256+
)
257+
responses.add(
258+
responses.POST,
259+
self.NEW_ALIAS_URL,
260+
json={'message': 'success'},
261+
status=201
262+
)
263+
responses.add(
264+
responses.POST,
265+
self.USERS_TRACK_URL,
266+
json={'message': 'success'},
254267
status=201
255268
)
256269

@@ -260,8 +273,12 @@ def test_create_braze_alias_user_exists(self):
260273
attributes=[]
261274
)
262275

263-
assert len(responses.calls) == 1
276+
assert len(responses.calls) == 3
264277
assert responses.calls[0].request.url == self.EXPORT_ID_URL
278+
assert responses.calls[1].request.url == self.NEW_ALIAS_URL
279+
alias_data = json.loads(responses.calls[1].request.body)
280+
assert alias_data['user_aliases'][0]['external_id'] == existing_enternal_id
281+
assert responses.calls[2].request.url == self.USERS_TRACK_URL
265282

266283
@responses.activate
267284
def test_create_braze_alias_batching(self):

0 commit comments

Comments
 (0)