using System.Text.Json;
using Api.Services.Contracts;
using CvMatcher.Models.Responses;
using CvSearch.Data;
using CvSearch.Data.Entities;
using CvMatcher.Models.Settings;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
namespace Api.Services;
///
/// Creates and validates one-time job search tokens, and creates the corresponding search sessions.
/// Provider configuration is read from cvSearch.JobProviders at session-creation time and
/// snapshotted into JobSearchSessionEntity.ProviderConfigJson so subsequent config changes
/// do not affect already-queued sessions.
/// Keywords are extracted by the LLM during the CV-to-job match call and stored on the token,
/// then copied to the session when the user clicks the link — no extra RAG call needed.
///
public sealed class JobTokenService : IJobTokenService
{
private readonly CvSearchDbContext _db;
private readonly JobSearchSettings _settings;
private readonly ILogger _logger;
public JobTokenService(
CvSearchDbContext db,
IOptions settings,
ILogger logger)
{
_db = db;
_settings = settings.Value;
_logger = logger;
}
///
public async Task CreateTokenAsync(string cvDocumentId, string email, string language, IReadOnlyList keywords, CancellationToken ct)
{
var hasEnabledProviders = await _db.JobProviders.AnyAsync(p => p.Enabled, ct);
if (!hasEnabledProviders)
{
_logger.LogDebug("Job search token skipped — no enabled providers in cvSearch.JobProviders");
return null;
}
var token = new JobSearchTokenEntity
{
Id = Guid.NewGuid().ToString("N"),
CvDocumentId = cvDocumentId,
Email = email,
Language = language,
Keywords = string.Join(",", keywords),
ExpiresAt = DateTime.UtcNow.AddDays(_settings.TokenExpiryDays),
Used = false,
CreatedAt = DateTime.UtcNow
};
_db.JobSearchTokens.Add(token);
await _db.SaveChangesAsync(ct);
_logger.LogInformation("Job search token created. TokenId={TokenId}, CvDocumentId={CvDocumentId}, Keywords={Keywords}", token.Id, cvDocumentId, token.Keywords);
return token.Id;
}
///
public async Task TriggerStartAsync(string tokenId, CancellationToken ct)
{
var token = await _db.JobSearchTokens.FirstOrDefaultAsync(x => x.Id == tokenId, ct);
if (token is null) return StartJobSearchStatus.NotFound;
if (token.Used) return StartJobSearchStatus.AlreadyUsed;
if (token.ExpiresAt <= DateTime.UtcNow) return StartJobSearchStatus.Expired;
token.Used = true;
await _db.SaveChangesAsync(ct);
var keywords = token.Keywords;
var enabledProviders = await _db.JobProviders
.Where(p => p.Enabled)
.OrderBy(p => p.DisplayOrder)
.ToListAsync(ct);
var providerConfigJson = JsonSerializer.Serialize(
enabledProviders.Select(ToConfig).ToList(),
new JsonSerializerOptions(JsonSerializerDefaults.Web));
var session = new JobSearchSessionEntity
{
Id = Guid.NewGuid().ToString("N"),
TokenId = token.Id,
CvDocumentId = token.CvDocumentId,
Email = token.Email,
Language = token.Language,
Status = JobSearchStatus.Pending,
Keywords = keywords,
ProviderConfigJson = providerConfigJson,
CreatedAt = DateTime.UtcNow
};
_db.JobSearchSessions.Add(session);
await _db.SaveChangesAsync(ct);
_logger.LogInformation(
"Job search session created. SessionId={SessionId}, Keywords={Keywords}, Providers={Providers}",
session.Id, keywords, string.Join(", ", enabledProviders.Select(p => p.Name)));
return StartJobSearchStatus.Started;
}
private static JobProviderConfig ToConfig(JobProviderEntity entity)
{
List keywords;
try
{
keywords = JsonSerializer.Deserialize>(entity.InitialKeywordsJson,
new JsonSerializerOptions(JsonSerializerDefaults.Web)) ?? [];
}
catch
{
keywords = [];
}
return new JobProviderConfig
{
Name = entity.Name,
Enabled = entity.Enabled,
SearchUrlTemplate = entity.SearchUrlTemplate,
JobLinkContains = entity.JobLinkContains,
InitialKeywords = keywords,
MaxResults = entity.MaxResults
};
}
}