Some checks failed
Backend CI / test (push) Waiting to run
Backend CI / build (push) Blocked by required conditions
Web App CI / test (push) Waiting to run
Web App CI / build (push) Blocked by required conditions
Deploy Mnemo Cards / Deploy Backend (push) Waiting to run
Deploy Mnemo Cards / Deploy Web App (push) Blocked by required conditions
Deploy Mnemo Cards / Final Verification (push) Blocked by required conditions
Mobile App CI / test (push) Has been cancelled
Deploy Telegram Bot / Deploy Telegram Bot (push) Has been cancelled
Mobile App CI / build-android (push) Has been cancelled
Mobile App CI / build-ios (push) Has been cancelled
62 lines
1.9 KiB
Dart
62 lines
1.9 KiB
Dart
import 'package:drift/drift.dart';
|
|
import 'package:drift_postgres/drift_postgres.dart';
|
|
import 'package:mnemo_cards_backend/user/admin_ids_service.dart';
|
|
import 'package:mnemo_cards_backend/user/user_repository.dart';
|
|
|
|
import '../database/database.dart';
|
|
import 'task.dart' as task;
|
|
|
|
class CheckAdminsTask with task.Task {
|
|
@override
|
|
String get name => 'check_admins';
|
|
|
|
final AppDatabase _db;
|
|
final UserRepository _userRepository;
|
|
|
|
CheckAdminsTask(this._db, this._userRepository);
|
|
|
|
@override
|
|
Future<void> task() async {
|
|
// Получаем список ID админов из переменной окружения или файла
|
|
final adminIdsList = await AdminIdsService.getAdminIds();
|
|
if (adminIdsList.isEmpty) {
|
|
return;
|
|
}
|
|
|
|
List<String> adminIds = [];
|
|
for (final externalUserId in adminIdsList) {
|
|
print('$name processing admin ID: $externalUserId');
|
|
if (externalUserId.isNotEmpty) {
|
|
// Найти токен по externalUserId
|
|
final token = await _db.userDao.getTokenByExternalUserId(
|
|
externalUserId,
|
|
);
|
|
|
|
if (token == null) {
|
|
continue;
|
|
}
|
|
|
|
// Получить пользователя по userId из токена
|
|
final user = await _userRepository.getUserById(token.userId);
|
|
if (user != null && user.id != null) {
|
|
adminIds.add(user.id!);
|
|
if (!user.admin) {
|
|
// Установить admin = true
|
|
await _userRepository.updateUserPartial(
|
|
UsersCompanion(
|
|
id: Value(user.id!),
|
|
admin: const Value(true),
|
|
updatedAt: Value(PgDateTime(DateTime.now())),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// TODO: Implement logic to remove admin flag from users not in adminIdsList
|
|
// if needed in the future
|
|
}
|
|
|
|
@override
|
|
int get intervalSeconds => 300;
|
|
}
|