feat(job-search): read providers from DB and suppress link when none enabled
JobTokenService.CreateTokenAsync queries cvSearch.JobProviders for any enabled row; returns null (no token created) when the table is empty or all providers are disabled. TriggerStartAsync snapshots enabled providers from DB at session-start time, preserving the existing snapshot contract. CvMatcherController guards link-building on a non-null TokenId so the "Start a job search" CTA is omitted from match emails when no providers are configured. JobSearchSettings.Providers list removed — provider config now lives exclusively in the DB. CvSearchJobTask.GetProviders falls back to an empty list with a warning (snapshot should always be populated from DB). Closes #35 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -183,8 +183,11 @@ public sealed class CvMatcherController : ControllerBase
|
|||||||
var tokenResp = await _jobSearchApi.CreateTokenAsync(
|
var tokenResp = await _jobSearchApi.CreateTokenAsync(
|
||||||
new CreateJobSearchTokenRequest { CvDocumentId = request.CvDocumentId, Email = request.Email, Language = language },
|
new CreateJobSearchTokenRequest { CvDocumentId = request.CvDocumentId, Email = request.Email, Language = language },
|
||||||
ct);
|
ct);
|
||||||
var baseUrl = _jobSearchLinkSettings.BaseUrl.TrimEnd('/');
|
if (!string.IsNullOrWhiteSpace(tokenResp.TokenId))
|
||||||
jobSearchLink = $"{baseUrl}/api/cv-matcher/job-search/start?t={tokenResp.TokenId}";
|
{
|
||||||
|
var baseUrl = _jobSearchLinkSettings.BaseUrl.TrimEnd('/');
|
||||||
|
jobSearchLink = $"{baseUrl}/api/cv-matcher/job-search/start?t={tokenResp.TokenId}";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,5 +2,9 @@ namespace CvMatcher.Models.Responses;
|
|||||||
|
|
||||||
public sealed class CreateJobSearchTokenResponse
|
public sealed class CreateJobSearchTokenResponse
|
||||||
{
|
{
|
||||||
public string TokenId { get; set; } = string.Empty;
|
/// <summary>
|
||||||
|
/// The generated token ID, or <c>null</c> when no job providers are currently enabled.
|
||||||
|
/// Callers must check for null before building the job-search link.
|
||||||
|
/// </summary>
|
||||||
|
public string? TokenId { get; set; }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,9 +7,12 @@ public sealed class JobSearchSettings
|
|||||||
public int TokenExpiryDays { get; set; } = 7;
|
public int TokenExpiryDays { get; set; } = 7;
|
||||||
public int MinMatchScore { get; set; } = 15;
|
public int MinMatchScore { get; set; } = 15;
|
||||||
public int MaxJobsToMatch { get; set; } = 15;
|
public int MaxJobsToMatch { get; set; } = 15;
|
||||||
public List<JobProviderConfig> Providers { get; set; } = [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Runtime DTO for a job provider. Populated from <c>cvSearch.JobProviders</c> at session-creation
|
||||||
|
/// time and snapshotted to <c>JobSearchSessionEntity.ProviderConfigJson</c>.
|
||||||
|
/// </summary>
|
||||||
public sealed class JobProviderConfig
|
public sealed class JobProviderConfig
|
||||||
{
|
{
|
||||||
public string Name { get; set; } = string.Empty;
|
public string Name { get; set; } = string.Empty;
|
||||||
|
|||||||
@@ -13,8 +13,11 @@ public interface IJobTokenService
|
|||||||
/// <param name="email">Email address of the user who will receive the results.</param>
|
/// <param name="email">Email address of the user who will receive the results.</param>
|
||||||
/// <param name="language">Preferred language for result emails (e.g. <c>"en"</c>, <c>"ro"</c>).</param>
|
/// <param name="language">Preferred language for result emails (e.g. <c>"en"</c>, <c>"ro"</c>).</param>
|
||||||
/// <param name="ct">Cancellation token.</param>
|
/// <param name="ct">Cancellation token.</param>
|
||||||
/// <returns>The generated token ID, to be embedded in the one-click job search link.</returns>
|
/// <returns>
|
||||||
Task<string> CreateTokenAsync(string cvDocumentId, string email, string language, CancellationToken ct);
|
/// The generated token ID to embed in the one-click job search link,
|
||||||
|
/// or <c>null</c> when no job providers are currently enabled (link should be suppressed).
|
||||||
|
/// </returns>
|
||||||
|
Task<string?> CreateTokenAsync(string cvDocumentId, string email, string language, CancellationToken ct);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Validates the token and, if valid, marks it as used and creates a <c>Pending</c> job search session.
|
/// Validates the token and, if valid, marks it as used and creates a <c>Pending</c> job search session.
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ namespace Api.Services;
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates and validates one-time job search tokens, and creates the corresponding search sessions.
|
/// Creates and validates one-time job search tokens, and creates the corresponding search sessions.
|
||||||
|
/// Provider configuration is read from <c>cvSearch.JobProviders</c> at session-creation time and
|
||||||
|
/// snapshotted into <c>JobSearchSessionEntity.ProviderConfigJson</c> so subsequent config changes
|
||||||
|
/// do not affect already-queued sessions.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed class JobTokenService : IJobTokenService
|
public sealed class JobTokenService : IJobTokenService
|
||||||
{
|
{
|
||||||
@@ -34,8 +37,15 @@ public sealed class JobTokenService : IJobTokenService
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public async Task<string> CreateTokenAsync(string cvDocumentId, string email, string language, CancellationToken ct)
|
public async Task<string?> CreateTokenAsync(string cvDocumentId, string email, string language, 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
|
var token = new JobSearchTokenEntity
|
||||||
{
|
{
|
||||||
Id = Guid.NewGuid().ToString("N"),
|
Id = Guid.NewGuid().ToString("N"),
|
||||||
@@ -67,8 +77,13 @@ public sealed class JobTokenService : IJobTokenService
|
|||||||
var cv = await _rag.GetDocumentAsync(token.CvDocumentId, ct);
|
var cv = await _rag.GetDocumentAsync(token.CvDocumentId, ct);
|
||||||
var keywords = cv is not null ? ExtractKeywords(cv.Text) : string.Empty;
|
var keywords = cv is not null ? ExtractKeywords(cv.Text) : string.Empty;
|
||||||
|
|
||||||
|
var enabledProviders = await _db.JobProviders
|
||||||
|
.Where(p => p.Enabled)
|
||||||
|
.OrderBy(p => p.DisplayOrder)
|
||||||
|
.ToListAsync(ct);
|
||||||
|
|
||||||
var providerConfigJson = JsonSerializer.Serialize(
|
var providerConfigJson = JsonSerializer.Serialize(
|
||||||
_settings.Providers.Where(p => p.Enabled).ToList(),
|
enabledProviders.Select(ToConfig).ToList(),
|
||||||
new JsonSerializerOptions(JsonSerializerDefaults.Web));
|
new JsonSerializerOptions(JsonSerializerDefaults.Web));
|
||||||
|
|
||||||
var session = new JobSearchSessionEntity
|
var session = new JobSearchSessionEntity
|
||||||
@@ -86,11 +101,41 @@ public sealed class JobTokenService : IJobTokenService
|
|||||||
|
|
||||||
_db.JobSearchSessions.Add(session);
|
_db.JobSearchSessions.Add(session);
|
||||||
await _db.SaveChangesAsync(ct);
|
await _db.SaveChangesAsync(ct);
|
||||||
_logger.LogInformation("Job search session created. SessionId={SessionId}, Keywords={Keywords}", session.Id, keywords);
|
_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;
|
return StartJobSearchStatus.Started;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Maps a <see cref="JobProviderEntity"/> to the <see cref="JobProviderConfig"/> DTO used by
|
||||||
|
/// <c>cv-search-job</c>. The <c>InitialKeywords</c> list is stored as a JSON array in the entity.
|
||||||
|
/// </summary>
|
||||||
|
private static JobProviderConfig ToConfig(JobProviderEntity entity)
|
||||||
|
{
|
||||||
|
List<string> keywords;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
keywords = JsonSerializer.Deserialize<List<string>>(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
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Extracts up to 10 meaningful keywords from the CV text using simple heuristics (no LLM).
|
/// Extracts up to 10 meaningful keywords from the CV text using simple heuristics (no LLM).
|
||||||
/// Takes the first 5 usable lines, splits them into words, strips punctuation, and deduplicates.
|
/// Takes the first 5 usable lines, splits them into words, strips punctuation, and deduplicates.
|
||||||
|
|||||||
@@ -204,20 +204,27 @@ public sealed class CvSearchJobTask : IJobTask
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Deserialises the provider configuration snapshot stored on the session.
|
/// Deserialises the provider configuration snapshot stored on the session.
|
||||||
/// Falls back to the current live config when the snapshot is absent or unparseable.
|
/// Providers are always snapshotted from the DB at session-creation time, so the snapshot
|
||||||
|
/// should always be present. Returns an empty list (with a warning) when it is missing or corrupt.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private List<JobProviderConfig> GetProviders(string? providerConfigJson)
|
private List<JobProviderConfig> GetProviders(string? providerConfigJson)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(providerConfigJson)) return _settings.Providers.Where(p => p.Enabled).ToList();
|
if (string.IsNullOrWhiteSpace(providerConfigJson))
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Session has no provider config snapshot — returning empty provider list");
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return JsonSerializer.Deserialize<List<JobProviderConfig>>(providerConfigJson,
|
return JsonSerializer.Deserialize<List<JobProviderConfig>>(providerConfigJson,
|
||||||
new JsonSerializerOptions(JsonSerializerDefaults.Web))
|
new JsonSerializerOptions(JsonSerializerDefaults.Web))
|
||||||
?? _settings.Providers.Where(p => p.Enabled).ToList();
|
?? [];
|
||||||
}
|
}
|
||||||
catch
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
return _settings.Providers.Where(p => p.Enabled).ToList();
|
_logger.LogWarning(ex, "Failed to deserialise provider config snapshot — returning empty provider list");
|
||||||
|
return [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user