Files
portal-api/app/Http/Controllers/Api/Admin/Hosting/ServicePlanController.php
T
baodev0andClaude Opus 5.5 30898ea46f feat: email gửi tay/nhắc gia hạn, gói hosting, sửa dữ liệu sản phẩm
- Gửi email theo template cho 1 khách, tab lịch sử email, nhắc gia hạn dịch vụ hàng loạt
- Mail lưu from/bcc/sent_by; mẫu MARKETING và mẫu cũ trong TemplateSeeder
- Gói hosting: giới hạn, trạng thái, lấy plan Plesk qua CLI gateway
- Backfill payment_method và ngày đơn hàng còn thiếu
- Bổ sung field CPU/RAM/Disk/Băng thông cho loại sản phẩm vps và cloud

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 11:32:05 +07:00

107 lines
3.2 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', 'dnsServer')->orderBy('sort_order')->orderBy('title')->get();
return response()->json(['error' => false, 'data' => $plans]);
}
public function show(int $id): JsonResponse
{
$plan = ServicePlan::with('plesk', 'dnsServer')->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());
$this->savePlesk($plan, $request->validated());
return response()->json([
'error' => false,
'message' => 'Tạo gói hosting thành công.',
'data' => $plan->load('plesk', 'dnsServer'),
], 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());
$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', 'dnsServer']),
]);
}
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]
);
}
}