Browse Source

add activity => model, controller, migration, routes

also add event and listener for model saved
remotes/origin/mohammad
mahdihty 4 years ago
parent
commit
3cf22ba267
  1. 38
      app/Events/ModelSaved.php
  2. 110
      app/Http/Controllers/ActivityController.php
  3. 73
      app/Listeners/ActivityRegistration.php
  4. 27
      app/Models/Model.php
  5. 5
      app/Providers/EventServiceProvider.php
  6. 45
      database/migrations/2021_03_06_085855_create_activities_table.php
  7. 36
      database/migrations/2021_03_06_114918_create_failed_jobs_table.php
  8. 5
      routes/api.php

38
app/Events/ModelSaved.php

@ -0,0 +1,38 @@
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class ModelSaved
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $message;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($message)
{
$this->message = $message;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}

110
app/Http/Controllers/ActivityController.php

@ -0,0 +1,110 @@
<?php
namespace App\Http\Controllers;
use App\Models\Activity;
use App\Rules\MaxBound;
use Illuminate\Http\Request;
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\QueryBuilder;
class ActivityController extends Controller
{
public function index($business, Request $request)
{
permit('businessAccess');
$this->indexValidation($request);
$per_page = $request->limit > 100 ? 10 : $request->limit;
return $this->indexFiltering($business)->paginate($per_page);
}
public function indexValidation($request)
{
$bound = 10;
$this->validate($request, [
'filter.project_id' => [new MaxBound($bound)] ,
'filter.system_id' => [new MaxBound($bound)] ,
'filter.workflow_id' => [new MaxBound($bound)] ,
'filter.status_id' => [new MaxBound($bound)] ,
'filter.sprint_id' => [new MaxBound($bound)] ,
'filter.actor_id' => [new MaxBound($bound)] ,
'filter.user_id' => [new MaxBound($bound)] ,
'filter.subject_id' => [new MaxBound($bound)] ,
//todo: validation for crud_id and table_id
'filter.creates_before' => 'bail|nullable|date|date_format:Y-m-d' ,
'filter.creates_after' => 'bail|nullable|date|date_format:Y-m-d' ,
'filter.creates_in' => 'bail|nullable|numeric|max:90' ,
]);
}
public function indexFiltering($business)
{
$query = Activity::where('business_id', $business);
$activityQ = QueryBuilder::for($query)
->allowedFilters([
AllowedFilter::exact('project_id'),
AllowedFilter::exact('system_id'),
AllowedFilter::exact('workflow_id'),
AllowedFilter::exact('status_id'),
AllowedFilter::exact('sprint_id'),
AllowedFilter::exact('task_id'),
AllowedFilter::exact('actor_id'),
AllowedFilter::exact('user_id'),
AllowedFilter::exact('crud_id'),
AllowedFilter::exact('table_id'),
AllowedFilter::exact('subject_id'),
AllowedFilter::scope('creates_before'),
AllowedFilter::scope('creates_after'),
AllowedFilter::scope('creates_in'),
])
->defaultSort('-id')
->allowedSorts('id', 'created_at');
if (\request('_business_info')['info']['users'][\auth()->id()]['level'] != enum('levels.owner.id')) {
$requested_projects = isset(\request('filter')['project_id']) ?
array_unique(explode(',',\request('filter')['project_id'] ?? null )) :
null;
$requested_projects = collect($requested_projects)->keyBy(null)->toArray();
$project_ids = $this->myStateProjects($requested_projects);
$activityQ->where(function ($q) use ($project_ids) {
$q->whereIn('project_id', $project_ids['non_guest_ids'])
->orWhere(function ($q) use ($project_ids) {
$q->whereIn('project_id', $project_ids['guest_ids'])
->where('user_id', auth()->id());
});
});
}
return $activityQ;
}
public function myStateProjects($requested_projects)
{
$non_guest_ids = [];
$guest_ids = [];
$is_empty = empty($requested_projects);
foreach (\request('_business_info')['info']['projects'] as $p_id => $p) {
$level = \request('_business_info')['info']['projects'][$p_id]['members'][\auth()->id()]['level'];
if (( $is_empty || isset($requested_projects[$p_id]))
&& $level > enum('levels.guest.id')) {
array_push($non_guest_ids, $p_id);
}
if (( $is_empty || isset($requested_projects[$p_id]))
&& $level == enum('levels.guest.id')) {
array_push($guest_ids, $p_id);
}
}
return ['non_guest_ids' => $non_guest_ids, 'guest_ids' => $guest_ids];
}
public function store($business, Request $request)
{
return Activity::create($request->merge(['business_id' => $business])->all());
}
public function delete()
{
}
}

73
app/Listeners/ActivityRegistration.php

@ -0,0 +1,73 @@
<?php
namespace App\Listeners;
use App\Events\ModelSaved;
use App\Models\Activity;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;
class ActivityRegistration implements ShouldQueue
{
/**
* If your queue connection's after_commit configuration option is set to true,
* indicate that a particular queued listener should be dispatched after all database transactions closed
*
* @var boolean
*/
public $afterCommit = true;
/**
* The name of the connection the job should be sent to.
*
* @var string|null
*/
public $connection = 'redis';
/**
* The name of the queue the job should be sent to.
* It's just a name and if don't set it, laravel use default queue name in the connection
*
* @var string|null
*/
public $queue = 'activities';
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param ModelSaved $event
* @return void
*/
public function handle(ModelSaved $event)
{
Log::info('listener:'. json_encode($event->message));
$message = json_decode($event->message);
Activity::create([
'business_id' => $message->business,
'project_id' => $message->project,
'actor_id' => $message->auth,
'system_id' => $message->data->system_id,
'workflow_id' => $message->data->workflow_id,
'status_id' => $message->data->status_id,
'sprint_id' => $message->data->sprint_id,
'task_id' => $message->data->task_id ?? null,
'subject_id' => $message->data->subject_id ?? null,
'user_id' => $message->data->user_id,
'crud_id' => $message->data->crud_id,
'table_id' => enum('tables.'.$message->data->table_name.'.id'),
'original' => $message->data->original,
'diff' => $message->data->diff,
]);
}
}

27
app/Models/Model.php

@ -4,6 +4,7 @@ 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\Http\JsonResponse;
@ -196,19 +197,21 @@ class Model extends EloquentModel
$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));
ModelSaved::dispatch(json_encode($payload));
Amqp::publish($message, "");
}
// $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, "");
// }
}
/**

5
app/Providers/EventServiceProvider.php

@ -2,6 +2,8 @@
namespace App\Providers;
use App\Events\ModelSaved;
use App\Listeners\ActivityRegistration;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
@ -18,6 +20,9 @@ class EventServiceProvider extends ServiceProvider
Registered::class => [
SendEmailVerificationNotification::class,
],
ModelSaved::class => [
ActivityRegistration::class
]
];
/**

45
database/migrations/2021_03_06_085855_create_activities_table.php

@ -0,0 +1,45 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateActivitiesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('activities', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('business_id');
$table->unsignedBigInteger('project_id')->nullable();
$table->unsignedBigInteger('system_id')->nullable();
$table->unsignedBigInteger('workflow_id')->nullable();
$table->unsignedBigInteger('status_id')->nullable();
$table->unsignedBigInteger('sprint_id')->nullable();
$table->unsignedBigInteger('task_id')->nullable();
$table->unsignedBigInteger('subject_id')->nullable();//row id
$table->unsignedBigInteger('actor_id');
$table->unsignedBigInteger('user_id')->nullable();
$table->unsignedBigInteger('crud_id')->nullable();
$table->unsignedBigInteger('table_id')->nullable();
$table->json('original')->nullable(); // a unique identifier that represent the type of action
$table->json('diff')->nullable(); // all data that has been changed
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('activities');
}
}

36
database/migrations/2021_03_06_114918_create_failed_jobs_table.php

@ -0,0 +1,36 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateFailedJobsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('failed_jobs', function (Blueprint $table) {
$table->id();
$table->string('uuid')->unique();
$table->text('connection');
$table->text('queue');
$table->longText('payload');
$table->longText('exception');
$table->timestamp('failed_at')->useCurrent();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('failed_jobs');
}
}

5
routes/api.php

@ -182,6 +182,11 @@ $router->group(['prefix' => 'businesses', 'middleware' => 'auth:api'], function
$router->delete('/', 'BusinessController@deleteUser');
});
});
$router->group(['prefix' => 'activities'], function () use ($router) {
$router->post('/', 'ActivityController@store');
$router->get('/', 'ActivityController@index');
});
});
});

Loading…
Cancel
Save