diff --git a/src/app/Observers/UserObserver.php b/src/app/Observers/UserObserver.php --- a/src/app/Observers/UserObserver.php +++ b/src/app/Observers/UserObserver.php @@ -107,27 +107,12 @@ // Remove owned users/domains/groups/resources/etc self::removeRelatedObjects($user, $user->isForceDeleting()); - // TODO: Especially in tests we're doing delete() on a already deleted user. - // Should we escape here - for performance reasons? - if (!$user->isForceDeleting()) { \App\Jobs\User\DeleteJob::dispatch($user->id); if (\App\Tenant::getConfig($user->tenant_id, 'pgp.enable')) { \App\Jobs\PGP\KeyDeleteJob::dispatch($user->id, $user->email); } - - // Debit the reseller's wallet with the user negative balance - $balance = 0; - foreach ($user->wallets as $wallet) { - // Note: here we assume all user wallets are using the same currency. - // It might get changed in the future - $balance += $wallet->balance; - } - - if ($balance < 0 && $user->tenant && ($wallet = $user->tenant->wallet())) { - $wallet->debit($balance * -1, "Deleted user {$user->email}"); - } } } diff --git a/src/tests/Feature/UserTest.php b/src/tests/Feature/UserTest.php --- a/src/tests/Feature/UserTest.php +++ b/src/tests/Feature/UserTest.php @@ -892,51 +892,6 @@ Queue::assertPushed(\App\Jobs\Group\UpdateJob::class, 2); } - /** - * Test handling negative balance on user deletion - */ - public function testDeleteWithNegativeBalance(): void - { - $user = $this->getTestUser('user-test@' . \config('app.domain')); - $wallet = $user->wallets()->first(); - $wallet->balance = -1000; - $wallet->save(); - $reseller_wallet = $user->tenant->wallet(); - $reseller_wallet->balance = 0; - $reseller_wallet->save(); - \App\Transaction::where('object_id', $reseller_wallet->id)->where('object_type', \App\Wallet::class)->delete(); - - $user->delete(); - - $reseller_transactions = \App\Transaction::where('object_id', $reseller_wallet->id) - ->where('object_type', \App\Wallet::class)->get(); - - $this->assertSame(-1000, $reseller_wallet->fresh()->balance); - $this->assertCount(1, $reseller_transactions); - $trans = $reseller_transactions[0]; - $this->assertSame("Deleted user {$user->email}", $trans->description); - $this->assertSame(-1000, $trans->amount); - $this->assertSame(\App\Transaction::WALLET_DEBIT, $trans->type); - } - - /** - * Test handling positive balance on user deletion - */ - public function testDeleteWithPositiveBalance(): void - { - $user = $this->getTestUser('user-test@' . \config('app.domain')); - $wallet = $user->wallets()->first(); - $wallet->balance = 1000; - $wallet->save(); - $reseller_wallet = $user->tenant->wallet(); - $reseller_wallet->balance = 0; - $reseller_wallet->save(); - - $user->delete(); - - $this->assertSame(0, $reseller_wallet->fresh()->balance); - } - /** * Test user deletion with PGP/WOAT enabled */