fix(mobile): повторный enroll и удаление устройств в UI (0.9.10).

При повторной привязке Seaca переиспользуется запись device_uuid вместо INSERT.
Добавлены POST /devices/{id}/revoke и DELETE для полного удаления записи в настройках SAC.
This commit is contained in:
Andrey Lutsenko
2026-06-13 16:36:07 +10:00
parent e022f2f213
commit 3d36faa49d
10 changed files with 186 additions and 39 deletions
+40 -21
View File
@@ -1,4 +1,4 @@
"""Создание и проверка кодов регистрации Seaca."""
"""Создание и проверка кодов регистрации Seaca."""
from __future__ import annotations
@@ -223,31 +223,50 @@ def enroll_device(
if code_row.target_user_id is not None and user.id != code_row.target_user_id:
raise ValueError("enrollment code is bound to another user")
existing = db.scalar(select(MobileDevice).where(MobileDevice.device_uuid == cleaned_uuid))
if existing is not None and existing.user_id != user.id:
raise ValueError("device_uuid already registered to another user")
active_count = _count_active_devices(db, user.id)
if (
existing is not None
and existing.user_id == user.id
and existing.revoked_at is None
):
active_count -= 1
if active_count >= mobile_cfg.max_devices_per_user:
raise ValueError("device limit reached for this user")
existing = db.scalar(select(MobileDevice).where(MobileDevice.device_uuid == cleaned_uuid))
if existing is not None:
if existing.revoked_at is None and existing.user_id != user.id:
raise ValueError("device_uuid already registered to another user")
if existing.revoked_at is None:
revoke_refresh_tokens_for_device(db, existing.id)
existing.revoked_at = now
existing = None
cleaned_display = (display_name or "").strip() or "Android"
cleaned_platform = (platform or "android").strip() or "android"
cleaned_app_version = (app_version or "").strip() or None
cleaned_fcm = (fcm_token or "").strip() or None
device = MobileDevice(
user_id=user.id,
device_uuid=cleaned_uuid,
display_name=(display_name or "").strip() or "Android",
platform=(platform or "android").strip() or "android",
app_version=(app_version or "").strip() or None,
fcm_token=(fcm_token or "").strip() or None,
fcm_token_updated_at=now if fcm_token else None,
enrollment_code_id=code_row.id,
last_seen_at=now,
)
db.add(device)
if existing is not None:
revoke_refresh_tokens_for_device(db, existing.id)
existing.revoked_at = None
existing.user_id = user.id
existing.display_name = cleaned_display
existing.platform = cleaned_platform
existing.app_version = cleaned_app_version
existing.fcm_token = cleaned_fcm
existing.fcm_token_updated_at = now if cleaned_fcm else None
existing.enrollment_code_id = code_row.id
existing.last_seen_at = now
device = existing
else:
device = MobileDevice(
user_id=user.id,
device_uuid=cleaned_uuid,
display_name=cleaned_display,
platform=cleaned_platform,
app_version=cleaned_app_version,
fcm_token=cleaned_fcm,
fcm_token_updated_at=now if cleaned_fcm else None,
enrollment_code_id=code_row.id,
last_seen_at=now,
)
db.add(device)
code_row.use_count += 1
db.flush()