Implement user creation and synchronization enhancements in admin sync handler
- Added functionality to create new users during synchronization if they are not found in the local database and have a valid Telegram ID. - Introduced logging for newly created users to improve tracking and debugging. - Enhanced synchronization statistics to include the count of newly created users, with localization support for both English and Russian. - Updated the details of synchronization status to reflect additional statistics, improving clarity in admin reports.
This commit is contained in:
@@ -31,6 +31,7 @@ async def perform_sync(panel_service: PanelApiService, session: AsyncSession,
|
||||
# Additional counters for detailed logging
|
||||
users_without_telegram_id = 0
|
||||
users_not_found_in_db = 0
|
||||
users_created = 0
|
||||
users_uuid_updated = 0
|
||||
subscriptions_created = 0
|
||||
subscriptions_updated = 0
|
||||
@@ -93,10 +94,33 @@ async def perform_sync(panel_service: PanelApiService, session: AsyncSession,
|
||||
if not existing_user:
|
||||
users_not_found_in_db += 1
|
||||
if telegram_id_from_panel:
|
||||
logging.debug(f"Panel user with telegramId {telegram_id_from_panel} and UUID {panel_uuid} not found in local DB")
|
||||
# Create new user if they have telegram_id
|
||||
try:
|
||||
user_data = {
|
||||
"user_id": telegram_id_from_panel,
|
||||
"username": None, # Username will be updated when user interacts with bot
|
||||
"first_name": None, # Panel doesn't provide this info
|
||||
"last_name": None, # Panel doesn't provide this info
|
||||
"language_code": "ru", # Default language
|
||||
"panel_user_uuid": panel_uuid,
|
||||
"is_banned": False,
|
||||
"referred_by_id": None
|
||||
}
|
||||
|
||||
new_user, was_created = await user_dal.create_user(session, user_data)
|
||||
if was_created:
|
||||
users_created += 1
|
||||
logging.info(f"Created new user {telegram_id_from_panel} from panel sync with UUID {panel_uuid}")
|
||||
|
||||
existing_user = new_user
|
||||
|
||||
except Exception as e_create:
|
||||
sync_errors.append(f"Error creating user {telegram_id_from_panel}: {str(e_create)}")
|
||||
logging.error(f"Error creating user {telegram_id_from_panel}: {e_create}")
|
||||
continue
|
||||
else:
|
||||
logging.debug(f"Panel user with UUID {panel_uuid} (no telegramId) not found in local DB")
|
||||
continue
|
||||
logging.debug(f"Panel user with UUID {panel_uuid} (no telegramId) not found in local DB - skipping")
|
||||
continue
|
||||
|
||||
# User found in local DB
|
||||
users_found_in_db += 1
|
||||
@@ -218,20 +242,27 @@ async def perform_sync(panel_service: PanelApiService, session: AsyncSession,
|
||||
|
||||
# Update sync status
|
||||
status = "completed_with_errors" if sync_errors else "completed"
|
||||
details = (f"📊 Статистика синхронизации:\n"
|
||||
f"🔍 Проверено записей панели: {panel_records_checked}\n"
|
||||
f"👥 Найдено пользователей в БД: {users_found_in_db}\n"
|
||||
f"🔄 Пользователей обновлено: {users_updated}\n"
|
||||
f"📋 Подписок синхронизировано: {subscriptions_synced_count}\n"
|
||||
f" ├── Создано новых: {subscriptions_created}\n"
|
||||
f" └── Обновлено существующих: {subscriptions_updated}")
|
||||
|
||||
# Build additional stats
|
||||
default_lang = settings.DEFAULT_LANGUAGE
|
||||
additional_stats = ""
|
||||
if users_without_telegram_id > 0:
|
||||
details += f"\n⚠️ Записей без telegramId: {users_without_telegram_id}"
|
||||
additional_stats += i18n_instance.gettext(default_lang, "admin_sync_no_telegram_id", count=users_without_telegram_id)
|
||||
if users_not_found_in_db > 0:
|
||||
details += f"\n❌ Не найдено в БД: {users_not_found_in_db}"
|
||||
additional_stats += i18n_instance.gettext(default_lang, "admin_sync_not_found_in_db", count=users_not_found_in_db)
|
||||
if sync_errors:
|
||||
details += f"\n🚫 Ошибок: {len(sync_errors)}"
|
||||
additional_stats += i18n_instance.gettext(default_lang, "admin_sync_errors", count=len(sync_errors))
|
||||
|
||||
# Build full details using localization
|
||||
details = i18n_instance.gettext(default_lang, "admin_sync_details",
|
||||
panel_records_checked=panel_records_checked,
|
||||
users_found_in_db=users_found_in_db,
|
||||
users_created=users_created,
|
||||
users_updated=users_updated,
|
||||
subscriptions_synced_count=subscriptions_synced_count,
|
||||
subscriptions_created=subscriptions_created,
|
||||
subscriptions_updated=subscriptions_updated,
|
||||
additional_stats=additional_stats
|
||||
)
|
||||
|
||||
await panel_sync_dal.update_panel_sync_status(
|
||||
session, status, details, panel_records_checked, subscriptions_synced_count
|
||||
@@ -244,6 +275,7 @@ async def perform_sync(panel_service: PanelApiService, session: AsyncSession,
|
||||
logging.info(f" Users without telegramId: {users_without_telegram_id}")
|
||||
logging.info(f" Users not found in local DB: {users_not_found_in_db}")
|
||||
logging.info(f" Users found in local DB: {users_found_in_db}")
|
||||
logging.info(f" Users created: {users_created}")
|
||||
logging.info(f" Users with UUID updated: {users_uuid_updated}")
|
||||
logging.info(f" Users updated overall: {users_updated}")
|
||||
logging.info(f" Subscriptions total synced: {subscriptions_synced_count}")
|
||||
@@ -256,6 +288,7 @@ async def perform_sync(panel_service: PanelApiService, session: AsyncSession,
|
||||
"details": details,
|
||||
"users_processed": panel_records_checked,
|
||||
"users_synced": users_found_in_db,
|
||||
"users_created": users_created,
|
||||
"subs_synced": subscriptions_synced_count,
|
||||
"errors": sync_errors
|
||||
}
|
||||
|
||||
+6
-1
@@ -518,5 +518,10 @@
|
||||
"admin_financial_week_label": "This week",
|
||||
"admin_financial_month_label": "This month",
|
||||
"admin_financial_all_time_label": "All time",
|
||||
"admin_financial_payments_label": "payments"
|
||||
"admin_financial_payments_label": "payments",
|
||||
|
||||
"admin_sync_details": "📊 Synchronization Statistics:\n🔍 Panel records checked: {panel_records_checked}\n👥 Users found in DB: {users_found_in_db}\n✨ New users created: {users_created}\n🔄 Users updated: {users_updated}\n📋 Subscriptions synced: {subscriptions_synced_count}\n ├── Created new: {subscriptions_created}\n └── Updated existing: {subscriptions_updated}{additional_stats}",
|
||||
"admin_sync_no_telegram_id": "\n⚠️ Records without telegramId: {count}",
|
||||
"admin_sync_not_found_in_db": "\n❌ Not found in DB: {count}",
|
||||
"admin_sync_errors": "\n🚫 Errors: {count}"
|
||||
}
|
||||
|
||||
+6
-1
@@ -526,5 +526,10 @@
|
||||
"admin_financial_week_label": "За неделю",
|
||||
"admin_financial_month_label": "За месяц",
|
||||
"admin_financial_all_time_label": "За все время",
|
||||
"admin_financial_payments_label": "платежей"
|
||||
"admin_financial_payments_label": "платежей",
|
||||
|
||||
"admin_sync_details": "📊 Статистика синхронизации:\n🔍 Проверено записей панели: {panel_records_checked}\n👥 Найдено пользователей в БД: {users_found_in_db}\n✨ Создано новых пользователей: {users_created}\n🔄 Пользователей обновлено: {users_updated}\n📋 Подписок синхронизировано: {subscriptions_synced_count}\n ├── Создано новых: {subscriptions_created}\n └── Обновлено существующих: {subscriptions_updated}{additional_stats}",
|
||||
"admin_sync_no_telegram_id": "\n⚠️ Записей без telegramId: {count}",
|
||||
"admin_sync_not_found_in_db": "\n❌ Не найдено в БД: {count}",
|
||||
"admin_sync_errors": "\n🚫 Ошибок: {count}"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user