AppointmentResource.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace App\Http\Resources;
  3. use Carbon\Carbon;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Http\Resources\Json\JsonResource;
  6. class AppointmentResource extends JsonResource
  7. {
  8. public function toArray(Request $request): array
  9. {
  10. return [
  11. 'id' => $this->id,
  12. 'order_number' => $this->order_number,
  13. 'user_id' => $this->user_id,
  14. 'user' => $this->whenLoaded('user', fn() => new UserResource($this->user)),
  15. 'user_dependent_id' => $this->user_dependent_id,
  16. 'user_dependent' => $this->whenLoaded('userDependent', fn() => $this->userDependent ? [
  17. 'id' => $this->userDependent->id,
  18. 'name' => $this->userDependent->name,
  19. 'kinship' => $this->userDependent->kinship,
  20. ] : null),
  21. 'is_for_dependent' => $this->user_dependent_id !== null,
  22. 'partner_agreement_id' => $this->partner_agreement_id,
  23. 'partner_agreement' => $this->whenLoaded('partnerAgreement', fn() => new PartnerAgreementResource($this->partnerAgreement)),
  24. 'partner_agreement_service_id' => $this->partner_agreement_service_id,
  25. 'partner_agreement_service' => $this->whenLoaded('partnerAgreementService', fn() => new PartnerAgreementServiceResource($this->partnerAgreementService)),
  26. 'date' => $this->date?->format('Y-m-d'),
  27. 'time' => $this->time ? Carbon::parse($this->time)->format('H:i') : null,
  28. 'observations' => $this->observations,
  29. 'requested_at' => $this->requested_at?->format('Y-m-d H:i:s'),
  30. 'status' => $this->status,
  31. 'service_price' => $this->service_price,
  32. 'guide_issued_at' => $this->guide_issued_at?->format('Y-m-d H:i:s'),
  33. 'guide_valid_until' => $this->guide_valid_until?->format('Y-m-d'),
  34. 'can_issue_guide' => $this->resource instanceof \App\Models\Appointment
  35. ? app(\App\Services\AppointmentGuideService::class)->canIssue($this->resource)
  36. : false,
  37. 'auto_approved' => $this->auto_approved,
  38. 'approved_by_user_id' => $this->approved_by_user_id,
  39. 'approved_by_user' => $this->whenLoaded('approvedByUser', fn() => new UserResource($this->approvedByUser)),
  40. 'created_at' => Carbon::parse($this->created_at)->format('Y-m-d H:i:s'),
  41. 'updated_at' => Carbon::parse($this->updated_at)->format('Y-m-d H:i:s'),
  42. ];
  43. }
  44. }