using System; using System.ComponentModel.DataAnnotations; using System.Threading; using System.Threading.Tasks; using FrameworkDAL.CustomCode.RoleVsDashboard; using FrameworkDAL.DTO.Dashboard; using GB5Shared.DaprCache; using GB5Shared.DTO.Framework.AutoNumber; using GB5Shared.DTO.Framework.Login; using GB5Shared.GenerateAutoNumber; using GB5Shared.Resource.Response; using GB5Shared.Validation; namespace FrameworkBLL.RoleVsDashboard { public class RoleVsDashboardBLL : IRoleVsDashboardBLL { private readonly IRoleVsDashboardDAL _RoleVsDashboardDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; private readonly KeyInvalidate _KeyInvalidate; public RoleVsDashboardBLL(IRoleVsDashboardDAL RoleVsDashboardDAL, AutoNumber AutoNumber, IValidation Validation, KeyInvalidate KeyInvalidate) { _RoleVsDashboardDAL = RoleVsDashboardDAL; _AutoNumber = AutoNumber; _Validation = Validation; _KeyInvalidate = KeyInvalidate; } public async Task GetRoleVsDashboard(int RoleId, LoginDTO LoginDTO, CancellationToken ct = default) { try { return await _RoleVsDashboardDAL.GetRoleVsDashboard(RoleId, LoginDTO, ct); } catch (Exception) { throw; } } public async Task SaveRoleVsDashboard(RoleVsDashboardDTO RoleVsDashboardDTO, LoginDTO LoginDTO, CancellationToken ct = default) { if (RoleVsDashboardDTO == null) throw new ArgumentNullException(nameof(RoleVsDashboardDTO)); AutoNumberDTO? autoNumberDTO = null; try { string result; if (RoleVsDashboardDTO.RoleVsDashboardId == 0) { autoNumberDTO = await _AutoNumber.GetAutoNumber(1, "ROLEVSDASHBOARD", LoginDTO); RoleVsDashboardDTO.RoleVsDashboardId = autoNumberDTO.StartNumber; await _RoleVsDashboardDAL.SaveRoleVsDashboard(RoleVsDashboardDTO, LoginDTO, ct); result = $"{SuccessResponse.SaveSuccessMessage} {RoleVsDashboardDTO.RoleVsDashboardId}"; } else { await _RoleVsDashboardDAL.UpdateRoleVsDashboard(RoleVsDashboardDTO, LoginDTO, ct); result = $"{SuccessResponse.UpdateSuccessMessage} {RoleVsDashboardDTO.RoleVsDashboardId}"; } // A Role->Dashboard mapping change affects GetDashboardListForUser for every user // who holds that role — an unbounded, unenumerable set from here (same reasoning // as DashboardBLL's own writes). Full flush; these are rare/admin-driven writes. await _KeyInvalidate.AllInvalidateCache("-1").ConfigureAwait(false); return result; } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (autoNumberDTO != null && RoleVsDashboardDTO.RoleVsDashboardId > 0) await _AutoNumber.RollbackAutoNumber("ROLEVSDASHBOARD", RoleVsDashboardDTO.RoleVsDashboardId, LoginDTO); throw; } } public async Task DeleteRoleVsDashboard(int RoleId, LoginDTO LoginDTO, CancellationToken ct = default) { try { var result = await _RoleVsDashboardDAL.DeleteRoleVsDashboard(RoleId, LoginDTO, ct); await _KeyInvalidate.AllInvalidateCache("-1").ConfigureAwait(false); return result; } catch (Exception) { throw; } } // Phase 6 reconciliation pair — these only compute a diff between MROLEVSDASHBOARD and // MUSERVSDASHBOARD, mirroring RoleVsPortletBLL's identical GetRoleVsPortletForUpdateUserVsPortlet/ // GetUserVsPortletForDeleteUserVsPortlet. Nothing here writes to MUSERVSDASHBOARD — the // caller applies the result via the existing SaveUserVsDashboard/DeleteUserVsDashboard endpoints. public async Task GetRoleVsDashboardForUpdateUserVsDashboard(int RoleId, LoginDTO LoginDTO, CancellationToken ct = default) { try { return await _RoleVsDashboardDAL.GetRoleVsDashboardForUpdateUserVsDashboard(RoleId, LoginDTO, ct); } catch (Exception) { throw; } } public async Task GetUserVsDashboardForDeleteUserVsDashboard(int RoleId, LoginDTO LoginDTO, CancellationToken ct = default) { try { return await _RoleVsDashboardDAL.GetUserVsDashboardForDeleteUserVsDashboard(RoleId, LoginDTO, ct); } catch (Exception) { throw; } } } }