Skip to content

Commit

Permalink
fixed import karma
Browse files Browse the repository at this point in the history
  • Loading branch information
ychebyshev committed Mar 16, 2024
1 parent c47707f commit cc139e1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
3 changes: 2 additions & 1 deletion app/handlers/change_karma.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ async def karma_import_handler(
user_repo: UserRepo,
):
logger.info("try to start import karma")
args = [dto.Import(d["id"], d["karma"]) for d in json.loads(command.args or "{}")]
args = [dto.Import(d["id"], d["karma"]) for d in json.loads(command.args or "[]")]
chat_owner: types.ChatMemberOwner = next(
filter(
lambda c: c.status == ChatMemberStatus.CREATOR,
Expand All @@ -177,3 +177,4 @@ async def karma_import_handler(
)
await import_karma(args, chat, user_repo)
logger.warning("completed import karma for chat %s", event_chat.id)
await message.reply("успешно выполнен импорт")
6 changes: 6 additions & 0 deletions app/infrastructure/database/repo/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ async def update_user_data(self, user: User, tg_user: types.User):
if changed:
await self.save(user)

async def get_or_create_from_tg_id(self, tg_id: int, using_db=None) -> User:
try:
return await User.get(tg_id=tg_id, using_db=using_db)
except DoesNotExist:
return await User.create(tg_id=tg_id, using_db=using_db)

async def get_or_create_from_tg_user(self, tg_user: types.User | dto.TargetUser) -> User:
if tg_user.id is None:
try:
Expand Down
4 changes: 2 additions & 2 deletions app/services/karma.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ async def get_chat(command_arguments: list[str], chat_repo: ChatRepo) -> Chat:


async def import_karma(import_data: list[dto.Import], chat: models.Chat, user_repo: UserRepo):
with in_transaction() as using_db:
async with in_transaction() as using_db:
for data in import_data:
target_user = await user_repo.get_by_id(data.id)
target_user = await user_repo.get_or_create_from_tg_id(data.id, using_db=using_db)
uk, _ = await UserKarma.get_or_create(
user=target_user,
chat=chat,
Expand Down

0 comments on commit cc139e1

Please sign in to comment.