diff --git a/ModeusSchedule.Abstractions/GlobalConsts.cs b/ModeusSchedule.Abstractions/GlobalConsts.cs index 10ac76e..849cf89 100644 --- a/ModeusSchedule.Abstractions/GlobalConsts.cs +++ b/ModeusSchedule.Abstractions/GlobalConsts.cs @@ -8,5 +8,4 @@ public static class GlobalConsts public static readonly JsonSerializerOptions JsonSerializerOptions = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping }; - public static string JwtFilePath { get; set; } = "data/jwt.txt"; } \ No newline at end of file diff --git a/SfeduSchedule/AppConsts.cs b/SfeduSchedule/AppConsts.cs new file mode 100644 index 0000000..70b5eb7 --- /dev/null +++ b/SfeduSchedule/AppConsts.cs @@ -0,0 +1,30 @@ +namespace SfeduSchedule; + +public static class AppConsts +{ + // Quartz Jobs Cron expressions + public const string UpdateJwtCronEnv = "UPDATE_JWT_CRON"; + public const string UpdateEmployeeCronEnv = "UPDATE_EMPLOYEES_CRON"; + + // Modeus + public const string PreinstalledJwtTokenEnv = "TOKEN"; + public const string ModeusUrlEnv = "MODEUS_URL"; + public const string ModeusDefaultUrl = "https://sfedu.modeus.org/"; + + // Telegram + public const string TgChatIdEnv = "TG_CHAT_ID"; + public const string TgTokenEnv = "TG_TOKEN"; + + // RateLimiter + public const string PermitLimitEnv = "PERMIT_LIMIT"; + public const string TimeLimitEnv = "TIME_LIMIT"; + + // MS Auth + public const string AuthUrlEnv = "AUTH_URL"; + public const string AuthApiKeyEnv = "AUTH_API_KEY"; + + // File paths + public const string JwtFileName = "jwt.txt"; + public const string EmployeesFileName = "employees.json"; + public const string DataFolderName = "Data"; +} \ No newline at end of file diff --git a/SfeduSchedule/Controllers/ScheduleController.cs b/SfeduSchedule/Controllers/ScheduleController.cs index cf5ed31..c6791e5 100644 --- a/SfeduSchedule/Controllers/ScheduleController.cs +++ b/SfeduSchedule/Controllers/ScheduleController.cs @@ -44,10 +44,14 @@ public class ScheduleController(ModeusService modeusService, ModeusEmployeeServi /// Возвращает список сотрудников с их GUID /// Сотрудник не найден /// Неавторизованный + /// Сервис сотрудников не инициализирован [HttpGet] [Route("searchemployee")] public async Task SearchEmployees([Required][MinLength(1)] string fullname) { + if (!modeusEmployeeService.IsInitialized()) + return StatusCode(503, "Сервис сотрудников не инициализирован, попробуйте позже."); + var employees = await modeusEmployeeService.GetEmployees(fullname, 10); if (employees.Count == 0) return NotFound(); diff --git a/SfeduSchedule/Jobs/UpdateJWTJob.cs b/SfeduSchedule/Jobs/UpdateJWTJob.cs index 18d497d..1cc2d0e 100644 --- a/SfeduSchedule/Jobs/UpdateJWTJob.cs +++ b/SfeduSchedule/Jobs/UpdateJWTJob.cs @@ -8,8 +8,7 @@ public class UpdateJwtJob( IConfiguration configuration, ILogger logger, IHttpClientFactory httpClientFactory, - ModeusHttpClient modeusHttpClient, - ModeusService modeusService) : IJob + ModeusHttpClient modeusHttpClient) : IJob { private const int MaxAttempts = 5; // Максимальное число попыток private const int DelaySeconds = 20; // Задержка между попытками в секундах @@ -19,8 +18,8 @@ public class UpdateJwtJob( { logger.LogInformation("Начало выполнения UpdateJwtJob"); - var authUrl = configuration["AUTH_URL"] ?? "http://msauth:8080/auth/ms"; - var apiKey = configuration["AUTH_API_KEY"] ?? string.Empty; + var authUrl = configuration[AppConsts.AuthUrlEnv] ?? "http://msauth:8080/auth/ms"; + var apiKey = configuration[AppConsts.AuthApiKeyEnv] ?? string.Empty; var client = httpClientFactory.CreateClient("authClient"); client.Timeout = TimeSpan.FromSeconds(TimeoutSeconds + 10); @@ -72,7 +71,7 @@ public class UpdateJwtJob( configuration["TOKEN"] = body.Jwt; modeusHttpClient.SetToken(body.Jwt); - await File.WriteAllTextAsync(GlobalConsts.JwtFilePath, + await File.WriteAllTextAsync(Path.Combine(Path.Combine(AppContext.BaseDirectory, AppConsts.DataFolderName), AppConsts.JwtFileName), body.Jwt + "\n" + DateTime.Now.ToString("O"), cts.Token); logger.LogInformation("JWT успешно обновлён"); return; diff --git a/SfeduSchedule/Services/ModeusEmployeeService.cs b/SfeduSchedule/Services/ModeusEmployeeService.cs index cc8888f..fe81756 100644 --- a/SfeduSchedule/Services/ModeusEmployeeService.cs +++ b/SfeduSchedule/Services/ModeusEmployeeService.cs @@ -1,13 +1,14 @@ -using SfeduSchedule.Logging; +using SfeduSchedule.Logging; namespace SfeduSchedule.Services; -public class ModeusEmployeeService(ILogger logger, ModeusService modeusService) +public class ModeusEmployeeService(ISchedulerFactory schedulerFactory) : IHostedService { private Dictionary)> _employees = []; private Task? _backgroundTask; private CancellationTokenSource? _cts; + private readonly string _employeesFilePath = Path.Combine(Path.Combine(AppContext.BaseDirectory, AppConsts.DataFolderName), AppConsts.EmployeesFileName); public async Task)>> GetEmployees(string fullname, int size = 10) { @@ -16,6 +17,11 @@ public class ModeusEmployeeService(ILogger logger, Modeus .Take(size) .ToDictionary(e => e.Key, e => e.Value); } + + public bool IsInitialized() + { + return _employees.Count > 0; + } public Task StartAsync(CancellationToken cancellationToken) { diff --git a/SfeduSchedule/SfeduSchedule.http b/SfeduSchedule/SfeduSchedule.http deleted file mode 100644 index 658f5a8..0000000 --- a/SfeduSchedule/SfeduSchedule.http +++ /dev/null @@ -1,21 +0,0 @@ -@SfeduSchedule_HostAddress = http://localhost:5087 - -### -[Получить расписание по списку GUID] -GET {{SfeduSchedule_HostAddress}}/api/schedule?attendeePersonId={{guid1}}&attendeePersonId={{guid2}} -Accept: application/json - -### -[Получить расписание через POST] -POST {{SfeduSchedule_HostAddress}}/api/schedule -Content-Type: application/json -Accept: application/json - -{ - "maxResults": 500, - "startDate": "2025-08-31T00:00:00Z", - "endDate": "2025-09-20T00:00:00Z", - "attendeePersonId": ["{{guid1}}", "{{guid2}}"] -} - -###