using GB5Shared.DTO.Framework.Login; using GB5Shared.EventLogPublish; using GB5Shared.Telemetry; using Microsoft.Extensions.Logging; using PERMBLL.Interfaces; using PERMDAL.Interfaces; using System.Text.Json; using static GB5Shared.GB5Constant.Constant; namespace PERMBLL.Implementations; public class TNABLL : ITNABLL { private readonly ITNADAL _dal; private readonly EventLogPublish _eventLog; private readonly ILogger _logger; public TNABLL(ITNADAL dal, EventLogPublish eventLog, ILogger logger) { _dal = dal; _eventLog = eventLog; _logger = logger; } public async Task GetTNARequestAsync(int tnaRequestId, LoginDTO login, CancellationToken ct) { try { GB5Trace.Step("get-tnarequest", new { tnaRequestId }); var dto = await _dal.GetTNARequestAsync(tnaRequestId, login, ct).ConfigureAwait(false); return JsonSerializer.Serialize(dto); } catch (Exception ex) { GB5Trace.MarkFailed("get-tnarequest-failed", ex); _logger.LogError(ex, "GetTNARequest failed for TNARequestId {TNARequestId}", tnaRequestId); throw; } } public async Task GetTNAListAsync( int appraisalId, int employeeId, short? status, int page, int pageSize, LoginDTO login, CancellationToken ct) { try { GB5Trace.Step("get-tnalist", new { appraisalId, employeeId, status, page }); var list = await _dal.GetTNAListAsync(appraisalId, employeeId, status, page, pageSize, login, ct).ConfigureAwait(false); return JsonSerializer.Serialize(list); } catch (Exception ex) { GB5Trace.MarkFailed("get-tnalist-failed", ex); _logger.LogError(ex, "GetTNAList failed"); throw; } } public async Task EnrichFromSkillGapAsync( int employeeId, IEnumerable<(int KRAId, decimal GapScore)> gaps, LoginDTO login, CancellationToken ct) { try { GB5Trace.Step("enrich-tna-from-skillgap", new { employeeId }); await _dal.InsertFromSkillGapAsync(employeeId, gaps, login, ct).ConfigureAwait(false); } catch (Exception ex) { GB5Trace.MarkFailed("enrich-tna-from-skillgap-failed", ex); _logger.LogError(ex, "EnrichFromSkillGap failed for EmployeeId {EmployeeId}", employeeId); throw; } } public async Task UpdateTNAStatusAsync( int tnaRequestId, short status, string? trainingSuggestion, LoginDTO login, CancellationToken ct) { try { GB5Trace.Step("update-tnastatus", new { tnaRequestId, status }); int rows = await _dal.UpdateTNAStatusAsync(tnaRequestId, status, trainingSuggestion, login, ct).ConfigureAwait(false); if (rows == 0) return "TNA request not found or already closed."; GB5Trace.Step("event-publish", new { EventTypeId = EventTypeConstant.SAVEPERMTNAEVENTTYPEID }); await _eventLog.PublishEventLogAsync( status == 1 ? "TNA Sent to Learning" : "TNA Request Closed", new { TNARequestId = tnaRequestId, Status = status }, EventTypeConstant.SAVEPERMTNAEVENTTYPEID, tnaRequestId, login, ct: ct).ConfigureAwait(false); return GB5Shared.Resource.Response.SuccessResponse.UpdateSuccess; } catch (Exception ex) { GB5Trace.MarkFailed("update-tnastatus-failed", ex); _logger.LogError(ex, "UpdateTNAStatus failed for TNARequestId {TNARequestId}", tnaRequestId); throw; } } }