From 1b98f926c870939a63afb1738cc15df130202a26 Mon Sep 17 00:00:00 2001 From: cesarLima1 <105736261+cesarLima1@users.noreply.github.com> Date: Fri, 29 Nov 2024 15:30:10 -0400 Subject: [PATCH] [TM-1528] send cover image for project in dashboard (#594) * [TM-1528] send cover image for project in dashboard * [TM-1528] send cover image for project in dashboard --- .../ProjectProfileDetailsResource.php | 20 ++++--- app/Models/Traits/HasProjectCoverImage.php | 57 +++++++++++++++++++ 2 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 app/Models/Traits/HasProjectCoverImage.php diff --git a/app/Http/Resources/V2/Dashboard/ProjectProfileDetailsResource.php b/app/Http/Resources/V2/Dashboard/ProjectProfileDetailsResource.php index b20ca295e..c4e18b813 100644 --- a/app/Http/Resources/V2/Dashboard/ProjectProfileDetailsResource.php +++ b/app/Http/Resources/V2/Dashboard/ProjectProfileDetailsResource.php @@ -2,19 +2,18 @@ namespace App\Http\Resources\V2\Dashboard; +use App\Models\Traits\HasProjectCoverImage; use App\Models\V2\Forms\FormOptionListOption; use Illuminate\Http\Resources\Json\JsonResource; class ProjectProfileDetailsResource extends JsonResource { - /** - * Transform the resource into an array. - * - * @param \Illuminate\Http\Request $request - * @return array - */ + use HasProjectCoverImage; + public function toArray($request) { + $coverImage = $this->getProjectCoverImage($this->resource); + $data = [ 'name' => $this->name, 'descriptionObjetive' => $this->objectives, @@ -26,9 +25,16 @@ public function toArray($request) 'targetLandUse' => $this->land_use_types, 'landTenure' => $this->land_tenure_project_area, 'framework' => $this->framework_key, + 'cover_image' => $coverImage ? [ + 'id' => $coverImage->id, + 'url' => $coverImage->getUrl(), + 'thumbnail' => $coverImage->getUrl('thumbnail'), + 'is_cover' => $coverImage->is_cover, + 'mime_type' => $coverImage->mime_type, + ] : null, ]; - return $this->appendFilesToResource($data); + return $data; } public function getCountryLabel($slug) diff --git a/app/Models/Traits/HasProjectCoverImage.php b/app/Models/Traits/HasProjectCoverImage.php new file mode 100644 index 000000000..b6656e9f1 --- /dev/null +++ b/app/Models/Traits/HasProjectCoverImage.php @@ -0,0 +1,57 @@ + get_class($project), 'ids' => [$project->id]], + ['type' => Site::class, 'ids' => $project->sites->pluck('id')->toArray()], + ['type' => Nursery::class, 'ids' => $project->nurseries->pluck('id')->toArray()], + ['type' => ProjectReport::class, 'ids' => $project->reports->pluck('id')->toArray()], + ['type' => SiteReport::class, 'ids' => $project->siteReports->pluck('id')->toArray()], + ['type' => NurseryReport::class, 'ids' => $project->nurseryReports->pluck('id')->toArray()], + ]; + + $coverMedia = Media::where(function ($query) use ($models) { + foreach ($models as $model) { + $query->orWhere(function ($query) use ($model) { + $query->where('model_type', $model['type']) + ->whereIn('model_id', $model['ids']); + }); + } + }) + ->where('is_cover', true) + ->first(); + + if ($coverMedia) { + return $coverMedia; + } + + // If no cover image found, the latest image is sent + return Media::where(function ($query) use ($models) { + foreach ($models as $model) { + $query->orWhere(function ($query) use ($model) { + $query->where('model_type', $model['type']) + ->whereIn('model_id', $model['ids']); + }); + } + }) + ->where(function ($query) { + $query->where('mime_type', 'like', 'image/jpeg') + ->orWhere('mime_type', 'like', 'image/png'); + }) + ->latest() + ->first(); + } +}