Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

survey only parameters for relations #4998

Merged
merged 6 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Ushahidi\Modules\V5\Actions\Survey;

use Ushahidi\Modules\V5\Actions\Survey\Queries\FetchTasksBySurveyIdQuery;
use Ushahidi\Modules\V5\Actions\Survey\Queries\FetchRolesCanCreateSurveyPostsQuery;

trait HandleSurveyOnlyParameters
{
private function addHydrateRelationships(&$survey, $only, $hydrate)
{
$relations = [
'tasks' => false,
'translations' => false,
'enabled_languages' => false,
];

foreach ($hydrate as $relation) {
switch ($relation) {
case 'can_create':
$this->addCanCreate($survey);
break;
case 'tasks':
$survey->tasks = $this->queryBus->handle(
new FetchTasksBySurveyIdQuery(
$survey->id,
FetchTasksBySurveyIdQuery::DEFAULT_SORT_BY,
FetchTasksBySurveyIdQuery::DEFAULT_ORDER
)
);
$relations['tasks'] = true;
break;
case 'translations':
$relations['translations'] = true;
break;
case 'enabled_languages':
$survey->enabled_languages = [
'default' => $survey->base_language,
'available' => $survey->translations->groupBy('language')->keys()
];
$relations['enabled_languages'] = true;
break;
}
}

if (!$relations['tasks']) {
$survey->tasks = null;
}
if (!$relations['translations']) {
$survey->translations = null;
}
}
private function addCanCreate(&$survey)
{
$survey_roles = $this->queryBus->handle(
new FetchRolesCanCreateSurveyPostsQuery(
$survey->id,
FetchRolesCanCreateSurveyPostsQuery::DEFAULT_SORT_BY,
FetchRolesCanCreateSurveyPostsQuery::DEFAULT_ORDER
)
);
$roles = [];
foreach ($survey_roles as $survey_role) {
$roles[] = $survey_role->role()->value('name');
}
$survey->can_create = $roles;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
use Ushahidi\Modules\V5\Actions\V5QueryHandler;
use App\Bus\Query\Query;
use Ushahidi\Modules\V5\Actions\Survey\Queries\FetchSurveyByIdQuery;
use Ushahidi\Modules\V5\Actions\Survey\Queries\FetchTasksBySurveyIdQuery;
use Ushahidi\Modules\V5\Actions\Survey\Queries\FetchRolesCanCreateSurveyPostsQuery;
use Ushahidi\Modules\V5\Repository\Survey\SurveyRepository;
use Ushahidi\Modules\V5\Models\Survey;
use App\Bus\Query\QueryBus;
use Ushahidi\Modules\V5\Actions\Survey\HandleSurveyOnlyParameters;

class FetchSurveyByIdQueryHandler extends V5QueryHandler
{

use HandleSurveyOnlyParameters;

private $survey_repository;
private $queryBus;

Expand All @@ -31,87 +32,28 @@ protected function isSupported(Query $query)
);
}


/**
* @param FetchSurveyByIdQuery $query
* @return Survey
*/
public function __invoke($query) //: array
public function __invoke($action) //: array
{
$only = $this->getSelectFields(
$query->getFormat(),
$query->getOnlyFields(),
Survey::$approved_fields_for_select,
Survey::$required_fields_for_select
$this->isSupported($action);
$survey = $this->survey_repository->findById(
$action->getId(),
array_unique(array_merge(
$action->getFields(),
$action->getFieldsForRelationship()
)),
$action->getWithRelationship()
);
$this->isSupported($query);
$survey = $this->survey_repository->findById($query->getId(), $only);
$this->addHydrateRelationships(
$survey,
$only,
$this->getHydrateRelationshpis(Survey::$relationships, $query->getHydrate())
$action->getFields(),
$action->getHydrates()
);
$survey->offsetUnset('base_language');

return $survey;
}
private function addHydrateRelationships(&$survey, $only, $hydrate)
{
$relations = [
'tasks' => false,
'translations' => false,
'enabled_languages' => false,
];

foreach ($hydrate as $relation) {
switch ($relation) {
case 'tasks':
$survey->tasks = $this->queryBus->handle(
new FetchTasksBySurveyIdQuery(
$survey->id,
FetchTasksBySurveyIdQuery::DEFAULT_SORT_BY,
FetchTasksBySurveyIdQuery::DEFAULT_ORDER
)
);
$relations['tasks'] = true;
break;
case 'translations':
$relations['translations'] = true;
break;
case 'enabled_languages':
$survey->enabled_languages = [
'default' => $survey->base_language,
'available' => $survey->translations->groupBy('language')->keys()
];
$relations['enabled_languages'] = true;
break;
}
}

if (!$relations['tasks']) {
$survey->tasks = null;
}
if (!$relations['translations']) {
$survey->translations = null;
}


$this->addCanCreate($survey);
}
private function addCanCreate(&$survey)
{

$survey_roles = $this->queryBus->handle(
new FetchRolesCanCreateSurveyPostsQuery(
$survey->id,
FetchRolesCanCreateSurveyPostsQuery::DEFAULT_SORT_BY,
FetchRolesCanCreateSurveyPostsQuery::DEFAULT_ORDER
)
);
$roles = [];
foreach ($survey_roles as $survey_role) {
$roles[] = $survey_role->role()->value('name');
}
$survey->can_create = $roles;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
use App\Bus\Query\Query;
use Ushahidi\Modules\V5\Actions\Survey\Queries\FetchSurveyQuery;
use Ushahidi\Modules\V5\Repository\Survey\SurveyRepository;
use Ushahidi\Modules\V5\Actions\Survey\Queries\FetchTasksBySurveyIdQuery;
use Ushahidi\Modules\V5\Actions\Survey\Queries\FetchRolesCanCreateSurveyPostsQuery;
use Ushahidi\Modules\V5\Models\Survey;
use App\Bus\Query\QueryBus;
use Illuminate\Pagination\LengthAwarePaginator;
use Ushahidi\Modules\V5\Actions\Survey\HandleSurveyOnlyParameters;

class FetchSurveyQueryHandler extends V5QueryHandler
{
use HandleSurveyOnlyParameters;

private $survey_repository;
private $queryBus;

Expand All @@ -35,99 +36,27 @@ protected function isSupported(Query $query)
* @param FetchSurveyQuery $query
* @return LengthAwarePaginator
*/
public function __invoke($query) //: LengthAwarePaginator
public function __invoke($action) //: LengthAwarePaginator
{
$this->isSupported($query);
$skip = $query->getLimit() * ($query->getPage() - 1);
$only = $this->getSelectFields(
$query->getFormat(),
$query->getOnlyFields(),
Survey::$approved_fields_for_select,
Survey::$required_fields_for_select
);
$this->isSupported($action);

$only_fields = array_unique(array_merge($action->getFields(), $action->getFieldsForRelationship()));

$surveys = $this->survey_repository->fetch(
$query->getLimit(),
$skip,
$query->getSortBy(),
$query->getOrder(),
$query->getSearchFields(),
$only
$surveys = $this->survey_repository->paginate(
$action->getPaging(),
$action->getSearchFields(),
$only_fields,
$action->getWithRelationship()
);

// TODO: This should happen at the repository level
$hydrates = $this->getHydrateRelationshpis(Survey::$relationships, $query->getHydrate());
foreach ($surveys as $survey) {
$this->addHydrateRelationships(
$survey,
$only,
$hydrates
$action->getFields(),
$action->getHydrates()
);
$survey->offsetUnset('base_language');
}
return $surveys;
}


private function addHydrateRelationships(&$survey, $only, $hydrate)
{
$relations = [
'tasks' => false,
'translations' => false,
'enabled_languages' => false,
];

foreach ($hydrate as $relation) {
switch ($relation) {
case 'tasks':
// TODO: This is wrong and should be done at the repository level instead to create an aggregate
$survey->tasks = $this->queryBus->handle(
new FetchTasksBySurveyIdQuery(
$survey->id,
FetchTasksBySurveyIdQuery::DEFAULT_SORT_BY,
FetchTasksBySurveyIdQuery::DEFAULT_ORDER
)
);
$relations['tasks'] = true;
break;
case 'translations':
$relations['translations'] = true;
break;
case 'enabled_languages':
$survey->enabled_languages = [
'default' => $survey->base_language,
'available' => $survey->translations->groupBy('language')->keys()
];
$relations['enabled_languages'] = true;
break;
}
}

if (!$relations['tasks']) {
$survey->tasks = null;
}
if (!$relations['translations']) {
$survey->translations = null;
}


$this->addCanCreate($survey);
}

private function addCanCreate(&$survey)
{

$survey_roles = $this->queryBus->handle(
new FetchRolesCanCreateSurveyPostsQuery(
$survey->id,
FetchRolesCanCreateSurveyPostsQuery::DEFAULT_SORT_BY,
FetchRolesCanCreateSurveyPostsQuery::DEFAULT_ORDER
)
);
$roles = [];
foreach ($survey_roles as $survey_role) {
$roles[] = $survey_role->role()->value('name');
}
$survey->can_create = $roles;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,37 @@

use App\Bus\Query\Query;
use Ushahidi\Modules\V5\Models\Survey;
use Ushahidi\Modules\V5\Traits\OnlyParameter\QueryWithOnlyParameter;
use Illuminate\Http\Request;

class FetchSurveyByIdQuery implements Query
{


use QueryWithOnlyParameter;
/**
* int
*/
private $id;
private $format;
private $only_fields;
private $hydrate;

public function __construct(
int $id = 0,
?string $format = null,
?string $only_fields = null,
?string $hydrate = null
int $id = 0
) {

$this->id = $id;
$this->format = $format;
$this->only_fields = $only_fields;
$this->hydrate = $hydrate;
}

public function getId(): int
public static function fromRequest(int $id, Request $request): self
{
return $this->id;
if ($id <= 0) {
throw new \InvalidArgumentException('Id must be a positive number');
}
$query = new self($id);
$query->addOnlyParameteresFromRequest($request, Survey::ALLOWED_FIELDS, Survey::ALLOWED_RELATIONSHIPS, Survey::REQUIRED_FIELDS);
return $query;
}

public function getFormat()
{
return $this->format;
}
public function getOnlyFields()
{
return $this->only_fields;
}

public function getHydrate()
public function getId(): int
{
return $this->hydrate;
return $this->id;
}
}
Loading
Loading