Skip to content

Commit

Permalink
Log user data changes and not just from to_json
Browse files Browse the repository at this point in the history
  • Loading branch information
esoergel committed Jun 21, 2024
1 parent 45137f3 commit 8e813e2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
4 changes: 3 additions & 1 deletion corehq/apps/users/tests/test_log_user_change.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ class TestLogUserChange(TestCase):
def setUpClass(cls):
cls.project = create_domain(cls.domain)
cls.web_user = WebUser.create(cls.domain, '[email protected]', '******',
user_data={'favorite_color': 'cyan'},
created_by=None, created_via=None)
cls.commcare_user = CommCareUser.create(cls.domain, f'test@{cls.domain}.commcarehq.org', '******',
user_data={'favorite_color': 'purple'},
created_by=cls.web_user, created_via=USER_CHANGE_VIA_WEB)

@classmethod
Expand Down Expand Up @@ -186,7 +188,7 @@ def _get_expected_changes_json(user):
'status': None,
'subscribed_to_commcare_users': False,
'two_factor_auth_disabled_until': None,
'user_data': {},
'user_data': user.get_user_data('test').raw,
'user_location_id': None,
'username': user_json['username']
}
5 changes: 3 additions & 2 deletions corehq/apps/users/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,19 +398,20 @@ def log_user_change(by_domain, for_domain, couch_user, changed_by_user, changed_
changed_by_repr=changed_by_repr,
user_id=couch_user.get_id,
changed_by=changed_by_id,
changes=_get_changed_details(couch_user, action, fields_changed),
changes=_get_changed_details(couch_user, action, fields_changed, for_domain),
changed_via=changed_via,
change_messages=change_messages,
action=action.value,
user_upload_record_id=bulk_upload_record_id,
)


def _get_changed_details(couch_user, action, fields_changed):
def _get_changed_details(couch_user, action, fields_changed, for_domain):
from corehq.apps.users.model_log import UserModelAction

if action in [UserModelAction.CREATE, UserModelAction.DELETE]:
changed_details = couch_user.to_json()
changed_details['user_data'] = couch_user.get_user_data(for_domain).raw if for_domain else {}
else:
changed_details = fields_changed.copy()

Expand Down

0 comments on commit 8e813e2

Please sign in to comment.