From 3cc13fb1ef34217ac03c11b429b7d27b6b7ea820 Mon Sep 17 00:00:00 2001 From: punkestu Date: Thu, 31 Oct 2024 16:01:58 +0700 Subject: [PATCH] create notification --- .../Controllers/NotificationController.php | 16 +++++++ .../Rotasi/PengajuanController.php | 2 +- .../Rotasi/SelektifAdminController.php | 27 +++++++++-- app/Models/Cabang.php | 13 +++++ app/Models/Notification.php | 35 ++++++++++++++ ...0_30_104942_create_notifications_table.php | 33 +++++++++++++ resources/views/components/header.blade.php | 8 ++++ resources/views/notification/index.blade.php | 47 +++++++++++++++++++ routes/web.php | 6 ++- 9 files changed, 179 insertions(+), 8 deletions(-) create mode 100644 app/Http/Controllers/NotificationController.php create mode 100644 app/Models/Notification.php create mode 100644 database/migrations/2024_10_30_104942_create_notifications_table.php create mode 100644 resources/views/notification/index.blade.php diff --git a/app/Http/Controllers/NotificationController.php b/app/Http/Controllers/NotificationController.php new file mode 100644 index 0000000..d1184eb --- /dev/null +++ b/app/Http/Controllers/NotificationController.php @@ -0,0 +1,16 @@ +user()->profile->cabang->notifications; + Notification::where('to', auth()->user()->profile->cabang->id)->update(['is_read' => true]); + return view('notification.index', compact('notifications')); + } +} diff --git a/app/Http/Controllers/Rotasi/PengajuanController.php b/app/Http/Controllers/Rotasi/PengajuanController.php index c077e74..8e5114b 100644 --- a/app/Http/Controllers/Rotasi/PengajuanController.php +++ b/app/Http/Controllers/Rotasi/PengajuanController.php @@ -55,7 +55,7 @@ public function input(Request $request) if (!auth()->user()->profile->cabang_id) { return redirect()->back()->with('invalid', 'Anda tidak memiliki cabang')->withInput(); } - if ($request->lokasi_awal_id !== auth()->user()->profile->cabang_id) { + if ($request->lokasi_awal_id != auth()->user()->profile->cabang_id) { return redirect()->back()->with('invalid', 'Cabang asal tidak sesuai')->withInput(); } } diff --git a/app/Http/Controllers/Rotasi/SelektifAdminController.php b/app/Http/Controllers/Rotasi/SelektifAdminController.php index 0f34c4a..cbc8fc8 100644 --- a/app/Http/Controllers/Rotasi/SelektifAdminController.php +++ b/app/Http/Controllers/Rotasi/SelektifAdminController.php @@ -5,6 +5,7 @@ use Illuminate\Http\Request; use App\Http\Controllers\Controller; use App\Models\Cabang; +use App\Models\Notification; use App\Models\Pengajuan; use App\Models\Personel; use Carbon\Carbon; @@ -65,12 +66,30 @@ public function selektif($id, Request $request) $request->validate([ 'status' => 'required|in:dapat,tidak,diterima' ]); + DB::beginTransaction(); $pengajuan = Pengajuan::find($id); if (!$pengajuan) { return response()->json(['message' => 'Data not found'], 404); } + $notification = Notification::create([ + 'status' => $request->status, + 'to' => $pengajuan->lokasi_awal_id, + 'cabang_asal_id' => $pengajuan->lokasi_awal_id, + 'cabang_tujuan_id' => $pengajuan->lokasi_tujuan_id, + 'pengajuan_id' => $pengajuan->id + ]); + $notification->save(); + if ($pengajuan->lokasi_awal_id != $pengajuan->lokasi_tujuan_id) { + $notification = Notification::create([ + 'status' => $request->status, + 'to' => $pengajuan->lokasi_tujuan_id, + 'cabang_asal_id' => $pengajuan->lokasi_awal_id, + 'cabang_tujuan_id' => $pengajuan->lokasi_tujuan_id, + 'pengajuan_id' => $pengajuan->id + ]); + $notification->save(); + } if ($request->status == 'dapat') { - DB::beginTransaction(); $personel = Personel::where('nik', $pengajuan->nik)->first(); if (!$personel) { $personel = new Personel(); @@ -97,11 +116,11 @@ public function selektif($id, Request $request) $pengajuan->primary->status = 'tidak_dapat'; $pengajuan->primary->save(); } - if ($pengajuan->primary->secondary) { + if ($pengajuan->primary && $pengajuan->primary->secondary && $pengajuan->primary->secondary_id != $pengajuan->id) { $pengajuan->primary->secondary->status = 'tidak_dapat'; $pengajuan->primary->secondary->save(); } - if ($pengajuan->primary->th3) { + if ($pengajuan->primary && $pengajuan->primary->th3 && $pengajuan->primary->th3_id != $pengajuan->id) { $pengajuan->primary->th3->status = 'tidak_dapat'; $pengajuan->primary->th3->save(); } @@ -114,7 +133,6 @@ public function selektif($id, Request $request) 'keterangan' => 'required', 'rekomendasi' => 'required' ]); - DB::beginTransaction(); $pengajuan->status = 'tidak_dapat'; $pengajuan->keteranganPenolakan()->create([ 'tipe' => 'keterangan_penolakan', @@ -128,7 +146,6 @@ public function selektif($id, Request $request) DB::commit(); return redirect()->back()->with('success', 'Data berhasil diupdate'); } else if ($request->status == 'diterima') { - DB::beginTransaction(); $pengajuan->status = 'diterima'; if ($pengajuan->posisi_sekarang == "ACO") { $pengajuan->lokasiAwal->jumlah_personel_aco -= 1; diff --git a/app/Models/Cabang.php b/app/Models/Cabang.php index d44d9e5..c16d0f6 100644 --- a/app/Models/Cabang.php +++ b/app/Models/Cabang.php @@ -32,6 +32,19 @@ class Cabang extends Model 'frms_ats_system', ]; + public function notifications() + { + return $this->hasMany(Notification::class, 'to'); + } + + public function notreadnotifications() + { + return $this->hasMany( + Notification::class, + 'to' + )->where("is_read", false); + } + public function coord() { return $this->hasOne(CabangCoord::class); diff --git a/app/Models/Notification.php b/app/Models/Notification.php new file mode 100644 index 0000000..525406d --- /dev/null +++ b/app/Models/Notification.php @@ -0,0 +1,35 @@ +belongsTo(User::class); + } + + public function pengajuan() + { + return $this->belongsTo(Pengajuan::class); + } + + public function notread() + { + return $this->where("is_read", false)->get(); + } +} diff --git a/database/migrations/2024_10_30_104942_create_notifications_table.php b/database/migrations/2024_10_30_104942_create_notifications_table.php new file mode 100644 index 0000000..7a7a59f --- /dev/null +++ b/database/migrations/2024_10_30_104942_create_notifications_table.php @@ -0,0 +1,33 @@ +id(); + $table->string("status"); + $table->foreignId("to")->constrained("cabangs")->onDelete("cascade"); + $table->boolean("is_read")->default(false); + $table->foreignId("cabang_asal_id")->constrained("cabangs")->onDelete("cascade"); + $table->foreignId("cabang_tujuan_id")->constrained("cabangs")->onDelete("cascade"); + $table->foreignId("pengajuan_id")->constrained("pengajuans")->onDelete("cascade"); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('notifications'); + } +}; diff --git a/resources/views/components/header.blade.php b/resources/views/components/header.blade.php index cd51e1e..38d229e 100644 --- a/resources/views/components/header.blade.php +++ b/resources/views/components/header.blade.php @@ -17,6 +17,14 @@ class="max-h-0 duration-300 absolute flex flex-col gap-1 bg-white mt-4 text-gray
Selektif Admin @endcan + @auth +
+ Notifikasi + @if (auth()->user()->profile->cabang->notreadnotifications->count() > 0) +

+ @endif +
+ @endauth diff --git a/resources/views/notification/index.blade.php b/resources/views/notification/index.blade.php new file mode 100644 index 0000000..e354de2 --- /dev/null +++ b/resources/views/notification/index.blade.php @@ -0,0 +1,47 @@ + + + + + @include('components/head') + Air Mutasi | Profil + + + + @include('components/header', ['static' => true]) + @include('components.modal-component') +
+

Notifikasi

+
+ @forelse ($notifications as $notification) +
+
+ + + + +
+

{{ $notification->created_at->diffForHumans() }}

+
+ @empty +

Belum ada notifikasi

+ @endforelse +
+ @include('components.footer') + + + + + diff --git a/routes/web.php b/routes/web.php index 9240a93..a9fa666 100644 --- a/routes/web.php +++ b/routes/web.php @@ -4,6 +4,7 @@ use Illuminate\Support\Facades\Route; use App\Http\Controllers\AuthController; +use App\Http\Controllers\NotificationController; use App\Http\Controllers\PersonelController; // use App\Http\Controllers\Rotasi\HomeController as RotasiHomeController; use App\Http\Controllers\Rotasi\CabangController as RotasiCabangController; @@ -56,8 +57,8 @@ Route::group(['prefix' => 'personel', 'middleware' => [ 'auth:web' ]], function () { - Route::get("/", [PersonelController::class, 'index'])->name('personel'); - Route::post("/import", [PersonelController::class, 'importAllWithCsv'])->name('personel.import'); + Route::middleware(['is.admin'])->get("/", [PersonelController::class, 'index'])->name('personel'); + Route::middleware(['is.admin'])->post("/import", [PersonelController::class, 'importAllWithCsv'])->name('personel.import'); Route::get('/cabang/{id}', [PersonelController::class, 'cabang'])->name('personel.index'); Route::group(['prefix' => 'add', 'middleware' => [ 'is.admin' @@ -103,6 +104,7 @@ Route::get('/', [RotasiSelektifAdminController::class, 'index'])->name('rotasi.selektif'); Route::post("/{id}", [RotasiSelektifAdminController::class, 'selektif']); }); + Route::get("/notification", [NotificationController::class, 'index'])->name('rotasi.notifikasi'); }); Route::group(['prefix' => 'promosi', 'middleware' => [