diff --git a/kdb-bot/src/bot_api/service/auth_service.py b/kdb-bot/src/bot_api/service/auth_service.py index 03fe78a5..0211687b 100644 --- a/kdb-bot/src/bot_api/service/auth_service.py +++ b/kdb-bot/src/bot_api/service/auth_service.py @@ -494,8 +494,14 @@ class AuthService(AuthServiceABC): added_user = True db_user = self._auth_users.get_auth_user_by_email(user_dto.email) - if db_user.users.count() == 0: - members.for_each(lambda x: self._auth_users.add_auth_user_user_rel(AuthUserUsersRelation(db_user, x))) + + auth_user_relation_ids = self._auth_users.get_auth_user_relation_ids(db_user) + + for user in db_user.users: + if user.id in auth_user_relation_ids: + continue + + self._auth_users.add_auth_user_user_rel(AuthUserUsersRelation(db_user, user)) if db_user.confirmation_id is not None and not added_user: raise ServiceException(ServiceErrorCode.Forbidden, "E-Mail not verified") diff --git a/kdb-bot/src/bot_data/abc/auth_user_repository_abc.py b/kdb-bot/src/bot_data/abc/auth_user_repository_abc.py index f0299068..9d9b06ec 100644 --- a/kdb-bot/src/bot_data/abc/auth_user_repository_abc.py +++ b/kdb-bot/src/bot_data/abc/auth_user_repository_abc.py @@ -14,6 +14,10 @@ class AuthUserRepositoryABC(ABC): def __init__(self): pass + @abstractmethod + def get_auth_user_relation_ids(self, auth_user: AuthUser) -> List[int]: + pass + @abstractmethod def get_all_auth_users(self) -> List[AuthUser]: pass diff --git a/kdb-bot/src/bot_data/service/auth_user_repository_service.py b/kdb-bot/src/bot_data/service/auth_user_repository_service.py index 87b39169..00afe7ae 100644 --- a/kdb-bot/src/bot_data/service/auth_user_repository_service.py +++ b/kdb-bot/src/bot_data/service/auth_user_repository_service.py @@ -50,13 +50,7 @@ class AuthUserRepositoryService(AuthUserRepositoryABC): auth_user_id=self._get_value_from_result(au_result[0]), ) - self._logger.trace( - __name__, - f"Send SQL command: {auth_user.get_select_user_id_from_relations()}", - ) - results = self._context.select(auth_user.get_select_user_id_from_relations()) - for result in results: - user_id = self._get_value_from_result(result[0]) + for user_id in self.get_auth_user_relation_ids(auth_user): if user_id is None: continue @@ -65,6 +59,19 @@ class AuthUserRepositoryService(AuthUserRepositoryABC): return auth_user + def get_auth_user_relation_ids(self, auth_user: AuthUser) -> List[int]: + self._logger.trace( + __name__, + f"Send SQL command: {auth_user.get_select_user_id_from_relations()}", + ) + relation_ids = List(int) + results = self._context.select(auth_user.get_select_user_id_from_relations()) + for result in results: + self._logger.trace(__name__, f"Got auth user relation with id {result[0]}") + relation_ids.append(result[0]) + + return relation_ids + def get_all_auth_users(self) -> List[AuthUser]: users = List(AuthUser) self._logger.trace(__name__, f"Send SQL command: {AuthUser.get_select_all_string()}")