Merge branch 'develop'
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -20,3 +20,4 @@ yarn-error.log
|
||||
/storage/views/footer.blade.php
|
||||
rr
|
||||
.rr.yaml
|
||||
.DS_Store
|
||||
|
||||
@@ -7,7 +7,9 @@ use App\DataTransferObjects\Support\Data;
|
||||
use App\Models\Provider;
|
||||
use App\Models\ProviderPlan;
|
||||
use App\Models\ProviderRegion;
|
||||
use App\Models\Server;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Spatie\LaravelData\Attributes\Validation\AlphaDash;
|
||||
use Spatie\LaravelData\Attributes\Validation\Exists;
|
||||
use Spatie\LaravelData\Attributes\Validation\In;
|
||||
@@ -21,6 +23,9 @@ class ServerData extends Data
|
||||
use BelongsToUser;
|
||||
|
||||
public function __construct(
|
||||
public ?int $id = null,
|
||||
#[StringType]
|
||||
public ?string $status = null,
|
||||
#[StringType, AlphaDash, Max( 40 )]
|
||||
public string $name,
|
||||
#[NotIn( 0 ), Exists( Provider::class, 'id' )]
|
||||
@@ -33,5 +38,11 @@ class ServerData extends Data
|
||||
public string $database_type,
|
||||
#[Exists( User::class, 'id' ), IntegerType]
|
||||
public ?int $user_id = null,
|
||||
public ?Carbon $created_at = null,
|
||||
) {}
|
||||
|
||||
public static function fromModel(Server $server): static
|
||||
{
|
||||
return static::from(array_merge($server->toArray(), ['user_id' => $server->user->id]));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,13 +5,13 @@ namespace App\DataTransferObjects;
|
||||
use App\DataTransferObjects\Support\Concerns\BelongsToUser;
|
||||
use App\DataTransferObjects\Support\Data;
|
||||
use App\DataTransferObjects\Support\Rules\CustomRule;
|
||||
use App\DataTransferObjects\Support\WithUser;
|
||||
use App\Models\Server;
|
||||
use App\Models\Site;
|
||||
use App\Models\User;
|
||||
use App\Rules\Hostname;
|
||||
use App\Rules\ValidateMaximumSites;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Spatie\LaravelData\Attributes\Validation\Exists;
|
||||
use Spatie\LaravelData\Attributes\Validation\IntegerType;
|
||||
use Spatie\LaravelData\Attributes\Validation\StringType;
|
||||
@@ -29,6 +29,7 @@ class SiteData extends Data
|
||||
public ?string $domain = null,
|
||||
#[Exists(User::class, 'id'), IntegerType]
|
||||
public ?int $user_id = null,
|
||||
public ?Carbon $created_at = null,
|
||||
) {}
|
||||
|
||||
public static function authorize(): bool
|
||||
@@ -40,6 +41,11 @@ class SiteData extends Data
|
||||
return auth()->user()->can('create', Site::class);
|
||||
}
|
||||
|
||||
public static function fromModel(Site $site): static
|
||||
{
|
||||
return static::from(array_merge($site->toArray(), ['user_id' => $site->user->id]));
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return Arr::only(parent::toArray(), [
|
||||
@@ -47,6 +53,8 @@ class SiteData extends Data
|
||||
'status',
|
||||
'server_id',
|
||||
'domain',
|
||||
'user_id',
|
||||
'created_at',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,6 @@ class ServerController extends Controller
|
||||
ServerData::validate($data)
|
||||
);
|
||||
|
||||
return response(content: ['data' => ServerData::from($server)->toArray()], status: 201);
|
||||
return response(content: ['data' => ServerData::from($server->refresh())->toArray()], status: 201);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
|
||||
|
||||
class Server extends Model
|
||||
{
|
||||
@@ -41,6 +42,11 @@ class Server extends Model
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
public function user(): HasOneThrough
|
||||
{
|
||||
return $this->hasOneThrough(User::class, UserService::class, 'service_id', 'id', 'id', 'user_id');
|
||||
}
|
||||
|
||||
public function logs()
|
||||
{
|
||||
return $this->morphMany(SystemLog::class, 'model');
|
||||
|
||||
@@ -6,6 +6,7 @@ use App\Casts\SiteAlias;
|
||||
use DateTimeInterface;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class Site extends Model
|
||||
@@ -72,6 +73,11 @@ class Site extends Model
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
public function user(): HasOneThrough
|
||||
{
|
||||
return $this->hasOneThrough(User::class, UserService::class, 'service_id', 'id', 'id', 'user_id');
|
||||
}
|
||||
|
||||
public function logs()
|
||||
{
|
||||
return $this->morphMany(SystemLog::class, 'model');
|
||||
|
||||
@@ -4,6 +4,8 @@ namespace Database\Factories;
|
||||
|
||||
use App\Models\Server;
|
||||
use App\Models\Site;
|
||||
use App\Models\User;
|
||||
use Closure;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
class SiteFactory extends Factory
|
||||
@@ -30,4 +32,11 @@ class SiteFactory extends Factory
|
||||
{
|
||||
return $this->set('domain', $domain);
|
||||
}
|
||||
|
||||
public function withUser(User|Closure $user): static
|
||||
{
|
||||
return $this->afterCreating(function(Site $site) use ($user): void {
|
||||
$site->users()->attach($user);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,9 +22,9 @@ it('can create a new server', function () {
|
||||
'id' => 1,
|
||||
'status' => 'created',
|
||||
'type' => 'server',
|
||||
'name' => 'example-server'
|
||||
]
|
||||
])
|
||||
'name' => 'example-server',
|
||||
],
|
||||
]),
|
||||
]);
|
||||
|
||||
Bus::fake([FetchServerStatus::class]);
|
||||
@@ -37,7 +37,7 @@ it('can create a new server', function () {
|
||||
$region = ProviderRegion::sole();
|
||||
$plan = ProviderPlan::sole();
|
||||
|
||||
api()
|
||||
$response = api()
|
||||
->post(route('api.server.store'), [
|
||||
'ploi_id' => 2,
|
||||
'provider_id' => $provider->id,
|
||||
@@ -47,7 +47,26 @@ it('can create a new server', function () {
|
||||
'database_type' => 'postgresql',
|
||||
'user_id' => $user->id,
|
||||
])
|
||||
->assertCreated();
|
||||
->assertCreated()
|
||||
->collect()
|
||||
->all();
|
||||
|
||||
$server = Server::sole();
|
||||
|
||||
expect($response)
|
||||
->toBe([
|
||||
'data' => [
|
||||
'id' => $server->id,
|
||||
'status' => 'busy',
|
||||
'name' => 'example-server',
|
||||
'provider_id' => $provider->id,
|
||||
'provider_region_id' => $region->id,
|
||||
'provider_plan_id' => $plan->id,
|
||||
'database_type' => 'postgresql',
|
||||
'user_id' => $user->id,
|
||||
'created_at' => $server->created_at->toIsoString()
|
||||
]
|
||||
]);
|
||||
|
||||
assertDatabaseHas(Server::class, [
|
||||
'name' => 'example-server',
|
||||
|
||||
@@ -15,6 +15,7 @@ it('can list sites', function () {
|
||||
->create();
|
||||
|
||||
$sites = Site::factory(20)
|
||||
->withUser($user = User::factory()->create())
|
||||
->withServer($server)
|
||||
->create();
|
||||
|
||||
@@ -33,6 +34,8 @@ it('can list sites', function () {
|
||||
'status' => $site->status,
|
||||
'server_id' => $site->server_id,
|
||||
'domain' => $site->domain,
|
||||
'user_id' => $site->users()->soleValue('id'),
|
||||
'created_at' => $site->created_at->toIsoString(),
|
||||
])
|
||||
->all(),
|
||||
'links' => [
|
||||
@@ -109,6 +112,7 @@ it('can create a site', function () {
|
||||
'domain' => 'example.ploi.io',
|
||||
'user_id' => $user->id,
|
||||
'server_id' => $server->id,
|
||||
'status' => 'malicious-status'
|
||||
])
|
||||
->assertCreated()
|
||||
->collect()
|
||||
@@ -122,6 +126,8 @@ it('can create a site', function () {
|
||||
'status' => Site::STATUS_ACTIVE,
|
||||
'server_id' => $server->id,
|
||||
'domain' => 'example.ploi.io',
|
||||
'user_id' => $user->id,
|
||||
'created_at' => $site->created_at->toIsoString()
|
||||
],
|
||||
]);
|
||||
|
||||
@@ -147,6 +153,8 @@ it('can create a site', function () {
|
||||
'status' => Site::STATUS_ACTIVE,
|
||||
'server_id' => $server->id,
|
||||
'domain' => 'example.ploi.io',
|
||||
'user_id' => $user->id,
|
||||
'created_at' => $site->created_at->toIsoString()
|
||||
],
|
||||
]);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user