Skip to content

Commit 980dcfc

Browse files
committed
Backup
1 parent fec1f93 commit 980dcfc

16 files changed

+1337
-11
lines changed

Database/Migrations/2020_11_16_194130_vh_saas_tenants.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public function up()
2525
$table->string('domain')->nullable()->index();
2626
$table->string('sub_domain')->nullable()->index();
2727

28+
$table->integer('count_apps_active')->nullable();
29+
$table->integer('count_apps')->nullable();
30+
2831
$table->string('database_name')->nullable();
2932
$table->string('database_charset')->nullable()->default('utf8mb4');
3033
$table->string('database_collation')->nullable()->default('utf8mb4_unicode_ci');

Database/Migrations/2020_11_19_133232_vh_saas_apps.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public function up()
3030
$table->string('seed_class')->nullable();
3131
$table->string('sample_data_class')->nullable();
3232

33+
34+
$table->integer('count_tenants_active')->nullable();
35+
$table->integer('count_tenants')->nullable();
36+
3337
$table->dateTime('activated_at')->nullable();
3438
$table->boolean('is_active')->nullable();
3539

Database/Migrations/2020_11_19_133353_vh_saas_tenant_apps.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@ public function up()
2323
$table->string('version')->nullable();
2424
$table->integer('version_number')->nullable();
2525

26-
$table->boolean('is_active')->nullable();
26+
$table->boolean('is_active')->nullable()->index();
2727

2828
$table->dateTime('last_migrated_at')->nullable();
2929
$table->dateTime('last_seeded_at')->nullable();
3030

3131
$table->timestamps();
3232

33-
3433
});
3534
}
3635

Entities/App.php

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class App extends Model {
2525
//-------------------------------------------------
2626
protected $fillable = [
2727
'uuid',
28+
'app_type',
2829
'name',
2930
'slug',
3031
'excerpt',
@@ -34,6 +35,8 @@ class App extends Model {
3435
'migration_path',
3536
'seed_class',
3637
'sample_data_class',
38+
'count_tenants_active',
39+
'count_tenants',
3740
'activated_at',
3841
'is_active',
3942
'is_deactivated_at',
@@ -71,6 +74,17 @@ public function deletedByUser()
7174
)->select('id', 'uuid', 'first_name', 'last_name', 'email');
7275
}
7376
//-------------------------------------------------
77+
public function tenants()
78+
{
79+
return $this->belongsToMany( Tenant::class,
80+
'vh_saas_tenant_apps',
81+
'vh_saas_app_id', 'vh_saas_tenant_id'
82+
)->withPivot('version',
83+
'version_number', 'is_active',
84+
'last_migrated_at', 'last_seeded_at',
85+
'created_at', 'updated_at');
86+
}
87+
//-------------------------------------------------
7488
public function getTableColumns() {
7589
return $this->getConnection()->getSchemaBuilder()
7690
->getColumnListing($this->getTable());
@@ -169,6 +183,8 @@ public static function createItem($request)
169183
$item->slug = Str::slug($inputs['slug']);
170184
$item->save();
171185

186+
187+
172188
$response['status'] = 'success';
173189
$response['data']['item'] = $item;
174190
$response['messages'][] = 'Saved successfully.';
@@ -455,6 +471,123 @@ public static function getActiveItems()
455471
return $item;
456472
}
457473
//-------------------------------------------------
474+
public static function countTenants($id)
475+
{
476+
477+
$item = static::withTrashed()->where('id', $id)->first();
478+
479+
if(!$item)
480+
{
481+
return 0;
482+
}
483+
484+
return $item->tenants()
485+
->count();
486+
}
487+
//-------------------------------------------------
488+
public static function countTenantsActive($id)
489+
{
490+
491+
$item = static::withTrashed()->where('id', $id)->first();
492+
493+
if(!$item)
494+
{
495+
return 0;
496+
}
497+
498+
return $item->tenants()
499+
->wherePivotNotNull('is_active')
500+
->count();
501+
502+
}
503+
//-------------------------------------------------
504+
505+
//-------------------------------------------------
506+
public static function updateCounts(App $app)
507+
{
508+
$app->count_tenants_active = static::countTenantsActive($app->id);
509+
$app->count_tenants = static::countTenants($app->id);
510+
$app->save();
511+
}
512+
//-------------------------------------------------
513+
public static function updateCountsForAll()
514+
{
515+
$all = static::all();
516+
517+
if($all)
518+
{
519+
foreach($all as $item)
520+
{
521+
static::updateCounts($item);
522+
}
523+
}
524+
}
525+
//-------------------------------------------------
526+
public static function getItemTenants($request, $id)
527+
{
528+
$item = static::withTrashed()->where('id', $id)->first();
529+
$response['data']['item'] = $item;
530+
531+
if($request->has("q"))
532+
{
533+
$list = $item->tenants()->where(function ($q) use ($request){
534+
$q->where('name', 'LIKE', '%'.$request->q.'%')
535+
->orWhere('slug', 'LIKE', '%'.$request->q.'%');
536+
});
537+
} else
538+
{
539+
$list = $item->tenants();
540+
}
541+
542+
543+
$list->orderBy('pivot_is_active', 'desc');
544+
545+
546+
$list = $list->paginate(config('vaahcms.per_page'));
547+
548+
549+
$response['data']['list'] = $list;
550+
$response['status'] = 'success';
551+
552+
return $response;
553+
554+
555+
}
556+
//-------------------------------------------------
557+
public static function syncAppsWithTenants()
558+
{
559+
$all_tenants = Tenant::select('id')->get()->pluck('id')->toArray();
560+
$all_apps = App::select('id', 'version', 'version_number')->get();
561+
562+
if(!$all_apps)
563+
{
564+
return false;
565+
}
566+
567+
568+
569+
foreach ($all_apps as $app)
570+
{
571+
572+
$pivots = [
573+
'version' => $app->version,
574+
'version_number' => $app->version_number,
575+
];
576+
577+
$pivotData = array_fill(0, count($all_tenants), $pivots);
578+
$syncData = array_combine($all_tenants, $pivotData);
579+
580+
$app->tenants()->syncWithoutDetaching($syncData);
581+
582+
}
583+
584+
585+
586+
587+
}
588+
//-------------------------------------------------
589+
//-------------------------------------------------
590+
//-------------------------------------------------
458591
//-------------------------------------------------
459592
//-------------------------------------------------
460593

Entities/Server.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -487,12 +487,7 @@ public static function countDatabaseInstances($id)
487487
->count();
488488
}
489489
//-------------------------------------------------
490-
public static function testServerConnection($inputs)
491-
{
492-
493490

494-
495-
}
496491
//-------------------------------------------------
497492
//-------------------------------------------------
498493
//-------------------------------------------------

Entities/Tenant.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,12 @@ class Tenant extends Model {
5454

5555
//-------------------------------------------------
5656
protected $appends = [
57+
5758
];
5859
//-------------------------------------------------
5960

61+
//-------------------------------------------------
62+
6063
public function createdByUser()
6164
{
6265
return $this->belongsTo(User::class,
@@ -80,6 +83,17 @@ public function deletedByUser()
8083
)->select('id', 'uuid', 'first_name', 'last_name', 'email');
8184
}
8285
//-------------------------------------------------
86+
public function apps()
87+
{
88+
return $this->belongsToMany( App::class,
89+
'vh_saas_tenant_apps',
90+
'vh_saas_tenant_id', 'vh_saas_app_id'
91+
)->withPivot('version',
92+
'version_number', 'is_active',
93+
'last_migrated_at', 'last_seeded_at',
94+
'created_at', 'updated_at');
95+
}
96+
//-------------------------------------------------
8397
public function getTableColumns() {
8498
return $this->getConnection()->getSchemaBuilder()
8599
->getColumnListing($this->getTable());
@@ -110,6 +124,8 @@ public function scopeBetweenDates($query, $from, $to)
110124

111125
$query->whereBetween('updated_at',[$from,$to]);
112126
}
127+
//-------------------------------------------------
128+
113129
//-------------------------------------------------
114130
public static function createItem($request)
115131
{
@@ -149,6 +165,10 @@ public static function createItem($request)
149165
$item->slug = Str::slug($inputs['slug']);
150166
$item->save();
151167

168+
App::syncAppsWithTenants();
169+
170+
static::updateCounts($item);
171+
152172
$response['status'] = 'success';
153173
$response['data']['item'] = $item;
154174
$response['messages'][] = 'Saved successfully.';
@@ -645,7 +665,55 @@ public static function seed($inputs, $value, $key='uuid')
645665
}
646666

647667
//-------------------------------------------------
668+
public static function countApps($id)
669+
{
670+
671+
$item = static::withTrashed()->where('id', $id)->first();
672+
673+
if(!$item)
674+
{
675+
return 0;
676+
}
677+
678+
return $item->apps()
679+
->count();
680+
}
681+
//-------------------------------------------------
682+
public static function countAppsActive($id)
683+
{
684+
685+
$item = static::withTrashed()->where('id', $id)->first();
686+
687+
if(!$item)
688+
{
689+
return 0;
690+
}
691+
692+
return $item->apps()
693+
->wherePivotNotNull('is_active')
694+
->count();
695+
696+
}
697+
//-------------------------------------------------
698+
public static function updateCounts(Tenant $tenant)
699+
{
700+
$tenant->count_apps_active = static::countAppsActive($tenant->id);
701+
$tenant->count_apps = static::countApps($tenant->id);
702+
$tenant->save();
703+
}
648704
//-------------------------------------------------
705+
public static function updateCountsForAll()
706+
{
707+
$all = static::all();
708+
709+
if($all)
710+
{
711+
foreach($all as $item)
712+
{
713+
static::updateCounts($item);
714+
}
715+
}
716+
}
649717
//-------------------------------------------------
650718
//-------------------------------------------------
651719
//-------------------------------------------------

Http/Controllers/Backend/AppsController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ public function postActions(Request $request, $action)
143143

144144
}
145145
//----------------------------------------------------------
146+
public function getItemTenants(Request $request, $id)
147+
{
148+
$response = App::getItemTenants($request, $id);
149+
return response()->json($response);
150+
}
146151
//----------------------------------------------------------
147152

148153

Observers/AppObserver.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php namespace VaahCms\Modules\Saas\Observers;
2+
3+
use VaahCms\Modules\Saas\Entities\App;
4+
use VaahCms\Modules\Saas\Entities\Tenant;
5+
6+
class AppObserver {
7+
8+
9+
public function created(App $item)
10+
{
11+
App::syncAppsWithTenants();
12+
App::updateCounts($item);
13+
Tenant::updateCountsForAll();
14+
}
15+
16+
17+
public function updated(App $item)
18+
{
19+
//
20+
}
21+
22+
23+
public function deleted(App $item)
24+
{
25+
App::syncAppsWithTenants();
26+
App::updateCounts($item);
27+
Tenant::updateCountsForAll();
28+
}
29+
30+
31+
public function restored(App $item)
32+
{
33+
//
34+
}
35+
36+
37+
public function forceDeleted(App $item)
38+
{
39+
App::syncAppsWithTenants();
40+
App::updateCounts($item);
41+
Tenant::updateCountsForAll();
42+
}
43+
44+
}

0 commit comments

Comments
 (0)