feat: move pelelong berlesen functions to operasi and update login and reports (#4)

Co-authored-by: nurafrinaalimi16 <nurafrinaalimi16@gmail.com>
Reviewed-on: #4
This commit was merged in pull request #4.
This commit is contained in:
2026-10-06 11:26:49 +08:00
parent 56ebb153b5
commit b1d393b1a1
37 changed files with 837 additions and 1318 deletions
@@ -0,0 +1,35 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use Spatie\Permission\Models\Role;
use Spatie\Permission\PermissionRegistrar;
class TransferAuctionControlsToOperasi extends Migration
{
public function up()
{
$this->transfer('jurulelong', 'operasi');
}
public function down()
{
$this->transfer('operasi', 'jurulelong');
}
private function transfer($from, $to)
{
DB::transaction(function () use ($from, $to) {
$source = Role::where('name', $from)->where('guard_name', 'web')->first();
$target = Role::findOrCreate($to, 'web');
foreach (['lelongan', 'janapemenang'] as $permission) {
$target->givePermissionTo($permission);
if ($source) {
$source->revokePermissionTo($permission);
}
}
});
app(PermissionRegistrar::class)->forgetCachedPermissions();
}
}
@@ -0,0 +1,84 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Spatie\Permission\PermissionRegistrar;
class MergeJurulelongRoleIntoOperasi extends Migration
{
private $backup = 'jurulelong_role_merge_backup';
public function up()
{
if (!Schema::hasTable($this->backup)) {
Schema::create($this->backup, function (Blueprint $table) {
$table->unsignedBigInteger('id')->primary();
$table->longText('payload');
});
}
DB::transaction(function () {
$tables = config('permission.table_names');
$key = config('permission.column_names.role_pivot_key') ?: 'role_id';
foreach (DB::table($tables['roles'])->where('name', 'jurulelong')->get() as $role) {
$target = DB::table($tables['roles'])->where('name', 'operasi')
->where('guard_name', $role->guard_name)->first();
$snapshot = ['role' => (array) $role, 'renamed' => !$target, 'rows' => [], 'added' => []];
if (!$target) {
DB::table($tables['roles'])->where('id', $role->id)->update(['name' => 'operasi']);
} else {
foreach (['model_has_roles', 'role_has_permissions'] as $pivot) {
$snapshot['rows'][$pivot] = [];
$snapshot['added'][$pivot] = [];
foreach (DB::table($tables[$pivot])->where($key, $role->id)->get() as $row) {
$original = (array) $row;
$snapshot['rows'][$pivot][] = $original;
$replacement = $original;
$replacement[$key] = $target->id;
if (!DB::table($tables[$pivot])->where($replacement)->exists()) {
DB::table($tables[$pivot])->insert($replacement);
$snapshot['added'][$pivot][] = $replacement;
}
}
DB::table($tables[$pivot])->where($key, $role->id)->delete();
}
DB::table($tables['roles'])->where('id', $role->id)->delete();
}
DB::table($this->backup)->insert([
'id' => $role->id,
'payload' => json_encode($snapshot, JSON_THROW_ON_ERROR),
]);
}
});
app(PermissionRegistrar::class)->forgetCachedPermissions();
}
public function down()
{
DB::transaction(function () {
$tables = config('permission.table_names');
foreach (DB::table($this->backup)->get() as $backup) {
$snapshot = json_decode($backup->payload, true, 512, JSON_THROW_ON_ERROR);
if ($snapshot['renamed']) {
DB::table($tables['roles'])->where('id', $backup->id)->update($snapshot['role']);
} else {
DB::table($tables['roles'])->insert($snapshot['role']);
foreach ($snapshot['rows'] as $pivot => $rows) {
foreach ($snapshot['added'][$pivot] as $row) {
DB::table($tables[$pivot])->where($row)->delete();
}
foreach ($rows as $row) {
DB::table($tables[$pivot])->insert($row);
}
}
}
}
});
Schema::dropIfExists($this->backup);
app(PermissionRegistrar::class)->forgetCachedPermissions();
}
}