MeteredBillingRestrictionsEngine update with the new rule for dunning

This commit is contained in:
Čarodej
2022-06-10 11:19:45 +02:00
parent 9bc8a9b27c
commit 119780317d
4 changed files with 206 additions and 11 deletions

View File

@@ -46,6 +46,10 @@ class AppServiceProvider extends ServiceProvider
private function setSubscriptionConfig(): void
{
if (app()->runningUnitTests()) {
return;
}
$settings = getAllSettings();
config([

View File

@@ -8,26 +8,46 @@ class MeteredBillingRestrictionsEngine implements RestrictionsEngine
{
public function canUpload(User $user, int $fileSize = 0): bool
{
// Check the count of the dunning emails
if ($this->getDunningSequenceCount($user) === 3) {
return false;
}
// Disable upload when user has more than 3 failed payments
return ! ($user->failedPayments()->count() >= 3);
return $this->checkFailedPayments($user);
}
public function canDownload(User $user): bool
{
// Check the count of the dunning emails
if ($this->getDunningSequenceCount($user) === 3) {
return false;
}
// Disable download when user has more than 3 failed payments
return ! ($user->failedPayments()->count() >= 3);
return $this->checkFailedPayments($user);
}
public function canCreateFolder(User $user): bool
{
// Check the count of the dunning emails
if ($this->getDunningSequenceCount($user) === 3) {
return false;
}
// Disable create folder when user has more than 3 failed payments
return ! ($user->failedPayments()->count() >= 3);
return $this->checkFailedPayments($user);
}
public function canCreateTeamFolder(User $user): bool
{
// Check the count of the dunning emails
if ($this->getDunningSequenceCount($user) === 3) {
return false;
}
// Disable create folder when user has more than 3 failed payments
return ! ($user->failedPayments()->count() >= 3);
return $this->checkFailedPayments($user);
}
public function canInviteTeamMembers(User $user, array $newInvites = []): bool
@@ -37,7 +57,22 @@ class MeteredBillingRestrictionsEngine implements RestrictionsEngine
public function canVisitShared(User $user): bool
{
// Check the count of the dunning emails
if ($this->getDunningSequenceCount($user) === 3) {
return false;
}
// Disable share visit when user has more than 3 failed payments
return ! ($user->failedPayments()->count() >= 3);
return $this->checkFailedPayments($user);
}
private function getDunningSequenceCount(User $user): int
{
return cache()->remember("dunning-count.$user->id", 3600, fn () => $user?->dunning->sequence ?? 0);
}
private function checkFailedPayments(User $user): bool
{
return cache()->remember("failed-payments-count.$user->id", 3600, fn () => !($user->failedPayments()->count() >= 3));
}
}