Skip to content

Commit

Permalink
Merge pull request #488 from wri/release/peaceful-palm
Browse files Browse the repository at this point in the history
[RELEASE] Peaceful Palm
  • Loading branch information
roguenet authored Oct 4, 2024
2 parents c48eb41 + 54d134c commit 8feffb4
Show file tree
Hide file tree
Showing 100 changed files with 3,400 additions and 416 deletions.
16 changes: 2 additions & 14 deletions app/Exports/V2/BaseExportFormSubmission.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,23 +102,11 @@ protected function getAnswer(array $field, array $answers): string
return $this->stringifyModel($answer, ['first_name', 'last_name', 'title', 'gender', 'percent_ownership', 'year_of_birth',]);

default:
if (is_string($answer)) {
$utf8Answer = mb_convert_encoding($answer, 'UTF-8', mb_detect_encoding($answer));
} else {
$utf8Answer = $answer;
}

return json_encode($utf8Answer, JSON_UNESCAPED_UNICODE);
return json_encode($answer);
}
}

if (is_string($answer)) {
$utf8Answer = mb_convert_encoding($answer, 'UTF-8', mb_detect_encoding($answer));
} else {
$utf8Answer = $answer;
}

return json_encode($utf8Answer, JSON_UNESCAPED_UNICODE);
return $answer ?? '';
}

protected function generateFieldMap(Form $form): array
Expand Down
10 changes: 5 additions & 5 deletions app/Exports/V2/EntityExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function map($entity): array
}

foreach ($this->auditFields as $key => $value) {
$mapped[] = mb_convert_encoding(data_get($entity, $key, ''), 'UTF-8', 'auto');
$mapped[] = data_get($entity, $key, '');
}

return $mapped;
Expand All @@ -91,8 +91,8 @@ protected function getAttachedMappedForEntity($entity): array

$mapped = array_merge($mapped, [
$organisation->readable_type ?? null,
mb_convert_encoding($organisation->name ?? null, 'UTF-8', 'auto'),
mb_convert_encoding($entity->project->name ?? null, 'UTF-8', 'auto'),
$organisation->name ?? null,
$entity->project->name ?? null,
$entity->status ?? null,
$entity->update_request_status ?? null,
$entity->due_at ?? null,
Expand All @@ -111,12 +111,12 @@ protected function getAttachedMappedForEntity($entity): array
}
if ($this->form->type === 'nursery-report') {
$mapped[] = $entity->nursery->old_id ?? ($entity->nursery->id ?? null);
$mapped[] = mb_convert_encoding($entity->nursery->name ?? null, 'UTF-8', 'auto');
$mapped[] = $entity->nursery->name ?? null;
}

if ($this->form->type === 'site-report') {
$mapped[] = $entity->site->ppc_external_id ?? $entity->site->id ?? null;
$mapped[] = mb_convert_encoding($entity->site->name ?? null, 'UTF-8', 'auto');
$mapped[] = $entity->site->name ?? null;
$sumTreeSpecies = $entity->treeSpecies()->sum('amount');
$mapped[] = $sumTreeSpecies > 0 ? $sumTreeSpecies : null;
$mapped[] = $entity->site->trees_planted_count ?? null;
Expand Down
5 changes: 5 additions & 0 deletions app/Helpers/GeometryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,9 @@ public static function getSitePolygonsUuids($uuid)
{
return SitePolygon::where('site_id', $uuid)->where('is_active', true)->get()->pluck('poly_id');
}

public static function getSitePolygonsOfPolygons(array $polygonUuids)
{
return SitePolygon::whereIn('poly_id', $polygonUuids)->where('is_active', true)->get()->pluck('uuid');
}
}
33 changes: 21 additions & 12 deletions app/Helpers/TerrafundDashboardQueryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,30 @@
use App\Models\V2\Projects\Project;
use App\Models\V2\Sites\SitePolygon;
use Illuminate\Support\Facades\Log;
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\QueryBuilder;

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']);
$query = QueryBuilder::for(Project::class)
->join('organisations', 'v2_projects.organisation_id', '=', 'organisations.id')
->select('v2_projects.*')
->allowedFilters([
AllowedFilter::exact('framework_key'),
AllowedFilter::exact('landscape'),
AllowedFilter::exact('country'),
AllowedFilter::exact('organisations.type'),
AllowedFilter::exact('v2_projects.status'),
AllowedFilter::exact('v2_projects.uuid'),
]);

if ($request->has('search')) {
$searchTerm = $request->query('search');
$query->where(function ($query) use ($searchTerm) {
$query->where('v2_projects.name', 'like', "%$searchTerm%");
});
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;
Expand All @@ -38,14 +47,14 @@ public static function retrievePolygonUuidsForProject($projectUuId)
public static function getPolygonIdsOfProject($request)
{
$projectUuId = TerrafundDashboardQueryHelper::buildQueryFromRequest($request)
->pluck('uuid')->first();
->pluck('v2_projects.uuid')->first();

return self::retrievePolygonUuidsForProject($projectUuId);
}

public static function getPolygonUuidsOfProject($request)
{
$projectUuId = $request->input('uuid');
$projectUuId = $request['filter']['v2_projects.uuid'];

return self::retrievePolygonUuidsForProject($projectUuId);
}
Expand Down Expand Up @@ -93,7 +102,7 @@ public static function retrievePolygonUuidsByStatusForProject($projectUuid)
public static function getPolygonsByStatusOfProject($request)
{
$projectUuid = TerrafundDashboardQueryHelper::buildQueryFromRequest($request)
->pluck('uuid')->first();
->pluck('v2_projects.uuid')->first();

return self::retrievePolygonUuidsByStatusForProject($projectUuid);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __invoke(Request $request): ApplicationsCollection
}

if (! empty($request->query('search'))) {
$ids = Application::searchApplications(trim($request->query('search')))->pluck('id')->toArray();
$ids = Application::search(trim($request->query('search')))->pluck('id')->toArray();
$qry->whereIn('id', $ids);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,28 @@

namespace App\Http\Controllers\V2\Dashboard;

use App\Helpers\TerrafundDashboardQueryHelper;
use App\Http\Controllers\Controller;
use App\Models\V2\Forms\FormOptionList;
use App\Models\V2\Forms\FormOptionListOption;
use App\Models\V2\Nurseries\Nursery;
use App\Models\V2\Projects\Project;
use App\Models\V2\Sites\Site;
use Illuminate\Http\Request;

class ActiveCountriesTableController extends Controller
{
public function __invoke()
public function __invoke(Request $request)
{
$response = (object) [
'data' => $this->getAllCountries(),
'data' => $this->getAllCountries($request),
];

return response()->json($response);
}

public function getAllCountries()
public function getAllCountries($request)
{
$projects = Project::where('framework_key', 'terrafund')
->whereHas('organisation', function ($query) {
$query->whereIn('type', ['for-profit-organization', 'non-profit-organization']);
})->get();
$projects = TerrafundDashboardQueryHelper::buildQueryFromRequest($request)->get();
$countryId = FormOptionList::where('key', 'countries')->value('id');
$countries = FormOptionListOption::where('form_option_list_id', $countryId)
->orderBy('label')
Expand Down
9 changes: 3 additions & 6 deletions app/Http/Controllers/V2/Dashboard/CountriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace App\Http\Controllers\V2\Dashboard;

use App\Helpers\TerrafundDashboardQueryHelper;
use App\Http\Controllers\Controller;
use App\Models\V2\Forms\FormOptionList;
use App\Models\V2\Forms\FormOptionListOption;
use App\Models\V2\Projects\Project;
use Illuminate\Http\Request;

class CountriesController extends Controller
Expand All @@ -19,18 +19,15 @@ public function __invoke(Request $request)

public function getAllCountries($request)
{
$projectsCountrieslug = Project::where('framework_key', 'terrafund')
->whereHas('organisation', function ($query) {
$query->whereIn('type', ['for-profit-organization', 'non-profit-organization']);
})->pluck('country');
$projectsCountrieslug = TerrafundDashboardQueryHelper::buildQueryFromRequest($request)->pluck('country');
$countryId = FormOptionList::where('key', 'countries')->value('id');
$countries = FormOptionListOption::where('form_option_list_id', $countryId)
->orderBy('label')
->select('id', 'label', 'slug')
->get();
$countriesResponse = [];
foreach ($countries as $country) {
if ($request->input('country')) {
if (data_get($request, 'filter.country') === $country->slug) {
$countriesResponse[] = [
'country_slug' => $country->slug,
'id' => $country->id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ 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();

Expand Down Expand Up @@ -87,7 +86,6 @@ private function calculateTotalNonYouth($jobsCreatedDetailed)
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();

Expand All @@ -97,7 +95,6 @@ private function getTotalJobsCreated($projectIds)
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function __invoke(Request $request)
$projects = TerrafundDashboardQueryHelper::buildQueryFromRequest($request)
->whereNotNull('long')
->whereNotNull('lat')
->select('uuid', 'long', 'lat', 'name')
->select('v2_projects.uuid', 'long', 'lat', 'v2_projects.name')
->get();

return response()->json(['data' => $projects]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public function __invoke(Request $request)
{
$projects = TerrafundDashboardQueryHelper::buildQueryFromRequest($request)->get();
$countryName = '';
if ($request->country) {
$countryName = WorldCountryGeneralized::where('iso', $request->country)->first()->country;
if (data_get($request, 'filter.country')) {
$countryName = WorldCountryGeneralized::where('iso', $request['filter']['country'])->first()->country;
}
$response = (object)[
'total_non_profit_count' => $this->getTotalNonProfitCount($projects),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ private function buildProjectQuery(Request $request)

private function getRestorationStrategy(array $projectIds)
{
if (empty($projectIds)) {
return;
}

$conditions = implode(' OR ', array_map(function ($strategy) {
return "JSON_UNQUOTE(JSON_EXTRACT(restoration_strategy, CONCAT('\$[', numbers.n, ']'))) = '$strategy'";
}, self::RESTORATION_STRATEGIES));
Expand All @@ -69,6 +73,9 @@ private function getRestorationStrategy(array $projectIds)

private function getLandUseType(array $projectIds)
{
if (empty($projectIds)) {
return;
}
$conditions = implode(' OR ', array_map(function ($type) {
return "JSON_UNQUOTE(JSON_EXTRACT(v2_sites.land_use_types, CONCAT('\$[', numbers.n, ']'))) = '$type'";
}, self::LAND_USE_TYPES));
Expand All @@ -95,6 +102,9 @@ private function getLandUseType(array $projectIds)

private function getLandTenures(array $projectIds)
{
if (empty($projectIds)) {
return;
}
$conditions = implode(' OR ', array_map(function ($type) {
return "JSON_UNQUOTE(JSON_EXTRACT(v2_sites.land_tenures, CONCAT('\$[', numbers.n, ']'))) = '$type'";
}, self::LAND_TENURES));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ private function prepareProjectQuery(Request $request)
private function getRawProjectIds($query)
{
return $query
->join('organisations', 'v2_projects.organisation_id', '=', 'organisations.id')
->select('v2_projects.id', 'organisations.type')
->get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ public function __invoke(EntityModel $entity, Request $request)
/** @var UpdateRequest $updateRequest */
$updateRequest = $entity->updateRequests()->isUnapproved()->first();
if (! empty($updateRequest)) {
$this->saveAuditStatusProjectDeveloperSubmit($entity, $updateRequest);
$updateRequest->submitForApproval();
$this->saveAuditStatusProjectDeveloperSubmit($entity, $updateRequest);
Action::forTarget($updateRequest)->delete();
} else {
$entity->submitForApproval();
$this->saveAuditStatusProjectDeveloperSubmitNotUpdateRequest($entity);
}

SendProjectManagerJobs::dispatch($entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Http\Controllers\Controller;
use App\Http\Requests\V2\Forms\UpdateFormSubmissionRequest;
use App\Models\Traits\SaveAuditStatusTrait;
use App\Models\V2\EntityModel;
use App\Models\V2\ReportModel;
use App\Models\V2\UpdateRequests\UpdateRequest;
Expand All @@ -13,6 +14,8 @@

class UpdateEntityWithFormController extends Controller
{
use SaveAuditStatusTrait;

public function __invoke(EntityModel $entity, UpdateFormSubmissionRequest $formSubmissionRequest)
{
$this->authorize('update', $entity);
Expand All @@ -31,12 +34,18 @@ public function __invoke(EntityModel $entity, UpdateFormSubmissionRequest $formS
if ($entity instanceof ReportModel) {
$entity->updateInProgress($isAdmin);
}
if (data_get($formSubmissionRequest, 'continue_later_action')) {
$this->saveAuditStatusProjectDeveloperSubmitDraft($entity);
}

return $entity->createSchemaResource();
}

if (! empty($updateRequest)) {
$updateRequest->update([ 'content' => array_merge($updateRequest->content, $answers) ]);
$updateRequest->update(['content' => array_merge($updateRequest->content, $answers)]);
if (data_get($formSubmissionRequest, 'continue_later_action')) {
$this->saveAuditStatusProjectDeveloperSubmitDraft($entity);
}
} else {
UpdateRequest::create([
'organisation_id' => $entity->organisation ? $entity->organisation->id : $entity->project->organisation_id,
Expand Down
Loading

0 comments on commit 8feffb4

Please sign in to comment.