ActionBrowserNotificationAbstract.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace App\Services\Notification\Abstracts;
  3. use App\Models\Action;
  4. use App\Models\Approval;
  5. use App\Models\Enums\ApprovalMode;
  6. use App\Models\Enums\NotificationObjectType;
  7. use App\Models\Enums\ObjectAction;
  8. use App\Models\Notification;
  9. use App\Models\NotificationRecord;
  10. use App\Services\Notification\Contacts\ActionBrowserNotificationContacts;
  11. use App\Services\Notification\Traits\ObjectActionApprovalNotificationUserHelper;
  12. use Illuminate\Database\Eloquent\Model;
  13. abstract class ActionBrowserNotificationAbstract implements ActionBrowserNotificationContacts
  14. {
  15. use ObjectActionApprovalNotificationUserHelper;
  16. protected ObjectAction $objectAction;
  17. public function __construct(protected Action $action, protected Model $object)
  18. {
  19. $this->objectAction = ObjectAction::tryFrom($this->action->action);
  20. }
  21. public function handle()
  22. {
  23. if ($this->objectAction->isApproval()) {
  24. $this->handleByApprovalAction();
  25. } else {
  26. $this->handleByObjectAction($this->userIDs());
  27. }
  28. }
  29. abstract protected function userIDs(): array;
  30. protected function handleByApprovalAction()
  31. {
  32. $approval = Approval::query()->find($this->action->additional_id);
  33. if (! $approval) {
  34. return;
  35. }
  36. list($approvalUserIDs, $notifiedUserIDs) = $this->objectActionApprovalNotificationUserIDs($approval, $this->action);
  37. $this->storeNotification($notifiedUserIDs);
  38. $this->storeNotification($approvalUserIDs, [
  39. 'action' => 'waitingForMyApproval'
  40. ]);
  41. }
  42. protected function handleByObjectAction(array $userIDs)
  43. {
  44. $this->storeNotification($userIDs);
  45. }
  46. protected function storeNotification(array $userIds, array $extraFields = [])
  47. {
  48. if (! $userIds) {
  49. return;
  50. }
  51. $notification = Notification::query()->create([
  52. 'object_type' => NotificationObjectType::ACTION->value,
  53. 'object_id' => $this->action->id,
  54. 'extra_fields' => $extraFields ?: null,
  55. ]);
  56. foreach ($userIds as $userId) {
  57. NotificationRecord::query()->create([
  58. 'notification_id' => $notification->id,
  59. 'user_id' => $userId,
  60. ]);
  61. }
  62. }
  63. protected function notificationNextApprovalUsers(Approval $approval)
  64. {
  65. $approvalFlow = $approval->approvalFlow()->first();
  66. if (! $approvalFlow) {
  67. return;
  68. }
  69. $nextNode = $approvalFlow->nodes[$this->action->extra_fields['next']];
  70. $approvalObjectUsers = array_values(array_filter(explode(',', $approval->users)));
  71. $userIds = match (ApprovalMode::tryFrom($nextNode['approval_mode'])) {
  72. ApprovalMode::ALTERNATIVE_APPROVAL, ApprovalMode::COUNTERSIGNATURE => $nextNode['approval_users'] ?? [],
  73. ApprovalMode::SEQUENTIAL_APPROVAL => $this->action->extra_fields['next_sequential_user'] ?? [],
  74. default => [],
  75. };
  76. $this->storeNotification($userIds, [
  77. 'action' => 'waitingForMyApproval'
  78. ]);
  79. }
  80. }