Skip to content

Commit

Permalink
Merge branch 'staging' into epic/audit-log
Browse files Browse the repository at this point in the history
  • Loading branch information
pachonjcl authored Jun 6, 2024
2 parents 20d52ca + e1c977c commit 219e51f
Show file tree
Hide file tree
Showing 12 changed files with 323 additions and 37 deletions.
60 changes: 60 additions & 0 deletions app/Helpers/TerrafundDashboardQueryHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace App\Helpers;

use App\Models\V2\Projects\Project;
use App\Models\V2\Sites\SitePolygon;

class TerrafundDashboardQueryHelper
{
public static function buildQueryFromRequest($request)
{
$query = Project::where('framework_key', 'terrafund')
->whereHas('organisation', function ($query) {
$query->whereIn('type', ['for-profit-organization', 'non-profit-organization']);
});
if ($request->has('country')) {
$country = $request->input('country');
$query = $query->where('country', $country);
} elseif ($request->has('uuid')) {
$projectId = $request->input('uuid');
$query = $query->where('v2_projects.uuid', $projectId);
}

return $query;
}

public static function getPolygonIdsOfProject($request)
{
$projectIds = TerrafundDashboardQueryHelper::buildQueryFromRequest($request)
->pluck('uuid');
$polygonsIds = SitePolygon::whereHas('site.project', function ($query) use ($projectIds) {
$query->whereIn('uuid', $projectIds);
})->pluck('poly_id');

return $polygonsIds;
}

public static function getPolygonsByStatusOfProject($request)
{
$projectIds = TerrafundDashboardQueryHelper::buildQueryFromRequest($request)
->pluck('uuid');

$statuses = ['Needs More Info', 'Submitted', 'Approved'];

$polygons = [];

foreach ($statuses as $status) {
// Get polygons of the project filtered by status
$polygonsOfProject = SitePolygon::whereHas('site.project', function ($query) use ($projectIds) {
$query->whereIn('uuid', $projectIds);
})
->where('status', $status)
->pluck('poly_id');

$polygons[$status] = $polygonsOfProject;
}

return $polygons;
}
}
115 changes: 115 additions & 0 deletions app/Http/Controllers/V2/Dashboard/GetJobsCreatedController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace App\Http\Controllers\V2\Dashboard;

use App\Helpers\TerrafundDashboardQueryHelper;
use App\Http\Controllers\Controller;
use App\Http\Resources\V2\Dashboard\JobsCreatedResource;
use App\Models\V2\Projects\ProjectReport;
use Illuminate\Http\Request;

class GetJobsCreatedController extends Controller
{
public function __invoke(Request $request): JobsCreatedResource
{

$query = TerrafundDashboardQueryHelper::buildQueryFromRequest($request);

$rawProjectIds = $query
->join('organisations', 'v2_projects.organisation_id', '=', 'organisations.id')
->select('v2_projects.id', 'organisations.type')
->get();

$forProfitProjectIds = $this->filterProjectIdsByType($rawProjectIds, 'for-profit-organization');
$nonProfitProjectIds = $this->filterProjectIdsByType($rawProjectIds, 'non-profit-organization');
$allProjectIds = $this->getAllProjectIds($rawProjectIds);

$forProfitJobsCreated = $this->getTotalJobsCreated($forProfitProjectIds);
$nonProfitJobsCreated = $this->getTotalJobsCreated($nonProfitProjectIds);
$totalJobsCreated = $this->getTotalJobsCreated($allProjectIds);
$jobsCreatedDetailed = $this->getJobsCreatedDetailed($allProjectIds);

$finalResult = (object) [
'totalJobsCreated' => $totalJobsCreated,
'forProfitJobsCreated' => $forProfitJobsCreated,
'nonProfitJobsCreated' => $nonProfitJobsCreated,
'total_ft' => $jobsCreatedDetailed->total_ft,
'total_pt' => $jobsCreatedDetailed->total_pt,
'total_men' => $this->calculateTotalMen($jobsCreatedDetailed),
'total_pt_men' => $jobsCreatedDetailed->total_pt_men,
'total_ft_men' => $jobsCreatedDetailed->total_ft_men,
'total_women' => $this->calculateTotalWomen($jobsCreatedDetailed),
'total_pt_women' => $jobsCreatedDetailed->total_pt_women,
'total_ft_women' => $jobsCreatedDetailed->total_ft_women,
'total_youth' => $this->calculateTotalYouth($jobsCreatedDetailed),
'total_pt_youth' => $jobsCreatedDetailed->total_pt_youth,
'total_ft_youth' => $jobsCreatedDetailed->total_ft_youth,
'total_non_youth' => $this->calculateTotalNonYouth($jobsCreatedDetailed),
'total_pt_non_youth' => $jobsCreatedDetailed->total_pt_non_youth,
'total_ft_non_youth' => $jobsCreatedDetailed->total_ft_non_youth,
];

return new JobsCreatedResource($finalResult);
}

private function filterProjectIdsByType($projectIds, $type)
{
return $projectIds->filter(function ($row) use ($type) {
return $row->type === $type;
})->pluck('id')->toArray();
}

private function getAllProjectIds($projectIds)
{
return $projectIds->pluck('id')->toArray();
}

private function calculateTotalMen($jobsCreatedDetailed)
{
return $jobsCreatedDetailed->total_pt_men + $jobsCreatedDetailed->total_ft_men;
}

private function calculateTotalWomen($jobsCreatedDetailed)
{
return $jobsCreatedDetailed->total_pt_women + $jobsCreatedDetailed->total_ft_women;
}

private function calculateTotalYouth($jobsCreatedDetailed)
{
return $jobsCreatedDetailed->total_pt_youth + $jobsCreatedDetailed->total_ft_youth;
}

private function calculateTotalNonYouth($jobsCreatedDetailed)
{
return $jobsCreatedDetailed->total_pt_non_youth + $jobsCreatedDetailed->total_ft_non_youth;
}

private function getTotalJobsCreated($projectIds)
{
$sumData = ProjectReport::whereIn('project_id', $projectIds)
->where('framework_key', 'terrafund')
->selectRaw('SUM(ft_total) as total_ft, SUM(pt_total) as total_pt')
->first();

return $sumData->total_ft + $sumData->total_pt;
}

private function getJobsCreatedDetailed($projectIds)
{
return ProjectReport::whereIn('project_id', $projectIds)
->where('framework_key', 'terrafund')
->selectRaw(
'SUM(ft_total) as total_ft,
SUM(pt_total) as total_pt,
SUM(pt_men) as total_pt_men,
SUM(ft_men) as total_ft_men,
SUM(pt_women) as total_pt_women,
SUM(ft_women) as total_ft_women,
SUM(pt_youth) as total_pt_youth,
SUM(ft_youth) as total_ft_youth,
SUM(pt_non_youth) as total_pt_non_youth,
SUM(ft_jobs_non_youth) as total_ft_non_youth'
)
->first();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function updateSitePolygon(string $uuid, Request $request)
'practice' => 'nullable|string',
'distr' => 'nullable|string',
'num_trees' => 'nullable|integer',
'est_area' => 'nullable|numeric',
'calc_area' => 'nullable|numeric',
'target_sys' => 'nullable|string',
]);

Expand All @@ -79,18 +79,30 @@ public function updateSitePolygon(string $uuid, Request $request)
}
}

public function createSitePolygon(string $uuid, Request $request)
public function createSitePolygon(string $uuid, string $siteUuid, Request $request)
{
try {
$validatedData = $request->validate([
'poly_name' => 'nullable|string',
'plantstart' => 'nullable|date',
'plantend' => 'nullable|date',
'practice' => 'nullable|string',
'distr' => 'nullable|string',
'num_trees' => 'nullable|integer',
'target_sys' => 'nullable|string',
]);
if ($request->getContent() === '{}') {
$validatedData = [
'poly_name' => null,
'plantstart' => null,
'plantend' => null,
'practice' => null,
'distr' => null,
'num_trees' => null,
'target_sys' => null,
];
} else {
$validatedData = $request->validate([
'poly_name' => 'nullable|string',
'plantstart' => 'nullable|date',
'plantend' => 'nullable|date',
'practice' => 'nullable|string',
'distr' => 'nullable|string',
'num_trees' => 'nullable|integer',
'target_sys' => 'nullable|string',
]);
}

$polygonGeometry = PolygonGeometry::where('uuid', $uuid)->first();
if (! $polygonGeometry) {
Expand All @@ -107,10 +119,12 @@ public function createSitePolygon(string $uuid, Request $request)
'practice' => $validatedData['practice'],
'distr' => $validatedData['distr'],
'num_trees' => $validatedData['num_trees'],
'est_area' => $areaHectares, // Assign the calculated area
'calc_area' => $areaHectares,
'target_sys' => $validatedData['target_sys'],
'poly_id' => $uuid,
'created_by' => Auth::user()?->id,
'status' => 'submitted',
'site_id' => $siteUuid,
]);
$sitePolygon->save();

Expand Down
37 changes: 37 additions & 0 deletions app/Http/Resources/V2/Dashboard/JobsCreatedResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Http\Resources\V2\Dashboard;

use Illuminate\Http\Resources\Json\JsonResource;

class JobsCreatedResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'totalJobsCreated' => (int) $this->totalJobsCreated,
'forProfitJobsCreated' => (int) $this->forProfitJobsCreated,
'nonProfitJobsCreated' => (int) $this->nonProfitJobsCreated,
'total_ft' => (int) $this->total_ft,
'total_pt' => (int) $this->total_pt,
'total_men' => (int) $this->total_men,
'total_pt_men' => (int) $this->total_pt_men,
'total_ft_men' => (int) $this->total_ft_men,
'total_women' => (int) $this->total_women,
'total_pt_women' => (int) $this->total_pt_women,
'total_ft_women' => (int) $this->total_ft_women,
'total_youth' => (int) $this->total_youth,
'total_pt_youth' => (int) $this->total_pt_youth,
'total_ft_youth' => (int) $this->total_ft_youth,
'total_non_youth' => (int) $this->total_non_youth,
'total_pt_non_youth' => (int) $this->total_pt_non_youth,
'total_ft_non_youth' => (int) $this->total_ft_non_youth,
];
}
}
10 changes: 3 additions & 7 deletions app/Models/V2/Sites/SitePolygon.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,18 @@ class SitePolygon extends Model
protected $table = 'site_polygon';

protected $fillable = [
'proj_name',
'org_name',
'poly_id',
'poly_name',
'site_id',
'site_name',
'project_id',
'poly_label',
'point_id',
'plantstart',
'plantend',
'practice',
'target_sys',
'distr',
'num_trees',
'est_area',
'country',
'calc_area',
'status',
'created_by',
];

Expand Down
10 changes: 3 additions & 7 deletions app/Services/PolygonService.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public function createCriteriaSite($polygonId, $criteriaId, $valid): bool|string
$criteriaSite->polygon_id = $polygonId;
$criteriaSite->criteria_id = $criteriaId;
$criteriaSite->valid = $valid;
$criteriaSite->created_by = Auth::user()?->id;

try {
$criteriaSite->save();
Expand Down Expand Up @@ -201,21 +202,16 @@ protected function validateSitePolygonProperties(string $polygonUuid, array $pro
$this->createCriteriaSite($polygonUuid, self::DATA_CRITERIA_ID, $validData);

return [
'project_id' => $properties['project_id'] ?? null,
'proj_name' => $properties['proj_name'] ?? null,
'org_name' => $properties['org_name'] ?? null,
'country' => $properties['country'] ?? null,
'poly_name' => $properties['poly_name'] ?? null,
'site_id' => $properties['site_id'] ?? null,
'site_name' => $properties['site_name'] ?? null,
'poly_label' => $properties['poly_label'] ?? null,
'plantstart' => $properties['plantstart'],
'plantend' => $properties['plantend'],
'practice' => $properties['practice'] ?? null,
'target_sys' => $properties['target_sys'] ?? null,
'distr' => $properties['distr'] ?? null,
'num_trees' => $properties['num_trees'],
'est_area' => $properties['area'] ?? null,
'calc_area' => $properties['area'] ?? null,
'status' => 'submitted',
];
}

Expand Down
2 changes: 1 addition & 1 deletion app/Validators/Extensions/Polygons/EstimatedArea.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static function getAreaData(string $polygonUuid): array
return ['valid' => false, 'error' => 'Total hectares restored goal not set for the project', 'status' => 500];
}

$sumEstArea = $project->sitePolygons()->sum('est_area');
$sumEstArea = $project->sitePolygons()->sum('calc_area');
$lowerBound = self::LOWER_BOUND_MULTIPLIER * $project->total_hectares_restored_goal;
$upperBound = self::UPPER_BOUND_MULTIPLIER * $project->total_hectares_restored_goal;
$valid = $sumEstArea >= $lowerBound && $sumEstArea <= $upperBound;
Expand Down
2 changes: 1 addition & 1 deletion database/factories/V2/Sites/SitePolygonFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function definition()
return [
'poly_id' => PolygonGeometry::factory()->create()->uuid,
'site_id' => Site::factory()->create()->uuid,
'est_area' => $this->faker->numberBetween(2.0, 50.0),
'calc_area' => $this->faker->numberBetween(2.0, 50.0),
];
}

Expand Down
Loading

0 comments on commit 219e51f

Please sign in to comment.