Skip to content

Commit

Permalink
Fix trying to add members to groups they are already part of.
Browse files Browse the repository at this point in the history
  • Loading branch information
HalfVoxel committed Jan 8, 2025
1 parent 44eb83d commit 7c8b692
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
33 changes: 22 additions & 11 deletions api/src/multiaccessy/accessy.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,7 @@ def get_all_members(self) -> list[AccessyMember]:
org_member_ids = set(item["id"] for item in self._get_users_org())

members = self._user_ids_to_accessy_members(org_member_ids)

lab_ids = set(item["userId"] for item in self._get_users_lab())
special_ids = set(item["userId"] for item in self._get_users_special())

for m in members:
if m.user_id in lab_ids:
m.groups.add(ACCESSY_LABACCESS_GROUP)
if m.user_id in special_ids:
m.groups.add(ACCESSY_SPECIAL_LABACCESS_GROUP)
self._populate_user_groups(members)

return members

Expand Down Expand Up @@ -547,7 +539,7 @@ def fill_membership_id(user: AccessyMember) -> None:
threads = []
user_ids = list(user_ids)
thread_count = min(4, len(user_ids))
accessy_members = []
accessy_members: List[AccessyMember] = []
for i in range(thread_count):
# Chunk the user_ids into equal parts for each thread
slice = user_ids[i::thread_count]
Expand All @@ -572,10 +564,29 @@ def worker(member_slice: list[AccessyMember]) -> None:

return accessy_members

def _populate_user_groups(self, members: List[AccessyMember]) -> None:
lab_ids = set(item["userId"] for item in self._get_users_lab())
special_ids = set(item["userId"] for item in self._get_users_special())

for m in members:
if m.user_id in lab_ids:
m.groups.add(ACCESSY_LABACCESS_GROUP)
if m.user_id in special_ids:
m.groups.add(ACCESSY_SPECIAL_LABACCESS_GROUP)

def get_org_user_from_phone(
self, phone_number: MSISDN, users_in_org: list[dict] | None = None
) -> Union[None, AccessyMember]:
"""Get a AccessyMember from a phone number (if in org)."""
member = self._get_org_user_from_phone(phone_number, users_in_org)
if member is not None:
self._populate_user_groups([member])
return member

def _get_org_user_from_phone(
self, phone_number: MSISDN, users_in_org: list[dict] | None = None
) -> Union[None, AccessyMember]:
"""Get a AccessyMember from a phone number (if in org)"""
"""Get a AccessyMember from a phone number (if in org). Groups are not set."""
if users_in_org is None:
users_in_org = self._get_users_org()

Expand Down
2 changes: 1 addition & 1 deletion api/src/multiaccessy/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def sync(today: Optional[date] = None, member_id: Optional[int] = None) -> None:
raise Exception("Member does not exist")
if member.phone is None:
return
accessy_member = accessy_session._get_org_user_from_phone(member.phone)
accessy_member = accessy_session.get_org_user_from_phone(member.phone)
actual_members = [accessy_member] if accessy_member is not None else []
else:
actual_members = accessy_session.get_all_members()
Expand Down

0 comments on commit 7c8b692

Please sign in to comment.