using GB5Shared.DaprCache; using GB5Shared.DTO.Framework.Login; using GB5Shared.EventLogPublish; using GB5Shared.Telemetry; using GB5Shared.Validation; using Microsoft.Extensions.Logging; using PERMBLL.Interfaces; using PERMDAL.DTOs; using PERMDAL.Interfaces; using System.Text.Json; using static GB5Shared.GB5Constant.Constant; namespace PERMBLL.Implementations; public class AppraisalEvalBLL : IAppraisalEvalBLL { private readonly IAppraisalEvalDAL _dal; private readonly IValidation _validation; private readonly EventLogPublish _eventLog; private readonly ILogger _logger; public AppraisalEvalBLL( IAppraisalEvalDAL dal, IValidation validation, EventLogPublish eventLog, ILogger logger) { _dal = dal; _validation = validation; _eventLog = eventLog; _logger = logger; } public async Task GetAppraisalEvalAsync(int evalId, LoginDTO login, CancellationToken ct) { try { GB5Trace.Step("get-appraisaleval", new { evalId }); var eval = await _dal.GetAppraisalEvalAsync(evalId, login, ct).ConfigureAwait(false); if (eval is null) return JsonSerializer.Serialize(null); var kras = (await _dal.GetEvalKRAsAsync(evalId, login, ct).ConfigureAwait(false)).ToList(); var details = (await _dal.GetEvalDetailsAsync(evalId, login, ct).ConfigureAwait(false)).ToList(); foreach (var kra in kras) kra.Details = details.Where(d => d.AppraisalEvalKRAId == kra.AppraisalEvalKRAId).ToList(); eval.KRAs = kras; return JsonSerializer.Serialize(eval); } catch (Exception ex) { GB5Trace.MarkFailed("get-appraisaleval-failed", ex); _logger.LogError(ex, "GetAppraisalEval failed for EvalId {EvalId}", evalId); throw; } } public async Task GetEvalListByAppraisalAsync(int appraisalId, LoginDTO login, CancellationToken ct) { try { GB5Trace.Step("get-evallist", new { appraisalId }); var list = await _dal.GetEvalListByAppraisalAsync(appraisalId, login, ct).ConfigureAwait(false); return JsonSerializer.Serialize(list); } catch (Exception ex) { GB5Trace.MarkFailed("get-evallist-failed", ex); _logger.LogError(ex, "GetEvalListByAppraisal failed for AppraisalId {AppraisalId}", appraisalId); throw; } } public async Task SaveAppraisalEvalAsync(AppraisalEvalDTO dto, LoginDTO login, CancellationToken ct) { try { GB5Trace.Step("validate-appraisaleval", new { dto.AppraisalEvalId, dto.AppraisalId }); await _validation.NotEmpty(dto.AppraisalId.ToString(), nameof(dto.AppraisalId)).ConfigureAwait(false); await _validation.NotEmpty(dto.EvaluatorId.ToString(), nameof(dto.EvaluatorId)).ConfigureAwait(false); var isNew = dto.AppraisalEvalId == 0; GB5Trace.Step("save-appraisaleval", new { dto.AppraisalEvalId, isNew }); int savedId = await _dal.SaveAppraisalEvalAsync(dto, isNew, login, ct).ConfigureAwait(false); GB5Trace.Step("event-publish", new { EventTypeId = isNew ? EventTypeConstant.SAVEPERMAPPRAISALEVALEVENTTYPEID : EventTypeConstant.UPDATEPERMAPPRAISALEVALEVENTTYPEID }); await _eventLog.PublishEventLogAsync( isNew ? "Appraisal Evaluation Created" : "Appraisal Evaluation Updated", new { EvalId = savedId, dto.AppraisalId, dto.EvaluatorId, dto.EvalType }, isNew ? EventTypeConstant.SAVEPERMAPPRAISALEVALEVENTTYPEID : EventTypeConstant.UPDATEPERMAPPRAISALEVALEVENTTYPEID, savedId, login, ct: ct).ConfigureAwait(false); return isNew ? $"{GB5Shared.Resource.Response.SuccessResponse.SaveSuccessMessage} {savedId}" : GB5Shared.Resource.Response.SuccessResponse.UpdateSuccess; } catch (Exception ex) { GB5Trace.MarkFailed("save-appraisaleval-failed", ex); _logger.LogError(ex, "SaveAppraisalEval failed for AppraisalId {AppraisalId}", dto.AppraisalId); throw; } } }