diff --git a/app/Http/Controllers/V2/Entities/GetRelationsForEntityController.php b/app/Http/Controllers/V2/Entities/GetRelationsForEntityController.php index 98c814b27..363a2a9da 100644 --- a/app/Http/Controllers/V2/Entities/GetRelationsForEntityController.php +++ b/app/Http/Controllers/V2/Entities/GetRelationsForEntityController.php @@ -17,6 +17,7 @@ use App\Models\V2\Sites\SiteReport; use App\Models\V2\Stratas\Strata; use App\Models\V2\TreeSpecies\TreeSpecies; +use App\StateMachines\ReportStatusStateMachine; use Illuminate\Http\Request; use Illuminate\Http\Resources\Json\JsonResource; @@ -82,12 +83,12 @@ private function handleTreeSpecies(Request $request, EntityModel $entity): JsonR private function handleSeedings(EntityModel $entity): JsonResource { if ($entity instanceof Project) { - $siteReportIds = $entity->submittedSiteReportIds()->pluck('id')->toArray(); + $siteReportIds = $entity->approvedSiteReportIds()->pluck('id')->toArray(); } elseif ($entity instanceof Site) { - $siteReportIds = $entity->submittedReportIds()->pluck('id')->toArray(); + $siteReportIds = $entity->approvedReportIds()->pluck('id')->toArray(); } elseif ($entity instanceof ProjectReport) { $siteReportIds = $entity->task->siteReports() - ->whereNotIn('status', SiteReport::UNSUBMITTED_STATUSES) + ->where('status', ReportStatusStateMachine::APPROVED) ->pluck('id')->toArray(); } elseif ($entity instanceof SiteReport) { $siteReportIds = [$entity->id]; diff --git a/app/Http/Resources/V2/TreeSpecies/TreeSpeciesTransformer.php b/app/Http/Resources/V2/TreeSpecies/TreeSpeciesTransformer.php index 047788ce9..92f58004e 100644 --- a/app/Http/Resources/V2/TreeSpecies/TreeSpeciesTransformer.php +++ b/app/Http/Resources/V2/TreeSpecies/TreeSpeciesTransformer.php @@ -8,6 +8,7 @@ use App\Models\V2\Sites\Site; use App\Models\V2\Sites\SiteReport; use App\Models\V2\TreeSpecies\TreeSpecies; +use App\StateMachines\ReportStatusStateMachine; use Illuminate\Database\Eloquent\Collection; use Illuminate\Support\Collection as SupportCollection; @@ -54,27 +55,31 @@ public function transform(): Collection $newSpecies = $this->getNewSpecies(); + $combinedSpecies = $this->entityTreeSpecies->concat($newSpecies); + return new Collection( - $this->entityTreeSpecies->concat($newSpecies) + $combinedSpecies->sortByDesc('report_amount') ); } private function transformProjectReport(): Collection { - return new Collection( - $this->siteReportTreeSpecies->map(function ($reportSpecies) { - $species = new TreeSpecies([ - 'name' => $reportSpecies['name'], - 'amount' => $reportSpecies['amount'], - 'collection' => $reportSpecies['collection'], - 'taxon_id' => $reportSpecies['taxon_id'], - ]); + $transformedSpecies = $this->siteReportTreeSpecies->map(function ($reportSpecies) { + $species = new TreeSpecies([ + 'name' => $reportSpecies['name'], + 'amount' => $reportSpecies['amount'], + 'collection' => $reportSpecies['collection'], + 'taxon_id' => $reportSpecies['taxon_id'], + ]); + + $species->report_amount = $reportSpecies['amount']; + $species->is_new_species = false; - $species->report_amount = $reportSpecies['amount']; - $species->is_new_species = false; + return $species; + }); - return $species; - }) + return new Collection( + $transformedSpecies->sortByDesc('report_amount') ); } @@ -104,9 +109,9 @@ private function getSiteReportTreeSpecies(): SupportCollection private function getSiteReportIds(): array { return match (true) { - $this->entity instanceof Project => $this->entity->submittedSiteReportIds()->pluck('id')->toArray(), - $this->entity instanceof Site => $this->entity->submittedReportIds()->pluck('id')->toArray(), - $this->entity instanceof ProjectReport => $this->entity->task->siteReports()->whereNotIn('status', SiteReport::UNSUBMITTED_STATUSES)->pluck('id')->toArray(), + $this->entity instanceof Project => $this->entity->approvedSiteReportIds()->pluck('id')->toArray(), + $this->entity instanceof Site => $this->entity->approvedReportIds()->pluck('id')->toArray(), + $this->entity instanceof ProjectReport => $this->entity->task->siteReports()->where('status', ReportStatusStateMachine::APPROVED)->pluck('id')->toArray(), default => [], }; } diff --git a/app/Models/V2/Projects/Project.php b/app/Models/V2/Projects/Project.php index c95d59a22..5848e0b3b 100644 --- a/app/Models/V2/Projects/Project.php +++ b/app/Models/V2/Projects/Project.php @@ -631,7 +631,7 @@ private function approvedSiteReports(): HasManyThrough * @return HasManyThrough The query of site report IDs for all reports associated with sites that have been * approved, and have a report status approved. */ - private function approvedSiteReportIds(): HasManyThrough + public function approvedSiteReportIds(): HasManyThrough { return $this->approvedSiteReports()->select('v2_site_reports.id'); } diff --git a/app/Models/V2/Sites/Site.php b/app/Models/V2/Sites/Site.php index 54828b05b..3aff26126 100644 --- a/app/Models/V2/Sites/Site.php +++ b/app/Models/V2/Sites/Site.php @@ -472,14 +472,14 @@ public function getTotalHectaresRestoredSumAttribute(): float return round($this->sitePolygons->where('status', 'approved')->sum('calc_area')); } - public function submittedReports(): HasMany + public function approvedReports(): HasMany { return $this->reports() - ->whereNotIn('status', SiteReport::UNSUBMITTED_STATUSES); + ->where('status', ReportStatusStateMachine::APPROVED); } - public function submittedReportIds(): HasMany + public function approvedReportIds(): HasMany { - return $this->submittedReports()->select('id'); + return $this->approvedReports()->select('id'); } }