| | | 1 | | using RoleRadar.Application.Abstractions; |
| | | 2 | | using RoleRadar.Domain.Common; |
| | | 3 | | using RoleRadar.Domain.Tracking; |
| | | 4 | | |
| | | 5 | | namespace RoleRadar.Application.Tracking.Queries; |
| | | 6 | | |
| | | 7 | | public sealed record PostingSummaryItemDto( |
| | | 8 | | Guid Id, |
| | | 9 | | Guid CompanyId, |
| | | 10 | | string Title, |
| | | 11 | | string NormalizedTitle, |
| | | 12 | | string Disposition, |
| | | 13 | | string GhostLikelihood, |
| | | 14 | | string ResurfacePolicy, |
| | | 15 | | Guid VersionToken, |
| | | 16 | | string? Location = null, |
| | | 17 | | string? RemoteType = null, |
| | | 18 | | decimal? Score = null, |
| | | 19 | | DateTimeOffset? LastVerifiedAt = null, |
| | | 20 | | bool? CompHighPriority = null); |
| | | 21 | | |
| | | 22 | | public sealed record DecidedPostingSummaryItemDto( |
| | | 23 | | Guid Id, |
| | | 24 | | Guid CompanyId, |
| | | 25 | | string Title, |
| | | 26 | | string Disposition, |
| | | 27 | | string? DispositionReason, |
| | | 28 | | string ResurfacePolicy, |
| | | 29 | | DateTimeOffset? DecidedAtUtc, |
| | | 30 | | bool? CompHighPriority = null); |
| | | 31 | | |
| | | 32 | | public sealed record CareerSourceSummaryItemDto( |
| | | 33 | | Guid Id, |
| | | 34 | | Guid CompanyId, |
| | | 35 | | string AtsType, |
| | | 36 | | string? Tenant, |
| | | 37 | | string? Slug, |
| | | 38 | | string FetchMethod, |
| | | 39 | | string? Notes, |
| | | 40 | | bool IsActive, |
| | | 41 | | Guid VersionToken); |
| | | 42 | | |
| | | 43 | | public sealed record CompanySummaryItemDto( |
| | | 44 | | Guid Id, |
| | | 45 | | string Name, |
| | | 46 | | string? Description, |
| | | 47 | | string? ParentCompany, |
| | | 48 | | string OwnershipType, |
| | | 49 | | string? Headquarters, |
| | | 50 | | string Status, |
| | | 51 | | IReadOnlyList<string> CategoryTags, |
| | | 52 | | IReadOnlyList<CareerSourceSummaryItemDto> CareerSources, |
| | | 53 | | DateTimeOffset? LastCheckedAt, |
| | | 54 | | string? LastCheckResult, |
| | | 55 | | Guid VersionToken, |
| | | 56 | | DateTimeOffset CreatedAt, |
| | | 57 | | DateTimeOffset UpdatedAt); |
| | | 58 | | |
| | | 59 | | public sealed record CriterionSummaryItemDto( |
| | | 60 | | Guid Id, |
| | | 61 | | string Key, |
| | | 62 | | string Category, |
| | | 63 | | string Value, |
| | | 64 | | string Provenance, |
| | | 65 | | DateTimeOffset EffectiveFrom, |
| | | 66 | | DateTimeOffset? EffectiveTo, |
| | | 67 | | bool IsActive, |
| | | 68 | | Guid VersionToken, |
| | | 69 | | DateTimeOffset CreatedAt); |
| | | 70 | | |
| | | 71 | | public sealed record ApplicationEventSummaryItemDto( |
| | | 72 | | Guid Id, |
| | | 73 | | Guid ApplicationId, |
| | | 74 | | string EventType, |
| | | 75 | | string EventDate, |
| | | 76 | | string? EventSource, |
| | | 77 | | string? Notes, |
| | | 78 | | DateTimeOffset CreatedAt); |
| | | 79 | | |
| | | 80 | | public sealed record ApplicationSummaryItemDto( |
| | | 81 | | Guid Id, |
| | | 82 | | Guid PostingId, |
| | | 83 | | string SubmittedDate, |
| | | 84 | | decimal? DesiredSalaryMin, |
| | | 85 | | decimal? DesiredSalaryMax, |
| | | 86 | | string DesiredSalaryCurrency, |
| | | 87 | | string? TargetStartDate, |
| | | 88 | | string ResumeVariant, |
| | | 89 | | string? ResumePointer, |
| | | 90 | | Guid? ReferralContactId, |
| | | 91 | | string? ConfirmationCode, |
| | | 92 | | string Outcome, |
| | | 93 | | string? OutcomeReason, |
| | | 94 | | Guid VersionToken, |
| | | 95 | | IReadOnlyList<ApplicationEventSummaryItemDto> Events, |
| | | 96 | | DateTimeOffset CreatedAt, |
| | | 97 | | DateTimeOffset UpdatedAt); |
| | | 98 | | |
| | | 99 | | public sealed record TrackerQueryResultDto( |
| | | 100 | | string Entity, |
| | | 101 | | int Page, |
| | | 102 | | int PageSize, |
| | | 103 | | int TotalCount, |
| | | 104 | | IReadOnlyList<object> Items); |
| | | 105 | | |
| | 300 | 106 | | public sealed record TrackerQueryRequest( |
| | 592 | 107 | | string Entity, |
| | 122 | 108 | | Guid? PostingId = null, |
| | 214 | 109 | | Guid? CompanyId = null, |
| | 194 | 110 | | string? Disposition = null, |
| | 46 | 111 | | string? Status = null, |
| | 34 | 112 | | string? Tag = null, |
| | 42 | 113 | | string? Name = null, |
| | 32 | 114 | | bool IncludeRetired = false, |
| | 30 | 115 | | bool IncludeStale = false, |
| | 4 | 116 | | int StaleAfterDays = 7, |
| | 32 | 117 | | string? EffectiveDate = null, |
| | 216 | 118 | | string? DedupKey = null, |
| | 286 | 119 | | int Page = 1, |
| | 570 | 120 | | int PageSize = 50, |
| | 612 | 121 | | string? OwnerId = null) : IQuery<TrackerQueryResultDto>; |
| | | 122 | | |
| | | 123 | | public sealed class TrackerQueryHandler : IQueryHandler<TrackerQueryRequest, TrackerQueryResultDto> |
| | | 124 | | { |
| | | 125 | | private readonly ITrackingTransactionContext _context; |
| | | 126 | | private readonly IUtcClock _clock; |
| | | 127 | | |
| | | 128 | | public TrackerQueryHandler(ITrackingTransactionContext context, IUtcClock clock) |
| | | 129 | | { |
| | | 130 | | _context = context ?? throw new ArgumentNullException(nameof(context)); |
| | | 131 | | _clock = clock ?? throw new ArgumentNullException(nameof(clock)); |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | public async Task<Result<TrackerQueryResultDto>> HandleAsync( |
| | | 135 | | TrackerQueryRequest query, |
| | | 136 | | CancellationToken cancellationToken = default) |
| | | 137 | | { |
| | | 138 | | ArgumentNullException.ThrowIfNull(query); |
| | | 139 | | |
| | | 140 | | if (string.IsNullOrWhiteSpace(query.Entity)) |
| | | 141 | | { |
| | | 142 | | return Result<TrackerQueryResultDto>.Failure(ErrorCode.ValidationError, "Entity discriminator is required.") |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | var entity = query.Entity.Trim().ToLowerInvariant(); |
| | | 146 | | if (entity == "contacts" || entity == "contact") |
| | | 147 | | { |
| | | 148 | | return Result<TrackerQueryResultDto>.Failure( |
| | | 149 | | ErrorCode.ValidationError, |
| | | 150 | | "Contacts are accessible exclusively via CLI commands (roleradar contact add|get|redact)."); |
| | | 151 | | } |
| | | 152 | | |
| | | 153 | | var ownerId = string.IsNullOrWhiteSpace(query.OwnerId) |
| | | 154 | | ? OwnerId.Default.Value |
| | | 155 | | : new OwnerId(query.OwnerId).Value; |
| | | 156 | | var now = _clock.UtcNow; |
| | | 157 | | |
| | | 158 | | // Bounded paging: default 50, max 200, min 1 |
| | | 159 | | var page = Math.Max(1, query.Page); |
| | | 160 | | var pageSize = Math.Clamp(query.PageSize <= 0 ? 50 : query.PageSize, 1, 200); |
| | | 161 | | |
| | | 162 | | try |
| | | 163 | | { |
| | | 164 | | return entity switch |
| | | 165 | | { |
| | | 166 | | "postings" => await HandlePostingsQueryAsync(query, ownerId, page, pageSize, now, cancellationToken), |
| | | 167 | | "decided_postings" => await HandleDecidedPostingsQueryAsync(ownerId, page, pageSize, cancellationToken), |
| | | 168 | | "posting_dedup_lookup" => await HandleDedupLookupQueryAsync(query, ownerId, cancellationToken), |
| | | 169 | | "companies" => await HandleCompaniesQueryAsync(query, ownerId, page, pageSize, now, cancellationToken), |
| | | 170 | | "criteria" => await HandleCriteriaQueryAsync(query, ownerId, page, pageSize, cancellationToken), |
| | | 171 | | "applications" => await HandleApplicationsQueryAsync(query, ownerId, page, pageSize, cancellationToken), |
| | | 172 | | _ => Result<TrackerQueryResultDto>.Failure( |
| | | 173 | | ErrorCode.ValidationError, |
| | | 174 | | $"Entity discriminator '{query.Entity}' is not supported by tracker_query postings handler.") |
| | | 175 | | }; |
| | | 176 | | } |
| | | 177 | | catch (TrackingIndexValidationException ex) |
| | | 178 | | { |
| | | 179 | | return Result<TrackerQueryResultDto>.Failure(ErrorCode.ValidationError, ex.Message); |
| | | 180 | | } |
| | | 181 | | } |
| | | 182 | | |
| | | 183 | | private async Task<Result<TrackerQueryResultDto>> HandlePostingsQueryAsync( |
| | | 184 | | TrackerQueryRequest query, |
| | | 185 | | string ownerId, |
| | | 186 | | int page, |
| | | 187 | | int pageSize, |
| | | 188 | | DateTimeOffset now, |
| | | 189 | | CancellationToken cancellationToken) |
| | | 190 | | { |
| | | 191 | | // Single posting lookup by ID |
| | | 192 | | if (query.PostingId.HasValue) |
| | | 193 | | { |
| | | 194 | | var singleResult = await _context.Postings.GetByIdAsync(ownerId, query.PostingId.Value, cancellationToken); |
| | | 195 | | if (!singleResult.IsSuccess || singleResult.Value is null) |
| | | 196 | | { |
| | | 197 | | return Result<TrackerQueryResultDto>.Success(new TrackerQueryResultDto("postings", 1, pageSize, 0, [])); |
| | | 198 | | } |
| | | 199 | | |
| | | 200 | | var p = singleResult.Value; |
| | | 201 | | var signalsResult = await _context.Postings.GetSignalsAsync(ownerId, p.Id, cancellationToken); |
| | | 202 | | var signals = signalsResult.IsSuccess && signalsResult.Value is not null ? signalsResult.Value : []; |
| | | 203 | | var ghost = GhostLikelihoodEvaluator.Evaluate(p, signals, now); |
| | | 204 | | |
| | | 205 | | var item = new PostingSummaryItemDto( |
| | | 206 | | Id: p.Id, |
| | | 207 | | CompanyId: p.CompanyId, |
| | | 208 | | Title: p.Title, |
| | | 209 | | NormalizedTitle: p.NormalizedTitle, |
| | | 210 | | Disposition: p.Disposition.ToSnakeCase(), |
| | | 211 | | GhostLikelihood: ghost, |
| | | 212 | | ResurfacePolicy: p.ResurfacePolicy.ToSnakeCase(), |
| | | 213 | | VersionToken: p.VersionToken, |
| | | 214 | | Location: p.Location, |
| | | 215 | | RemoteType: p.RemoteType.ToSnakeCase(), |
| | | 216 | | Score: p.Score, |
| | | 217 | | LastVerifiedAt: p.LastVerifiedAt, |
| | | 218 | | CompHighPriority: p.CompHighPriority); |
| | | 219 | | |
| | | 220 | | return Result<TrackerQueryResultDto>.Success(new TrackerQueryResultDto("postings", 1, pageSize, 1, [item])); |
| | | 221 | | } |
| | | 222 | | |
| | | 223 | | // Paged listing with optional disposition and company filters |
| | | 224 | | PostingDisposition? dispositionFilter = null; |
| | | 225 | | if (!string.IsNullOrWhiteSpace(query.Disposition) && |
| | | 226 | | TrackingEnumExtensions.TryParseDisposition(query.Disposition, out var parsedDisp)) |
| | | 227 | | { |
| | | 228 | | dispositionFilter = parsedDisp; |
| | | 229 | | } |
| | | 230 | | |
| | | 231 | | var postingsResult = await _context.Postings.ListPostingsAsync( |
| | | 232 | | ownerId, query.CompanyId, dispositionFilter, page, pageSize, cancellationToken); |
| | | 233 | | if (!postingsResult.IsSuccess || postingsResult.Value is null) |
| | | 234 | | { |
| | | 235 | | return Result<TrackerQueryResultDto>.Failure(postingsResult.Error ?? new ResultError(ErrorCode.ValidationErr |
| | | 236 | | } |
| | | 237 | | |
| | | 238 | | var countResult = await _context.Postings.CountPostingsAsync( |
| | | 239 | | ownerId, query.CompanyId, dispositionFilter, cancellationToken); |
| | | 240 | | var totalCount = countResult.IsSuccess ? countResult.Value : postingsResult.Value.Count; |
| | | 241 | | |
| | | 242 | | var postingIds = postingsResult.Value.Select(p => p.Id).ToList(); |
| | | 243 | | var signalsLookupResult = await _context.Postings.GetSignalsForPostingsAsync(ownerId, postingIds, cancellationTo |
| | | 244 | | var signalsLookup = signalsLookupResult.IsSuccess && signalsLookupResult.Value is not null |
| | | 245 | | ? signalsLookupResult.Value |
| | | 246 | | : Enumerable.Empty<PostingSignal>().ToLookup(s => s.PostingId); |
| | | 247 | | |
| | | 248 | | var items = new List<object>(postingsResult.Value.Count); |
| | | 249 | | foreach (var p in postingsResult.Value) |
| | | 250 | | { |
| | | 251 | | var sigs = signalsLookup[p.Id].ToList(); |
| | | 252 | | var ghost = GhostLikelihoodEvaluator.Evaluate(p, sigs, now); |
| | | 253 | | |
| | | 254 | | items.Add(new PostingSummaryItemDto( |
| | | 255 | | Id: p.Id, |
| | | 256 | | CompanyId: p.CompanyId, |
| | | 257 | | Title: p.Title, |
| | | 258 | | NormalizedTitle: p.NormalizedTitle, |
| | | 259 | | Disposition: p.Disposition.ToSnakeCase(), |
| | | 260 | | GhostLikelihood: ghost, |
| | | 261 | | ResurfacePolicy: p.ResurfacePolicy.ToSnakeCase(), |
| | | 262 | | VersionToken: p.VersionToken, |
| | | 263 | | Location: p.Location, |
| | | 264 | | RemoteType: p.RemoteType.ToSnakeCase(), |
| | | 265 | | Score: p.Score, |
| | | 266 | | LastVerifiedAt: p.LastVerifiedAt, |
| | | 267 | | CompHighPriority: p.CompHighPriority)); |
| | | 268 | | } |
| | | 269 | | |
| | | 270 | | return Result<TrackerQueryResultDto>.Success(new TrackerQueryResultDto("postings", page, pageSize, totalCount, i |
| | | 271 | | } |
| | | 272 | | |
| | | 273 | | private async Task<Result<TrackerQueryResultDto>> HandleDecidedPostingsQueryAsync( |
| | | 274 | | string ownerId, |
| | | 275 | | int page, |
| | | 276 | | int pageSize, |
| | | 277 | | CancellationToken cancellationToken) |
| | | 278 | | { |
| | | 279 | | var postingsResult = await _context.Postings.ListDecidedPostingsAsync(ownerId, page, pageSize, cancellationToken |
| | | 280 | | if (!postingsResult.IsSuccess || postingsResult.Value is null) |
| | | 281 | | { |
| | | 282 | | return Result<TrackerQueryResultDto>.Failure(postingsResult.Error ?? new ResultError(ErrorCode.ValidationErr |
| | | 283 | | } |
| | | 284 | | |
| | | 285 | | var countResult = await _context.Postings.CountDecidedPostingsAsync(ownerId, cancellationToken); |
| | | 286 | | var totalCount = countResult.IsSuccess ? countResult.Value : postingsResult.Value.Count; |
| | | 287 | | |
| | | 288 | | var items = new List<object>(postingsResult.Value.Count); |
| | | 289 | | foreach (var p in postingsResult.Value) |
| | | 290 | | { |
| | | 291 | | items.Add(new DecidedPostingSummaryItemDto( |
| | | 292 | | Id: p.Id, |
| | | 293 | | CompanyId: p.CompanyId, |
| | | 294 | | Title: p.Title, |
| | | 295 | | Disposition: p.Disposition.ToSnakeCase(), |
| | | 296 | | DispositionReason: p.DispositionReason, |
| | | 297 | | ResurfacePolicy: p.ResurfacePolicy.ToSnakeCase(), |
| | | 298 | | DecidedAtUtc: p.DecidedAt, |
| | | 299 | | CompHighPriority: p.CompHighPriority)); |
| | | 300 | | } |
| | | 301 | | |
| | | 302 | | return Result<TrackerQueryResultDto>.Success(new TrackerQueryResultDto("decided_postings", page, pageSize, total |
| | | 303 | | } |
| | | 304 | | |
| | | 305 | | private async Task<Result<TrackerQueryResultDto>> HandleDedupLookupQueryAsync( |
| | | 306 | | TrackerQueryRequest query, |
| | | 307 | | string ownerId, |
| | | 308 | | CancellationToken cancellationToken) |
| | | 309 | | { |
| | | 310 | | if (string.IsNullOrWhiteSpace(query.DedupKey)) |
| | | 311 | | { |
| | | 312 | | return Result<TrackerQueryResultDto>.Failure(ErrorCode.ValidationError, "DedupKey is required for entity 'po |
| | | 313 | | } |
| | | 314 | | |
| | | 315 | | var key = query.DedupKey.Trim(); |
| | | 316 | | |
| | | 317 | | // 1. Try Tier A |
| | | 318 | | var matchA = await _context.Postings.GetByDedupKeyAAsync(ownerId, key, cancellationToken); |
| | | 319 | | if (matchA.IsSuccess && matchA.Value is not null) |
| | | 320 | | { |
| | | 321 | | return BuildSinglePostingResult(matchA.Value); |
| | | 322 | | } |
| | | 323 | | |
| | | 324 | | // 2. Try Tier B |
| | | 325 | | var normalizedKey = UrlNormalizer.Normalize(key) ?? key; |
| | | 326 | | var matchB = await _context.Postings.GetByDedupKeyBAsync(ownerId, normalizedKey, cancellationToken); |
| | | 327 | | if (matchB.IsSuccess && matchB.Value is not null) |
| | | 328 | | { |
| | | 329 | | return BuildSinglePostingResult(matchB.Value); |
| | | 330 | | } |
| | | 331 | | |
| | | 332 | | // 3. Try Tier C |
| | | 333 | | var matchC = await _context.Postings.GetByDedupKeyCAsync(ownerId, key, cancellationToken); |
| | | 334 | | if (matchC.IsSuccess && matchC.Value is not null) |
| | | 335 | | { |
| | | 336 | | return BuildSinglePostingResult(matchC.Value); |
| | | 337 | | } |
| | | 338 | | |
| | | 339 | | return Result<TrackerQueryResultDto>.Success(new TrackerQueryResultDto("posting_dedup_lookup", 1, 1, 0, [])); |
| | | 340 | | } |
| | | 341 | | |
| | | 342 | | private Result<TrackerQueryResultDto> BuildSinglePostingResult(Posting p) |
| | | 343 | | { |
| | | 344 | | var item = new PostingSummaryItemDto( |
| | | 345 | | Id: p.Id, |
| | | 346 | | CompanyId: p.CompanyId, |
| | | 347 | | Title: p.Title, |
| | | 348 | | NormalizedTitle: p.NormalizedTitle, |
| | | 349 | | Disposition: p.Disposition.ToSnakeCase(), |
| | | 350 | | GhostLikelihood: "low", |
| | | 351 | | ResurfacePolicy: p.ResurfacePolicy.ToSnakeCase(), |
| | | 352 | | VersionToken: p.VersionToken, |
| | | 353 | | Location: p.Location, |
| | | 354 | | RemoteType: p.RemoteType.ToSnakeCase(), |
| | | 355 | | Score: p.Score, |
| | | 356 | | LastVerifiedAt: p.LastVerifiedAt, |
| | | 357 | | CompHighPriority: p.CompHighPriority); |
| | | 358 | | |
| | | 359 | | return Result<TrackerQueryResultDto>.Success(new TrackerQueryResultDto("posting_dedup_lookup", 1, 1, 1, [item])) |
| | | 360 | | } |
| | | 361 | | |
| | | 362 | | private async Task<Result<TrackerQueryResultDto>> HandleCompaniesQueryAsync( |
| | | 363 | | TrackerQueryRequest query, |
| | | 364 | | string ownerId, |
| | | 365 | | int page, |
| | | 366 | | int pageSize, |
| | | 367 | | DateTimeOffset now, |
| | | 368 | | CancellationToken cancellationToken) |
| | | 369 | | { |
| | | 370 | | CompanyStatus? statusFilter = null; |
| | | 371 | | if (!string.IsNullOrWhiteSpace(query.Status) && |
| | | 372 | | TrackingEnumExtensions.TryParseCompanyStatus(query.Status, out var parsedStatus)) |
| | | 373 | | { |
| | | 374 | | statusFilter = parsedStatus; |
| | | 375 | | } |
| | | 376 | | |
| | | 377 | | var listResult = await _context.Companies.ListAsync(ownerId, statusFilter, cancellationToken); |
| | | 378 | | if (!listResult.IsSuccess || listResult.Value is null) |
| | | 379 | | { |
| | | 380 | | return Result<TrackerQueryResultDto>.Success(new TrackerQueryResultDto("companies", page, pageSize, 0, [])); |
| | | 381 | | } |
| | | 382 | | |
| | | 383 | | var companies = listResult.Value.AsEnumerable(); |
| | | 384 | | |
| | | 385 | | if (!string.IsNullOrWhiteSpace(query.Tag)) |
| | | 386 | | { |
| | | 387 | | var tag = query.Tag.Trim(); |
| | | 388 | | companies = companies.Where(c => c.CategoryTags.Contains(tag, StringComparer.OrdinalIgnoreCase)); |
| | | 389 | | } |
| | | 390 | | |
| | | 391 | | if (!string.IsNullOrWhiteSpace(query.Name)) |
| | | 392 | | { |
| | | 393 | | var name = query.Name.Trim(); |
| | | 394 | | companies = companies.Where(c => c.Name.Contains(name, StringComparison.OrdinalIgnoreCase)); |
| | | 395 | | } |
| | | 396 | | |
| | | 397 | | if (query.IncludeStale) |
| | | 398 | | { |
| | | 399 | | var threshold = now.AddDays(-query.StaleAfterDays); |
| | | 400 | | companies = companies.Where(c => !c.LastCheckedAt.HasValue || c.LastCheckedAt.Value <= threshold); |
| | | 401 | | } |
| | | 402 | | |
| | | 403 | | var filteredList = companies.ToList(); |
| | | 404 | | var totalCount = filteredList.Count; |
| | | 405 | | var pagedCompanies = filteredList.Skip((page - 1) * pageSize).Take(pageSize).ToList(); |
| | | 406 | | |
| | | 407 | | var items = new List<object>(pagedCompanies.Count); |
| | | 408 | | foreach (var c in pagedCompanies) |
| | | 409 | | { |
| | | 410 | | var sourcesRes = await _context.Companies.GetCareerSourcesAsync( |
| | | 411 | | ownerId, |
| | | 412 | | c.Id, |
| | | 413 | | includeInactive: query.IncludeRetired, |
| | | 414 | | cancellationToken); |
| | | 415 | | |
| | | 416 | | var sources = sourcesRes.IsSuccess && sourcesRes.Value is not null |
| | | 417 | | ? sourcesRes.Value.Select(s => new CareerSourceSummaryItemDto( |
| | | 418 | | Id: s.Id, |
| | | 419 | | CompanyId: s.CompanyId, |
| | | 420 | | AtsType: s.AtsType, |
| | | 421 | | Tenant: s.Tenant, |
| | | 422 | | Slug: s.Slug, |
| | | 423 | | FetchMethod: s.FetchMethod.ToSnakeCase(), |
| | | 424 | | Notes: s.Notes, |
| | | 425 | | IsActive: s.IsActive, |
| | | 426 | | VersionToken: s.VersionToken)).ToArray() |
| | | 427 | | : []; |
| | | 428 | | |
| | | 429 | | items.Add(new CompanySummaryItemDto( |
| | | 430 | | Id: c.Id, |
| | | 431 | | Name: c.Name, |
| | | 432 | | Description: c.Description, |
| | | 433 | | ParentCompany: c.ParentCompany, |
| | | 434 | | OwnershipType: c.OwnershipType.ToSnakeCase(), |
| | | 435 | | Headquarters: c.Headquarters, |
| | | 436 | | Status: c.Status.ToSnakeCase(), |
| | | 437 | | CategoryTags: c.CategoryTags, |
| | | 438 | | CareerSources: sources, |
| | | 439 | | LastCheckedAt: c.LastCheckedAt, |
| | | 440 | | LastCheckResult: c.LastCheckResult, |
| | | 441 | | VersionToken: c.VersionToken, |
| | | 442 | | CreatedAt: c.CreatedAt, |
| | | 443 | | UpdatedAt: c.UpdatedAt)); |
| | | 444 | | } |
| | | 445 | | |
| | | 446 | | return Result<TrackerQueryResultDto>.Success(new TrackerQueryResultDto( |
| | | 447 | | Entity: "companies", |
| | | 448 | | Page: page, |
| | | 449 | | PageSize: pageSize, |
| | | 450 | | TotalCount: totalCount, |
| | | 451 | | Items: items)); |
| | | 452 | | } |
| | | 453 | | |
| | | 454 | | private async Task<Result<TrackerQueryResultDto>> HandleCriteriaQueryAsync( |
| | | 455 | | TrackerQueryRequest query, |
| | | 456 | | string ownerId, |
| | | 457 | | int page, |
| | | 458 | | int pageSize, |
| | | 459 | | CancellationToken cancellationToken) |
| | | 460 | | { |
| | | 461 | | IReadOnlyList<Criterion> criteria; |
| | | 462 | | |
| | | 463 | | if (!string.IsNullOrWhiteSpace(query.EffectiveDate)) |
| | | 464 | | { |
| | | 465 | | if (!DateTimeOffset.TryParse(query.EffectiveDate, System.Globalization.CultureInfo.InvariantCulture, out var |
| | | 466 | | { |
| | | 467 | | return Result<TrackerQueryResultDto>.Failure( |
| | | 468 | | ErrorCode.ValidationError, |
| | | 469 | | $"Invalid EffectiveDate format '{query.EffectiveDate}'. Expected YYYY-MM-DD or ISO 8601 string."); |
| | | 470 | | } |
| | | 471 | | |
| | | 472 | | var effResult = await _context.Criteria.GetEffectiveAtAsync(ownerId, parsedEffectiveDate, cancellationToken) |
| | | 473 | | if (!effResult.IsSuccess || effResult.Value is null) |
| | | 474 | | { |
| | | 475 | | return Result<TrackerQueryResultDto>.Failure(effResult.Error ?? new ResultError(ErrorCode.ValidationErro |
| | | 476 | | } |
| | | 477 | | |
| | | 478 | | criteria = effResult.Value; |
| | | 479 | | } |
| | | 480 | | else |
| | | 481 | | { |
| | | 482 | | var activeResult = await _context.Criteria.ListActiveAsync(ownerId, cancellationToken); |
| | | 483 | | if (!activeResult.IsSuccess || activeResult.Value is null) |
| | | 484 | | { |
| | | 485 | | return Result<TrackerQueryResultDto>.Failure(activeResult.Error ?? new ResultError(ErrorCode.ValidationE |
| | | 486 | | } |
| | | 487 | | |
| | | 488 | | criteria = activeResult.Value; |
| | | 489 | | } |
| | | 490 | | |
| | | 491 | | var totalCount = criteria.Count; |
| | | 492 | | var pagedCriteria = criteria.Skip((page - 1) * pageSize).Take(pageSize).ToList(); |
| | | 493 | | |
| | | 494 | | var items = pagedCriteria.Select(c => new CriterionSummaryItemDto( |
| | | 495 | | Id: c.Id, |
| | | 496 | | Key: c.Key, |
| | | 497 | | Category: c.Category.ToSnakeCase(), |
| | | 498 | | Value: c.Value, |
| | | 499 | | Provenance: c.Provenance.ToSnakeCase(), |
| | | 500 | | EffectiveFrom: c.EffectiveFrom, |
| | | 501 | | EffectiveTo: c.EffectiveTo, |
| | | 502 | | IsActive: c.IsActive, |
| | | 503 | | VersionToken: c.VersionToken, |
| | | 504 | | CreatedAt: c.CreatedAt)).ToArray(); |
| | | 505 | | |
| | | 506 | | return Result<TrackerQueryResultDto>.Success(new TrackerQueryResultDto( |
| | | 507 | | Entity: "criteria", |
| | | 508 | | Page: page, |
| | | 509 | | PageSize: pageSize, |
| | | 510 | | TotalCount: totalCount, |
| | | 511 | | Items: items)); |
| | | 512 | | } |
| | | 513 | | |
| | | 514 | | private async Task<Result<TrackerQueryResultDto>> HandleApplicationsQueryAsync( |
| | | 515 | | TrackerQueryRequest query, |
| | | 516 | | string ownerId, |
| | | 517 | | int page, |
| | | 518 | | int pageSize, |
| | | 519 | | CancellationToken cancellationToken) |
| | | 520 | | { |
| | | 521 | | string? outcomeFilter = null; |
| | | 522 | | if (!string.IsNullOrWhiteSpace(query.Status)) |
| | | 523 | | { |
| | | 524 | | outcomeFilter = query.Status.Trim(); |
| | | 525 | | } |
| | | 526 | | else if (!string.IsNullOrWhiteSpace(query.Disposition)) |
| | | 527 | | { |
| | | 528 | | outcomeFilter = query.Disposition.Trim(); |
| | | 529 | | } |
| | | 530 | | |
| | | 531 | | var listResult = await _context.Applications.ListApplicationsAsync( |
| | | 532 | | ownerId, |
| | | 533 | | outcomeFilter, |
| | | 534 | | page, |
| | | 535 | | pageSize, |
| | | 536 | | cancellationToken); |
| | | 537 | | |
| | | 538 | | if (!listResult.IsSuccess || listResult.Value is null) |
| | | 539 | | { |
| | | 540 | | return Result<TrackerQueryResultDto>.Failure(listResult.Error ?? new ResultError(ErrorCode.ValidationError, |
| | | 541 | | } |
| | | 542 | | |
| | | 543 | | var countResult = await _context.Applications.CountApplicationsAsync( |
| | | 544 | | ownerId, |
| | | 545 | | outcomeFilter, |
| | | 546 | | cancellationToken); |
| | | 547 | | |
| | | 548 | | var totalCount = countResult.IsSuccess ? countResult.Value : listResult.Value.Count; |
| | | 549 | | |
| | | 550 | | var items = new List<object>(listResult.Value.Count); |
| | | 551 | | foreach (var app in listResult.Value) |
| | | 552 | | { |
| | | 553 | | var eventsResult = await _context.Applications.GetEventsAsync(ownerId, app.Id, cancellationToken); |
| | | 554 | | var events = eventsResult.IsSuccess && eventsResult.Value is not null |
| | | 555 | | ? eventsResult.Value.Select(e => new ApplicationEventSummaryItemDto( |
| | | 556 | | Id: e.Id, |
| | | 557 | | ApplicationId: e.ApplicationId, |
| | | 558 | | EventType: e.EventType.ToSnakeCase(), |
| | | 559 | | EventDate: e.EventDate, |
| | | 560 | | EventSource: e.EventSource, |
| | | 561 | | Notes: e.Notes, |
| | | 562 | | CreatedAt: e.CreatedAt)).ToArray() |
| | | 563 | | : []; |
| | | 564 | | |
| | | 565 | | items.Add(new ApplicationSummaryItemDto( |
| | | 566 | | Id: app.Id, |
| | | 567 | | PostingId: app.PostingId, |
| | | 568 | | SubmittedDate: app.SubmittedDate, |
| | | 569 | | DesiredSalaryMin: app.DesiredSalary.Min, |
| | | 570 | | DesiredSalaryMax: app.DesiredSalary.Max, |
| | | 571 | | DesiredSalaryCurrency: app.DesiredSalary.Currency, |
| | | 572 | | TargetStartDate: app.TargetStartDate, |
| | | 573 | | ResumeVariant: app.ResumeVariant.ToSnakeCase(), |
| | | 574 | | ResumePointer: app.ResumePointer, |
| | | 575 | | ReferralContactId: app.ReferralContactId, |
| | | 576 | | ConfirmationCode: app.ConfirmationCode, |
| | | 577 | | Outcome: app.Outcome.ToSnakeCase(), |
| | | 578 | | OutcomeReason: app.OutcomeReason, |
| | | 579 | | VersionToken: app.VersionToken, |
| | | 580 | | Events: events, |
| | | 581 | | CreatedAt: app.CreatedAt, |
| | | 582 | | UpdatedAt: app.UpdatedAt)); |
| | | 583 | | } |
| | | 584 | | |
| | | 585 | | return Result<TrackerQueryResultDto>.Success(new TrackerQueryResultDto( |
| | | 586 | | Entity: "applications", |
| | | 587 | | Page: page, |
| | | 588 | | PageSize: pageSize, |
| | | 589 | | TotalCount: totalCount, |
| | | 590 | | Items: items)); |
| | | 591 | | } |
| | | 592 | | } |