Update Status kelengkapan data
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Concerns;
|
||||
|
||||
use App\Enums\StatusDataPetani;
|
||||
use App\Models\Pertanian\Petani;
|
||||
use Closure;
|
||||
|
||||
/**
|
||||
* Data petani yang sudah ditandai "Lengkap" lalu diubah/ditambah/dihapus
|
||||
* otomatis kembali ke "Draf", supaya enumerator menandai lengkap lagi setelah selesai.
|
||||
*
|
||||
* Dipasang di semua model yang menyimpan data profil petani (web maupun API).
|
||||
* Model yang tidak punya kolom petani_id meng-override petaniIdUntukStatus().
|
||||
*/
|
||||
trait KembalikanStatusPetaniKeDraf
|
||||
{
|
||||
// Kolom otomatis/audit yang tidak dihitung sebagai perubahan data.
|
||||
private static array $kolomBukanPerubahanData = [
|
||||
'created_at', 'updated_at', 'deleted_at', 'created_by', 'updated_by', 'deleted_by', 'status',
|
||||
];
|
||||
|
||||
public static function bootKembalikanStatusPetaniKeDraf(): void
|
||||
{
|
||||
static::saved(function ($model) {
|
||||
$kolomBerubah = array_diff(array_keys($model->getChanges()), self::$kolomBukanPerubahanData);
|
||||
|
||||
if ($model->wasRecentlyCreated || $kolomBerubah) {
|
||||
$model->kembalikanStatusPetaniKeDraf();
|
||||
}
|
||||
});
|
||||
|
||||
static::deleted(fn ($model) => $model->kembalikanStatusPetaniKeDraf());
|
||||
}
|
||||
|
||||
protected function petaniIdUntukStatus(): ?int
|
||||
{
|
||||
return $this->petani_id;
|
||||
}
|
||||
|
||||
public function kembalikanStatusPetaniKeDraf(): void
|
||||
{
|
||||
$petaniId = $this->petaniIdUntukStatus();
|
||||
if (! $petaniId) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Satu simpan bisa memicu ratusan baris (mis. sinkron jenis hama ke semua tanaman).
|
||||
// Cukup sekali per petani per request: setelah dicek, status pasti bukan "Lengkap" lagi.
|
||||
self::ingatPerRequest('status_petani.dicek', $petaniId, function () use ($petaniId) {
|
||||
// Query langsung (tanpa event model) supaya tidak memicu hook lain / berulang.
|
||||
$diubah = Petani::withoutGlobalScopes()
|
||||
->whereKey($petaniId)
|
||||
->where('status', StatusDataPetani::LENGKAP->value)
|
||||
->update(['status' => StatusDataPetani::DRAFT->value]);
|
||||
|
||||
if ($diubah && request()->hasSession()) {
|
||||
request()->session()->flash('status_petani_info', 'Data petani diubah, status kembali ke Draf. Tandai lengkap lagi setelah selesai.');
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Simpan hasil $ambil() selama satu request web, per $kunci + $id.
|
||||
* Di console/queue tidak disimpan (proses berjalan lama, data bisa berubah antar-job).
|
||||
*/
|
||||
protected static function ingatPerRequest(string $kunci, int|string|null $id, Closure $ambil): mixed
|
||||
{
|
||||
if ($id === null || app()->runningInConsole()) {
|
||||
return $ambil();
|
||||
}
|
||||
|
||||
$bag = request()->attributes;
|
||||
$cache = $bag->get($kunci, []);
|
||||
|
||||
if (! array_key_exists($id, $cache)) {
|
||||
$cache[$id] = $ambil();
|
||||
$bag->set($kunci, $cache);
|
||||
}
|
||||
|
||||
return $cache[$id];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user