|
4 | 4 |
|
5 | 5 | use App\Models\Appointment; |
6 | 6 | use App\Models\User; |
| 7 | +use App\ResourceConditions; |
| 8 | +use App\TimeOfDay; |
7 | 9 | use Illuminate\Http\Request; |
8 | 10 | use Illuminate\Http\Resources\Json\JsonResource; |
9 | 11 |
|
10 | | -/** @mixin Appointment */ |
| 12 | +/** @mixin Appointment |
| 13 | + * @property bool $listing |
| 14 | + * @property bool $showing |
| 15 | + */ |
11 | 16 | class AppointmentResource extends JsonResource |
12 | 17 | { |
| 18 | + use ResourceConditions; |
| 19 | + |
| 20 | + protected array $conditions = ['listing', 'showing']; |
| 21 | + |
13 | 22 | public function toArray(Request $request): array |
14 | 23 | { |
15 | 24 | return [ |
16 | | - 'id' => $this->id, |
17 | | - 'preferred_date' => $this->preferred_date_formatted, |
18 | | - 'preferred_time' => $this->preferred_time_formatted, |
19 | | - 'symptoms' => $this->symptoms, |
| 25 | + 'appointment' => [ |
| 26 | + 'id' => $this->when($this->listing, $this->id), |
| 27 | + 'preferred_date_formatted' => $this->when($this->listing, $this->preferred_date_formatted), |
| 28 | + 'preferred_time_formatted' => $this->when($this->listing, $this->preferred_time_formatted), |
| 29 | + 'preferred_date' => $this->when($this->showing, $this->preferred_date->toDateString()), |
| 30 | + 'preferred_time' => $this->when($this->showing, TimeOfDay::toInputData($this->preferred_time)), |
| 31 | + 'symptoms' => $this->symptoms, |
| 32 | + ], |
20 | 33 | 'animal' => [ |
21 | 34 | 'name' => $this->animal->name, |
22 | 35 | 'type' => $this->animal->type, |
23 | | - 'age' => $this->animal_age_formatted, |
| 36 | + 'age_human' => $this->animal_age->human(), |
| 37 | + 'age_years' => $this->animal_age->years(), |
| 38 | + 'age_months' => $this->animal_age->months(), |
24 | 39 | ], |
25 | 40 | 'client' => [ |
26 | 41 | 'name' => $this->animal->client->name, |
| 42 | + 'email' => $this->when($this->showing, $this->animal->client->email), |
27 | 43 | ], |
28 | 44 | 'medic' => $this->whenLoaded('medic', fn (User $medic) => [ |
29 | 45 | 'name' => $medic->name, |
|
0 commit comments