From f8a985035ef494e821c136a4f66f53295087a8f8 Mon Sep 17 00:00:00 2001 From: glovetleong Date: Fri, 18 Feb 2022 21:57:25 +0800 Subject: [PATCH] notification --- app/Classes/General/Interfaces/Notifiable.php | 15 +++ .../NotificationObject.php | 101 ++++++++++++++++++ .../CreateNotificationProcessor.php | 27 +++++ .../Services/CreatesNotification.php | 30 ++++++ .../Constants/NotificationType.php | 11 ++ .../ValueObjects/Constants/Notifications.php | 8 +- app/Models/Notification.php | 18 ++++ app/Models/Order.php | 28 ++++- ...2_18_120837_create_notifications_table.php | 40 +++++++ 9 files changed, 272 insertions(+), 6 deletions(-) create mode 100644 app/Classes/General/Interfaces/Notifiable.php create mode 100644 app/Classes/Modules/Notifications/DataTransferObjects/NotificationObject.php create mode 100644 app/Classes/Modules/Notifications/Processors/CreateNotificationProcessor.php create mode 100644 app/Classes/Modules/Notifications/Services/CreatesNotification.php create mode 100644 app/Classes/ValueObjects/Constants/NotificationType.php create mode 100644 app/Models/Notification.php create mode 100644 database/migrations/2022_02_18_120837_create_notifications_table.php diff --git a/app/Classes/General/Interfaces/Notifiable.php b/app/Classes/General/Interfaces/Notifiable.php new file mode 100644 index 00000000..b97a08c7 --- /dev/null +++ b/app/Classes/General/Interfaces/Notifiable.php @@ -0,0 +1,15 @@ +title = $title; + $this->description = $description; + $this->subject = $subject; + $this->target = $target; + $this->causer = $causer; + $this->status = $status; + } + + /** + * @return int + */ + public function getTitle(): string + { + return $this->title; + } + + /** + * @return int + */ + public function getDescription(): string + { + return $this->description; + } + + /** + * @return Notifiable + */ + public function getSubject(): Notifiable + { + return $this->subject; + } + + /** + * @return Notifiable + */ + public function getTarget(): Notifiable + { + return $this->target; + } + + /** + * @return Notifiable + */ + public function getCauser(): Notifiable + { + return $this->causer; + } + + /** + * @return int + */ + public function getStatus(): int + { + return $this->status; + } + + +} diff --git a/app/Classes/Modules/Notifications/Processors/CreateNotificationProcessor.php b/app/Classes/Modules/Notifications/Processors/CreateNotificationProcessor.php new file mode 100644 index 00000000..d6b077f8 --- /dev/null +++ b/app/Classes/Modules/Notifications/Processors/CreateNotificationProcessor.php @@ -0,0 +1,27 @@ +createsNotification = $createsNotification; + } + + public function execute(NotificationObject $object) + { + $notification = $this->createsNotification->execute($object); + return $notification; + } +} diff --git a/app/Classes/Modules/Notifications/Services/CreatesNotification.php b/app/Classes/Modules/Notifications/Services/CreatesNotification.php new file mode 100644 index 00000000..4f5ca5fd --- /dev/null +++ b/app/Classes/Modules/Notifications/Services/CreatesNotification.php @@ -0,0 +1,30 @@ +title = $object->getTitle(); + $model->description = $object->getDescription(); + $model->status = $object->getStatus(); + $model->subject_type = get_class($object->getSubject()); + $model->subject_id = $object->getSubject()->id; + $model->target_type = get_class($object->getTarget()); + $model->target_id = $object->getTarget()->id; + $model->causer_type = get_class($object->getCauser()); + $model->causer_id = $object->getCauser()->id; + $model->save(); + + return $model; + } +} \ No newline at end of file diff --git a/app/Classes/ValueObjects/Constants/NotificationType.php b/app/Classes/ValueObjects/Constants/NotificationType.php new file mode 100644 index 00000000..0c9ec5a4 --- /dev/null +++ b/app/Classes/ValueObjects/Constants/NotificationType.php @@ -0,0 +1,11 @@ + 'Unknown Action', - 'message' => 'unknown message..' + public const NEW_ARRIVED_PARCEL = [ + 'title' => 'New Arrived Parcel', + 'message' => 'you have received new parcel for your order #-' ]; } \ No newline at end of file diff --git a/app/Models/Notification.php b/app/Models/Notification.php new file mode 100644 index 00000000..06bd6024 --- /dev/null +++ b/app/Models/Notification.php @@ -0,0 +1,18 @@ +BelongsTo(Package::class, 'package_id', 'id'); + } +} diff --git a/app/Models/Order.php b/app/Models/Order.php index 5aabfd44..24f344c2 100644 --- a/app/Models/Order.php +++ b/app/Models/Order.php @@ -6,6 +6,7 @@ use App\Classes\General\Interfaces\Addressable; use App\Classes\General\Interfaces\Contactable; use App\Classes\General\Interfaces\Packable; use App\Classes\General\Interfaces\Remarkable; +use App\Classes\General\Interfaces\Notifiable; use App\Classes\ValueObjects\Constants\PackingListType; use App\Classes\ValueObjects\Constants\RoleTypes; use App\Classes\ValueObjects\Constants\ApprovalStatus; @@ -16,7 +17,7 @@ use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\MorphMany; -class Order extends AbstractModel implements Addressable, Packable, Remarkable +class Order extends AbstractModel implements Addressable, Packable, Remarkable, Notifiable { use SoftDeletes; @@ -33,6 +34,31 @@ class Order extends AbstractModel implements Addressable, Packable, Remarkable return $this->belongsTo(CompanyModule::class, 'company_module_id', 'id'); } + /** + * @return MorphMany + */ + public function subject(): morphMany + { + return $this->morphMany(Notification::class, 'subject_type'); + } + + /** + * @return MorphMany + */ + public function target(): morphMany + { + return $this->morphMany(Notification::class, 'target'); + } + + /** + * @return MorphMany + */ + public function causer(): morphMany + { + return $this->morphMany(Notification::class, 'causer'); + } + + /** * @return MorphMany */ diff --git a/database/migrations/2022_02_18_120837_create_notifications_table.php b/database/migrations/2022_02_18_120837_create_notifications_table.php new file mode 100644 index 00000000..d14cdc89 --- /dev/null +++ b/database/migrations/2022_02_18_120837_create_notifications_table.php @@ -0,0 +1,40 @@ +id(); + $table->string('title'); + $table->text('description'); + $table->morphs('subject'); + $table->morphs('target'); + $table->morphs('causer'); + $table->integer('status')->default(ApprovalStatus::APPROVED); + $table->softDeletes(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('notifications'); + } +}