Files
portal-api/app/Http/Resources/Api/Admin/ProductResource.php
T
baodev0andClaude Opus 5.5 e888a6ed5d feat: Turnstile captcha, menu khách + nút tắt topbar cấu hình từ admin, ẩn setting nhạy cảm
- Turnstile (App\Support\Turnstile, middleware turnstile) cho login admin và login/register/forgot/reset của khách; cấu hình ở setting key=turnstile, secret mã hoá SignRSA.
- Menu sidebar khách (setting client_menu) và nút tắt topbar (setting client_quick_links) do admin sửa qua administrator/client-menu[/quick-links], khách đọc v1/setting/menu, v1/setting/quick-links.
- v1/setting và settings/public chỉ trả whitelist PublicSetting::FIELDS (trước đây lộ bot_token, inet_api_key).
- Cài đặt tài khoản khách (preferences), rate limit theo IP cho register/resend-verify.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 15:53:43 +07:00

75 lines
4.0 KiB
PHP

<?php
namespace App\Http\Resources\Api\Admin;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class ProductResource extends JsonResource
{
public function toArray(Request $request): array
{
$minPrice = $this->whenLoaded('pricings', function () {
return $this->pricings->min('price');
});
$osIds = $this->whenLoaded('operatingSystems', function () {
return $this->operatingSystems->pluck('id')->toArray();
});
return [
'id' => $this->id,
'uuid' => $this->uuid,
'name' => $this->name,
'slug' => $this->slug,
'thumbnail' => $this->thumbnail,
'product_type' => $this->product_type,
'page_type' => $this->resolvePageType($this->product_type),
'product_categories_id' => $this->whenLoaded('category', fn() => $this->category?->id),
'category' => $this->whenLoaded('category', fn() => $this->category?->name),
// Which billing_cycle values this product's category expects (drives cycle
// pickers in the UI) — e.g. domain is annual-only, IT service can be one_time.
'category_billing_cycles' => $this->whenLoaded('category', fn() => $this->category?->billing_cycles),
'service_plan_id' => $this->service_plan_id,
'cpu' => $this->cpu,
'ram' => $this->ram,
'disk' => $this->disk,
'bandwidth' => $this->bandwidth,
'domain_count' => $this->domain_count,
'features' => $this->features,
'setup_fee' => $this->setup_fee,
'min_price' => $minPrice,
'billing_type' => $this->whenLoaded('pricings', function () {
$oneTimeCycles = ['onetime', 'one_time'];
$hasOnetime = $this->pricings->whereIn('billing_cycle', $oneTimeCycles)->isNotEmpty();
$hasRecurring = $this->pricings->whereNotIn('billing_cycle', $oneTimeCycles)->isNotEmpty();
return ($hasOnetime && !$hasRecurring) ? 'onetime' : 'recurring';
}),
'currency' => $this->whenLoaded('pricings', fn () => $this->pricings->first()?->currency ?? 'VND'),
'price_monthly' => $this->whenLoaded('pricings', fn () => optional($this->pricings->firstWhere('billing_cycle', 'monthly'))->price),
'price_quarterly' => $this->whenLoaded('pricings', fn () => optional($this->pricings->firstWhere('billing_cycle', 'quarterly'))->price),
'price_semiannually' => $this->whenLoaded('pricings', fn () => optional($this->pricings->first(fn ($pricing) => in_array($pricing->billing_cycle, ['semi_annually', 'semiannually'], true)))->price),
'price_annually' => $this->whenLoaded('pricings', fn () => optional($this->pricings->firstWhere('billing_cycle', 'annually'))->price),
'price_biennially' => $this->whenLoaded('pricings', fn () => optional($this->pricings->firstWhere('billing_cycle', 'biennially'))->price),
'price_triennially' => $this->whenLoaded('pricings', fn () => optional($this->pricings->firstWhere('billing_cycle', 'triennially'))->price),
'price_onetime' => $this->whenLoaded('pricings', fn () => optional($this->pricings->first(fn ($pricing) => in_array($pricing->billing_cycle, ['onetime', 'one_time'], true)))->price),
'is_active' => $this->is_active,
'created_at' => $this->created_at,
'os_ids' => $osIds,
'module_host_id' => $this->module_host_id,
'module_datastore_id' => $this->module_datastore_id,
];
}
private function resolvePageType(?string $productType): string
{
return match ($productType) {
'vps', 'cloud' => 'service_vps',
'hosting', 'email', 'ssl' => 'service_hosting',
'domain' => 'service_domain',
'blog' => 'blog',
default => 'service_vps',
};
}
}