Add bulk update gateway endpoint to vCenter API routes

- Introduced a new POST route 'bulk-update-gateway' for bulk updating gateways.
- This route is protected by the 'module-vcenter.manager' permission middleware.
This commit is contained in:
baodev0
2026-08-06 15:32:51 +07:00
parent 90f2c97bb4
commit 79f06c2fa2
9 changed files with 5555 additions and 19 deletions
+64 -7
View File
@@ -13,16 +13,12 @@ class IpPool extends Model
use HasFactory, SoftDeletes, LogsActivity;
protected $fillable = [
'uuid',
'uuid',
'ip_address',
'gateway',
'cidr',
'netmask',
'module_network_id',
'gateway',
'cidr',
'netmask',
'status',
'owner_type',
'ip_role',
@@ -32,6 +28,7 @@ class IpPool extends Model
'allocated_at',
'revoked_at',
'allocation_status',
'service_id',
'contact_email',
'contact_cc',
'contact_phone',
@@ -51,9 +48,6 @@ class IpPool extends Model
'monthly_cost' => 'decimal:2',
'selling_price' => 'decimal:2',
];
'status',
'service_id',
];
protected static function boot(): void
{
@@ -64,6 +58,14 @@ class IpPool extends Model
$model->uuid = (string) Str::uuid();
}
});
// Keep ip_pool_contacts in sync whenever contact_email/contact_cc change via normal Eloquent saves.
// Bulk import (WithUpserts) bypasses this — call resyncAllContacts() after Excel::import() instead.
static::saved(function (self $model) {
if ($model->wasChanged(['contact_email', 'contact_cc']) || $model->wasRecentlyCreated) {
$model->syncContacts();
}
});
}
public function network()
@@ -80,4 +82,59 @@ class IpPool extends Model
{
return $this->hasMany(VirtualMachines::class, 'ip_pool_id');
}
public function contacts()
{
return $this->hasMany(IpPoolContact::class);
}
/**
* contact_email/contact_cc may hold multiple addresses separated by comma/semicolon
* (e.g. imported from Google Sheet). Split into a clean array, dropping invalid/empty entries.
*/
public static function splitEmails(?string $value): array
{
if (!$value) {
return [];
}
return collect(preg_split('/[,;]+/', $value))
->map(fn ($e) => trim($e))
->filter(fn ($e) => $e !== '' && filter_var($e, FILTER_VALIDATE_EMAIL))
->unique()
->values()
->all();
}
public function syncContacts(): void
{
$rows = [];
foreach (self::splitEmails($this->contact_email) as $email) {
$rows[] = ['ip_pool_id' => $this->id, 'email' => $email, 'type' => 'to'];
}
foreach (self::splitEmails($this->contact_cc) as $email) {
$rows[] = ['ip_pool_id' => $this->id, 'email' => $email, 'type' => 'cc'];
}
$this->contacts()->delete();
if ($rows) {
IpPoolContact::insert($rows);
}
}
/**
* Full resync for bulk paths (CSV import) that bypass the saved() hook above.
*/
public static function resyncAllContacts(): void
{
self::withTrashed()
->where(function ($q) {
$q->whereNotNull('contact_email')->orWhereNotNull('contact_cc');
})
->chunkById(500, function ($pools) {
foreach ($pools as $pool) {
$pool->syncContacts();
}
});
}
}