CustomScheduleResource.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace App\Http\Resources;
  3. use App\Models\ScheduleProposal;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Http\Resources\Json\JsonResource;
  6. use Illuminate\Support\Facades\Auth;
  7. use Illuminate\Support\Facades\Storage;
  8. class CustomScheduleResource extends JsonResource
  9. {
  10. /**
  11. * Transform the resource into an array.
  12. *
  13. * @return array<string, mixed>
  14. */
  15. public function toArray(Request $request): array
  16. {
  17. return [
  18. 'id' => $this->id,
  19. 'schedule_id' => $this->schedule_id,
  20. 'proposal_sent' => $this->proposalSentByAuthProvider(),
  21. 'address_type' => $this->address_type,
  22. 'service_type_id' => $this->service_type_id,
  23. 'service_type_name' => $this->whenLoaded('serviceType', function () {
  24. return $this->serviceType->description;
  25. }),
  26. 'description' => $this->description,
  27. 'min_price' => number_format($this->min_price, 2, '.', ''),
  28. 'max_price' => number_format($this->max_price, 2, '.', ''),
  29. 'offers_meal' => $this->offers_meal,
  30. 'created_at' => $this->created_at?->format('Y-m-d H:i'),
  31. 'updated_at' => $this->updated_at?->format('Y-m-d H:i'),
  32. 'schedule' => $this->whenLoaded('schedule', function () {
  33. return [
  34. 'id' => $this->schedule->id,
  35. 'client_id' => $this->schedule->client_id,
  36. 'provider_id' => $this->schedule->provider_id,
  37. 'client_name' => $this->schedule->client?->user?->name,
  38. 'customer_photo' => (function () {
  39. $path = $this->schedule->client?->profileMedia?->path;
  40. return $path ? Storage::temporaryUrl($path, now()->addMinutes(60)) : null;
  41. })(),
  42. 'address_id' => $this->schedule->address_id,
  43. 'address' => $this->schedule->address ? [
  44. 'address' => $this->schedule->address->address,
  45. 'number' => $this->schedule->address->number,
  46. 'district' => $this->schedule->address->district,
  47. 'complement' => $this->schedule->address->complement,
  48. 'zip_code' => $this->schedule->address->zip_code,
  49. 'city' => $this->schedule->address->city,
  50. 'state' => $this->schedule->address->state,
  51. 'address_full' => implode(', ', array_filter([
  52. $this->schedule->address->address,
  53. $this->schedule->address->number ? "nº {$this->schedule->address->number}" : null,
  54. $this->schedule->address->district,
  55. $this->schedule->address->city ? "{$this->schedule->address->city->name}/{$this->schedule->address->state?->code}" : null,
  56. ])),
  57. ] : null,
  58. 'date' => $this->schedule->date?->format('d/m/Y'),
  59. 'period_type' => $this->schedule->period_type,
  60. 'start_time' => $this->schedule->start_time,
  61. 'end_time' => $this->schedule->end_time,
  62. 'status' => $this->schedule->status,
  63. ];
  64. }),
  65. 'specialities' => $this->whenLoaded('specialities', function () {
  66. return $this->specialities->map(function ($item) {
  67. return [
  68. 'id' => $item->id,
  69. 'name' => $item->description,
  70. ];
  71. });
  72. }),
  73. ];
  74. }
  75. private function proposalSentByAuthProvider(): bool
  76. {
  77. $providerId = Auth::user()?->provider?->id;
  78. if (! $providerId) {
  79. return false;
  80. }
  81. return ScheduleProposal::where('schedule_id', $this->schedule_id)
  82. ->where('provider_id', $providerId)
  83. ->exists();
  84. }
  85. }