1) user invitation 2) app access control 3) txn quota guard
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
class UsageGuard {
|
||||
|
||||
private PDO $pdo;
|
||||
private int $company_id;
|
||||
private array $pkg;
|
||||
|
||||
public function __construct(PDO $pdo1, int $company_id, array $packages) {
|
||||
$this->pdo = $pdo1;
|
||||
$this->company_id = $company_id;
|
||||
$this->pkg = $this->resolvePackage($packages);
|
||||
}
|
||||
|
||||
private function resolvePackage(array $packages): array {
|
||||
$sth = $this->pdo->prepare(
|
||||
"SELECT package FROM company_list WHERE company_id = :cid LIMIT 1"
|
||||
);
|
||||
$sth->execute([':cid' => $this->company_id]);
|
||||
$name = $sth->fetchColumn() ?: 'starter';
|
||||
return $packages[$name] ?? $packages['starter'] ?? [
|
||||
'daily_limit' => 30,
|
||||
'weekly_limit' => 100,
|
||||
'lock_on_limit' => ['dashboard'],
|
||||
];
|
||||
}
|
||||
|
||||
public function increment(): void {
|
||||
$today = date('Y-m-d');
|
||||
$this->pdo->prepare(
|
||||
"INSERT INTO company_usage (company_id, day_date, daily_count)
|
||||
VALUES (:cid, :today, 1)
|
||||
ON DUPLICATE KEY UPDATE daily_count = daily_count + 1"
|
||||
)->execute([':cid' => $this->company_id, ':today' => $today]);
|
||||
}
|
||||
|
||||
public function assertFeatureAccessible(string $feature): void {
|
||||
$lock_on = $this->pkg['lock_on_limit'] ?? [];
|
||||
if (!in_array($feature, $lock_on, true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$status = $this->getStatus();
|
||||
$daily_limit = (int)($this->pkg['daily_limit'] ?? 0);
|
||||
$weekly_limit = (int)($this->pkg['weekly_limit'] ?? 0);
|
||||
|
||||
$over_daily = $daily_limit > 0 && $status['daily_count'] >= $daily_limit;
|
||||
$over_weekly = $weekly_limit > 0 && $status['weekly_count'] >= $weekly_limit;
|
||||
|
||||
if ($over_daily || $over_weekly) {
|
||||
http_response_code(402);
|
||||
exit(json_encode([
|
||||
'success' => 0,
|
||||
'message' => 'Usage limit reached. Upgrade your package to access reports.',
|
||||
'limit_reached' => true,
|
||||
'daily_count' => $status['daily_count'],
|
||||
'daily_limit' => $status['daily_limit'],
|
||||
'weekly_count' => $status['weekly_count'],
|
||||
'weekly_limit' => $status['weekly_limit'],
|
||||
]));
|
||||
}
|
||||
}
|
||||
|
||||
public function getStatus(): array {
|
||||
$today = date('Y-m-d');
|
||||
$day_of_week = (int)date('N');
|
||||
$week_start = date('Y-m-d', strtotime('-' . ($day_of_week - 1) . ' days'));
|
||||
|
||||
$sth = $this->pdo->prepare(
|
||||
"SELECT daily_count FROM company_usage
|
||||
WHERE company_id = :cid AND day_date = :today LIMIT 1"
|
||||
);
|
||||
$sth->execute([':cid' => $this->company_id, ':today' => $today]);
|
||||
$daily_count = (int)($sth->fetchColumn() ?: 0);
|
||||
|
||||
$sth = $this->pdo->prepare(
|
||||
"SELECT COALESCE(SUM(daily_count), 0) FROM company_usage
|
||||
WHERE company_id = :cid AND day_date BETWEEN :wstart AND :today"
|
||||
);
|
||||
$sth->execute([
|
||||
':cid' => $this->company_id,
|
||||
':wstart'=> $week_start,
|
||||
':today' => $today,
|
||||
]);
|
||||
$weekly_count = (int)$sth->fetchColumn();
|
||||
|
||||
$daily_limit = (int)($this->pkg['daily_limit'] ?? 0);
|
||||
$weekly_limit = (int)($this->pkg['weekly_limit'] ?? 0);
|
||||
|
||||
return [
|
||||
'daily_count' => $daily_count,
|
||||
'weekly_count' => $weekly_count,
|
||||
'daily_limit' => $daily_limit,
|
||||
'weekly_limit' => $weekly_limit,
|
||||
'daily_pct' => $daily_limit > 0 ? min(100, (int)round($daily_count / $daily_limit * 100)) : 0,
|
||||
'weekly_pct' => $weekly_limit > 0 ? min(100, (int)round($weekly_count / $weekly_limit * 100)) : 0,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user