You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
4.7 KiB

  1. <?php
  2. namespace App\Models;
  3. use Anik\Amqp\Exchange;
  4. use Anik\Amqp\Facades\Amqp;
  5. use App\Events\ModelSaved;
  6. use PhpAmqpLib\Wire\AMQPTable;
  7. use Anik\Amqp\PublishableMessage;
  8. use Illuminate\Support\Facades\Auth;
  9. use Illuminate\Database\Eloquent\Relations\Pivot;
  10. class ReportableRelation extends Pivot
  11. {
  12. public const CREATED = 10;
  13. public const UPDATED = 20;
  14. public const DELETED = 30;
  15. protected static function booted()
  16. {
  17. static::created(function ($model) {
  18. $model->action = static::CREATED;
  19. static::report($model);
  20. });
  21. static::updated(function ($model) {
  22. $model->action = static::UPDATED;
  23. static::report($model);
  24. });
  25. static::deleted(function ($model) {
  26. $model->action = static::DELETED;
  27. static::report($model);
  28. });
  29. }
  30. public static function report($model)
  31. {
  32. $payload = [
  33. 'auth' => Auth::id(),
  34. 'timestamp' => $model->freshTimestamp(),
  35. 'business' => $model->pivotParent->getValueOf('business_id'),
  36. 'info' => \request('_business_info') ?? null,
  37. 'project' => $model->pivotParent->getValueOf('project_id'),
  38. 'data' => [
  39. 'sprint_id' => $model->pivotParent->getValueOf('sprint_id'),
  40. 'system_id' => $model->pivotParent->getValueOf('system_id'),
  41. 'workflow_id' => $model->pivotParent->getValueOf('workflow_id'),
  42. 'status_id' => $model->pivotParent->getValueOf('status_id'),
  43. 'task_id' => $model->pivotParent->getValueOf('task_id'),
  44. 'user_id' => $model->user_id,
  45. 'table_name' => $model->getTable(),
  46. 'crud_id' => $model->action,
  47. 'original' => $model->getOriginal() + $model->getAttributes(),
  48. 'diff' => $model->getChanges(),
  49. ],
  50. 'from' => env('CONTAINER_NAME'),
  51. ];
  52. ModelSaved::dispatch(json_encode($payload));
  53. // $message = new PublishableMessage(json_encode($payload));
  54. //
  55. // $routers = [
  56. // "activity_exchange" => ["name" => "activity",],
  57. // "notif_exchange" => ["name" => "notif",],
  58. // "socket_exchange" => ["name" => "socket",],
  59. // ];
  60. //
  61. // foreach ($routers as $exchange => $properties) {
  62. // $message->setProperties(["application_headers" => new AMQPTable($properties)]);
  63. //
  64. // $message->setExchange(new Exchange($exchange));
  65. //
  66. // Amqp::publish($message, "");
  67. // }
  68. }
  69. public function properties()
  70. {
  71. return $properties = [
  72. // Message properties
  73. 'Persistent' => '1', // Marks a message as persistent
  74. // those familiar with the protocol may choose to use this property instead of Persistent. They control the same thing.
  75. // Non-persistent (1) or persistent (2).
  76. 'DeliveryMode' => '',
  77. // Used to describe the mime-type of the encoding. For example for the often used JSON encoding it is a good practice to set this property to: application/json.
  78. // MIME content type.
  79. // short string (max. 256 characters)
  80. 'content_type' => '',
  81. // Commonly used to name a callback queue.
  82. // Address to reply to.
  83. // short string (max. 256 characters)
  84. 'reply_to' => '',
  85. // Useful to correlate RPC responses with requests.
  86. // Application correlation identifier.
  87. // short string (max. 256 characters)
  88. 'correlation_id' => '',
  89. /** Rarley Used Properties */
  90. // This defines the message priority
  91. // Message priority, 0 to 9
  92. 'priority' => '',
  93. // This is the message time stamp
  94. // Message timestamp.
  95. 'timestamp' => '',
  96. // This is the broker with whom the user sends the message (by default, it is "guest").
  97. // Creating user id.
  98. // short string (max. 256 characters)
  99. 'user_id' => '',
  100. // MIME content encoding.
  101. // short string (max. 256 characters)
  102. 'content_encoding' => '',
  103. // Message expiration specification.
  104. // short string (max. 256 characters)
  105. 'expiration' => '',
  106. // Application message identifier.
  107. // short string (max. 256 characters)
  108. 'message_id' => '',
  109. // Message type name.
  110. // short string (max. 256 characters)
  111. 'type' => '',
  112. // Creating application id.
  113. // short string (max. 256 characters)
  114. 'app_id' => '',
  115. 'cluster_id' => '',
  116. ];
  117. }
  118. }