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.

138 lines
4.6 KiB

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