From d87a3fa8015b39c0665b7aa79642aae1b33b49cc Mon Sep 17 00:00:00 2001 From: Nathan Curtis Date: Tue, 28 May 2024 14:38:11 -0700 Subject: [PATCH 1/2] [TM-950] Allow the collection for linked fields to get stored in the DB form question. --- app/Http/Requests/V2/Forms/StoreFormRequest.php | 2 ++ app/Http/Requests/V2/Forms/UpdateFormRequest.php | 2 ++ 2 files changed, 4 insertions(+) diff --git a/app/Http/Requests/V2/Forms/StoreFormRequest.php b/app/Http/Requests/V2/Forms/StoreFormRequest.php index 586bdaf53..b5d2791fa 100644 --- a/app/Http/Requests/V2/Forms/StoreFormRequest.php +++ b/app/Http/Requests/V2/Forms/StoreFormRequest.php @@ -34,6 +34,7 @@ public function rules() 'form_sections.*.form_questions' => ['sometimes', 'array'], 'form_sections.*.form_questions.*.linked_field_key' => ['sometimes', 'nullable', 'string'], + 'form_sections.*.form_questions.*.collection' => ['sometimes', 'nullable', 'string'], 'form_sections.*.form_questions.*.label' => ['required', 'string'], 'form_sections.*.form_questions.*.input_type' => ['sometimes', 'nullable'], 'form_sections.*.form_questions.*.name' => ['sometimes', 'nullable'], @@ -58,6 +59,7 @@ public function rules() 'form_sections.*.form_questions.*.child_form_questions' => ['sometimes'], 'form_sections.*.form_questions.*.child_form_questions.*.linked_field_key' => ['sometimes', 'required', 'string'], + 'form_sections.*.form_questions.*.child_form_questions.*.collection' => ['sometimes', 'nullable', 'string'], 'form_sections.*.form_questions.*.child_form_questions.*.label' => ['sometimes', 'required', 'string'], 'form_sections.*.form_questions.*.child_form_questions.*.name' => ['sometimes', 'nullable'], 'form_sections.*.form_questions.*.child_form_questions.*.input_type' => ['sometimes', 'nullable'], diff --git a/app/Http/Requests/V2/Forms/UpdateFormRequest.php b/app/Http/Requests/V2/Forms/UpdateFormRequest.php index 6e02393a9..c7dea033a 100644 --- a/app/Http/Requests/V2/Forms/UpdateFormRequest.php +++ b/app/Http/Requests/V2/Forms/UpdateFormRequest.php @@ -34,6 +34,7 @@ public function rules() 'form_sections.*.form_questions' => ['sometimes', 'nullable', 'array'], 'form_sections.*.form_questions.*.linked_field_key' => ['sometimes', 'nullable', 'string'], + 'form_sections.*.form_questions.*.collection' => ['sometimes', 'nullable', 'string'], 'form_sections.*.form_questions.*.label' => ['sometimes', 'nullable', 'string'], 'form_sections.*.form_questions.*.uuid' => ['sometimes', 'nullable', 'string'], 'form_sections.*.form_questions.*.name' => ['sometimes', 'nullable'], @@ -63,6 +64,7 @@ public function rules() 'form_sections.*.form_questions.*.child_form_questions' => ['sometimes', 'nullable'], 'form_sections.*.form_questions.*.child_form_questions.*.uuid' => ['sometimes', 'nullable', 'string'], 'form_sections.*.form_questions.*.child_form_questions.*.linked_field_key' => ['sometimes', 'nullable', 'string'], + 'form_sections.*.form_questions.*.child_form_questions.*.collection' => ['sometimes', 'nullable', 'string'], 'form_sections.*.form_questions.*.child_form_questions.*.label' => ['sometimes', 'nullable', 'string'], 'form_sections.*.form_questions.*.child_form_questions.*.name' => ['sometimes', 'nullable'], 'form_sections.*.form_questions.*.child_form_questions.*.input_type' => ['sometimes', 'nullable'], From a434779764ace7437003c41b608b8fa8c9014500 Mon Sep 17 00:00:00 2001 From: Nathan Curtis Date: Wed, 29 May 2024 09:27:40 -0700 Subject: [PATCH 2/2] [TM-938] Send both the old count and the new count to the client as separate fields. --- .../Resources/V2/Projects/ProjectResource.php | 2 ++ app/Http/Resources/V2/Sites/SiteResource.php | 2 ++ app/Models/V2/Projects/Project.php | 15 +++++++++++++++ app/Models/V2/Sites/Site.php | 11 +++++++++++ 4 files changed, 30 insertions(+) diff --git a/app/Http/Resources/V2/Projects/ProjectResource.php b/app/Http/Resources/V2/Projects/ProjectResource.php index 4417e7d5c..eae518e46 100644 --- a/app/Http/Resources/V2/Projects/ProjectResource.php +++ b/app/Http/Resources/V2/Projects/ProjectResource.php @@ -52,6 +52,8 @@ public function toArray($request) 'seeds_planted_count' => $this->seeds_planted_count, 'regenerated_trees_count' => $this->regenerated_trees_count, 'workday_count' => $this->workday_count, + // Temporary until we have bulk import completed. + 'self_reported_workday_count' => $this->self_reported_workday_count, 'total_jobs_created' => $this->total_jobs_created, 'total_sites' => $this->total_sites, 'total_nurseries' => $this->total_nurseries, diff --git a/app/Http/Resources/V2/Sites/SiteResource.php b/app/Http/Resources/V2/Sites/SiteResource.php index eb98d950f..88e2bfff3 100644 --- a/app/Http/Resources/V2/Sites/SiteResource.php +++ b/app/Http/Resources/V2/Sites/SiteResource.php @@ -48,6 +48,8 @@ public function toArray($request) 'site_reports_total' => $this->site_reports_total, 'overdue_site_reports_total' => $this->overdue_site_reports_total, 'workday_count' => $this->workday_count, + // Temporary until we have bulk import completed. + 'self_reported_workday_count' => $this->self_reported_workday_count, 'trees_planted_count' => $this->trees_planted_count, 'regenerated_trees_count' => $this->regenerated_trees_count, 'migrated' => ! empty($this->old_model), diff --git a/app/Models/V2/Projects/Project.php b/app/Models/V2/Projects/Project.php index c1d76cb03..c946f5722 100644 --- a/app/Models/V2/Projects/Project.php +++ b/app/Models/V2/Projects/Project.php @@ -36,6 +36,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasManyThrough; use Illuminate\Database\Eloquent\SoftDeletes; +use Illuminate\Support\Facades\DB; use Laravel\Scout\Searchable; use OwenIt\Auditing\Auditable; use OwenIt\Auditing\Contracts\Auditable as AuditableContract; @@ -372,6 +373,20 @@ public function getWorkdayCountAttribute(): int )->gender()->sum('amount') ?? 0; } + public function getSelfReportedWorkdayCountAttribute(): int + { + $sumQueries = [ + DB::raw('sum(`workdays_paid`) as paid'), + DB::raw('sum(`workdays_volunteer`) as volunteer'), + ]; + $projectTotals = $this->reports()->hasBeenSubmitted()->get($sumQueries)->first(); + // The groupBy is superfluous, but required because Laravel adds "v2_sites.project_id as laravel_through_key" to + // the SQL select. + $siteTotals = $this->submittedSiteReports()->groupBy('v2_sites.project_id')->get($sumQueries)->first(); + + return $projectTotals?->paid + $projectTotals?->volunteer + $siteTotals?->paid + $siteTotals?->volunteer; + } + public function getTotalJobsCreatedAttribute(): int { $ftTotal = ProjectReport::where('project_id', $this->id) diff --git a/app/Models/V2/Sites/Site.php b/app/Models/V2/Sites/Site.php index d3dda28d5..96b69b828 100644 --- a/app/Models/V2/Sites/Site.php +++ b/app/Models/V2/Sites/Site.php @@ -31,6 +31,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\MorphMany; use Illuminate\Database\Eloquent\SoftDeletes; +use Illuminate\Support\Facades\DB; use Laravel\Scout\Searchable; use OwenIt\Auditing\Auditable; use OwenIt\Auditing\Contracts\Auditable as AuditableContract; @@ -311,6 +312,16 @@ public function getWorkdayCountAttribute(): int )->gender()->sum('amount') ?? 0; } + public function getSelfReportedWorkdayCountAttribute(): int + { + $totals = $this->reports()->hasBeenSubmitted()->get([ + DB::raw('sum(`workdays_volunteer`) as volunteer'), + DB::raw('sum(`workdays_paid`) as paid'), + ])->first(); + + return $totals?->paid + $totals?->volunteer; + } + public function getFrameworkUuidAttribute(): ?string { return $this->framework ? $this->framework->uuid : null;