Wiring up Activities table and events

This commit is contained in:
David Bomba 2019-04-21 22:24:26 +10:00
parent 9790ed17f5
commit 2545935d31
7 changed files with 115 additions and 13 deletions

View File

@ -2,19 +2,22 @@
namespace App\Listeners\Client;
use Illuminate\Queue\InteractsWithQueue;
use App\Models\Activity;
use App\Repositories\ActivityRepository;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
class CreatedClient implements ShouldQueue
class CreatedClient
{
protected $activityRepo;
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
public function __construct(ActivityRepository $activityRepo)
{
//
$this->activityRepo = $activityRepo;
}
/**
@ -25,6 +28,14 @@ class CreatedClient implements ShouldQueue
*/
public function handle($event)
{
//
$fields = new \stdClass;
$fields->client_id = $event->client->id;
$fields->user_id = $event->client->user_id;
$fields->company_id = $event->client->company_id;
$fields->activity_type_id = Activity::CREATE_CLIENT;
$this->activityRepo->save($fields, $event->client);
}
}

View File

@ -4,7 +4,7 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Activity extends Model
class Activity extends BaseModel
{
const CREATE_CLIENT=1;
const ARCHIVE_CLIENT=2;
@ -54,7 +54,11 @@ class Activity extends Model
const UPDATE_EXPENSE=47;
public function backup()
{
return $this->hasOne(Backup::class);
}
/**
* @return mixed
*/

16
app/Models/Backup.php Normal file
View File

@ -0,0 +1,16 @@
<?php
namespace App\Models;
use App\Utils\Traits\MakesHash;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Laracasts\Presenter\PresentableTrait;
class Backup extends BaseModel
{
public function activity()
{
return $this->belongsTo(Activity::class);
}
}

View File

@ -2,6 +2,7 @@
namespace App\Observers;
use App\Events\Client\ClientWasCreated;
use App\Models\Client;
class ClientObserver
@ -14,7 +15,7 @@ class ClientObserver
*/
public function created(Client $client)
{
//
event(new ClientWasCreated($client));
}
/**

View File

@ -23,7 +23,7 @@ class EventServiceProvider extends ServiceProvider
// Clients
ClientWasCreated::class => [
CreatedClient::class,
'App\Listeners\SubscriptionListener@createdClient',
// 'App\Listeners\SubscriptionListener@createdClient',
],
'App\Events\ClientWasArchived' => [
'App\Listeners\ActivityListener@archivedClient',

View File

@ -0,0 +1,37 @@
<?php
namespace App\Repositories;
use App\Models\Activity;
use App\Models\Backup;
class ActivityRepository extends BaseRepository
{
public function save($fields, $entity)
{
$activity = new Activity();
$activity->is_system = app()->runningInConsole();
$activity->ip = request()->getClientIp();
foreach($fields as $key => $value) {
$activity->{$key} = $value;
}
$activity->save();
$this->createBackup($entity, $activity);
}
public function createBackup($entity, $activity)
{
$backup = new Backup();
$backup->activity_id = $activity->id;
$backup->json_backup = $entity->toJson();
$backup->save();
}
}

View File

@ -590,18 +590,51 @@ class CreateUsersTable extends Migration
Schema::create('activities', function ($table) {
$table->increments('id');
$table->unsignedInteger('user_id');
$table->unsignedInteger('user_id')->nullable();
$table->unsignedInteger('company_id');
$table->unsignedInteger('activity_type_id');
$table->unsignedInteger('client_id')->nullable();
$table->unsignedInteger('client_contact_id')->nullable();
$table->unsignedInteger('account_id')->nullable();
$table->unsignedInteger('payment_id')->nullable();
$table->unsignedInteger('invoice_id')->nullable();
$table->unsignedInteger('invitation_id')->nullable();
$table->unsignedInteger('task_id')->nullable();
$table->unsignedInteger('expense_id')->nullable();
$table->unsignedInteger('activity_type_id')->nullable();
$table->decimal('adjustment', 13, 2)->nullable();
$table->decimal('balance', 13, 2)->nullable();
$table->string('ip');
$table->boolean('is_system')->default(0);
$table->text('notes');
$table->timestamps();
$table->index(['user_id', 'company_id']);
$table->index(['client_id', 'company_id']);
$table->index(['payment_id', 'company_id']);
$table->index(['invoice_id', 'company_id']);
$table->index(['invitation_id', 'company_id']);
$table->index(['task_id', 'company_id']);
$table->index(['expense_id', 'company_id']);
$table->index(['client_contact_id', 'company_id']);
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');
});
Schema::create('backups', function ($table) {
$table->increments('id');
$table->unsignedInteger('activity_id');
$table->text('json_backup');
$table->timestamps();
$table->foreign('activity_id')->references('id')->on('activities')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*