diff --git a/app/Console/Commands/OneOff/UpdateTreeCollections.php b/app/Console/Commands/OneOff/UpdateTreeCollections.php index 75608cc2..294f476c 100644 --- a/app/Console/Commands/OneOff/UpdateTreeCollections.php +++ b/app/Console/Commands/OneOff/UpdateTreeCollections.php @@ -2,7 +2,12 @@ namespace App\Console\Commands\OneOff; +use App\Models\V2\Organisation; +use App\Models\V2\ProjectPitch; +use App\Models\V2\Projects\Project; +use App\Models\V2\Sites\SiteReport; use App\Models\V2\TreeSpecies\TreeSpecies; +use App\Models\V2\UpdateRequests\UpdateRequest; use Illuminate\Console\Command; class UpdateTreeCollections extends Command @@ -26,6 +31,36 @@ class UpdateTreeCollections extends Command */ public function handle() { - TreeSpecies::where('collection', 'restored')->update(['collection' => TreeSpecies::COLLECTION_HISTORICAL]); + $this->info('Updating collections in v2_tree_species'); + TreeSpecies::withoutTimestamps(function () { + TreeSpecies::withTrashed()->where('speciesable_type', ProjectPitch::class) + ->update(['collection' => TreeSpecies::COLLECTION_PLANTED]); + TreeSpecies::withTrashed()->where('speciesable_type', Project::class)->where('collection', 'primary') + ->update(['collection' => TreeSpecies::COLLECTION_PLANTED]); + TreeSpecies::withTrashed()->where('speciesable_type', Organisation::class) + ->update(['collection' => TreeSpecies::COLLECTION_HISTORICAL]); + TreeSpecies::withTrashed()->where('speciesable_type', SiteReport::class)->where('collection', null) + ->update(['collection' => TreeSpecies::COLLECTION_NON_TREE]); + }); + + $this->info('Updating collections in v2_update_requests content'); + // This is kind of a hassle; fortunately, the only model type above that has bad data embedded in update requests + // is Project + UpdateRequest::withoutTimestamps(function () { + $updateRequests = UpdateRequest::where('updaterequestable_type', Project::class) + ->where('content', 'LIKE', '%"collection":"primary"%') + ->get(); + foreach ($updateRequests as $updateRequest) { + $content = $updateRequest->content; + foreach (array_keys($content) as $key) { + $collections = data_get($content, "$key.*.collection"); + if (is_array($collections) && in_array("primary", $collections)) { + data_set($content, "$key.*.collection", TreeSpecies::COLLECTION_PLANTED); + } + } + + $updateRequest->update(['content' => $content]); + } + }); } } diff --git a/app/Http/Controllers/V2/Projects/CreateProjectWithFormController.php b/app/Http/Controllers/V2/Projects/CreateProjectWithFormController.php index ca6495a3..74183c3c 100644 --- a/app/Http/Controllers/V2/Projects/CreateProjectWithFormController.php +++ b/app/Http/Controllers/V2/Projects/CreateProjectWithFormController.php @@ -101,7 +101,7 @@ public function __invoke(Request $request): EntityWithSchemaResource foreach ($projectPitch->treeSpecies()->get() as $treeSpecies) { $project->treeSpecies()->create([ - 'collection' => $treeSpecies->collection ?? TreeSpecies::COLLECTION_PRIMARY, + 'collection' => $treeSpecies->collection ?? TreeSpecies::COLLECTION_HISTORICAL, 'name' => $treeSpecies->name, 'amount' => $treeSpecies->amount, ]); diff --git a/app/Models/V2/TreeSpecies/TreeSpecies.php b/app/Models/V2/TreeSpecies/TreeSpecies.php index ed77eb60..c1995f25 100644 --- a/app/Models/V2/TreeSpecies/TreeSpecies.php +++ b/app/Models/V2/TreeSpecies/TreeSpecies.php @@ -48,7 +48,6 @@ class TreeSpecies extends Model implements EntityRelationModel public const COLLECTION_NON_TREE = 'non-tree'; public const COLLECTION_NURSERY = 'nursery-seedling'; public const COLLECTION_HISTORICAL = 'historical-tree-species'; - public const COLLECTION_PRIMARY = 'primary'; public static $collections = [ self::COLLECTION_DIRECT_SEEDING => 'Direct Seeding', @@ -56,7 +55,6 @@ class TreeSpecies extends Model implements EntityRelationModel self::COLLECTION_NON_TREE => 'Non Tree', self::COLLECTION_NURSERY => 'Nursery Seedling', self::COLLECTION_HISTORICAL => 'Historical Tree Species', - self::COLLECTION_PRIMARY => 'Primary', ]; public static function createResourceCollection(EntityModel $entity): JsonResource diff --git a/config/wri/linked-fields.php b/config/wri/linked-fields.php index c0d212b5..afbe3169 100644 --- a/config/wri/linked-fields.php +++ b/config/wri/linked-fields.php @@ -277,7 +277,7 @@ 'label' => 'Tree Species', 'resource' => App\Http\Resources\V2\TreeSpecies\TreeSpeciesResource::class, 'input_type' => 'treeSpecies', - 'collection' => 'primary', + 'collection' => 'tree-planted', ], ], ], @@ -364,7 +364,7 @@ 'label' => 'Tree Species', 'resource' => 'App\Http\Resources\V2\TreeSpecies\TreeSpeciesResource', 'input_type' => 'treeSpecies', - 'collection' => 'primary', + 'collection' => 'tree-planted', ], ], ],