|
|
<?php
namespace App\Models;
use Anik\Amqp\Exchange; use Anik\Amqp\Facades\Amqp; use App\Events\ModelSaved; use PhpAmqpLib\Wire\AMQPTable; use Anik\Amqp\PublishableMessage; use Illuminate\Support\Facades\Auth; use Illuminate\Database\Eloquent\Relations\Pivot;
class ReportableRelation extends Pivot { public const CREATED = 10;
public const UPDATED = 20;
public const DELETED = 30;
protected static function booted() { static::created(function ($model) { $model->action = static::CREATED; static::report($model); }); static::updated(function ($model) { $model->action = static::UPDATED; static::report($model); }); static::deleted(function ($model) { $model->action = static::DELETED; static::report($model); }); }
public static function report($model) { $payload = [ 'auth' => Auth::id(), 'timestamp' => $model->freshTimestamp(), 'business' => $model->pivotParent->getValueOf('business_id'), 'info' => \request('_business_info') ?? null, 'project' => $model->pivotParent->getValueOf('project_id'), 'data' => [ 'sprint_id' => $model->pivotParent->getValueOf('sprint_id'), 'system_id' => $model->pivotParent->getValueOf('system_id'), 'workflow_id' => $model->pivotParent->getValueOf('workflow_id'), 'status_id' => $model->pivotParent->getValueOf('status_id'), 'task_id' => $model->pivotParent->getValueOf('task_id'), 'user_id' => $model->user_id, 'table_name' => $model->getTable(), 'crud_id' => $model->action, 'original' => $model->getOriginal() + $model->getAttributes(), 'diff' => $model->getChanges(), ], 'from' => env('CONTAINER_NAME'), ];
ModelSaved::dispatch(json_encode($payload)); // $message = new PublishableMessage(json_encode($payload));
//
// $routers = [
// "activity_exchange" => ["name" => "activity",],
// "notif_exchange" => ["name" => "notif",],
// "socket_exchange" => ["name" => "socket",],
// ];
//
// foreach ($routers as $exchange => $properties) {
// $message->setProperties(["application_headers" => new AMQPTable($properties)]);
//
// $message->setExchange(new Exchange($exchange));
//
// Amqp::publish($message, "");
// }
}
public function properties() { return $properties = [ // Message properties
'Persistent' => '1', // Marks a message as persistent
// those familiar with the protocol may choose to use this property instead of Persistent. They control the same thing.
// Non-persistent (1) or persistent (2).
'DeliveryMode' => '',
// 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.
// MIME content type.
// short string (max. 256 characters)
'content_type' => '',
// Commonly used to name a callback queue.
// Address to reply to.
// short string (max. 256 characters)
'reply_to' => '',
// Useful to correlate RPC responses with requests.
// Application correlation identifier.
// short string (max. 256 characters)
'correlation_id' => '',
/** Rarley Used Properties */
// This defines the message priority
// Message priority, 0 to 9
'priority' => '',
// This is the message time stamp
// Message timestamp.
'timestamp' => '',
// This is the broker with whom the user sends the message (by default, it is "guest").
// Creating user id.
// short string (max. 256 characters)
'user_id' => '',
// MIME content encoding.
// short string (max. 256 characters)
'content_encoding' => '',
// Message expiration specification.
// short string (max. 256 characters)
'expiration' => '',
// Application message identifier.
// short string (max. 256 characters)
'message_id' => '',
// Message type name.
// short string (max. 256 characters)
'type' => '',
// Creating application id.
// short string (max. 256 characters)
'app_id' => '', 'cluster_id' => '', ]; } }
|