-
Notifications
You must be signed in to change notification settings - Fork 3
Create Model for Term Of Use acceptance #977
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 12 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
d768691
Create Model for Term Of Use acceptance
dati18 c458ee1
minor fix
dati18 7aceaa9
Add missing Migration for tou_acceptances table
dati18 3c8924c
add lastest() method to get lastest version of ToU
dati18 dad1cd7
Add test case
dati18 47e7875
Add test for ToU acceptance when user was created
dati18 456bf9d
Fix test
dati18 89eeda0
fix linting errors
dati18 bf56e3e
fix linting errors
dati18 95a05e0
fix linting errors
dati18 eb6640e
Fix typo in classes' names
dati18 33bb2f0
Add Job to generate 'tou_version' and 'tou_accepted_at' for existing …
dati18 90954d3
fix linting errors
dati18 39440e3
Change UserTermsOfUseAcceptances' relation with 'user_id'
dati18 5e21e2f
Minor fix
dati18 0eb8579
fix UserTouAcceptanceJobTest
dati18 e816ab6
fix linting error
dati18 e6e779b
Refactor: Change TermsOfUseVersion from enum to Model
dati18 734a52a
Fix: Remove hard dependency on pre-seeded ToU version
dati18 2652871
fix linting errors
dati18 0b54f8d
fix linting error
dati18 b5979d7
minor changes
dati18 7f30a37
Fix: removed unnecessary fields
dati18 43d6608
fix testing wrong version
dati18 9ab90cc
fix linting error
dati18 f095740
rename the method that retrieve latest active version of ToU
dati18 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,42 @@ | ||
| <?php | ||
|
|
||
| namespace App\Jobs; | ||
|
|
||
| use App\TermsOfUseVersion; | ||
| use App\User; | ||
| use App\UserTermsOfUseAcceptance; | ||
| use Illuminate\Bus\Batchable; | ||
| use Illuminate\Bus\Queueable; | ||
| use Illuminate\Contracts\Queue\ShouldQueue; | ||
| use Illuminate\Foundation\Bus\Dispatchable; | ||
| use Illuminate\Queue\InteractsWithQueue; | ||
| use Illuminate\Queue\Middleware\WithoutOverlapping; | ||
| use Illuminate\Queue\SerializesModels; | ||
| use Illuminate\Support\Facades\Log; | ||
| use Throwable; | ||
|
|
||
| class UserTouAcceptanceJob extends Job { | ||
| use Batchable; | ||
| use Dispatchable; | ||
|
|
||
| private $users; | ||
|
|
||
| public function __construct($users) { | ||
| $this->users = $users; | ||
| } | ||
| public function handle(): void { | ||
| $this->users = User::all(); | ||
| foreach ($this->users as $user) { | ||
| try { | ||
| UserTermsOfUseAcceptance::create([ | ||
| 'user_id' => $user->id, | ||
| 'tou_version' => TermsOfUseVersion::latest(), | ||
| 'tou_accepted_at' => $user->created_at, | ||
| ]); | ||
| } catch (Throwable $exception) { | ||
| Log::error('Failure processing user ' . $user->getAttribute('email') . ' for UserTouAcceptanceJob: ' . $exception->getMessage()); | ||
| $this->fail($exception); | ||
|
tarrow marked this conversation as resolved.
|
||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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,26 @@ | ||
| <?php | ||
|
|
||
| namespace App; | ||
|
|
||
| enum TermsOfUseVersion: string { | ||
| // case v2 = 'yyyy-mm-dd'; | ||
| // case v1 = 'yyyy-mm-dd'; | ||
| case v0 = '2025-08-21'; | ||
|
|
||
| public static function latest(): self { | ||
| $latestVersion = self::v0; | ||
| $latestNum = 0; | ||
| foreach (self::cases() as $case) { | ||
| if (!str_starts_with($case->name, 'v')) { | ||
| continue; | ||
| } | ||
| $n = (int) substr($case->name, 1); | ||
| if ($n > $latestNum) { | ||
| $latestNum = $n; | ||
| $latestVersion = $case; | ||
| } | ||
| } | ||
|
|
||
| return $latestVersion; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,27 @@ | ||
| <?php | ||
|
|
||
| namespace App; | ||
|
|
||
| use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
| use Illuminate\Database\Eloquent\Model; | ||
|
|
||
| class UserTermsOfUseAcceptance extends Model { | ||
| use HasFactory; | ||
|
|
||
| const FIELDS = [ | ||
| 'user_id', | ||
| 'tou_version', | ||
| 'tou_accepted_at', | ||
|
tarrow marked this conversation as resolved.
tarrow marked this conversation as resolved.
|
||
| ]; | ||
|
|
||
| protected $fillable = self::FIELDS; | ||
|
|
||
| protected $visible = self::FIELDS; | ||
|
|
||
| protected $casts = [ | ||
| 'tou_version' => TermsOfUseVersion::class, | ||
| 'tou_accepted_at' => 'datetime', | ||
| ]; | ||
|
|
||
| protected $table = 'tou_acceptances'; | ||
| } | ||
This file contains hidden or 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,29 @@ | ||
| <?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('tou_acceptances', function (Blueprint $table) { | ||
| $table->id(); | ||
| $table->unsignedInteger('user_id'); | ||
| $table->string('tou_version', 10); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't seem to be linked to the |
||
| $table->timestamp('tou_accepted_at'); | ||
| $table->timestamps(); | ||
| $table->unique(['user_id', 'tou_version']); | ||
| $table->foreign('user_id')->references('id')->on('users')->cascadeOnDelete(); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Reverse the migrations. | ||
| */ | ||
| public function down(): void { | ||
| Schema::dropIfExists('tou_acceptances'); | ||
| } | ||
| }; | ||
This file contains hidden or 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,37 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Unit\Jobs; | ||
|
tarrow marked this conversation as resolved.
Outdated
|
||
|
|
||
| use App\Jobs\UserTouAcceptanceJob; | ||
| use App\TermsOfUseVersion; | ||
| use App\User; | ||
| use App\UserTermsOfUseAcceptance; | ||
| use Illuminate\Foundation\Testing\RefreshDatabase; | ||
| use Illuminate\Support\Carbon; | ||
| use Tests\TestCase; | ||
|
|
||
| class UserTouAcceptanceJobTest extends TestCase{ | ||
|
tarrow marked this conversation as resolved.
Outdated
|
||
| use RefreshDatabase; | ||
|
|
||
| public function testTouAcceptanceJob(): void{ | ||
|
tarrow marked this conversation as resolved.
Outdated
|
||
| $t1 = Carbon::parse('2025-01-01 10:00:00'); | ||
| $t2 = Carbon::parse('2025-01-02 11:00:00'); | ||
| $t3 = Carbon::parse('2025-01-03 12:00:00'); | ||
|
|
||
| $u1 = User::factory()->create(['created_at' => $t1]); | ||
| $u2 = User::factory()->create(['created_at' => $t2]); | ||
| $u3 = User::factory()->create(['created_at' => $t3]); | ||
|
|
||
| (new UserTouAcceptanceJob([]))->handle(); | ||
|
|
||
| $latest = TermsOfUseVersion::latest()->value; | ||
|
|
||
| $this->assertDatabaseHas('tou_acceptances', ['user_id' => $u1->id, 'tou_version' => $latest]); | ||
| $this->assertDatabaseHas('tou_acceptances', ['user_id' => $u2->id, 'tou_version' => $latest]); | ||
| $this->assertDatabaseHas('tou_acceptances', ['user_id' => $u3->id, 'tou_version' => $latest]); | ||
|
|
||
| $this->assertTrue($t1->equalTo(UserTermsOfUseAcceptance::where('user_id', $u1->id)->firstOrFail()->tou_accepted_at)); | ||
| $this->assertTrue($t2->equalTo(UserTermsOfUseAcceptance::where('user_id', $u2->id)->firstOrFail()->tou_accepted_at)); | ||
| $this->assertTrue($t3->equalTo(UserTermsOfUseAcceptance::where('user_id', $u3->id)->firstOrFail()->tou_accepted_at)); | ||
| } | ||
| } | ||
This file contains hidden or 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,31 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Routes\User; | ||
|
|
||
| use App\Jobs\UserCreateJob; | ||
| use App\TermsOfUseVersion; | ||
| use App\UserTermsOfUseAcceptance; | ||
| use Illuminate\Foundation\Testing\RefreshDatabase; | ||
| use Tests\TestCase; | ||
|
|
||
| class UserTermsOfUseAcceptanceTest extends TestCase { | ||
| use RefreshDatabase; | ||
|
|
||
| public function testUserCreationCreatesTouAcceptance(): void { | ||
| $email = 'test+' . uniqid('', true) . '@example.com'; | ||
| $user = (new UserCreateJob($email, 'thisisapassword123', true))->handle(); | ||
|
|
||
| $this->assertDatabaseHas('tou_acceptances', [ | ||
| 'user_id' => $user->id, | ||
| 'tou_version' => TermsOfUseVersion::latest()->value, | ||
| ]); | ||
|
|
||
| $rows = UserTermsOfUseAcceptance::where('user_id', $user->id)->get(); | ||
| $this->assertCount(1, $rows); | ||
| $acceptance = $rows->first(); | ||
|
|
||
| $this->assertInstanceOf(TermsOfUseVersion::class, $acceptance->tou_version); | ||
| $this->assertSame(TermsOfUseVersion::latest(), $acceptance->tou_version); | ||
| $this->assertNotNull($acceptance->tou_accepted_at); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.