diff --git a/app/Models/PermohonanHistory.php b/app/Models/PermohonanHistory.php new file mode 100644 index 0000000..2b58a8f --- /dev/null +++ b/app/Models/PermohonanHistory.php @@ -0,0 +1,31 @@ +belongsTo(Permohonan::class); + } + + public function user() + { + return $this->belongsTo(User::class); + } +} diff --git a/app/Services/PermohonanHistoryService.php b/app/Services/PermohonanHistoryService.php new file mode 100644 index 0000000..783d41b --- /dev/null +++ b/app/Services/PermohonanHistoryService.php @@ -0,0 +1,43 @@ +store('permohonan_history_files', 'public'); + } + + + try { + $history = PermohonanHistory::create([ + 'permohonan_id' => $permohonan->id, + 'status' => $status, + 'keterangan' => $keterangan, + 'before_request' => json_encode($beforeRequest), + 'after_request' => json_encode($afterRequest), + 'file_path' => $filePath, + 'user_id' => auth()->id(), + ]); + + } catch (\Exception $e) { + // Log the error + \Log::error('Error creating PermohonanHistory: ' . $e->getMessage()); + + // You might want to delete the uploaded file if the database operation fails + if ($filePath) { + \Storage::disk('public')->delete($filePath); + } + + // Rethrow the exception or handle it as per your application's error handling policy + throw new \Exception('Failed to create PermohonanHistory: ' . $e->getMessage()); + } + } +} diff --git a/database/migrations/2024_10_31_032940_create_penilai_team_table.php b/database/migrations/2024_11_08_081149_create_permohonan_histories_table.php similarity index 50% rename from database/migrations/2024_10_31_032940_create_penilai_team_table.php rename to database/migrations/2024_11_08_081149_create_permohonan_histories_table.php index db287b2..7f55b5d 100644 --- a/database/migrations/2024_10_31_032940_create_penilai_team_table.php +++ b/database/migrations/2024_11_08_081149_create_permohonan_histories_table.php @@ -4,20 +4,22 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class () extends Migration { +return new class extends Migration +{ /** * Run the migrations. */ public function up(): void { - Schema::create('penilaian_team', function (Blueprint $table) { + Schema::create('permohonan_histories', function (Blueprint $table) { $table->id(); - $table->unsignedBigInteger('penilaian_id'); - $table->unsignedBigInteger('team_id'); - $table->unsignedBigInteger('user_id'); - $table->string('role'); - $table->boolean('status')->default(true); - $table->char('authorized_status', 1)->nullable(); + $table->unsignedBigInteger('permohonan_id'); + $table->string('status'); + $table->text('keterangan')->nullable(); + $table->json('before_request')->nullable(); + $table->json('after_request')->nullable(); + $table->string('file_path')->nullable(); + $table->unsignedBigInteger('user_id')->nullable(); $table->timestamps(); $table->timestamp('authorized_at')->nullable(); $table->unsignedBigInteger('authorized_by')->nullable(); @@ -25,6 +27,9 @@ return new class () extends Migration { $table->unsignedBigInteger('created_by')->nullable(); $table->unsignedBigInteger('updated_by')->nullable(); $table->unsignedBigInteger('deleted_by')->nullable(); + + $table->foreign('permohonan_id')->references('id')->on('permohonan')->onDelete('cascade'); + $table->foreign('user_id')->references('id')->on('users')->onDelete('set null'); }); } @@ -33,6 +38,6 @@ return new class () extends Migration { */ public function down(): void { - Schema::dropIfExists('penilai_team'); + Schema::dropIfExists('permohonan_histories'); } };