Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TM-1603] only data from approved reports #659

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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];
Expand Down
37 changes: 21 additions & 16 deletions app/Http/Resources/V2/TreeSpecies/TreeSpeciesTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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')
);
}

Expand Down Expand Up @@ -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 => [],
};
}
Expand Down
2 changes: 1 addition & 1 deletion app/Models/V2/Projects/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
8 changes: 4 additions & 4 deletions app/Models/V2/Sites/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
Loading