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

[MERGE] main -> staging post WW release. #665

Merged
merged 10 commits into from
Jan 27, 2025
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Console\Commands\OneOff;

use App\Models\V2\Forms\FormQuestion;
use Illuminate\Console\Command;

class SetDefaultConditionalValuesForFormQuestions extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'one-off:set-default-conditional-values-for-form-questions';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Sets a default value for conditional values in form questions';

/**
* Execute the console command.
*/
public function handle()
{
FormQuestion::whereIn('id', [
875,878,880,900,907,981,985,1001,1088,1089,1090,1097,1103,1104,1105,1118,1180,1216,1219,1274,1275,1291,1295,1311,1520,1540,1605,1625,1705,1707,1709,1727,1795,1815,1845,1846,1860,1878,1891,1900,1931,1935,1951,1963,1965,2990,2994,3010,3022,3024,3051,3055,3071,3083,3085,3105,3106,3384,3386,3388,3398,3400,3406,3408,3410,3420,3422,3443,3470,3472,3474,3484,3486,
])->each(function (FormQuestion $formQuestion): void {
$formQuestion->children()->update(['is_parent_conditional_default' => true]);
});
}
}
1 change: 1 addition & 0 deletions app/Helpers/RestorationByEcoregionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public static function getCategoryEcoRegion($value, ?bool $isExport = false)
'Cameroonian Highlands forests',
'Celtic broadleaf forests',
'Atlantic Coast restingas',
'Gulf of Oman desert and semi-desert',
],
'neotropical' => [
'Sinú Valley dry forests',
Expand Down
2 changes: 1 addition & 1 deletion app/Helpers/TerrafundDashboardQueryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static function buildQueryFromRequest(Request $request)
$query->when(data_get($filters, 'filter.cohort'), function ($query, $cohort) {
$query->where('v2_projects.cohort', $cohort);
}, function ($query) {
$query->whereIn('v2_projects.cohort', ['terrafund', 'terrafund-landscapes', 'enterprises']);
$query->whereIn('v2_projects.cohort', ['terrafund', 'terrafund-landscapes']);
});

$query->when(data_get($filters, 'filter.landscapes'), function ($query, $landscapes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers\V2\Entities;

use App\Http\Controllers\Controller;
use App\Http\Resources\V2\Seedings\SeedingsCollection;
use App\Http\Resources\V2\TreeSpecies\TreeSpeciesCollection;
use App\Http\Resources\V2\TreeSpecies\TreeSpeciesTransformer;
use App\Models\V2\Disturbance;
Expand All @@ -13,8 +14,10 @@
use App\Models\V2\Projects\ProjectReport;
use App\Models\V2\Seeding;
use App\Models\V2\Sites\Site;
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 @@ -42,6 +45,9 @@ public function __invoke(Request $request, string $relationType, EntityModel $en
return $this->handleTreeSpecies($request, $entity);
}

if ($relationType === 'seedings') {
return $this->handleSeedings($entity);
}
/** @var EntityRelationModel $type */
$type = self::RELATIONS[$relationType];

Expand All @@ -50,16 +56,25 @@ public function __invoke(Request $request, string $relationType, EntityModel $en

private function handleTreeSpecies(Request $request, EntityModel $entity): JsonResource
{
$query = TreeSpecies::query()
->where('speciesable_type', get_class($entity))
->where('speciesable_id', $entity->id)
->visible();
$query = TreeSpecies::query()->visible();

if ($entity instanceof Site || $entity instanceof ProjectReport) {
$speciesableType = Project::class;
$speciesableId = $entity->project_id;
} else {
$speciesableType = get_class($entity);
$speciesableId = $entity->id;
}

$query->where('speciesable_type', $speciesableType)
->where('speciesable_id', $speciesableId);

if ($filter = $request->query('filter')) {
if (! empty($filter['collection'])) {
$query->where('collection', $filter['collection']);
$entityTreeSpecies = $query->get();

$entityTreeSpecies = $query->get()->groupBy('taxon_id')->map(function ($group) {
return $group->first();
})->values();
$transformer = new TreeSpeciesTransformer($entity, $entityTreeSpecies, $filter['collection']);
$transformedData = $transformer->transform();

Expand All @@ -73,4 +88,44 @@ private function handleTreeSpecies(Request $request, EntityModel $entity): JsonR

return new TreeSpeciesCollection($query->get());
}

private function handleSeedings(EntityModel $entity): JsonResource
{
if ($entity instanceof Project) {
$siteReportIds = $entity->approvedSiteReportIds()->pluck('id')->toArray();
} elseif ($entity instanceof Site) {
$siteReportIds = $entity->approvedReportIds()->pluck('id')->toArray();
} elseif ($entity instanceof ProjectReport) {
$siteReportIds = $entity->task->siteReports()
->where('status', ReportStatusStateMachine::APPROVED)
->pluck('id')->toArray();
} elseif ($entity instanceof SiteReport) {
$siteReportIds = [$entity->id];
} else {
return response()->json(['error' => 'Unsupported entity type for seedings.'], 400);
}

$query = Seeding::query()
->where('seedable_type', SiteReport::class)
->whereIn('seedable_id', $siteReportIds)
->visible();

$groupedSeedings = $query->get()
->groupBy('name')
->map(function ($group) {
$first = $group->first();

return new Seeding([
'uuid' => $first->uuid,
'name' => $first->name,
'weight_of_sample' => $group->sum('weight_of_sample'),
'seeds_in_sample' => null,
'amount' => $group->sum('amount'),
]);
})
->sortByDesc('amount')
->values();

return new SeedingsCollection($groupedSeedings);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function __invoke(EntityModel $entity, string $slug)
'calc_area',
])
->where('is_active', 1)
->where('status', 'approved')
->get()
->map(function ($polygon) use ($slugMappings, $slug) {
$indicator = $polygon->{$slugMappings[$slug]['relation_name']}()
Expand Down Expand Up @@ -122,8 +123,14 @@ public function processValuesHectares($values)
foreach ($values as $key => $value) {
$array = explode(',', str_replace('-', '_', $key));
$arrayTrim = array_map('trim', $array);
$counter = 0;
foreach ($arrayTrim as $item) {
$separateKeys[$item] = round((float) $value, 1);
if ($counter == 0) {
$separateKeys[$item] = round((float) $value, 1);
$counter++;
} else {
$separateKeys[$item] = null;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ public function __invoke(EntityModel $entity, string $slug)
$query->where('project_id', $entity->project->id);
}
})
->select(['id', 'poly_id', 'is_active'])
->select(['id', 'poly_id', 'is_active', 'status'])
->where('is_active', 1)
->where('status', 'approved')
->get()
->map(function ($polygon) use ($slugMappings, $slug) {
$indicator = $polygon->{$slugMappings[$slug]['relation_name']}()
Expand Down
1 change: 1 addition & 0 deletions app/Http/Resources/V2/Forms/FormQuestionResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function toArray($request)
'show_on_parent_condition' => $this->show_on_parent_condition,
'linked_field_key' => $this->linked_field_key,
'reference' => $this->buildReference($this->params),
'is_parent_conditional_default' => $this->is_parent_conditional_default,
]);

if (count($this->children) > 0) {
Expand Down
62 changes: 46 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,32 @@ 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'],
]);
$reportTreeSpecies = $this->collectionType === TreeSpecies::COLLECTION_NURSERY ? $this->getProjectReportTreeSpecies() : $this->siteReportTreeSpecies;
$transformedSpecies = $reportTreeSpecies->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 All @@ -83,6 +89,7 @@ private function getSiteReportTreeSpecies(): SupportCollection
$ids = $this->getSiteReportIds();

return TreeSpecies::whereIn('speciesable_id', $ids)
->where('speciesable_type', SiteReport::class)
->where('collection', $this->collectionType)
->where('hidden', false)
->get()
Expand All @@ -100,12 +107,35 @@ private function getSiteReportTreeSpecies(): SupportCollection
->values();
}

private function getProjectReportTreeSpecies(): SupportCollection
{
$id = $this->entity->id;

return TreeSpecies::where('speciesable_id', $id)
->where('speciesable_type', ProjectReport::class)
->where('collection', TreeSpecies::COLLECTION_NURSERY)
->where('hidden', false)
->get()
->groupBy(function ($species) {
return $species->taxon_id ?? $species->name;
})
->map(function ($group) {
return [
'taxon_id' => $group->first()->taxon_id,
'name' => $group->first()->name,
'amount' => $group->sum('amount'),
'collection' => $group->first()->collection,
];
})
->values();
}

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: 2 additions & 0 deletions app/Models/V2/Forms/FormQuestion.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class FormQuestion extends Model
'options_list',
'options_other',
'show_on_parent_condition',
'is_parent_conditional_default',
];

protected $with = [
Expand All @@ -55,6 +56,7 @@ class FormQuestion extends Model
'additional_props' => 'json',
'options_other' => 'boolean',
'show_on_parent_condition' => 'boolean',
'is_parent_conditional_default' => 'boolean',
];

public function section(): BelongsTo
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');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('form_questions', function (Blueprint $table) {
$table->boolean('conditional_default')->default(true);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('form_questions', function (Blueprint $table) {
$table->dropColumn('conditional_default');
});
}
};
Loading
Loading