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:
co-authored by
Claude Opus 5.5
parent
2ca11d42fb
commit
ecd6f20a5b
@@ -14,13 +14,22 @@ trait ManagesCustomerServices
|
||||
return response()->json(['error' => false, 'data' => $this->serviceProvisioning->getServices($uuid)]);
|
||||
}
|
||||
|
||||
public function attachService(string $uuid, Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate(['service_id' => 'required|integer|exists:services,id']);
|
||||
|
||||
$service = $this->serviceProvisioning->attachService($uuid, $data['service_id']);
|
||||
|
||||
return response()->json(['error' => false, 'message' => 'Đã gán dịch vụ cho khách hàng', 'data' => $service]);
|
||||
}
|
||||
|
||||
public function bulkDeleteServices(string $uuid, Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate(['ids' => 'required|array|min:1', 'ids.*' => 'integer']);
|
||||
|
||||
$deleted = $this->serviceProvisioning->bulkDeleteServices($uuid, $data['ids']);
|
||||
|
||||
return response()->json(['error' => false, 'message' => "Đã xoá {$deleted} dịch vụ", 'data' => ['deleted' => $deleted]]);
|
||||
return response()->json(['error' => false, 'message' => "Đã gỡ {$deleted} dịch vụ khỏi khách hàng", 'data' => ['deleted' => $deleted]]);
|
||||
}
|
||||
|
||||
public function updateService(string $uuid, int $serviceId, Request $request): JsonResponse
|
||||
|
||||
@@ -5,14 +5,19 @@ namespace App\Http\Controllers\Api\Admin\Service;
|
||||
use App\Helpers\OrderHelper;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Jobs\VirtualMachine\Provision\ResendVmCredentialsEmailJob;
|
||||
use App\Models\ActivityLog;
|
||||
use App\Models\DomainPricing;
|
||||
use App\Models\ModuleVmware;
|
||||
use App\Models\Service;
|
||||
use App\Services\Customer\CustomerServiceAssignment;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ServiceController extends Controller
|
||||
{
|
||||
public function __construct(private readonly CustomerServiceAssignment $assignment) {}
|
||||
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$perPage = min((int) ($request->per_page ?? 15), 100);
|
||||
@@ -21,7 +26,8 @@ class ServiceController extends Controller
|
||||
->where(fn($q) => $q->whereNull('order_item_id')->orWhereNull('type')->orWhere('type', '!=', 'domain'))
|
||||
->when($request->filter, fn($q, $v) => $q->where('server_name', 'like', "%{$v}%"))
|
||||
->when($request->status, fn($q, $v) => $q->where('status', $v))
|
||||
->when($request->customer_id, fn($q, $v) => $q->where('customer_id', $v));
|
||||
->when($request->customer_id, fn($q, $v) => $q->where('customer_id', $v))
|
||||
->when($request->boolean('unassigned'), fn($q) => $q->whereNull('customer_id'));
|
||||
|
||||
$paginator = $query->latest()->paginate($perPage);
|
||||
|
||||
@@ -71,6 +77,12 @@ class ServiceController extends Controller
|
||||
|
||||
ModuleVmware::resolveVirtualMachineIds([$service->vmware]);
|
||||
|
||||
$service->setAttribute('activity_logs', ActivityLog::where('subject_type', Service::class)
|
||||
->where('subject_id', $service->id)
|
||||
->latest('id')
|
||||
->limit(50)
|
||||
->get(['id', 'event', 'description', 'causer_type', 'causer_name', 'old_values', 'new_values', 'created_at']));
|
||||
|
||||
return response()->json(['error' => false, 'data' => $service]);
|
||||
}
|
||||
|
||||
@@ -82,14 +94,17 @@ class ServiceController extends Controller
|
||||
'action' => 'required|in:activate,suspend,cancel,detach-customer',
|
||||
]);
|
||||
|
||||
$updates = match ($data['action']) {
|
||||
'activate' => ['status' => 'active', 'suspended_at' => null, 'cancelled_at' => null],
|
||||
'suspend' => ['status' => 'suspended', 'suspended_at' => now()],
|
||||
'cancel' => ['status' => 'cancelled', 'cancelled_at' => now()],
|
||||
'detach-customer' => ['customer_id' => null],
|
||||
};
|
||||
if ($data['action'] === 'detach-customer') {
|
||||
$updated = $this->detachCustomers($data['ids']);
|
||||
} else {
|
||||
$updates = match ($data['action']) {
|
||||
'activate' => ['status' => 'active', 'suspended_at' => null, 'cancelled_at' => null],
|
||||
'suspend' => ['status' => 'suspended', 'suspended_at' => now()],
|
||||
'cancel' => ['status' => 'cancelled', 'cancelled_at' => now()],
|
||||
};
|
||||
|
||||
$updated = Service::whereIn('id', $data['ids'])->update($updates);
|
||||
$updated = Service::whereIn('id', $data['ids'])->update($updates);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
@@ -100,12 +115,21 @@ class ServiceController extends Controller
|
||||
|
||||
public function detachCustomer(int $id): JsonResponse
|
||||
{
|
||||
$service = Service::findOrFail($id);
|
||||
$service->update(['customer_id' => null]);
|
||||
$this->detachCustomers([Service::findOrFail($id)->id]);
|
||||
|
||||
return response()->json(['error' => false, 'message' => 'Đã gỡ khách hàng khỏi dịch vụ.']);
|
||||
}
|
||||
|
||||
private function detachCustomers(array $ids): int
|
||||
{
|
||||
return DB::transaction(function () use ($ids) {
|
||||
$services = Service::with('customer')->whereIn('id', $ids)->whereNotNull('customer_id')->get();
|
||||
$services->each(fn (Service $service) => $this->assignment->detach($service));
|
||||
|
||||
return $services->count();
|
||||
});
|
||||
}
|
||||
|
||||
public function resendCredentials(int $id): JsonResponse
|
||||
{
|
||||
$service = Service::findOrFail($id);
|
||||
|
||||
Reference in New Issue
Block a user