3434use Exception ;
3535use GuzzleHttp \Exception \ClientException ;
3636use Illuminate \Database \Eloquent \Model ;
37+ use Illuminate \Notifications \Notification as BaseNotification ;
3738use Illuminate \Support \Facades \Context ;
3839use Illuminate \Support \Facades \Log ;
3940use Illuminate \Support \Facades \Mail ;
@@ -126,12 +127,12 @@ public function onCheckedOut($event)
126127 if ($ shouldSendWebhookNotification ) {
127128 try {
128129 if ($ this ->newMicrosoftTeamsWebhookEnabled ()) {
129- $ message = $ this ->getCheckoutNotification ($ event )->toMicrosoftTeams ();
130+ $ message = $ this ->getCheckoutNotification ($ event, $ acceptance , true )->toMicrosoftTeams ();
130131 $ notification = new TeamsNotification (Setting::getSettings ()->webhook_endpoint );
131132 $ notification ->success ()->sendMessage ($ message [0 ], $ message [1 ]); // Send the message to Microsoft Teams
132133 } else {
133134 Notification::route ($ this ->webhookSelected (), Setting::getSettings ()->webhook_endpoint )
134- ->notify ($ this ->getCheckoutNotification ($ event , $ acceptance ));
135+ ->notify ($ this ->getCheckoutNotification ($ event , $ acceptance, true ));
135136 }
136137 } catch (ClientException $ e ) {
137138 $ status = $ e ->getResponse ()->getStatusCode ();
@@ -233,12 +234,12 @@ public function onCheckedIn($event)
233234 // Send Webhook notification
234235 try {
235236 if ($ this ->newMicrosoftTeamsWebhookEnabled ()) {
236- $ message = $ this ->getCheckinNotification ($ event )->toMicrosoftTeams ();
237+ $ message = $ this ->getCheckinNotification ($ event, true )->toMicrosoftTeams ();
237238 $ notification = new TeamsNotification (Setting::getSettings ()->webhook_endpoint );
238239 $ notification ->success ()->sendMessage ($ message [0 ], $ message [1 ]); // Send the message to Microsoft Teams
239240 } else {
240241 Notification::route ($ this ->webhookSelected (), Setting::getSettings ()->webhook_endpoint )
241- ->notify ($ this ->getCheckinNotification ($ event ));
242+ ->notify ($ this ->getCheckinNotification ($ event, true ));
242243 }
243244 } catch (ClientException $ e ) {
244245 $ status = $ e ->getResponse ()->getStatusCode ();
@@ -312,12 +313,12 @@ private function getCheckoutAcceptance($event)
312313 * @param CheckoutableCheckedIn $event
313314 * @return Notification
314315 */
315- private function getCheckinNotification ($ event)
316+ private function getCheckinNotification ($ event, bool $ refreshCheckoutable = false ): BaseNotification
316317 {
317-
318318 $ notificationClass = null ;
319+ $ checkoutable = $ this ->getCheckoutableForNotification ($ event ->checkoutable , $ refreshCheckoutable );
319320
320- switch (get_class ($ event -> checkoutable )) {
321+ switch (get_class ($ checkoutable )) {
321322 case Accessory::class:
322323 $ notificationClass = CheckinAccessoryNotification::class;
323324 break ;
@@ -334,7 +335,7 @@ private function getCheckinNotification($event)
334335
335336 Log::debug ('Notification class: ' .$ notificationClass );
336337
337- return new $ notificationClass ($ event -> checkoutable , $ event ->checkedOutTo , $ event ->checkedInBy , $ event ->note );
338+ return new $ notificationClass ($ checkoutable , $ event ->checkedOutTo , $ event ->checkedInBy , $ event ->note );
338339 }
339340
340341 /**
@@ -344,11 +345,12 @@ private function getCheckinNotification($event)
344345 * @param CheckoutAcceptance|null $acceptance
345346 * @return Notification
346347 */
347- private function getCheckoutNotification ($ event , $ acceptance = null )
348+ private function getCheckoutNotification ($ event , $ acceptance = null , bool $ refreshCheckoutable = false ): BaseNotification
348349 {
349350 $ notificationClass = null ;
351+ $ checkoutable = $ this ->getCheckoutableForNotification ($ event ->checkoutable , $ refreshCheckoutable );
350352
351- switch (get_class ($ event -> checkoutable )) {
353+ switch (get_class ($ checkoutable )) {
352354 case Accessory::class:
353355 $ notificationClass = CheckoutAccessoryNotification::class;
354356 break ;
@@ -366,7 +368,16 @@ private function getCheckoutNotification($event, $acceptance = null)
366368 break ;
367369 }
368370
369- return new $ notificationClass ($ event ->checkoutable , $ event ->checkedOutTo , $ event ->checkedOutBy , $ acceptance , $ event ->note );
371+ return new $ notificationClass ($ checkoutable , $ event ->checkedOutTo , $ event ->checkedOutBy , $ acceptance , $ event ->note );
372+ }
373+
374+ private function getCheckoutableForNotification (Model $ checkoutable , bool $ shouldRefresh ): Model
375+ {
376+ if (! $ shouldRefresh ) {
377+ return $ checkoutable ;
378+ }
379+
380+ return $ checkoutable ->fresh () ?? $ checkoutable ;
370381 }
371382
372383 private function getCheckoutMailType ($ event , $ acceptance )
0 commit comments