Added the condition to check if it is the root tenant

This commit is contained in:
ashutosh.nehete 2025-08-18 12:41:00 +05:30
parent cf161e4a04
commit 90a2b23c1a
2 changed files with 20 additions and 87 deletions

View File

@ -33,40 +33,6 @@ namespace MarcoBMS.Services.Controllers
tenantId = userHelper.GetTenantId();
}
private ICollection<FeaturePermissionVM> GetFeaturePermissionVM(Feature model)
{
if (model.FeaturePermissions == null)
{
return [];
}
ICollection<FeaturePermissionVM> features = model.FeaturePermissions.Select(p => _mapper.Map<FeaturePermissionVM>(p)).OrderBy(f => f.Name).ToList();
return features;
}
[HttpGet("features")]
public async Task<IActionResult> GetAllFeatures()
{
List<Guid> featureIds = await _generalHelper.GetFeatureIdsByTenentId(tenantId);
var roles = await _context.Features
.Include(f => f.FeaturePermissions)
.Include(f => f.Module)
.Where(f => featureIds.Contains(f.Id))
.ToListAsync();
var rolesVM = roles.Select(c => new FeatureVM()
{
Id = c.Id,
Name = c.Name,
Description = c.Description,
FeaturePermissions = GetFeaturePermissionVM(c),
ModuleId = c.ModuleId,
ModuleName = c.Module != null ? c.Module.Name : string.Empty,
IsActive = c.IsActive
}).OrderBy(f => f.Name).ToList();
return Ok(ApiResponse<object>.SuccessResponse(rolesVM, "Success.", 200));
}
/// <summary>
/// Converts FeaturePermissions from Feature entity into FeaturePermissionVM collection.
/// </summary>
@ -100,6 +66,11 @@ namespace MarcoBMS.Services.Controllers
{
_logger.LogInfo("Fetching all features for tenant: {TenantId}", tenantId);
var featureQuery = _context.Features
.AsNoTracking(); // Optimization: Read-only query
if (tenantId != Guid.Parse("b3466e83-7e11-464c-b93a-daf047838b26"))
{
// Step 1: Get tenant-specific FeatureIds
List<Guid> featureIds = await _generalHelper.GetFeatureIdsByTenentIdAsync(tenantId);
if (featureIds == null || !featureIds.Any())
@ -107,17 +78,15 @@ namespace MarcoBMS.Services.Controllers
_logger.LogWarning("No features found for tenant: {TenantId}", tenantId);
return Ok(ApiResponse<object>.SuccessResponse(new List<FeatureVM>(), "No features found.", 200));
}
_logger.LogDebug("Retrieved {Count} feature IDs for tenant: {TenantId}", featureIds.Count, tenantId);
// Step 2: Query Features with related FeaturePermissions & Module
var features = await _context.Features
.AsNoTracking() // Optimization: Read-only query
.Include(f => f.FeaturePermissions)
.Include(f => f.Module)
.Where(f => featureIds.Contains(f.Id))
.ToListAsync();
featureQuery = featureQuery.Where(f => featureIds.Contains(f.Id));
}
var features = await featureQuery
.Include(f => f.FeaturePermissions)
.Include(f => f.Module).ToListAsync();
_logger.LogDebug("Fetched {Count} features from DB for tenant: {TenantId}", features.Count, tenantId);
// Step 3: Map features to ViewModels

View File

@ -216,43 +216,6 @@ namespace Marco.Pms.Services.Helpers
}
}
public async Task<List<Guid>> GetFeatureIdsByTenentId(Guid tenantId)
{
var tenantSubscription = await _context.TenantSubscriptions.Include(ts => ts.Plan)
.FirstOrDefaultAsync(ts => ts.TenantId == tenantId && ts.Plan != null && !ts.IsCancelled && ts.EndDate.Date < DateTime.UtcNow.Date);
if (tenantSubscription == null)
{
_logger.LogWarning("Cannot found the tenant subscription for tenant {TenantId}", tenantId);
return new List<Guid>();
}
var featureDetails = await _featureDetailsHelper.GetFeatureDetails(tenantSubscription.Plan!.FeaturesId);
if (featureDetails == null)
{
_logger.LogWarning("Cannot found the feature details for tenant {TenantId}", tenantId);
return new List<Guid>();
}
var featureIds = new List<Guid>();
if (featureDetails.Modules!.Attendance!.Enabled)
{
featureIds.AddRange(featureDetails.Modules.Attendance.FeatureId);
}
if (featureDetails.Modules.ProjectManagement!.Enabled)
{
featureIds.AddRange(featureDetails.Modules.ProjectManagement.FeatureId);
}
if (featureDetails.Modules.Directory!.Enabled)
{
featureIds.AddRange(featureDetails.Modules.Directory.FeatureId);
}
if (featureDetails.Modules.Expense!.Enabled)
{
featureIds.AddRange(featureDetails.Modules.Expense.FeatureId);
}
return featureIds;
}
/// <summary>
/// Retrieves all enabled feature IDs for a given tenant based on their active subscription.
/// </summary>
@ -283,6 +246,8 @@ namespace Marco.Pms.Services.Helpers
_logger.LogDebug("Active subscription found for tenant: {TenantId}, PlanId: {PlanId}",
tenantId, tenantSubscription.Plan!.Id);
var featureIds = new List<Guid> { new Guid("2f3509b7-160d-410a-b9b6-daadd96c986d"), new Guid("be3b3afc-6ccf-4566-b9b6-aafcb65546be") };
// Step 2: Get feature details from Plan
var featureDetails = await _featureDetailsHelper.GetFeatureDetails(tenantSubscription.Plan!.FeaturesId);
@ -294,7 +259,6 @@ namespace Marco.Pms.Services.Helpers
}
// Step 3: Collect all enabled feature IDs from modules
var featureIds = new List<Guid> { new Guid("2f3509b7-160d-410a-b9b6-daadd96c986d"), new Guid("be3b3afc-6ccf-4566-b9b6-aafcb65546be") };
if (featureDetails.Modules?.Attendance?.Enabled == true)
{