feat(service): detach/attach service to customer with activity log

- Customer tab "delete" no longer deletes the service: it detaches the
  customer, discards the order/invoices and logs a `detached` activity
  with the previous customer and order.
- New POST customers/{uuid}/services/attach assigns an unassigned
  service (GET services?unassigned=1) to a customer, creating a pending
  order + unpaid invoice when missing, and logs `attached`.
- Detach/attach also sync module_vmware/module_hosting_accounts
  customer_id: ResolvesVm authorises by module_vmware.customer_id, so a
  detached customer could still control the VM.
- /services detach-customer (single + bulk) go through the same path.
- GET services/{id} returns the service activity logs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
baodev0
2026-10-15 09:39:21 +07:00
co-authored by Claude Opus 5.5
parent 2ca11d42fb
commit ecd6f20a5b
6 changed files with 195 additions and 16 deletions
@@ -5,7 +5,6 @@ namespace App\Services\Customer;
use App\Helpers\OrderHelper;
use App\Mail\ServiceRenewalReminderMail;
use App\Models\Customer;
use App\Models\Invoice;
use App\Models\ModuleVmware;
use App\Models\Order;
use App\Models\Product;
@@ -35,6 +34,7 @@ class CustomerServiceProvisioningService
private readonly VmwareRepositoryInterface $vmwareRepository,
private readonly ServiceRepositoryInterface $serviceRepository,
private readonly ServiceOrderRecorder $orderRecorder,
private readonly CustomerServiceAssignment $assignment,
) {}
public function getServices(string $uuid): Collection
@@ -55,6 +55,11 @@ class CustomerServiceProvisioningService
return $services;
}
public function attachService(string $uuid, int $serviceId): Service
{
return $this->assignment->attach(Service::findOrFail($serviceId), $this->resolveCustomer($uuid));
}
public function bulkDeleteServices(string $uuid, array $ids): int
{
$customer = $this->resolveCustomer($uuid);
@@ -63,9 +68,7 @@ class CustomerServiceProvisioningService
$services = Service::where('customer_id', $customer->id)->whereIn('id', $ids)->get();
foreach ($services as $service) {
$service->delete();
Invoice::where('service_id', $service->id)->delete();
$this->orderRecorder->discardOrder($service->order_item_id);
$this->assignment->detach($service);
}
return $services->count();