Skip to content

Commit

Permalink
[8.x] Customize type column in database notifications using databaseT…
Browse files Browse the repository at this point in the history
…ype me… (#39811)

* Customize type column in database notifications using databaseType method.

* Update DatabaseChannel.php

* formatting

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
mvaliolahi and taylorotwell committed Nov 29, 2021
1 parent ab69133 commit a8f9d65
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 17 deletions.
36 changes: 19 additions & 17 deletions src/Illuminate/Notifications/Channels/DatabaseChannel.php
Expand Up @@ -21,6 +21,25 @@ public function send($notifiable, Notification $notification)
);
}

/**
* Build an array payload for the DatabaseNotification Model.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return array
*/
protected function buildPayload($notifiable, Notification $notification)
{
return [
'id' => $notification->id,
'type' => method_exists($notification, 'databaseType')
? $notification->databaseType($notifiable)
: get_class($notification),
'data' => $this->getData($notifiable, $notification),
'read_at' => null,
];
}

/**
* Get the data for the notification.
*
Expand All @@ -43,21 +62,4 @@ protected function getData($notifiable, Notification $notification)

throw new RuntimeException('Notification is missing toDatabase / toArray method.');
}

/**
* Build an array payload for the DatabaseNotification Model.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return array
*/
protected function buildPayload($notifiable, Notification $notification)
{
return [
'id' => $notification->id,
'type' => get_class($notification),
'data' => $this->getData($notifiable, $notification),
'read_at' => null,
];
}
}
31 changes: 31 additions & 0 deletions tests/Notifications/NotificationDatabaseChannelTest.php
Expand Up @@ -49,6 +49,24 @@ public function testCorrectPayloadIsSentToDatabase()
$channel = new ExtendedDatabaseChannel;
$channel->send($notifiable, $notification);
}

public function testCustomizeTypeIsSentToDatabase()
{
$notification = new NotificationDatabaseChannelCustomizeTypeTestNotification;
$notification->id = 1;
$notifiable = m::mock();

$notifiable->shouldReceive('routeNotificationFor->create')->with([
'id' => 1,
'type' => 'MONTHLY',
'data' => ['invoice_id' => 1],
'read_at' => null,
'something' => 'else',
]);

$channel = new ExtendedDatabaseChannel;
$channel->send($notifiable, $notification);
}
}

class NotificationDatabaseChannelTestNotification extends Notification
Expand All @@ -59,6 +77,19 @@ public function toDatabase($notifiable)
}
}

class NotificationDatabaseChannelCustomizeTypeTestNotification extends Notification
{
public function toDatabase($notifiable)
{
return new DatabaseMessage(['invoice_id' => 1]);
}

public function databaseType()
{
return 'MONTHLY';
}
}

class ExtendedDatabaseChannel extends DatabaseChannel
{
protected function buildPayload($notifiable, Notification $notification)
Expand Down

0 comments on commit a8f9d65

Please sign in to comment.