Skip to content

Commit

Permalink
Use Laravel Db notification system model
Browse files Browse the repository at this point in the history
  • Loading branch information
rxcod9 committed May 4, 2022
1 parent 4c381b0 commit 13a1f08
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 168 deletions.
Empty file added database/factories/.gitkeep
Empty file.
72 changes: 0 additions & 72 deletions database/factories/NotificationFactory.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ class CreateNotificationsTable extends Migration
public function up()
{
Schema::create('notifications', function (Blueprint $table) {
$table->id();
$table->string('name')->nullable();
$table->text('description')->nullable();
$table->string('image')->nullable();
$table->uuid('id')->primary();
$table->string('type');
$table->morphs('notifiable');
$table->text('data');

$table->bigInteger('created_by_id')->unsigned()->nullable()->default(null);
$table->foreign('created_by_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('set null');
Expand All @@ -28,14 +28,17 @@ public function up()
$table->bigInteger('assigned_to_id')->unsigned()->nullable()->default(null);
$table->foreign('assigned_to_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('set null');

$table->bigInteger('parent_id')->unsigned()->nullable()->default(null);
$table->foreign('parent_id')->references('id')->on('notifications')->onUpdate('cascade')->onDelete('set null');
$table->uuid('parent_id')->nullable()->default(null);


$table->enum('status', ['ACTIVE', 'INACTIVE', 'EXPIRED'])->default('ACTIVE');

$table->timestamp('read_at')->nullable();
$table->timestamps();
});

Schema::table('notifications', function (Blueprint $table) {
$table->foreign('parent_id')->references('id')->on('notifications')->onUpdate('cascade')->onDelete('set null');
});
}

/**
Expand Down
55 changes: 35 additions & 20 deletions database/seeders/DataRowsTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function run()
$dataRow = $this->dataRow($notificationDataType, 'id');
if (!$dataRow->exists) {
$dataRow->fill([
'type' => 'number',
'type' => 'text',
'display_name' => __('joy-voyager-bread-notification::seeders.data_rows.id'),
'required' => 1,
'browse' => 0,
Expand All @@ -31,11 +31,11 @@ public function run()
])->save();
}

$dataRow = $this->dataRow($notificationDataType, 'name');
$dataRow = $this->dataRow($notificationDataType, 'type');
if (!$dataRow->exists) {
$dataRow->fill([
'type' => 'text',
'display_name' => __('joy-voyager-bread-notification::seeders.data_rows.name'),
'display_name' => __('joy-voyager-bread-notification::seeders.data_rows.type'),
'required' => 1,
'browse' => 1,
'read' => 1,
Expand All @@ -46,11 +46,11 @@ public function run()
])->save();
}

$dataRow = $this->dataRow($notificationDataType, 'description');
$dataRow = $this->dataRow($notificationDataType, 'notifiable_type');
if (!$dataRow->exists) {
$dataRow->fill([
'type' => 'text',
'display_name' => __('joy-voyager-bread-notification::seeders.data_rows.description'),
'display_name' => __('joy-voyager-bread-notification::seeders.data_rows.notifiable_type'),
'required' => 1,
'browse' => 1,
'read' => 1,
Expand All @@ -61,6 +61,36 @@ public function run()
])->save();
}

$dataRow = $this->dataRow($notificationDataType, 'notifiable_id');
if (!$dataRow->exists) {
$dataRow->fill([
'type' => 'text',
'display_name' => __('joy-voyager-bread-notification::seeders.data_rows.notifiable_id'),
'required' => 1,
'browse' => 1,
'read' => 1,
'edit' => 1,
'add' => 1,
'delete' => 1,
'order' => 4,
])->save();
}

$dataRow = $this->dataRow($notificationDataType, 'data');
if (!$dataRow->exists) {
$dataRow->fill([
'type' => 'code_editor',
'display_name' => __('joy-voyager-bread-notification::seeders.data_rows.data'),
'required' => 1,
'browse' => 1,
'read' => 1,
'edit' => 1,
'add' => 1,
'delete' => 1,
'order' => 5,
])->save();
}

$dataRow = $this->dataRow($notificationDataType, 'created_at');
if (!$dataRow->exists) {
$dataRow->fill([
Expand Down Expand Up @@ -91,21 +121,6 @@ public function run()
])->save();
}

$dataRow = $this->dataRow($notificationDataType, 'image');
if (!$dataRow->exists) {
$dataRow->fill([
'type' => 'image',
'display_name' => __('joy-voyager-bread-notification::seeders.data_rows.image'),
'required' => 0,
'browse' => 1,
'read' => 1,
'edit' => 1,
'add' => 1,
'delete' => 1,
'order' => 8,
])->save();
}

$dataRow = $this->dataRow($notificationDataType, 'notification_belongsto_created_by_relationship');
if (!$dataRow->exists) {
$dataRow->fill([
Expand Down
17 changes: 1 addition & 16 deletions database/seeders/DummyNotificationsTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,6 @@ class DummyNotificationsTableSeeder extends Seeder
*/
public function run()
{
$count = 20;
Notification::factory()
->count($count)
->state(function (array $attributes) use ($count) {
return [
'name' => 'Notification ' . time()
. ' ' . rand(1, $count)
. ' ' . rand(1, $count)
. ' ' . rand(1, $count),
'description' => 'Notification Description ' . time()
. ' ' . rand(1, $count)
. ' ' . rand(1, $count)
. ' ' . rand(1, $count)
];
})
->create();
//
}
}
17 changes: 1 addition & 16 deletions database/seeders/NotificationsTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,6 @@ public function run()
return false;
}

$count = 20;
Notification::factory()
->count($count)
->state(function (array $attributes) use ($count) {
return [
'name' => 'Notification ' . time()
. ' ' . rand(1, $count)
. ' ' . rand(1, $count)
. ' ' . rand(1, $count),
'description' => 'Notification Description ' . time()
. ' ' . rand(1, $count)
. ' ' . rand(1, $count)
. ' ' . rand(1, $count)
];
})
->create();
//
}
}
10 changes: 0 additions & 10 deletions database/seeders/TranslationsTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,6 @@ private function notificationsTranslations()
{
// Adding translations for 'notifications'
//
$cat = Notification::where('name', 'notification-1')->first();
if ($cat->exists) {
$this->trans('pt', $this->arr(['notifications', 'name'], $cat->id), 'notification-1');
$this->trans('pt', $this->arr(['notifications', 'description'], $cat->id), 'Notification 1');
}
$cat = Notification::where('name', 'notification-2')->first();
if ($cat->exists) {
$this->trans('pt', $this->arr(['notifications', 'name'], $cat->id), 'notification-2');
$this->trans('pt', $this->arr(['notifications', 'description'], $cat->id), 'Notification 2');
}
}

/**
Expand Down
7 changes: 4 additions & 3 deletions resources/lang/en/seeders.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

return [
'data_rows' => [
'name' => 'Name',
'description' => 'Description',
'image' => 'Image',
'type' => 'Type',
'notifiable_type' => 'Notifiable type',
'notifiable_id' => 'Notifiable id',
'data' => 'Data',
'created_by' => 'Created by',
'modified_by' => 'Modified by',
'assigned_to' => 'Assigned to',
Expand Down
31 changes: 7 additions & 24 deletions src/Models/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,13 @@
namespace Joy\VoyagerBreadNotification\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Notifications\DatabaseNotification;
use Illuminate\Support\Facades\Auth;
use Joy\VoyagerBreadNotification\Database\Factories\NotificationFactory;
use Ramsey\Uuid\Uuid;
use TCG\Voyager\Facades\Voyager;
use TCG\Voyager\Traits\Resizable;
use TCG\Voyager\Traits\Translatable;

class Notification extends Model
class Notification extends DatabaseNotification
{
use HasFactory;
use Notifiable;
use Translatable;
use Resizable;

protected $translatable = ['name', 'description'];

public const ACTIVE = 'ACTIVE';
public const INACTIVE = 'INACTIVE';
public const EXPIRED = 'EXPIRED';
Expand All @@ -38,6 +27,10 @@ public function save(array $options = [])
$this->modified_by_id = Auth::user()->getKey();
}

if (empty($this->{$this->getKeyName()})) {
$this->{$this->getKeyName()} = Uuid::uuid4()->toString();
}

return parent::save();
}

Expand Down Expand Up @@ -99,14 +92,4 @@ public function parent()
{
return $this->belongsTo(Voyager::modelClass('Notification'));
}

/**
* Create a new factory instance for the model.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
protected static function newFactory()
{
return new NotificationFactory();
}
}

0 comments on commit 13a1f08

Please sign in to comment.