Skip to content

Commit

Permalink
Merge pull request #236 from wri/feat/TM-621_dashboard_jobs_created
Browse files Browse the repository at this point in the history
[TM-621] start up jobs created controller
  • Loading branch information
cesarLima1 authored Jun 6, 2024
2 parents 35cb0c0 + 22be956 commit 0254df8
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 0 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();
}
}
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,
];
}
}
5 changes: 5 additions & 0 deletions routes/api_v2.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use App\Http\Controllers\V2\CoreTeamLeader\DeleteCoreTeamLeaderController;
use App\Http\Controllers\V2\CoreTeamLeader\StoreCoreTeamLeaderController;
use App\Http\Controllers\V2\CoreTeamLeader\UpdateCoreTeamLeaderController;
use App\Http\Controllers\V2\Dashboard\GetJobsCreatedController;
use App\Http\Controllers\V2\Disturbances\DeleteDisturbanceController;
use App\Http\Controllers\V2\Disturbances\GetDisturbancesForEntityController;
use App\Http\Controllers\V2\Disturbances\StoreDisturbanceController;
Expand Down Expand Up @@ -651,3 +652,7 @@ function () {
Route::resource('files', FilePropertiesController::class);
//Route::put('file/{uuid}', [FilePropertiesController::class, 'update']);
//Route::delete('file/{uuid}', [FilePropertiesController::class, 'destroy']);

Route::prefix('dashboard')->group(function () {
Route::get('/jobs-created', GetJobsCreatedController::class);
});

0 comments on commit 0254df8

Please sign in to comment.