chore: cleanup

This commit is contained in:
2025-07-25 16:37:42 +03:00
parent 7ceb8f656e
commit b15a77557e
28 changed files with 224 additions and 198 deletions

View File

@@ -10,28 +10,36 @@ using RabbitMQ.Client;
namespace JOBot.Backend.Services;
public class HeadHunterService(
IChannel channel,
ILogger<HeadHunterService> logger,
IOptions<HeadHunterConfig> config,
IChannel channel,
ILogger<HeadHunterService> logger,
IOptions<HeadHunterConfig> config,
AppDbContext dbContext)
{
public enum Status
{
UserAuthRejectedError,
HeadHunterAuthRejectedError,
UserNotFoundError,
HeadHunterResponseDeserializationFailedError,
Success
}
private readonly HeadHunterConfig _config = config.Value;
/// <summary>
/// Generate HeadHunter oauth authorization link
/// Generate HeadHunter oauth authorization link
/// </summary>
/// <param name="userId">Telegram UserId</param>
/// <returns>Link for auth</returns>
public string GenerateAuthLink(long userId)
{
return string.Format(_config.Links.AuthLink, [_config.ClientId, GetRedirectUrl(userId)]);
}
public async Task<Status> AuthUser(int userId, string authorizationCode, string? error)
public async Task<Status> AuthUser(int userId, string authorizationCode, string? error) //TODO: Разбить этот метод
{
logger.LogInformation($"Authorization for user {userId} in process...");
if (!string.IsNullOrEmpty(error))
{
logger.LogWarning($"User {userId} auth completed with error {error}");
@@ -69,7 +77,7 @@ public class HeadHunterService(
logger.LogWarning($"{res.Content.ReadAsStringAsync().Result}");
return Status.HeadHunterAuthRejectedError;
}
var responseDto = JsonSerializer.Deserialize<HeadHunterTokenResponseDto>(await res.Content.ReadAsStringAsync());
if (responseDto == null)
@@ -78,7 +86,7 @@ public class HeadHunterService(
$"{nameof(Status.HeadHunterResponseDeserializationFailedError)}");
return Status.HeadHunterResponseDeserializationFailedError;
}
var user = await dbContext.Users.FirstOrDefaultAsync(x => x.UserId == userId);
if (user == null)
@@ -86,20 +94,20 @@ public class HeadHunterService(
logger.LogWarning($"User {userId} search completed with error {nameof(Status.UserNotFoundError)}");
return Status.UserNotFoundError;
}
user.AccessToken = responseDto.AccessToken;
user.RefreshToken = responseDto.RefreshToken;
await dbContext.SaveChangesAsync();
logger.LogInformation($"User {userId} auth completed!");
await channel.BasicPublishAsync(
string.Empty,
RabbitQueues.AuthQueue,
string.Empty,
RabbitQueues.AuthQueue,
Encoding.UTF8.GetBytes(userId.ToString()));
logger.LogInformation($"RabbitMQ event was created");
logger.LogInformation("RabbitMQ event was created");
return Status.Success;
}
@@ -114,13 +122,4 @@ public class HeadHunterService(
Query = $"?userId={userId}"
}.ToString();
}
public enum Status
{
UserAuthRejectedError,
HeadHunterAuthRejectedError,
UserNotFoundError,
HeadHunterResponseDeserializationFailedError,
Success
}
}