Some checks are pending
Backend CI / test (push) Waiting to run
Backend 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
Deploy Telegram Bot / Deploy Telegram Bot (push) Waiting to run
55 lines
1.7 KiB
Dart
55 lines
1.7 KiB
Dart
import 'package:isar/isar.dart';
|
|
import 'package:mnemo_cards_backend/cron/task.dart';
|
|
import 'package:mnemo_cards_backend/user/admin_ids_service.dart';
|
|
import 'package:mnemo_cards_common_backend/mnemo_cards_common_backend.dart';
|
|
|
|
import '../main.dart';
|
|
|
|
class CheckAdminsTask with Task {
|
|
@override
|
|
String get name => 'check_admins';
|
|
|
|
@override
|
|
Future<void> task() async {
|
|
// Получаем список ID админов из переменной окружения или файла
|
|
final adminIdsList = await AdminIdsService.getAdminIds();
|
|
if (adminIdsList.isEmpty) {
|
|
return;
|
|
}
|
|
|
|
List<int> adminIds = [];
|
|
for (final token in adminIdsList) {
|
|
print('$name processing admin ID: $token');
|
|
if (token.isNotEmpty) {
|
|
await isar.writeTxn(() async {
|
|
final tokenModel = await isar.tokenModels
|
|
.filter()
|
|
.externalUserIdEqualTo(token)
|
|
.findFirst();
|
|
if (tokenModel == null) {
|
|
return;
|
|
}
|
|
final user = await isar.userModels.get(tokenModel.userId);
|
|
if (user != null) {
|
|
adminIds.add(user.id!);
|
|
if (!user.admin) {
|
|
await isar.userModels.put(user.copyWith.admin(true));
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|
|
// isar.writeTxn(() async {
|
|
// final admins =
|
|
// await isar.userModels.filter().adminEqualTo(true).findAll();
|
|
// final notAdmins = admins
|
|
// .where((u) => !adminIds.contains(u.id))
|
|
// .map((u) => u.copyWith.admin(false))
|
|
// .toList();
|
|
// await isar.userModels.putAll(notAdmins);
|
|
// });
|
|
}
|
|
|
|
@override
|
|
int get intervalSeconds => 300;
|
|
}
|