Skip to content

Commit 6a3589f

Browse files
committed
Refactor UserTermsOfUseAcceptance and TermsOfUseVersion Models
Bug: T407722
1 parent 9956e88 commit 6a3589f

4 files changed

Lines changed: 27 additions & 3 deletions

File tree

app/TermsOfUseVersion.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
class TermsOfUseVersion extends Model {
1414
use HasFactory;
1515

16+
protected $primaryKey = 'version';
17+
protected $keyType = 'string';
1618
protected $table = 'tou_versions';
19+
public $incrementing = false;
1720

1821
const FIELDS = [
1922
'version',
@@ -32,4 +35,17 @@ class TermsOfUseVersion extends Model {
3235
public static function latestActiveVersion(): ?self {
3336
return self::query()->where('active', true)->latest()->first();
3437
}
38+
39+
protected static function booted(): void {
40+
static::saving(function (self $model): void {
41+
if (! $model->active) {
42+
return;
43+
}
44+
45+
self::query()
46+
->where('version', '!=', $model->version)
47+
->where('active', true)
48+
->update(['active' => false]);
49+
});
50+
}
3551
}

app/UserTermsOfUseAcceptance.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
class UserTermsOfUseAcceptance extends Model {
1010
use HasFactory;
1111

12+
public $incrementing = false;
13+
protected $primaryKey = null;
14+
protected $keyType = 'string';
15+
1216
public const FIELDS = [
1317
'user_id',
1418
'tou_version',
@@ -29,4 +33,10 @@ class UserTermsOfUseAcceptance extends Model {
2933
public function user(): BelongsTo {
3034
return $this->belongsTo(User::class);
3135
}
36+
37+
protected function setKeysForSaveQuery($query) {
38+
return $query
39+
->where('user_id', $this->getAttribute('user_id'))
40+
->where('tou_version', $this->getAttribute('tou_version'));
41+
}
3242
}

database/migrations/2025_09_29_194758_tou_acceptances.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
*/
1111
public function up(): void {
1212
Schema::create('tou_acceptances', function (Blueprint $table) {
13-
$table->id();
1413
$table->unsignedInteger('user_id');
15-
$table->string('tou_version', 10);
14+
$table->string('tou_version');
1615
$table->timestamp('tou_accepted_at');
1716
$table->timestamps();
1817
$table->unique(['user_id', 'tou_version']);

database/migrations/2025_10_14_091126_tou_versions.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
*/
1111
public function up(): void {
1212
Schema::create('tou_versions', function (Blueprint $table) {
13-
$table->id();
1413
$table->string('version')->unique();
1514
$table->boolean('active')->default(false);
1615
$table->timestamps();

0 commit comments

Comments
 (0)