update
This commit is contained in:
@@ -5,14 +5,8 @@ namespace App\Services\Api\Order;
|
||||
use App\Enums\Order\OrderStatus;
|
||||
use App\Helpers\OrderHelper;
|
||||
use App\Jobs\Mail\Admin\AdminOrderNotificationJob;
|
||||
use App\Jobs\VirtualMachine\Create\CreateVirtualMachineJob;
|
||||
use App\Models\VmProvisionLock;
|
||||
use App\Models\Addon;
|
||||
use App\Models\Log as CustomerActivityLog;
|
||||
use App\Models\Order;
|
||||
use App\Models\OrderItem;
|
||||
use App\Models\Service;
|
||||
use App\Models\ServiceAddon;
|
||||
use App\Repositories\Api\Invoice\InvoiceRepository;
|
||||
use App\Repositories\Api\Order\OrderRepository;
|
||||
use App\Repositories\Api\Service\ServiceRepository;
|
||||
@@ -144,110 +138,6 @@ class OrderService
|
||||
});
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Private — provisionService
|
||||
// =========================================================================
|
||||
|
||||
private function provisionService(Order $order, OrderItem $item, $customer): void
|
||||
{
|
||||
$invoice = $order->invoice;
|
||||
$months = $item->months ?: OrderHelper::cycleToMonths($item->billing_cycle);
|
||||
$expiresAt = now()->addMonths($months);
|
||||
|
||||
$service = Service::create([
|
||||
'customer_id' => $customer->id,
|
||||
'product_id' => $item->product_id,
|
||||
'type' => $item->product?->product_type,
|
||||
'order_item_id' => $item->id,
|
||||
'invoice_id' => $invoice?->id,
|
||||
'server_name' => $item->server_name,
|
||||
'status' => 'pending',
|
||||
'auto_renew' => false,
|
||||
'started_at' => now(),
|
||||
'expires_at' => $expiresAt,
|
||||
]);
|
||||
|
||||
$this->attachServiceAddons($service, $item);
|
||||
|
||||
// VPS: requires product + os_id → dispatch VM creation job
|
||||
if ($item->product_id && $item->os_id) {
|
||||
$this->dispatchVmJob($order, $item, $service, $customer);
|
||||
} else {
|
||||
// Hosting / Domain / non-VM services activate immediately
|
||||
$service->update(['status' => 'active']);
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Create ServiceAddon rows from:
|
||||
* 1. Base product specs (cpu, ram, disk, bandwidth)
|
||||
* 2. Customer's configurable addon selections stored in addons_meta
|
||||
*/
|
||||
private function attachServiceAddons(Service $service, OrderItem $item): void
|
||||
{
|
||||
// 1. Base product specs
|
||||
$product = $item->product;
|
||||
if ($product) {
|
||||
foreach (['cpu' => $product->cpu, 'ram' => $product->ram, 'disk' => $product->disk, 'bandwidth' => $product->bandwidth] as $type => $value) {
|
||||
if ($value !== null && $value !== '') {
|
||||
ServiceAddon::create([
|
||||
'service_id' => $service->id,
|
||||
'addon_type' => $type,
|
||||
'addon_value' => (string) $value,
|
||||
'price' => 0,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Customer-selected addons
|
||||
$addonsMeta = is_array($item->addons_meta)
|
||||
? $item->addons_meta
|
||||
: json_decode($item->addons_meta ?? '[]', true);
|
||||
|
||||
foreach ((array) $addonsMeta as $meta) {
|
||||
$addon = Addon::find($meta['addon_id'] ?? null);
|
||||
if (!$addon) {
|
||||
continue;
|
||||
}
|
||||
ServiceAddon::create([
|
||||
'service_id' => $service->id,
|
||||
'addon_type' => strtolower((string) $addon->code),
|
||||
'addon_value' => (string) ($meta['qty'] ?? 1),
|
||||
'price' => (int) ($meta['price'] ?? $addon->price),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
private function dispatchVmJob(Order $order, OrderItem $item, Service $service, $customer): void
|
||||
{
|
||||
try {
|
||||
if (VmProvisionLock::isRunning($customer->id)) {
|
||||
Log::warning('dispatchVmJob: blocked — customer already has a running VM provision', [
|
||||
'customer_id' => $customer->id,
|
||||
'order_item_id' => $item->id,
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
dispatch(new CreateVirtualMachineJob($item));
|
||||
|
||||
Log::info('dispatchVmJob: CreateVirtualMachineJob dispatched', [
|
||||
'order_id' => $order->id,
|
||||
'service_id' => $service->id,
|
||||
'order_item_id' => $item->id,
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
Log::error('dispatchVmJob: exception', [
|
||||
'order_id' => $order->id,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Helpers
|
||||
|
||||
Reference in New Issue
Block a user