Skip to content

Commit

Permalink
Merge pull request #215 from wri/feat/TM-921-hbf-new-fields
Browse files Browse the repository at this point in the history
[HOTFIX] [TM-921] HBF new fields
  • Loading branch information
roguenet authored May 23, 2024
2 parents 8a03c70 + 89aef7b commit 250bad4
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 26 deletions.
5 changes: 4 additions & 1 deletion app/Helpers/LinkedFieldsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ public static function fields(): Collection
foreach (data_get(self::config(), 'models', []) as $model) {
foreach ($includes as $section) {
foreach (data_get($model, $section, []) as $fieldKey => $value) {
throw_if(self::fields()->where('field_key', $fieldKey)->count() > 0, 'Repeated field');
throw_if(
self::fields()->where('field_key', $fieldKey)->count() > 0,
"Repeated field [$fieldKey]"
);

self::$fields->add(array_merge(['field_key' => $fieldKey], $value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Http\Controllers\Controller;
use App\Http\Requests\V2\Forms\UpdateFormSubmissionRequest;
use App\Http\Resources\V2\Forms\AnswersCollection;
use App\Http\Resources\V2\Forms\FormSubmissionResource;
use App\Models\V2\Forms\FormSubmission;
use Illuminate\Support\Facades\App;
Expand All @@ -18,7 +17,7 @@ public function __invoke(FormSubmission $formSubmission, UpdateFormSubmissionReq

$data = $updateFormSubmissionRequest->validated();

$data['answers'] = $formSubmission->updateAllAnswers($updateFormSubmissionRequest->get('answers')); //AnswersCollection::fromArray($updateFormSubmissionRequest->get('answers'));
$data['answers'] = $formSubmission->updateAllAnswers($updateFormSubmissionRequest->get('answers'));
$data['user_id'] = Auth::user()->uuid;

$formSubmission->update($data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Models\V2\Forms\Application;
use App\Models\V2\Forms\Form;
use App\Models\V2\Projects\Project;
use App\Models\V2\TreeSpecies\TreeSpecies;
use App\StateMachines\EntityStatusStateMachine;
use Illuminate\Http\Request;

Expand Down Expand Up @@ -37,24 +38,24 @@ public function __invoke(Request $request): EntityWithSchemaResource
'project_status' => null,
'name' => $projectPitch->project_name,
'boundary_geojson' => $projectPitch->proj_boundary,
'land_use_types' => $projectPitch->land_use_types,
'restoration_strategy' => null,
'land_use_types' => $projectPitch->land_use_types ?? $projectPitch->land_systems,
'restoration_strategy' => $projectPitch->restoration_strategy ?? $projectPitch->tree_restoration_practices,
'country' => $projectPitch->project_country,
'continent' => null,
'planting_start_date' => $projectPitch->expected_active_restoration_start_date,
'planting_end_date' => $projectPitch->expected_active_restoration_end_date,
'description' => $projectPitch->expected_active_restoration_end_date,
'history' => $projectPitch->description_of_project_timeline,
'description' => $projectPitch->description_of_project_timeline,
'history' => $projectPitch->curr_land_degradation,
'objectives' => $projectPitch->project_objectives,
'environmental_goals' => $projectPitch->environmental_goals,
'socioeconomic_goals' => $projectPitch->socioeconomic_goals,
'socioeconomic_goals' => $projectPitch->proj_impact_socieconom,
'sdgs_impacted' => null,
'long_term_growth' => null,
'community_incentives' => null,
'budget' => $projectPitch->project_budget,
'jobs_created_goal' => null,
'total_hectares_restored_goal' => null,
'trees_grown_goal' => null,
'jobs_created_goal' => $projectPitch->num_jobs_created,
'total_hectares_restored_goal' => $projectPitch->total_hectares,
'trees_grown_goal' => $projectPitch->total_trees,
'survival_rate' => null,
'year_five_crown_cover' => null,
'monitored_tree_cover' => null,
Expand All @@ -76,12 +77,29 @@ public function __invoke(Request $request): EntityWithSchemaResource
'pct_beneficiaries_small' => $projectPitch->pct_beneficiaries_small,
'pct_beneficiaries_large' => $projectPitch->pct_beneficiaries_large,
'pct_beneficiaries_youth' => $projectPitch->pct_beneficiaries_youth,
'land_tenure_project_area' => $projectPitch->land_tenure_project_area,
'land_tenure_project_area' => $projectPitch->land_tenure_proj_area,
'detailed_project_budget' => $projectPitch->detailed_project_budget,
'proof_of_land_tenure_mou' => $projectPitch->proof_of_land_tenure_mou,

'detailed_intervention_types' => $projectPitch->detailed_intervention_types,
'proj_impact_foodsec' => $projectPitch->proj_impact_foodsec,
'pct_employees_marginalised' => $projectPitch->pct_employees_marginalised,
'pct_beneficiaries_marginalised' => $projectPitch->pct_beneficiaries_marginalised,
'pct_beneficiaries_men' => $projectPitch->pct_beneficiaries_men,
'proposed_gov_partners' => $projectPitch->proposed_gov_partners,
'proposed_num_nurseries' => $projectPitch->proposed_num_nurseries,
'proj_boundary' => $projectPitch->proj_boundary,
'states' => $projectPitch->states,
'proj_impact_biodiv' => $projectPitch->biodiversity_impact,
]);

foreach ($projectPitch->treeSpecies()->get() as $treeSpecies) {
$project->treeSpecies()->create([
'collection' => $treeSpecies->collection ?? TreeSpecies::COLLECTION_PRIMARY,
'name' => $treeSpecies->name,
'amount' => $treeSpecies->amount,
]);
}

$request->user()->projects()->sync([$project->id => ['is_monitoring' => false]], false);
$project->dispatchStatusChangeEvent($request->user());

Expand Down
17 changes: 11 additions & 6 deletions app/Models/Traits/UsesLinkedFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,29 +244,34 @@ private function updateLinkedFieldValue(array $linkedFieldInfo, $answer): void
if ($linkedFieldInfo['link-type'] == 'fields') {
$model->$property = $answer;
$model->save();
} elseif ($linkedFieldInfo['link-type'] == 'relations') {
$inputType = data_get($linkedFieldInfo, 'input_type');
$this->syncRelation($property, $inputType, collect($answer), $model);
}
}

private function syncRelation(string $property, string $inputType, $data): void
private function syncRelation(string $property, string $inputType, $data, $entity = null): void
{
$entity ??= $this;

// This will expand as we complete more tickets in TM-747, until eventually we support all form relations.
if (! in_array($inputType, ['treeSpecies', 'workdays'])) {
return;
}

$class = get_class($this->$property()->make());
$class = get_class($entity->$property()->make());
if (is_a($class, HandlesLinkedFieldSync::class, true)) {
$class::syncRelation($this, $property, $data);
$class::syncRelation($entity, $property, $data);

return;
}

$this->$property()->whereNotIn('uuid', $data->pluck('uuid')->filter())->delete();
$entity->$property()->whereNotIn('uuid', $data->pluck('uuid')->filter())->delete();

// This would be better as a bulk operation, but too much processing is required to make that feasible
// in Eloquent (upsert isn't supported on MorphMany, for instance), and these sets will always be small
// so doing them one at a time is OK.
$entries = $this->$property()->get();
$entries = $entity->$property()->get();
foreach ($data as $entry) {
$model = null;
if (! empty($entry['uuid'])) {
Expand All @@ -277,7 +282,7 @@ private function syncRelation(string $property, string $inputType, $data): void
} else {
// protection against updating a deleted entry
unset($entry['uuid']);
$this->$property()->create($entry);
$entity->$property()->create($entry);
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions app/Models/V2/Projects/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ class Project extends Model implements MediaModel, AuditableContract, EntityMode
'land_tenure_project_area',
'answers',
'ppc_external_id',
'detailed_intervention_types',
'proj_impact_foodsec',
'pct_employees_marginalised',
'pct_beneficiaries_marginalised',
'pct_beneficiaries_men',
'proposed_gov_partners',
'proposed_num_nurseries',
'proj_boundary',
'states',
'proj_impact_biodiv',
];

public $fileConfiguration = [
Expand Down Expand Up @@ -170,6 +180,8 @@ class Project extends Model implements MediaModel, AuditableContract, EntityMode
'restoration_strategy' => 'array',
'sdgs_impacted' => 'array',
'answers' => 'array',
'detailed_intervention_types' => 'array',
'states' => 'array',
];

public const PROJECT_STATUS_NEW = 'new_project';
Expand Down
2 changes: 2 additions & 0 deletions app/Models/V2/Sites/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class Site extends Model implements MediaModel, AuditableContract, EntityModel
'feedback_fields',
'answers',
'ppc_external_id',
'detailed_intervention_types',
];

public $fileConfiguration = [
Expand Down Expand Up @@ -140,6 +141,7 @@ class Site extends Model implements MediaModel, AuditableContract, EntityModel
'restoration_strategy' => 'array',
'answers' => 'array',
'control_site' => 'boolean',
'detailed_intervention_types' => 'array',
];

public function registerMediaConversions(Media $media = null): void
Expand Down
15 changes: 15 additions & 0 deletions config/wri/linked-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,16 +220,19 @@
'pro-pit-pct-beneficiaries-small' => ['property' => 'pct_beneficiaries_small', 'label' => '% Beneficiaries smallholder', 'input_type' => 'number-percentage'],
'pro-pit-pct-beneficiaries-large' => ['property' => 'pct_beneficiaries_large', 'label' => '% Beneficiaries large', 'input_type' => 'number-percentage'],
'pro-pit-pct-beneficiaries-women' => ['property' => 'pct_beneficiaries_women', 'label' => '% Beneficiaries women', 'input_type' => 'number-percentage'],
'pro-pit-pct-beneficiaries-men' => ['property' => 'pct_beneficiaries_men', 'label' => '% Beneficiaries men', 'input_type' => 'number-percentage'],
'pro-pit-pct-beneficiaries-35below' => ['property' => 'pct_beneficiaries_youth', 'label' => '% Beneficiaries youth', 'input_type' => 'number-percentage'],
'pro-pit-pct-beneficiaries-sch-classes' => ['property' => 'pct_beneficiaries_scheduled_classes', 'label' => '% Beneficiaries Scheduled Classes', 'input_type' => 'number-percentage'],
'pro-pit-pct-beneficiaries-sch-tribes' => ['property' => 'pct_beneficiaries_scheduled_tribes', 'label' => '% Beneficiaries Scheduled Tribes', 'input_type' => 'number-percentage'],
'pro-pit-pct-beneficiaries-marginalised' => ['property' => 'pct_beneficiaries_marginalised', 'label' => '% Beneficiaries Marginalised Communities', 'input_type' => 'number-percentage'],

'pro-pit-main-degradation_causes' => ['property' => 'main_degradation_causes', 'label' => 'Main degradation causes', 'input_type' => 'long-text'],
'pro-pit-seedlings-source' => ['property' => 'seedlings_source', 'label' => 'Seedlings source', 'input_type' => 'long-text'],
'pro-pit-pct-employees-men' => ['property' => 'pct_employees_men', 'label' => '% of total employees that would be men', 'input_type' => 'number-percentage'],
'pro-pit-pct-employees-women' => ['property' => 'pct_employees_women', 'label' => '% of total employees that would be women', 'input_type' => 'number-percentage'],
'pro-pit-pct-employees-18to35' => ['property' => 'pct_employees_18to35', 'label' => '% of total employees that would be between the ages of 18 and 35', 'input_type' => 'number-percentage'],
'pro-pit-pct-employees-older35' => ['property' => 'pct_employees_older35', 'label' => '% of total employees that would be older than 35 years of age', 'input_type' => 'number-percentage'],
'pro-pit-pct-employees-marginalised' => ['property' => 'pct_employees_marginalised', 'label' => '% of total employees that would be part of a marginalised community', 'input_type' => 'number-percentage'],

'pro-pit-states' => ['property' => 'states', 'label' => 'States', 'input_type' => 'select', 'multichoice' => true, 'option_list_key' => 'states'],
'pro-pit-hec-yr1' => ['property' => 'hectares_first_yr', 'label' => 'Hectares to be restored in the first year', 'input_type' => 'number'],
Expand Down Expand Up @@ -314,11 +317,22 @@
'pro-pct-employees-women' => ['property' => 'pct_employees_women', 'label' => '% of total employees that would be women', 'input_type' => 'number-percentage'],
'pro-pct-employees-18to35' => ['property' => 'pct_employees_18to35', 'label' => '% of total employees that would be between the ages of 18 and 35', 'input_type' => 'number-percentage'],
'pro-pct-employees-older35' => ['property' => 'pct_employees_older35', 'label' => '% of total employees that would be older than 35 years of age', 'input_type' => 'number-percentage'],
'pro-pct-employees-marginalised' => ['property' => 'pct_employees_marginalised', 'label' => '% of total employees that would be part of a marginalised community', 'input_type' => 'number-percentage'],
'pro-beneficiaries' => ['property' => 'proj_beneficiaries', 'label' => 'Project beneficiaries Total', 'input_type' => 'number'],
'pro-pct-beneficiaries-women' => ['property' => 'pct_beneficiaries_women', 'label' => '% of female beneficiaries', 'input_type' => 'number-percentage'],
'pro-pct-beneficiaries-men' => ['property' => 'pct_beneficiaries_men', 'label' => '% Beneficiaries men', 'input_type' => 'number-percentage'],
'pro-pct-beneficiaries-small' => ['property' => 'pct_beneficiaries_small', 'label' => '% of smallholder farmers beneficiaries', 'input_type' => 'number-percentage'],
'pro-pct-beneficiaries-large' => ['property' => 'pct_beneficiaries_large', 'label' => '% of large-scale farmers beneficiaries', 'input_type' => 'number-percentage'],
'pro-pct-beneficiaries-35below' => ['property' => 'pct_beneficiaries_youth', 'label' => '% of beneficiaries younger than 36', 'input_type' => 'number-percentage'],
'pro-pct-beneficiaries-marginalised' => ['property' => 'pct_beneficiaries_marginalised', 'label' => '% Beneficiaries Marginalised Communities', 'input_type' => 'number-percentage'],
'pro-detailed-rst-inv-types' => ['property' => 'detailed_intervention_types', 'label' => 'Detailed intervention types', 'input_type' => 'select', 'multichoice' => true, 'option_list_key' => 'interventions'],
'pro-proj-impact-foodsec' => ['property' => 'proj_impact_foodsec', 'label' => 'Potential project impact: food security', 'input_type' => 'long-text'],
// This is breaking convention for linked field keys because project pitch was already using pro-proposed-gov-partners.
'project-proposed-gov-partners' => ['property' => 'proposed_gov_partners', 'label' => 'Proposed government partners', 'input_type' => 'long-text'],
'pro-proposed-num-nurseries' => ['property' => 'proposed_num_nurseries', 'label' => 'Proposed Number of Nurseries', 'input_type' => 'number'],
'pro-proj-boundary' => ['property' => 'proj_boundary', 'label' => 'Project Boundary', 'input_type' => 'mapInput'],
'pro-states' => ['property' => 'states', 'label' => 'States', 'input_type' => 'select', 'multichoice' => true, 'option_list_key' => 'states'],
'pro-impact-biodiv' => ['property' => 'proj_impact_biodiv', 'label' => 'Biodiversity Impact (project)', 'input_type' => 'long-text'],
],
'file-collections' => [
'pro-col-media' => ['property' => 'media', 'label' => 'Media', 'input_type' => 'file', 'multichoice' => true],
Expand Down Expand Up @@ -509,6 +523,7 @@
'site-end-date' => ['property' => 'end_date', 'label' => 'End date', 'input_type' => 'date'],
'site-description-siting-strategy' => ['property' => 'description_siting_strategy', 'label' => 'Description siting strategy', 'input_type' => 'text'],
'site-col-siting-strategy' => ['property' => 'siting_strategy', 'label' => 'Siting Strategy', 'input_type' => 'select', 'multichoice' => false, 'option_list_key' => 'siting-strategy-collection'],
'site-detailed-rst-inv-types' => ['property' => 'detailed_intervention_types', 'label' => 'Detailed intervention types', 'input_type' => 'select', 'multichoice' => true, 'option_list_key' => 'interventions'],
],
'file-collections' => [
'site-col-media' => ['property' => 'media', 'label' => 'Media', 'input_type' => 'file', 'multichoice' => true],
Expand Down
Loading

0 comments on commit 250bad4

Please sign in to comment.