2026-10-27 12:04:20 +07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Database\Seeders;
|
|
|
|
|
|
|
|
|
|
use App\Models\Gate;
|
|
|
|
|
use App\Models\SecurityGuard;
|
2026-10-27 13:08:44 +07:00
|
|
|
use App\Models\User;
|
2026-10-27 12:04:20 +07:00
|
|
|
use Illuminate\Database\Seeder;
|
|
|
|
|
use Illuminate\Support\Facades\File;
|
2026-10-27 13:08:44 +07:00
|
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
|
use Spatie\Permission\Models\Role;
|
2026-10-27 12:04:20 +07:00
|
|
|
|
|
|
|
|
class SecurityGuardSeeder extends Seeder
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Run the database seeds.
|
2026-10-27 13:08:44 +07:00
|
|
|
* Mengimpor data personil satpam lapangan resmi dari Plotingan Jaga Perpos
|
|
|
|
|
* sekaligus membuatkan akun login User (role: gate_officer) dengan password default 123456.
|
2026-10-27 12:04:20 +07:00
|
|
|
*/
|
|
|
|
|
public function run(): void
|
|
|
|
|
{
|
|
|
|
|
$jsonPath = database_path('data/security_guards_plotingan.json');
|
|
|
|
|
|
|
|
|
|
if (!File::exists($jsonPath)) {
|
|
|
|
|
$this->command?->error("File data katalog satpam tidak ditemukan: {$jsonPath}");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data = json_decode(File::get($jsonPath), true);
|
|
|
|
|
$personnel = $data['personnel'] ?? [];
|
|
|
|
|
|
2026-10-27 13:08:44 +07:00
|
|
|
$this->command?->info("Memulai sinkronisasi " . count($personnel) . " personil Satpam beserta akun login...");
|
|
|
|
|
|
|
|
|
|
$gateOfficerRole = Role::firstOrCreate(['name' => 'gate_officer', 'guard_name' => 'web']);
|
|
|
|
|
$defaultPasswordHash = Hash::make('123456');
|
2026-10-27 12:04:20 +07:00
|
|
|
|
|
|
|
|
$gateCache = [];
|
2026-10-27 13:08:44 +07:00
|
|
|
$insertedGuards = 0;
|
|
|
|
|
$updatedGuards = 0;
|
|
|
|
|
$usersCreated = 0;
|
2026-10-27 12:04:20 +07:00
|
|
|
|
|
|
|
|
foreach ($personnel as $p) {
|
|
|
|
|
$gateCode = $p['gate_code'];
|
|
|
|
|
if (!isset($gateCache[$gateCode])) {
|
|
|
|
|
$gate = Gate::firstOrCreate(
|
|
|
|
|
['code' => $gateCode],
|
|
|
|
|
['name' => $p['gate_name'], 'is_active' => true]
|
|
|
|
|
);
|
|
|
|
|
$gateCache[$gateCode] = $gate->id;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-27 13:08:44 +07:00
|
|
|
// 1. Buat / Ambil Akun Login User untuk Satpam
|
|
|
|
|
$username = strtoupper(trim($p['badge_id']));
|
|
|
|
|
$user = User::firstOrCreate(
|
|
|
|
|
['username' => $username],
|
|
|
|
|
[
|
|
|
|
|
'name' => $p['name'],
|
|
|
|
|
'email' => strtolower($username) . '@kmh.co.id',
|
|
|
|
|
'password' => $defaultPasswordHash,
|
|
|
|
|
'role' => 'gate_officer',
|
|
|
|
|
'gate_id' => $gateCache[$gateCode],
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($user->wasRecentlyCreated) {
|
|
|
|
|
$usersCreated++;
|
|
|
|
|
if (!$user->hasRole('gate_officer')) {
|
|
|
|
|
$user->assignRole($gateOfficerRole);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2. Simpan Data Satpam
|
2026-10-27 12:04:20 +07:00
|
|
|
$guard = SecurityGuard::updateOrCreate(
|
|
|
|
|
['badge_id' => $p['badge_id']],
|
|
|
|
|
[
|
|
|
|
|
'name' => $p['name'],
|
|
|
|
|
'team' => $p['team'],
|
|
|
|
|
'gate_id' => $gateCache[$gateCode],
|
2026-10-27 13:08:44 +07:00
|
|
|
'user_id' => $user->id,
|
2026-10-27 12:04:20 +07:00
|
|
|
'is_active' => $p['is_active'] ?? true,
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($guard->wasRecentlyCreated) {
|
2026-10-27 13:08:44 +07:00
|
|
|
$insertedGuards++;
|
2026-10-27 12:04:20 +07:00
|
|
|
} else {
|
2026-10-27 13:08:44 +07:00
|
|
|
$updatedGuards++;
|
2026-10-27 12:04:20 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-27 13:08:44 +07:00
|
|
|
$this->command?->info("Selesai! {$insertedGuards} satpam baru, {$updatedGuards} satpam diperbarui, {$usersCreated} akun login user dibuat (password default: 123456).");
|
2026-10-27 12:04:20 +07:00
|
|
|
}
|
|
|
|
|
}
|