Files
portal-api/app/Http/Resources/Api/Admin/ProductResource.php
T
baodev0 7314313198 Refactor order payment process and enhance invoice details
- Updated `payOrder` method in `OrderControllerRefactor` to accept `orderNum` instead of `id` for better clarity and functionality.
- Implemented `findByOrderNum` method in `OrderRepository` to retrieve orders based on order number and customer ID.
- Enhanced `InvoiceDetailResource` to include VMware details and removed unnecessary fields.
- Added `service_plan_id` to `ProductResource` and updated the `Product` model to establish a relationship with `ServicePlan`.
- Created `ServicePlan` and `ServicePlanPlesk` models along with their respective controllers and migrations for managing hosting service plans.
- Updated API routes to include endpoints for managing service plans.
- Modified email templates to standardize support phone number across various customer communications.
2026-08-11 10:12:25 +07:00

69 lines
3.5 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),
'service_plan_id' => $this->service_plan_id,
'cpu' => $this->cpu,
'ram' => $this->ram,
'disk' => $this->disk,
'bandwidth' => $this->bandwidth,
'setup_fee' => $this->setup_fee,
'min_price' => $minPrice,
'billing_type' => $this->whenLoaded('pricings', function () {
$hasOnetime = $this->pricings->where('billing_cycle', 'onetime')->isNotEmpty();
$hasRecurring = $this->pricings->where('billing_cycle', '!=', 'onetime')->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->firstWhere('billing_cycle', 'semiannually'))->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->firstWhere('billing_cycle', 'onetime'))->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',
};
}
}