From 5fa094481132981b8a5efe52531d781cf36f01c2 Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Sun, 16 Nov 2025 15:33:51 +0700 Subject: [PATCH] feat(push notification): menjadikan notificakasi menggunakan job dan dispatch di beberapa komponen yang membuatuhkan --- app/Jobs/StorePushNotification.php | 50 +++++++++++++++++++ .../Forms/Studio/Finance/PayrollForm.php | 16 +++++- .../Forms/Studio/Manage/ArticleForm.php | 4 +- .../Forms/Studio/Manage/OrderForm.php | 7 ++- .../Studio/Catalog/Perfume/Create.php | 10 ++++ app/Livewire/Studio/Finance/Payroll.php | 12 ++++- .../Studio/Loyalty/Voucher/Create.php | 10 ++++ app/Livewire/Studio/Manage/Article/Create.php | 12 ++++- app/Livewire/Studio/Manage/Order/Create.php | 13 +++++ app/Livewire/Studio/Master/Formula.php | 30 ----------- 10 files changed, 127 insertions(+), 37 deletions(-) create mode 100644 app/Jobs/StorePushNotification.php diff --git a/app/Jobs/StorePushNotification.php b/app/Jobs/StorePushNotification.php new file mode 100644 index 0000000..ab453a9 --- /dev/null +++ b/app/Jobs/StorePushNotification.php @@ -0,0 +1,50 @@ + [ + 'subject' => config('app.url'), + 'publicKey' => config('services.vapid.public_key'), + 'privateKey' => config('services.vapid.private_key'), + ], + + ]; + + $webPush = new WebPush($auth); + + $payload = json_encode($this->payload); + + foreach ($this->notificationQuery as $notification) { + $webPush->sendOneNotification( + Subscription::create(json_decode($notification->subscriptions, true)), + $payload, + ['TTL' => 5000], + ); + } + } +} diff --git a/app/Livewire/Forms/Studio/Finance/PayrollForm.php b/app/Livewire/Forms/Studio/Finance/PayrollForm.php index 19618f3..8a37bee 100644 --- a/app/Livewire/Forms/Studio/Finance/PayrollForm.php +++ b/app/Livewire/Forms/Studio/Finance/PayrollForm.php @@ -49,7 +49,7 @@ public function store() $amount = (int) replaceCurrency($this->amount); - DB::transaction(function () use ($amount) { + DB::transaction(function () use ($amount, &$adjustment) { foreach ($this->user_ids as $user_id) { $periodMonth = Carbon::now()->format('Y-m'); @@ -61,7 +61,7 @@ public function store() throw new \Exception('Penggajian tidak ditemukan.'); } - PayrollAdjustment::create([ + $adjustment = PayrollAdjustment::create([ 'payroll_id' => $payroll->id, 'type' => $this->type, 'description' => $this->description, @@ -79,6 +79,18 @@ public function store() $payroll->save(); } }); + + if ($adjustment->type == SalaryAdjustmentType::BONUS->value) { + return [ + 'userIds' => $this->user_ids, + 'message' => 'Horee, Anda mendapatkan bonus sebesar '.currency($amount, 'Rp').' untuk bulan ini, Lihat detailnya di menu Penggajian.', + ]; + } elseif ($adjustment->type == SalaryAdjustmentType::DEDUCTION->value) { + return [ + 'userIds' => $this->user_ids, + 'message' => 'Yahh, gaji Anda dipotong sebesar '.currency($amount, 'Rp').' untuk bulan ini, Lihat detailnya di menu Penggajian.', + ]; + } } public function update() diff --git a/app/Livewire/Forms/Studio/Manage/ArticleForm.php b/app/Livewire/Forms/Studio/Manage/ArticleForm.php index 184d4c9..1eaef66 100644 --- a/app/Livewire/Forms/Studio/Manage/ArticleForm.php +++ b/app/Livewire/Forms/Studio/Manage/ArticleForm.php @@ -60,7 +60,7 @@ public function store() { $this->validate(); - DB::transaction(function () { + DB::transaction(function () use (&$article) { $article = Article::create([ 'author_id' => auth()->id(), 'title' => $this->title, @@ -72,6 +72,8 @@ public function store() $this->uploadMedia($this->thumbnail, $article, 'thumbnail'); }); + + return $article; } public function update() diff --git a/app/Livewire/Forms/Studio/Manage/OrderForm.php b/app/Livewire/Forms/Studio/Manage/OrderForm.php index e453877..cbc56b5 100644 --- a/app/Livewire/Forms/Studio/Manage/OrderForm.php +++ b/app/Livewire/Forms/Studio/Manage/OrderForm.php @@ -116,7 +116,7 @@ public function store() // Wrap the entire operation in a database transaction // Ensures atomicity — if any step fails, all changes are rolled back try { - DB::transaction(function () use ($invoiceNumber, $cogs, $subtotal, $discount, $total, $items, $pointsEarned) { + DB::transaction(function () use (&$order, $invoiceNumber, $cogs, $subtotal, $discount, $total, $items, $pointsEarned) { $member = User::whereHas('customer', fn ($q) => $q->where('id', $this->member_id)) ->with(['membership' => fn ($q) => $q->lockForUpdate()]) ->first(); @@ -201,6 +201,9 @@ public function store() ]; } - return ['status' => true]; + return [ + 'status' => true, + 'data' => $order, + ]; } } diff --git a/app/Livewire/Studio/Catalog/Perfume/Create.php b/app/Livewire/Studio/Catalog/Perfume/Create.php index 697b37c..40e3550 100644 --- a/app/Livewire/Studio/Catalog/Perfume/Create.php +++ b/app/Livewire/Studio/Catalog/Perfume/Create.php @@ -2,10 +2,12 @@ namespace App\Livewire\Studio\Catalog\Perfume; +use App\Jobs\StorePushNotification; use App\Livewire\Forms\Studio\Catalog\PerfumeForm; use App\Models\Brand; use App\Models\Category; use App\Models\Outlet; +use App\Models\PushNotification; use App\Traits\WithAuthorization; use App\Traits\WithCategorySelector; use App\Traits\WithOutletSelector; @@ -38,6 +40,14 @@ public function mount() public function save() { + StorePushNotification::dispatch( + PushNotification::all(), + [ + 'title' => 'Parfum', + 'body' => 'Ada yang baru nih! Aroma terbaru sudah siap kamu coba. Yuk intip dulu sebelum kehabisan.', + ], + ); + $this->canOrAbort('create perfume'); $this->form->store(); diff --git a/app/Livewire/Studio/Finance/Payroll.php b/app/Livewire/Studio/Finance/Payroll.php index eac0092..65fee8a 100644 --- a/app/Livewire/Studio/Finance/Payroll.php +++ b/app/Livewire/Studio/Finance/Payroll.php @@ -3,9 +3,11 @@ namespace App\Livewire\Studio\Finance; use App\Enums\SalaryAdjustmentType; +use App\Jobs\StorePushNotification; use App\Livewire\Forms\Studio\Finance\PayrollForm; use App\Models\Payroll as PayrollModel; use App\Models\PayrollAdjustment; +use App\Models\PushNotification; use App\Models\User; use App\Traits\Notification\WithSubscribeNotification; use App\Traits\WithAuthorization; @@ -69,7 +71,15 @@ public function create() { $this->canOrAbort('manage adjustment'); - $this->form->store(); + $result = $this->form->store(); + + StorePushNotification::dispatch( + PushNotification::where('user_id', $result['userIds'])->get(), + [ + 'title' => 'Order', + 'body' => $result['message'], + ], + ); $this->loadPayrolls(); diff --git a/app/Livewire/Studio/Loyalty/Voucher/Create.php b/app/Livewire/Studio/Loyalty/Voucher/Create.php index 8c606a6..c852d16 100644 --- a/app/Livewire/Studio/Loyalty/Voucher/Create.php +++ b/app/Livewire/Studio/Loyalty/Voucher/Create.php @@ -2,8 +2,10 @@ namespace App\Livewire\Studio\Loyalty\Voucher; +use App\Jobs\StorePushNotification; use App\Livewire\Forms\Studio\Loyalty\VoucherForm; use App\Models\Outlet; +use App\Models\PushNotification; use App\Traits\WithAuthorization; use App\Traits\WithOutletSelector; use App\Traits\WithToast; @@ -27,6 +29,14 @@ public function mount() public function save() { + StorePushNotification::dispatch( + PushNotification::all(), + [ + 'title' => 'Voucher', + 'body' => 'Ada voucher baru untuk kamu! Jangan sampai kelewatan, yuk cek sekarang dan nikmati penawarannya.', + ], + ); + $this->canOrAbort('create voucher'); $this->form->store(); diff --git a/app/Livewire/Studio/Manage/Article/Create.php b/app/Livewire/Studio/Manage/Article/Create.php index d1663f9..5d44297 100644 --- a/app/Livewire/Studio/Manage/Article/Create.php +++ b/app/Livewire/Studio/Manage/Article/Create.php @@ -2,7 +2,9 @@ namespace App\Livewire\Studio\Manage\Article; +use App\Jobs\StorePushNotification; use App\Livewire\Forms\Studio\Manage\ArticleForm; +use App\Models\PushNotification; use App\Traits\WithAuthorization; use App\Traits\WithToast; use App\Traits\WithUpdatedData; @@ -20,7 +22,15 @@ public function save() { $this->canOrAbort('create article'); - $this->form->store(); + $result = $this->form->store(); + + StorePushNotification::dispatch( + PushNotification::all(), + [ + 'title' => 'Artikel', + 'body' => $result->title, + ], + ); $this->dispatch('refreshDatatable'); diff --git a/app/Livewire/Studio/Manage/Order/Create.php b/app/Livewire/Studio/Manage/Order/Create.php index 48d378f..bb657d6 100644 --- a/app/Livewire/Studio/Manage/Order/Create.php +++ b/app/Livewire/Studio/Manage/Order/Create.php @@ -2,10 +2,13 @@ namespace App\Livewire\Studio\Manage\Order; +use App\Jobs\StorePushNotification; use App\Livewire\Forms\Studio\Manage\OrderForm; use App\Models\Bottle; use App\Models\Perfume; use App\Models\Product; +use App\Models\PushNotification; +use App\Models\User; use App\Traits\Order\WithAddItem; use App\Traits\Order\WithCalculateTotal; use App\Traits\Order\WithDiscount; @@ -87,6 +90,16 @@ public function save() return; } + $userIds = User::role(['Developer', 'Owner', 'Leader'])->pluck('id')->toArray(); + + StorePushNotification::dispatch( + PushNotification::whereIn('user_id', $userIds)->get(), + [ + 'title' => 'Order', + 'body' => 'Ada order baru masuk dengan nominal '.$result->total, + ], + ); + $this->dispatch('refreshDatatable'); $this->toast('Order berhasil ditambahkan.'); diff --git a/app/Livewire/Studio/Master/Formula.php b/app/Livewire/Studio/Master/Formula.php index aa979b4..6fb5763 100644 --- a/app/Livewire/Studio/Master/Formula.php +++ b/app/Livewire/Studio/Master/Formula.php @@ -5,7 +5,6 @@ use App\Livewire\Forms\Studio\Master\FormulaForm; use App\Models\Bottle; use App\Models\Formula as FormulaModel; -use App\Models\PushNotification; use App\Traits\Notification\WithSubscribeNotification; use App\Traits\WithAuthorization; use App\Traits\WithCloseModal; @@ -16,8 +15,6 @@ use Livewire\Attributes\On; use Livewire\Attributes\Title; use Livewire\Component; -use Minishlink\WebPush\Subscription; -use Minishlink\WebPush\WebPush; #[Title('Rumus')] class Formula extends Component @@ -97,8 +94,6 @@ public function create() public function update() { - $this->pushNotification(); - $exists = FormulaModel::where('quality', $this->form->quality) ->where('size', $this->form->size) ->where('id', '!=', $this->form->formula->id) @@ -155,31 +150,6 @@ public function delete(FormulaModel $formula) Flux::modals()->close(); } - protected function pushNotification() - { - $auth = [ - 'VAPID' => [ - 'subject' => config('app.url'), - 'publicKey' => config('services.vapid.public_key'), - 'privateKey' => config('services.vapid.private_key'), - ], - ]; - $webPush = new WebPush($auth); - - $payload = json_encode([ - 'title' => 'Penggajian', - 'body' => 'Penggajian baru telah ditambahkan.', - ]); - - foreach (PushNotification::all() as $notification) { - $webPush->sendOneNotification( - Subscription::create(json_decode($notification->subscriptions, true)), - $payload, - ['TTL' => 5000], - ); - } - } - public function render() { return view('livewire.studio.master.formulas', [