-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #236 from wri/feat/TM-621_dashboard_jobs_created
[TM-621] start up jobs created controller
- Loading branch information
Showing
4 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
115
app/Http/Controllers/V2/Dashboard/GetJobsCreatedController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters