-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
56d6399
commit e0db817
Showing
16 changed files
with
230 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace App\Actions\User; | ||
|
||
use App\Models\DeactivationRequest; | ||
use App\Models\User; | ||
|
||
class RequestDeactivationAction | ||
{ | ||
public function execute(User $user): void | ||
{ | ||
DeactivationRequest::create([ | ||
'user_id' => $user->id, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Models\DeactivationRequest; | ||
use App\Models\User; | ||
use Illuminate\Console\Command; | ||
|
||
class HandleDeactivationRequests extends Command | ||
{ | ||
protected $signature = 'app:handle-deactivation-requests'; | ||
|
||
protected $description = 'Handles deactivation requests'; | ||
|
||
public function handle() | ||
{ | ||
$userIds = DeactivationRequest::query() | ||
->where('created_at', '<', now()->subDays(10)) | ||
->pluck('user_id'); | ||
|
||
$users = User::query() | ||
->whereIn('id', $userIds) | ||
->get(); | ||
|
||
foreach ($users as $user) { | ||
$user->delete(); | ||
} | ||
|
||
DeactivationRequest::query() | ||
->whereIn('user_id', $userIds) | ||
->delete(); | ||
|
||
return $this->comment('Done!'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Actions\User\LogoutUserAction; | ||
use App\Actions\User\RequestDeactivationAction; | ||
|
||
class DeactivationRequestsController extends Controller | ||
{ | ||
public function __construct() | ||
{ | ||
$this->middleware('auth:sanctum'); | ||
} | ||
|
||
public function store(RequestDeactivationAction $requestDeactivationAction, LogoutUserAction $logoutUserAction) | ||
{ | ||
$requestDeactivationAction->execute(request()->user()); | ||
|
||
$logoutUserAction->execute(request()->user()); | ||
|
||
$this->ok(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class DeactivationRequest extends Model | ||
{ | ||
protected $guarded = []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
database/migrations/2023_09_10_170829_create_deactivation_requests_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('deactivation_requests', function (Blueprint $table) { | ||
$table->id(); | ||
$table->foreignId('user_id')->unique(); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists('deactivation_requests'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use App\Models\Country; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Laravel\Sanctum\Sanctum; | ||
use Tests\TestCase; | ||
|
||
class DeactivationRequestsControllerTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
private $user; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
Country::create([ | ||
'code' => 'GB', | ||
'name' => 'United Kingdom', | ||
]); | ||
|
||
$this->user = $this->signIn(); | ||
} | ||
|
||
public function test_user_requests_account_deactivation() | ||
{ | ||
Sanctum::actingAs($this->user); | ||
$this->user->currentAccessToken()->shouldReceive('delete')->once(); | ||
|
||
$this | ||
->postJson('api/deactivate') | ||
->assertOk(); | ||
|
||
$this->assertDatabaseHas('deactivation_requests', [ | ||
'user_id' => $this->user->id, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace Tests\Unit; | ||
|
||
use App\Actions\User\RequestDeactivationAction; | ||
use App\Models\Challenge; | ||
use App\Models\Country; | ||
use App\Models\User; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Tests\TestCase; | ||
|
||
class HandleDeactivationRequestsTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
private User $user; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
Country::create([ | ||
'code' => 'GB', | ||
'name' => 'United Kingdom', | ||
]); | ||
|
||
$this->user = $this->signIn(); | ||
} | ||
|
||
public function test_deletes_user_data(): void | ||
{ | ||
Challenge::factory()->create(); | ||
|
||
Challenge::query() | ||
->first() | ||
->comments() | ||
->create([ | ||
'user_id' => 1, | ||
'text' => 'Keep fighting, dude!', | ||
]); | ||
|
||
Challenge::query() | ||
->first() | ||
->likes() | ||
->create([ | ||
'user_id' => 1, | ||
]); | ||
|
||
app(RequestDeactivationAction::class)->execute($this->user); | ||
|
||
$this->travelTo(now()->addDays(11)); | ||
|
||
$this->artisan('app:handle-deactivation-requests'); | ||
|
||
$this->assertDatabaseCount('deactivation_requests', 0); | ||
$this->assertDatabaseCount('users', 0); | ||
$this->assertDatabaseCount('challenges', 0); | ||
$this->assertDatabaseCount('comments', 0); | ||
$this->assertDatabaseCount('likes', 0); | ||
} | ||
} |