Skip to content

Commit

Permalink
[TM-1400] Update collections that are missing or incorrect.
Browse files Browse the repository at this point in the history
  • Loading branch information
roguenet committed Nov 26, 2024
1 parent be70f3b commit 8a8a44f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
37 changes: 36 additions & 1 deletion app/Console/Commands/OneOff/UpdateTreeCollections.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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_PLANTED,
'name' => $treeSpecies->name,
'amount' => $treeSpecies->amount,
]);
Expand Down
2 changes: 0 additions & 2 deletions app/Models/V2/TreeSpecies/TreeSpecies.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,13 @@ 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',
self::COLLECTION_PLANTED => 'Planted',
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
Expand Down
4 changes: 2 additions & 2 deletions config/wri/linked-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@
'label' => 'Tree Species',
'resource' => App\Http\Resources\V2\TreeSpecies\TreeSpeciesResource::class,
'input_type' => 'treeSpecies',
'collection' => 'primary',
'collection' => 'tree-planted',
],
],
],
Expand Down Expand Up @@ -364,7 +364,7 @@
'label' => 'Tree Species',
'resource' => 'App\Http\Resources\V2\TreeSpecies\TreeSpeciesResource',
'input_type' => 'treeSpecies',
'collection' => 'primary',
'collection' => 'tree-planted',
],
],
],
Expand Down

0 comments on commit 8a8a44f

Please sign in to comment.