Deploy: 2026-05-23 00:20:07
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace App\Models;
|
||||
|
||||
use App\Core\Database;
|
||||
use App\Core\Cache;
|
||||
|
||||
/**
|
||||
* CompanySubscription Model
|
||||
@@ -17,19 +18,23 @@ class CompanySubscription extends BaseModel
|
||||
*/
|
||||
public static function findActiveByCompany(int $companyId): ?array
|
||||
{
|
||||
$now = date('Y-m-d H:i:s');
|
||||
return Database::selectOne(
|
||||
"SELECT cs.*, sp.name as plan_name, sp.max_sessions, sp.max_requests,
|
||||
sp.max_voice_requests, sp.max_ocr_requests, sp.features
|
||||
FROM " . static::$table . " cs
|
||||
JOIN subscription_plans sp ON cs.plan_id = sp.id
|
||||
WHERE cs.company_id = ?
|
||||
AND cs.status = 'active'
|
||||
AND cs.starts_at <= ?
|
||||
AND cs.ends_at >= ?
|
||||
LIMIT 1",
|
||||
[$companyId, $now, $now]
|
||||
);
|
||||
$cacheKey = "company_subscription_{$companyId}";
|
||||
|
||||
return Cache::remember($cacheKey, 300, function () use ($companyId) {
|
||||
$now = date('Y-m-d H:i:s');
|
||||
return Database::selectOne(
|
||||
"SELECT cs.*, sp.name as plan_name, sp.max_sessions, sp.max_requests,
|
||||
sp.max_voice_requests, sp.max_ocr_requests, sp.features
|
||||
FROM " . static::$table . " cs
|
||||
JOIN subscription_plans sp ON cs.plan_id = sp.id
|
||||
WHERE cs.company_id = ?
|
||||
AND cs.status = 'active'
|
||||
AND cs.starts_at <= ?
|
||||
AND cs.ends_at >= ?
|
||||
LIMIT 1",
|
||||
[$companyId, $now, $now]
|
||||
) ?: [];
|
||||
}) ?: null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -47,6 +52,9 @@ class CompanySubscription extends BaseModel
|
||||
[$companyId]
|
||||
);
|
||||
|
||||
// Clear active subscription cache for the company
|
||||
Cache::delete("company_subscription_{$companyId}");
|
||||
|
||||
return self::create([
|
||||
'company_id' => $companyId,
|
||||
'plan_id' => $planId,
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Models;
|
||||
|
||||
use App\Core\Database;
|
||||
use App\Core\Cache;
|
||||
|
||||
/**
|
||||
* CompanySubscriptionUsage Model
|
||||
@@ -20,7 +21,14 @@ class CompanySubscriptionUsage extends BaseModel
|
||||
$billingStart = date('Y-m-d', strtotime($activeSubscription['starts_at']));
|
||||
$billingEnd = date('Y-m-d', strtotime($activeSubscription['ends_at']));
|
||||
|
||||
// Check if usage record already exists
|
||||
$cacheKey = "company_usage_{$companyId}_{$billingStart}_{$billingEnd}";
|
||||
|
||||
$cached = Cache::get($cacheKey);
|
||||
if ($cached) {
|
||||
return $cached;
|
||||
}
|
||||
|
||||
// Check if usage record already exists in database
|
||||
$usage = Database::selectOne(
|
||||
"SELECT * FROM " . static::$table . "
|
||||
WHERE company_id = ? AND billing_start = ? AND billing_end = ?
|
||||
@@ -39,7 +47,7 @@ class CompanySubscriptionUsage extends BaseModel
|
||||
'voice_count' => 0,
|
||||
'ocr_count' => 0
|
||||
]);
|
||||
return [
|
||||
$usage = [
|
||||
'id' => $id,
|
||||
'company_id' => $companyId,
|
||||
'billing_start' => $billingStart,
|
||||
@@ -62,6 +70,9 @@ class CompanySubscriptionUsage extends BaseModel
|
||||
}
|
||||
}
|
||||
|
||||
// Cache usage data for 60 seconds (short TTL since usage changes frequently)
|
||||
Cache::set($cacheKey, $usage, 60);
|
||||
|
||||
return $usage;
|
||||
}
|
||||
|
||||
@@ -84,12 +95,24 @@ class CompanySubscriptionUsage extends BaseModel
|
||||
$column = 'ocr_count';
|
||||
}
|
||||
|
||||
return Database::execute(
|
||||
$success = Database::execute(
|
||||
"UPDATE " . static::$table . "
|
||||
SET {$column} = {$column} + ?
|
||||
WHERE id = ?",
|
||||
[$amount, $currentUsage['id']]
|
||||
) > 0;
|
||||
|
||||
if ($success) {
|
||||
// Sync Redis cache with incremented values
|
||||
$cacheKey = "company_usage_{$companyId}_{$currentUsage['billing_start']}_{$currentUsage['billing_end']}";
|
||||
$cached = Cache::get($cacheKey);
|
||||
if ($cached) {
|
||||
$cached[$column] += $amount;
|
||||
Cache::set($cacheKey, $cached, 60);
|
||||
}
|
||||
}
|
||||
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user