- Mail campaign: mẫu nhắc gia hạn thanh toán QR, chọn dịch vụ + chu kỳ từng dòng (renewal_services) - Hosting: nhiều server Plesk cho một gói (service_plan_servers), chuyển server khi server chết - Admin xem/sửa giấy tờ định danh khách, tuỳ chọn gia hạn cho khách, nhật ký cấp phát dịch vụ - Đánh dấu mẫu email dịch vụ là non-marketing Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
109 lines
3.3 KiB
PHP
109 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Admin\Hosting;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Admin\Hosting\ServicePlanRequest;
|
|
use App\Models\DnsServer;
|
|
use App\Models\ServicePlan;
|
|
use App\Services\Plesk\PleskServicePlanService;
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
class ServicePlanController extends Controller
|
|
{
|
|
public function index(): JsonResponse
|
|
{
|
|
$plans = ServicePlan::with('plesk', 'servers')->orderBy('sort_order')->orderBy('title')->get();
|
|
|
|
return response()->json(['error' => false, 'data' => $plans]);
|
|
}
|
|
|
|
public function show(int $id): JsonResponse
|
|
{
|
|
$plan = ServicePlan::with('plesk', 'servers')->find($id);
|
|
|
|
if (!$plan) {
|
|
return response()->json(['error' => true, 'message' => 'Không tìm thấy gói hosting.'], 404);
|
|
}
|
|
|
|
return response()->json(['error' => false, 'data' => $plan]);
|
|
}
|
|
|
|
public function store(ServicePlanRequest $request): JsonResponse
|
|
{
|
|
$plan = ServicePlan::create($request->planAttributes());
|
|
|
|
$plan->syncServers($request->serverIds());
|
|
$this->savePlesk($plan, $request->validated());
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'message' => 'Tạo gói hosting thành công.',
|
|
'data' => $plan->load('plesk', 'servers'),
|
|
], 201);
|
|
}
|
|
|
|
public function update(ServicePlanRequest $request, int $id): JsonResponse
|
|
{
|
|
$plan = ServicePlan::find($id);
|
|
|
|
if (!$plan) {
|
|
return response()->json(['error' => true, 'message' => 'Không tìm thấy gói hosting.'], 404);
|
|
}
|
|
|
|
$plan->update($request->planAttributes());
|
|
|
|
$plan->syncServers($request->serverIds());
|
|
$this->savePlesk($plan, $request->validated());
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'message' => 'Cập nhật gói hosting thành công.',
|
|
'data' => $plan->fresh(['plesk', 'servers']),
|
|
]);
|
|
}
|
|
|
|
public function destroy(int $id): JsonResponse
|
|
{
|
|
$plan = ServicePlan::find($id);
|
|
|
|
if (!$plan) {
|
|
return response()->json(['error' => true, 'message' => 'Không tìm thấy gói hosting.'], 404);
|
|
}
|
|
|
|
$plan->delete();
|
|
|
|
return response()->json(['error' => false, 'message' => 'Xoá gói hosting thành công.']);
|
|
}
|
|
|
|
public function pleskPlans(int $serverId, PleskServicePlanService $plans): JsonResponse
|
|
{
|
|
$server = DnsServer::find($serverId);
|
|
|
|
if (!$server || !$server->servesHosting()) {
|
|
return response()->json(['error' => true, 'message' => 'Server không hỗ trợ hosting.'], 404);
|
|
}
|
|
|
|
$names = $plans->names($server);
|
|
|
|
if ($names === null) {
|
|
return response()->json(['error' => true, 'message' => 'Không lấy được danh sách plan từ Plesk.'], 502);
|
|
}
|
|
|
|
return response()->json(['error' => false, 'data' => $names]);
|
|
}
|
|
|
|
private function savePlesk(ServicePlan $plan, array $data): void
|
|
{
|
|
if (empty($data['plesk_plan_name'])) {
|
|
$plan->plesk()->delete();
|
|
return;
|
|
}
|
|
|
|
$plan->plesk()->updateOrCreate(
|
|
['service_plan_id' => $plan->id],
|
|
['plesk_plan_name' => $data['plesk_plan_name'], 'note' => $data['note'] ?? null]
|
|
);
|
|
}
|
|
}
|