diff --git a/app/Http/Controllers/OrderShipmentController.php b/app/Http/Controllers/OrderShipmentController.php index 0436aff..282a639 100644 --- a/app/Http/Controllers/OrderShipmentController.php +++ b/app/Http/Controllers/OrderShipmentController.php @@ -4,7 +4,9 @@ use App\Mail\OrderShipped; use App\Models\Order; +use App\Notifications\OrderShipmentNotification; use Illuminate\Support\Facades\Mail; +use Illuminate\Support\Facades\Notification; class OrderShipmentController extends Controller { @@ -37,4 +39,14 @@ public function shipOrderAdvanced(Order $order) return ['message' => 'Your order has been shipped.']; } + + public function notifyForOrderShipment(Order $order) + { + $order->status = 'shipped'; + $order->save(); + + Notification::send(auth()->user(), new OrderShipmentNotification($order)); + + return ['message' => 'Your order has been shipped.']; + } } diff --git a/app/Notifications/OrderShipmentNotification.php b/app/Notifications/OrderShipmentNotification.php new file mode 100644 index 0000000..e164b59 --- /dev/null +++ b/app/Notifications/OrderShipmentNotification.php @@ -0,0 +1,71 @@ +order = $order; + } + + /** + * Get the notification's delivery channels. + * + * @param mixed $notifiable + * @return array + */ + public function via($notifiable) + { + // $notifiable is $user in this case. + + return ['mail']; + } + + /** + * Get the mail representation of the notification. + * + * @param mixed $notifiable + * @return \Illuminate\Notifications\Messages\MailMessage + */ + public function toMail($notifiable) + { + // $notifiable is $user in this case. + + $url = url('/orders/'.$this->order->id); + + return (new MailMessage) + ->greeting("Hello! {$notifiable->name}") + ->line('Your order has been shipped!') + ->action('View Order', $url) + ->line('Thank you for using our application!'); + } + + /** + * Get the array representation of the notification. + * + * @param mixed $notifiable + * @return array + */ + public function toArray($notifiable) + { + return [ + // + ]; + } +} diff --git a/routes/web.php b/routes/web.php index 88c689d..40cba8c 100644 --- a/routes/web.php +++ b/routes/web.php @@ -69,6 +69,9 @@ Route::post('orders/{order}/shipped/advanced', [OrderShipmentController::class, 'shipOrderAdvanced']) ->name('order.shipped.advanced'); + + Route::patch('orders/{order}/shipment/notification', [OrderShipmentController::class, 'notifyForOrderShipment']) + ->name('order.shipment.notification'); }); Route::prefix('orders')->group(function() { diff --git a/tests/Feature/NotificationTest.php b/tests/Feature/NotificationTest.php new file mode 100644 index 0000000..9b5e29a --- /dev/null +++ b/tests/Feature/NotificationTest.php @@ -0,0 +1,34 @@ +actingAs($user = User::factory()->create()); + + Notification::fake(); + + $this->patch(route('order.shipment.notification', $order = Order::factory()->create())); + + Notification::assertSentTo($user, OrderShipmentNotification::class, function ($notification) use ($user, $order) { + return $notification->order->id === $order->id && + in_array('mail', $notification->via($user)) && + ($notification->toMail($user) instanceof MailMessage); + }); + } +}