feat: implemented available resumes on Back-end
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using JOBot.Backend.DAL.Context;
|
||||
using JOBot.Backend.DTOs.HeadHunterHook;
|
||||
using JOBot.Backend.DAL.Models;
|
||||
using JOBot.Backend.DTOs.HeadHunterApi;
|
||||
using JOBot.Backend.Infrastructure.Config;
|
||||
using JOBot.Infrastructure.Config;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
@@ -38,6 +39,13 @@ public class HeadHunterService(
|
||||
return string.Format(_config.Links.AuthLink, [_config.ClientId, GetRedirectUrl(userId)]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Auth user on HeadHunter API
|
||||
/// </summary>
|
||||
/// <param name="userId"></param>
|
||||
/// <param name="authorizationCode"></param>
|
||||
/// <param name="error"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<Status> AuthUser(int userId, string authorizationCode, string? error) //TODO: Разбить этот метод
|
||||
{
|
||||
logger.LogInformation($"Authorization for user {userId} in process...");
|
||||
@@ -123,6 +131,49 @@ public class HeadHunterService(
|
||||
return Status.Success;
|
||||
}
|
||||
|
||||
public async Task<Status> UpdateUserAccessToken(User user)
|
||||
{
|
||||
if(user.RefreshToken == null)
|
||||
return Status.UserAuthRejectedError;
|
||||
|
||||
using var client = new HttpClient(); //TODO: Написать wrapper для работы с HH API
|
||||
var form = new Dictionary<string, string>
|
||||
{
|
||||
{ "refresh_token", user.RefreshToken },
|
||||
{ "grant_type", "refresh_token" }
|
||||
};
|
||||
client.BaseAddress = new UriBuilder(_config.Links.HeadHunterApiDomain)
|
||||
{
|
||||
Port = -1,
|
||||
Scheme = "https"
|
||||
}.Uri;
|
||||
client.DefaultRequestHeaders.UserAgent.ParseAdd("Jobot BackEnd Service");
|
||||
|
||||
using var res = await client.SendAsync(
|
||||
new HttpRequestMessage(
|
||||
HttpMethod.Post,
|
||||
_config.Links.HeadHunterTokenRoute)
|
||||
{
|
||||
Content = new FormUrlEncodedContent(form)
|
||||
});
|
||||
|
||||
var responseDto =
|
||||
JsonSerializer.Deserialize<HeadHunterTokenResponseDto>(await res.Content.ReadAsStringAsync());
|
||||
|
||||
if (responseDto == null)
|
||||
{
|
||||
logger.LogWarning($"User {user.UserId} access token accept completed with error " +
|
||||
$"{nameof(Status.HeadHunterResponseDeserializationFailedError)}");
|
||||
return Status.HeadHunterResponseDeserializationFailedError;
|
||||
}
|
||||
|
||||
user.RefreshToken = responseDto.RefreshToken;
|
||||
user.AccessToken = responseDto.AccessToken;
|
||||
await dbContext.SaveChangesAsync();
|
||||
|
||||
return Status.Success;
|
||||
}
|
||||
|
||||
private string GetRedirectUrl(long userId)
|
||||
{
|
||||
return new UriBuilder(_config.Links.HookDomain)
|
||||
|
||||
Reference in New Issue
Block a user