Skip to content

Commit

Permalink
create notification
Browse files Browse the repository at this point in the history
  • Loading branch information
punkestu committed Oct 31, 2024
1 parent a147050 commit 3cc13fb
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 8 deletions.
16 changes: 16 additions & 0 deletions app/Http/Controllers/NotificationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Http\Controllers;

use App\Models\Notification;
use Illuminate\Http\Request;

class NotificationController extends Controller
{
public function index()
{
$notifications = auth()->user()->profile->cabang->notifications;
Notification::where('to', auth()->user()->profile->cabang->id)->update(['is_read' => true]);
return view('notification.index', compact('notifications'));
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/Rotasi/PengajuanController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
27 changes: 22 additions & 5 deletions app/Http/Controllers/Rotasi/SelektifAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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();
}
Expand All @@ -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',
Expand All @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions app/Models/Cabang.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
35 changes: 35 additions & 0 deletions app/Models/Notification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Notification extends Model
{
use HasFactory;

protected $fillable = [
"is_read",
"status",
"to",
"cabang_asal_id",
"cabang_tujuan_id",
"pengajuan_id",
];

public function user()
{
return $this->belongsTo(User::class);
}

public function pengajuan()
{
return $this->belongsTo(Pengajuan::class);
}

public function notread()
{
return $this->where("is_read", false)->get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('notifications', function (Blueprint $table) {
$table->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');
}
};
8 changes: 8 additions & 0 deletions resources/views/components/header.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ class="max-h-0 duration-300 absolute flex flex-col gap-1 bg-white mt-4 text-gray
<hr>
<a href="/rotasi/selektif">Selektif Admin</a>
@endcan
@auth
<hr>
<a href="/rotasi/notification" class="flex">Notifikasi
@if (auth()->user()->profile->cabang->notreadnotifications->count() > 0)
<p class="bg-blue-500 w-2 h-2 rounded-full"></p>
@endif
</a>
@endauth
</div>
</div>
</div>
Expand Down
47 changes: 47 additions & 0 deletions resources/views/notification/index.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">

<head>
@include('components/head')
<title>Air Mutasi | Profil</title>
</head>

<body class="font-sans tracking-wider">
@include('components/header', ['static' => true])
@include('components.modal-component')
<main class="px-8 py-16">
<h1 class="text-3xl font-semibold mb-2">Notifikasi</h1>
<section class="flex flex-col gap-4 min-h-[50vh]">
@forelse ($notifications as $notification)
<div class="flex flex-col gap-2 p-4 bg-[#003285] text-white rounded-md">
<div class="flex flex-wrap gap-6">
<aside>
<p>Status:</p>
<strong>{{ $notification->status == 'dapat' ? 'Diterima' : ($notification->status == 'tidak' ? 'Tidak Diterima' : 'Disetujui') }}</strong>
</aside>
<aside>
<p>Atas nama:</p>
<strong>{{ $notification->pengajuan->nama_lengkap }}</strong>
</aside>
<aside>
<p>Dari:</p>
<strong>{{ $notification->pengajuan->lokasiAwal->nama }}
({{ $notification->pengajuan->posisi_sekarang }})</strong>
</aside>
<aside>
<p>Ke:</p>
<strong>{{ $notification->pengajuan->lokasiTujuan->nama }} ({{ $notification->pengajuan->posisi_tujuan }})</strong>
</aside>
</div>
<p class="text-sm opacity-50 self-end">{{ $notification->created_at->diffForHumans() }}</p>
</div>
@empty
<p>Belum ada notifikasi</p>
@endforelse
</main>
@include('components.footer')
<script src="/script/nav.js"></script>
<script src="/script/chatbot.js"></script>
</body>

</html>
6 changes: 4 additions & 2 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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' => [
Expand Down

0 comments on commit 3cc13fb

Please sign in to comment.