From 9765ce1b8f81ba833f7fef7175cf1176eee312c7 Mon Sep 17 00:00:00 2001 From: "ashutosh.nehete" Date: Mon, 25 Aug 2025 11:02:38 +0530 Subject: [PATCH] Added the check to check the max user in add subscription as well --- .../Controllers/TenantController.cs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/Marco.Pms.Services/Controllers/TenantController.cs b/Marco.Pms.Services/Controllers/TenantController.cs index de5cbe1..10a031d 100644 --- a/Marco.Pms.Services/Controllers/TenantController.cs +++ b/Marco.Pms.Services/Controllers/TenantController.cs @@ -348,6 +348,8 @@ namespace Marco.Pms.Services.Controllers // Calculate active/inactive employees count var activeEmployeesCount = employees.Count(e => e.IsActive); var inActiveEmployeesCount = employees.Count - activeEmployeesCount; + var activeUserCount = employees.Count(e => e.IsActive && e.ApplicationUserId != null && e.Email != null); + // Filter current active (non-cancelled) subscription plan var currentPlan = plans.FirstOrDefault(ts => !ts.IsCancelled); @@ -365,7 +367,7 @@ namespace Marco.Pms.Services.Controllers response.OnHoldProjects = projects.Count(p => p.ProjectStatusId == projectOnHoldStatus); response.InActiveProjects = projects.Count(p => p.ProjectStatusId == projectInActiveStatus); response.CompletedProjects = projects.Count(p => p.ProjectStatusId == projectCompletedStatus); - response.SeatsAvailable = (int)(currentPlan?.MaxUsers ?? 1) - activeEmployeesCount; + response.SeatsAvailable = (int)(currentPlan?.MaxUsers ?? 1) - activeUserCount; response.ExpiryDate = expiryDate; response.NextBillingDate = nextBillingDate; response.CreatedBy = createdBy; @@ -900,7 +902,12 @@ namespace Marco.Pms.Services.Controllers _logger.LogWarning("Subscription plan {PlanId} not found in database", model.PlanId); return NotFound(ApiResponse.ErrorResponse("Subscription plan not found", "Subscription plan not found", 404)); } - + var activeUsers = await _context.Employees.CountAsync(e => e.Email != null && e.ApplicationUserId != null && e.TenantId == tenant.Id && e.IsActive); + if (activeUsers > model.MaxUsers) + { + _logger.LogWarning("Employee {EmployeeId} add less max user than the active user in the tenant {TenantId}", loggedInEmployee.Id, tenant.Id); + return BadRequest(ApiResponse.ErrorResponse("Invalid Max user count", "Max User count must be higher than active user count", 400)); + } await using var transaction = await _context.Database.BeginTransactionAsync(); var utcNow = DateTime.UtcNow; @@ -1146,9 +1153,8 @@ namespace Marco.Pms.Services.Controllers var currentSubscription = currentSubscriptionTask.Result; var utcNow = DateTime.UtcNow; - var activeUsers = await context.Employees.CountAsync(e => e.Email != null && e.ApplicationUserId != null); - - if (activeUsers < model.MaxUsers) + var activeUsers = await context.Employees.CountAsync(e => e.Email != null && e.ApplicationUserId != null && e.TenantId == tenant.Id && e.IsActive); + if (activeUsers > model.MaxUsers) { _logger.LogWarning("Employee {EmployeeId} add less max user than the active user in the tenant {TenantId}", loggedInEmployee.Id, tenant.Id); return BadRequest(ApiResponse.ErrorResponse("Invalid Max user count", "Max User count must be higher than active user count", 400));