fix(customer): flush customer repository cache on model writes

Writes that bypass the cached repository (e.g. client preferences)
left stale customers in cache, so admin sending a marketing template
got 422 although the customer had opted back in.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
baodev0
2026-10-15 09:39:21 +07:00
co-authored by Claude Opus 5.5
parent ecd6f20a5b
commit 5de62b9083
+12
View File
@@ -2,11 +2,13 @@
namespace App\Models;
use App\Services\Cache\CacheServiceInterface;
use App\Traits\LogsActivity;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\DB;
use Tymon\JWTAuth\Contracts\JWTSubject;
class Customer extends Authenticatable implements JWTSubject
@@ -15,6 +17,16 @@ class Customer extends Authenticatable implements JWTSubject
protected $table = 'customers';
protected static function booted(): void
{
$flushRepositoryCache = fn () => DB::afterCommit(
fn () => app(CacheServiceInterface::class)->clearByModule('customer')
);
static::saved($flushRepositoryCache);
static::deleted($flushRepositoryCache);
static::restored($flushRepositoryCache);
}
/**
* Chỉ định cột sẽ nhận UUID.
* Mặc định HasUuids dùng cột 'id', mình đổi sang 'uuid'.