From 6a3589f9102551abdd5aecbafeb24967e50c3619 Mon Sep 17 00:00:00 2001 From: Dat Date: Mon, 29 Dec 2025 15:05:05 +0100 Subject: [PATCH 1/7] Refactor UserTermsOfUseAcceptance and TermsOfUseVersion Models Bug: T407722 --- app/TermsOfUseVersion.php | 16 ++++++++++++++++ app/UserTermsOfUseAcceptance.php | 10 ++++++++++ .../2025_09_29_194758_tou_acceptances.php | 3 +-- .../2025_10_14_091126_tou_versions.php | 1 - 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/app/TermsOfUseVersion.php b/app/TermsOfUseVersion.php index e4472407..c99e7e27 100644 --- a/app/TermsOfUseVersion.php +++ b/app/TermsOfUseVersion.php @@ -13,7 +13,10 @@ class TermsOfUseVersion extends Model { use HasFactory; + protected $primaryKey = 'version'; + protected $keyType = 'string'; protected $table = 'tou_versions'; + public $incrementing = false; const FIELDS = [ 'version', @@ -32,4 +35,17 @@ class TermsOfUseVersion extends Model { public static function latestActiveVersion(): ?self { return self::query()->where('active', true)->latest()->first(); } + + protected static function booted(): void { + static::saving(function (self $model): void { + if (! $model->active) { + return; + } + + self::query() + ->where('version', '!=', $model->version) + ->where('active', true) + ->update(['active' => false]); + }); + } } diff --git a/app/UserTermsOfUseAcceptance.php b/app/UserTermsOfUseAcceptance.php index 3a13bcf9..9a891870 100644 --- a/app/UserTermsOfUseAcceptance.php +++ b/app/UserTermsOfUseAcceptance.php @@ -9,6 +9,10 @@ class UserTermsOfUseAcceptance extends Model { use HasFactory; + public $incrementing = false; + protected $primaryKey = null; + protected $keyType = 'string'; + public const FIELDS = [ 'user_id', 'tou_version', @@ -29,4 +33,10 @@ class UserTermsOfUseAcceptance extends Model { public function user(): BelongsTo { return $this->belongsTo(User::class); } + + protected function setKeysForSaveQuery($query) { + return $query + ->where('user_id', $this->getAttribute('user_id')) + ->where('tou_version', $this->getAttribute('tou_version')); + } } diff --git a/database/migrations/2025_09_29_194758_tou_acceptances.php b/database/migrations/2025_09_29_194758_tou_acceptances.php index bbc1e76c..5ddd1137 100644 --- a/database/migrations/2025_09_29_194758_tou_acceptances.php +++ b/database/migrations/2025_09_29_194758_tou_acceptances.php @@ -10,9 +10,8 @@ */ public function up(): void { Schema::create('tou_acceptances', function (Blueprint $table) { - $table->id(); $table->unsignedInteger('user_id'); - $table->string('tou_version', 10); + $table->string('tou_version'); $table->timestamp('tou_accepted_at'); $table->timestamps(); $table->unique(['user_id', 'tou_version']); diff --git a/database/migrations/2025_10_14_091126_tou_versions.php b/database/migrations/2025_10_14_091126_tou_versions.php index c7aa71d9..07c41480 100644 --- a/database/migrations/2025_10_14_091126_tou_versions.php +++ b/database/migrations/2025_10_14_091126_tou_versions.php @@ -10,7 +10,6 @@ */ public function up(): void { Schema::create('tou_versions', function (Blueprint $table) { - $table->id(); $table->string('version')->unique(); $table->boolean('active')->default(false); $table->timestamps(); From e0f554c5653b693e268741299a3734e95fbfd098 Mon Sep 17 00:00:00 2001 From: Dat Date: Mon, 29 Dec 2025 15:09:45 +0100 Subject: [PATCH 2/7] add comment on booted() hook --- app/TermsOfUseVersion.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/TermsOfUseVersion.php b/app/TermsOfUseVersion.php index c99e7e27..6849320a 100644 --- a/app/TermsOfUseVersion.php +++ b/app/TermsOfUseVersion.php @@ -36,7 +36,11 @@ public static function latestActiveVersion(): ?self { return self::query()->where('active', true)->latest()->first(); } + /** + * Ensure only one ToU version remains active after any save operation. + */ protected static function booted(): void { + static::saving(function (self $model): void { if (! $model->active) { return; From a630e3a91dff0103a17c65aaaf6559d12784a475 Mon Sep 17 00:00:00 2001 From: Dat Date: Mon, 29 Dec 2025 15:13:21 +0100 Subject: [PATCH 3/7] fix linting error --- app/TermsOfUseVersion.php | 5 ++++- app/UserTermsOfUseAcceptance.php | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/app/TermsOfUseVersion.php b/app/TermsOfUseVersion.php index 6849320a..9bdf720d 100644 --- a/app/TermsOfUseVersion.php +++ b/app/TermsOfUseVersion.php @@ -14,8 +14,11 @@ class TermsOfUseVersion extends Model { use HasFactory; protected $primaryKey = 'version'; + protected $keyType = 'string'; + protected $table = 'tou_versions'; + public $incrementing = false; const FIELDS = [ @@ -42,7 +45,7 @@ public static function latestActiveVersion(): ?self { protected static function booted(): void { static::saving(function (self $model): void { - if (! $model->active) { + if (!$model->active) { return; } diff --git a/app/UserTermsOfUseAcceptance.php b/app/UserTermsOfUseAcceptance.php index 9a891870..01827096 100644 --- a/app/UserTermsOfUseAcceptance.php +++ b/app/UserTermsOfUseAcceptance.php @@ -10,7 +10,9 @@ class UserTermsOfUseAcceptance extends Model { use HasFactory; public $incrementing = false; + protected $primaryKey = null; + protected $keyType = 'string'; public const FIELDS = [ From 3754c00cdc847088e6409a990e5176e1d9b8f2a1 Mon Sep 17 00:00:00 2001 From: Dat Date: Mon, 29 Dec 2025 15:22:25 +0100 Subject: [PATCH 4/7] Tied tou_acceptances and tou_versions with a foreign key --- app/UserTermsOfUseAcceptance.php | 12 ------------ .../migrations/2025_09_29_194758_tou_acceptances.php | 6 ++++++ 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/app/UserTermsOfUseAcceptance.php b/app/UserTermsOfUseAcceptance.php index 01827096..3a13bcf9 100644 --- a/app/UserTermsOfUseAcceptance.php +++ b/app/UserTermsOfUseAcceptance.php @@ -9,12 +9,6 @@ class UserTermsOfUseAcceptance extends Model { use HasFactory; - public $incrementing = false; - - protected $primaryKey = null; - - protected $keyType = 'string'; - public const FIELDS = [ 'user_id', 'tou_version', @@ -35,10 +29,4 @@ class UserTermsOfUseAcceptance extends Model { public function user(): BelongsTo { return $this->belongsTo(User::class); } - - protected function setKeysForSaveQuery($query) { - return $query - ->where('user_id', $this->getAttribute('user_id')) - ->where('tou_version', $this->getAttribute('tou_version')); - } } diff --git a/database/migrations/2025_09_29_194758_tou_acceptances.php b/database/migrations/2025_09_29_194758_tou_acceptances.php index 5ddd1137..57041849 100644 --- a/database/migrations/2025_09_29_194758_tou_acceptances.php +++ b/database/migrations/2025_09_29_194758_tou_acceptances.php @@ -10,12 +10,18 @@ */ public function up(): void { Schema::create('tou_acceptances', function (Blueprint $table) { + $table->id(); $table->unsignedInteger('user_id'); $table->string('tou_version'); $table->timestamp('tou_accepted_at'); $table->timestamps(); $table->unique(['user_id', 'tou_version']); $table->foreign('user_id')->references('id')->on('users')->cascadeOnDelete(); + $table->foreign('tou_version') + ->references('version') + ->on('tou_versions') + ->cascadeOnUpdate() + ->restrictOnDelete(); }); } From e1038765cf35423c4452db0c03404e758ec43a0a Mon Sep 17 00:00:00 2001 From: Dat Date: Mon, 29 Dec 2025 15:32:52 +0100 Subject: [PATCH 5/7] rename tou_acceptances migration so tou_versions run first --- ..._tou_acceptances.php => 2025_10_15_194758_tou_acceptances.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename database/migrations/{2025_09_29_194758_tou_acceptances.php => 2025_10_15_194758_tou_acceptances.php} (100%) diff --git a/database/migrations/2025_09_29_194758_tou_acceptances.php b/database/migrations/2025_10_15_194758_tou_acceptances.php similarity index 100% rename from database/migrations/2025_09_29_194758_tou_acceptances.php rename to database/migrations/2025_10_15_194758_tou_acceptances.php From 28de5d50d32df8a0b2f0f6c1ebf405a9d111b7f9 Mon Sep 17 00:00:00 2001 From: Dat Date: Tue, 6 Jan 2026 14:15:01 +0100 Subject: [PATCH 6/7] Create new migrations, delete old ones --- ..._091126_tou_versions.php => 2026_01_06_131233_tou_version.php} | 0 ..._tou_acceptances.php => 2026_01_06_131319_tou_acceptances.php} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename database/migrations/{2025_10_14_091126_tou_versions.php => 2026_01_06_131233_tou_version.php} (100%) rename database/migrations/{2025_10_15_194758_tou_acceptances.php => 2026_01_06_131319_tou_acceptances.php} (100%) diff --git a/database/migrations/2025_10_14_091126_tou_versions.php b/database/migrations/2026_01_06_131233_tou_version.php similarity index 100% rename from database/migrations/2025_10_14_091126_tou_versions.php rename to database/migrations/2026_01_06_131233_tou_version.php diff --git a/database/migrations/2025_10_15_194758_tou_acceptances.php b/database/migrations/2026_01_06_131319_tou_acceptances.php similarity index 100% rename from database/migrations/2025_10_15_194758_tou_acceptances.php rename to database/migrations/2026_01_06_131319_tou_acceptances.php From 61a7904ad09753b8926316b30b7dc27b21ee9fb4 Mon Sep 17 00:00:00 2001 From: Dat Date: Tue, 6 Jan 2026 16:11:55 +0100 Subject: [PATCH 7/7] remove unnecessary booted() method --- app/TermsOfUseVersion.php | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/app/TermsOfUseVersion.php b/app/TermsOfUseVersion.php index 9bdf720d..1d40439c 100644 --- a/app/TermsOfUseVersion.php +++ b/app/TermsOfUseVersion.php @@ -38,21 +38,4 @@ class TermsOfUseVersion extends Model { public static function latestActiveVersion(): ?self { return self::query()->where('active', true)->latest()->first(); } - - /** - * Ensure only one ToU version remains active after any save operation. - */ - protected static function booted(): void { - - static::saving(function (self $model): void { - if (!$model->active) { - return; - } - - self::query() - ->where('version', '!=', $model->version) - ->where('active', true) - ->update(['active' => false]); - }); - } }