Update: 2026-05-15 15:02:14

This commit is contained in:
Hamza-Ayed
2026-05-15 15:02:14 +03:00
parent 54a4acdcab
commit 7ee897ff3d
6 changed files with 277 additions and 45 deletions

View File

@@ -11,6 +11,7 @@ declare(strict_types=1);
namespace App\Middleware;
use App\Core\Database;
use App\Core\Cache;
final class QuotaMiddleware
{
@@ -22,17 +23,26 @@ final class QuotaMiddleware
*/
public static function checkInvoiceQuota(string $tenantId): array
{
$db = Database::getInstance();
$cacheKey = "quota_sub_{$tenantId}";
$sub = Cache::get($cacheKey);
// Fetch subscription with plan info
$stmt = $db->prepare("
SELECT s.*, sp.name_ar as plan_name, sp.ai_features, sp.jofotara_enabled
FROM subscriptions s
LEFT JOIN subscription_plans sp ON s.plan_id = sp.id
WHERE s.tenant_id = ?
");
$stmt->execute([$tenantId]);
$sub = $stmt->fetch();
if ($sub === false || $sub === null) {
$db = Database::getInstance();
// Fetch subscription with plan info
$stmt = $db->prepare("
SELECT s.*, sp.name_ar as plan_name, sp.ai_features, sp.jofotara_enabled
FROM subscriptions s
LEFT JOIN subscription_plans sp ON s.plan_id = sp.id
WHERE s.tenant_id = ?
");
$stmt->execute([$tenantId]);
$sub = $stmt->fetch();
if ($sub) {
Cache::set($cacheKey, $sub, 300); // Cache for 5 minutes
}
}
if (!$sub) {
json_error('لا يوجد اشتراك فعّال لهذا المكتب. يرجى التواصل مع الإدارة.', 403);
@@ -100,6 +110,9 @@ final class QuotaMiddleware
WHERE tenant_id = ?
");
$stmt->execute([$tenantId]);
// Invalidate cache
Cache::forget("quota_sub_{$tenantId}");
}
/**