77 lines
3.1 KiB
PHP
77 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Resources\Client;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
|
|
class CaseDetailResource extends JsonResource
|
|
{
|
|
public function toArray(Request $request): array
|
|
{
|
|
$report = $this->reports->sortByDesc('submitted_at')->first();
|
|
|
|
return [
|
|
'id' => $this->id,
|
|
'code' => $this->code,
|
|
'status' => $this->status,
|
|
'modality' => $this->modality,
|
|
'priority' => $this->priority,
|
|
'note' => $this->note,
|
|
'rejection_reason' => $this->rejection_reason,
|
|
'submitted_at' => $this->submitted_at ? verta($this->submitted_at)->format('%Y/%m/%d - H:i') : null,
|
|
'processing_deadline' => $this->processing_deadline?->toIso8601String(),
|
|
'pet' => $this->whenLoaded('pet', fn () => [
|
|
'name' => $this->pet->name,
|
|
'type' => $this->pet->type,
|
|
'gender' => $this->pet->gender,
|
|
'breed' => $this->pet->breed,
|
|
'age' => $this->pet->birthday ? (int) $this->pet->birthday->diffInYears(now()) : null,
|
|
'weight' => $this->pet->weight,
|
|
'sterilized' => $this->pet->sterilized,
|
|
]),
|
|
'requester' => $this->whenLoaded('requester', fn () => [
|
|
'level' => $this->requester->level,
|
|
'fullname' => $this->requester->fullname,
|
|
'phone' => $this->requester->phone,
|
|
'specialty' => $this->requester->specialty,
|
|
'clinic_name' => $this->requester->clinic_name,
|
|
'clinic_phone' => $this->requester->clinic_phone,
|
|
'province' => $this->requester->province,
|
|
'city' => $this->requester->city,
|
|
]),
|
|
'primary_files' => $this->mediaFiles('primary'),
|
|
'history_files' => $this->mediaFiles('history'),
|
|
'report' => $report ? [
|
|
'id' => $report->id,
|
|
'status' => $report->status,
|
|
'technique' => $report->technique,
|
|
'finding' => $report->finding,
|
|
'diagnose' => $report->diagnose,
|
|
'recommendation' => $report->recommendation,
|
|
'submitted_at' => $report->submitted_at ? verta($report->submitted_at)->format('%Y/%m/%d - H:i') : null,
|
|
'attachments' => $report->attachments->map(fn ($medium) => $this->mediumPayload($medium))->values(),
|
|
] : null,
|
|
];
|
|
}
|
|
|
|
protected function mediaFiles(string $collection): array
|
|
{
|
|
return $this->media
|
|
->filter(fn ($medium) => $medium->pivot->collection === $collection)
|
|
->map(fn ($medium) => $this->mediumPayload($medium))
|
|
->values()
|
|
->all();
|
|
}
|
|
|
|
protected function mediumPayload($medium): array
|
|
{
|
|
return [
|
|
'id' => $medium->id,
|
|
'filename' => $medium->filename,
|
|
'url' => $medium->isPrivate() ? route('dashboard.media.download', $medium) : $medium->url,
|
|
'mime_type' => $medium->mime_type,
|
|
];
|
|
}
|
|
}
|