using Dapr; using GB5Shared.DTO.Framework.Login; using Microsoft.AspNetCore.Mvc; using PERMBLL.Interfaces; namespace PERMSL.Subscriptions; [ApiController] public class SkillGapSubscriber : ControllerBase { private readonly ITNABLL _tnaBll; private readonly ILogger _logger; public SkillGapSubscriber(ITNABLL tnaBll, ILogger logger) { _tnaBll = tnaBll; _logger = logger; } [Topic("pubsub", "cmgm.skillgap.identified")] [HttpPost("/PerfMgmt/Subscribe/SkillGapIdentified")] public async Task HandleSkillGapIdentifiedAsync( [FromBody] SkillGapIdentifiedEvent evt, CancellationToken ct) { _logger.LogInformation( "cmgm.skillgap.identified received: EmployeeId {EmployeeId} Gaps {GapCount}", evt.EmployeeId, evt.Gaps?.Count ?? 0); if (evt.Gaps is null || evt.Gaps.Count == 0) return Ok(); try { var login = new LoginDTO { ClientId = evt.ClientId, DatabaseName = evt.DatabaseName, UserId = evt.UserId }; var gaps = evt.Gaps .Where(g => g.KRAId > 0) .Select(g => (g.KRAId, (decimal)g.GapLevel)); await _tnaBll.EnrichFromSkillGapAsync(evt.EmployeeId, gaps, login, ct); return Ok(); } catch (Exception ex) { _logger.LogError(ex, "Failed to process cmgm.skillgap.identified for EmployeeId {EmployeeId}", evt.EmployeeId); return StatusCode(500, ex.Message); } } } public sealed class SkillGapIdentifiedEvent { public int EmployeeId { get; set; } public int FrameworkId { get; set; } public int TenantId { get; set; } public int ClientId { get; set; } public string DatabaseName { get; set; } = string.Empty; public int UserId { get; set; } public List? Gaps { get; set; } } public sealed class SkillGapItem { public int KRAId { get; set; } public int SkillId { get; set; } public int GapLevel { get; set; } }