Differential D3584 Diff 10283 src/database/migrations/2022_05_13_100000_shares_and_room_subscriptions.php
Changeset View
Changeset View
Standalone View
Standalone View
src/database/migrations/2022_05_13_100000_shares_and_room_subscriptions.php
- This file was added.
<?php | |||||
use Illuminate\Database\Migrations\Migration; | |||||
use Illuminate\Database\Schema\Blueprint; | |||||
use Illuminate\Support\Facades\Schema; | |||||
return new class extends Migration | |||||
{ | |||||
/** | |||||
* Run the migrations. | |||||
*/ | |||||
public function up(): void | |||||
{ | |||||
Schema::create( | |||||
'shares', | |||||
function (Blueprint $table) { | |||||
$table->string('id', 36)->primary(); | |||||
$table->bigInteger('shareable_id'); | |||||
$table->string('shareable_type'); | |||||
$table->integer('rights')->default(0); | |||||
$table->string('user'); | |||||
$table->timestamps(); | |||||
$table->index('user'); | |||||
$table->index(['shareable_id', 'shareable_type']); | |||||
} | |||||
); | |||||
Schema::table( | |||||
'openvidu_rooms', | |||||
function (Blueprint $table) { | |||||
$table->bigInteger('tenant_id')->unsigned()->nullable(); | |||||
$table->string('description')->nullable(); | |||||
$table->softDeletes(); | |||||
$table->foreign('tenant_id')->references('id')->on('tenants')->onDelete('set null'); | |||||
} | |||||
); | |||||
// Create the new SKU | |||||
if (!\App\Sku::where('title', 'room')->first()) { | |||||
$sku = \App\Sku::create([ | |||||
'title' => 'room', | |||||
'name' => 'Conference room', | |||||
'description' => 'Audio & Video Conference room', | |||||
'cost' => 0, | |||||
'units_free' => 0, | |||||
'period' => 'monthly', | |||||
'handler_class' => 'App\Handlers\Room', | |||||
'active' => true, | |||||
]); | |||||
$counts = []; | |||||
// Create the entitlement for every room | |||||
foreach (\App\Meet\Room::get() as $room) { | |||||
$user = \App\User::find($room->user_id); // @phpstan-ignore-line | |||||
if (!$user) { | |||||
$room->forceDelete(); | |||||
continue; | |||||
} | |||||
// Set tenant_id | |||||
if ($user->tenant_id) { | |||||
$room->tenant_id = $user->tenant_id; | |||||
$room->save(); | |||||
} | |||||
$wallet = $user->wallet(); | |||||
$counts[$wallet->id] = ($counts[$wallet->id] ?? 0) + 1; | |||||
\App\Entitlement::create([ | |||||
'wallet_id' => $wallet->id, | |||||
'sku_id' => $sku->id, | |||||
'cost' => $counts[$wallet->id] > $sku->units_free ? $sku->cost : 0, | |||||
'fee' => $counts[$wallet->id] > $sku->units_free ? $sku->fee : 0, | |||||
'entitleable_id' => $room->id, | |||||
'entitleable_type' => \App\Meet\Room::class | |||||
]); | |||||
// Set shares for room users that do not own them | |||||
if ($wallet->id != $user->wallets()->first()->id) { | |||||
$room->shares()->create([ | |||||
'user' => $user->email, | |||||
'rights' => \App\Share::ADMIN, | |||||
]); | |||||
} | |||||
} | |||||
} | |||||
Schema::table( | |||||
'openvidu_rooms', | |||||
function (Blueprint $table) { | |||||
$table->dropForeign('openvidu_rooms_user_id_foreign'); | |||||
$table->dropColumn('user_id'); | |||||
} | |||||
); | |||||
} | |||||
/** | |||||
* Reverse the migrations. | |||||
*/ | |||||
public function down(): void | |||||
{ | |||||
Schema::table( | |||||
'openvidu_rooms', | |||||
function (Blueprint $table) { | |||||
$table->dropForeign('openvidu_rooms_tenant_id_foreign'); | |||||
$table->dropColumn('tenant_id'); | |||||
$table->dropColumn('description'); | |||||
$table->dropSoftDeletes(); | |||||
$table->bigInteger('user_id')->nullable(); | |||||
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); | |||||
} | |||||
); | |||||
// TODO: Set user_id back | |||||
\App\Entitlement::where('entitleable_type', \App\Meet\Room::class)->forceDelete(); | |||||
\App\Sku::where('title', 'room')->delete(); | |||||
Schema::dropIfExists('shares'); | |||||
} | |||||
}; |