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

fix(post-entity): address regression with handling posts when forms h… #3937

Merged
merged 2 commits into from
Apr 20, 2020
Merged
Changes from 1 commit
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
33 changes: 26 additions & 7 deletions src/App/Repository/PostRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
use Ushahidi\App\Multisite\OhanzeeResolver;
use Ushahidi\Core\Tool\Permissions\InteractsWithPostPermissions;

use Illuminate\Support\Collection;
use League\Event\ListenerInterface;

use Ushahidi\Core\Traits\Event;

class PostRepository extends OhanzeeRepository implements
Expand Down Expand Up @@ -249,13 +251,30 @@ public function getEntity(array $data = null): Post
/* Check which types are used in the form */
$form_attributes = $this->form_attributes_by_form->get(intval($data['form_id']));
if ($form_attributes) {
$types_to_fetch = $form_attributes
->pluck('type')
->unique()
->toArray();

/* Intersect with requested types, subtract types already provided in $data */
$types_to_fetch = array_intersect($types_to_fetch, $this->include_value_types);
/* Count how many of each form attribute type we have */
$types_to_fetch_with_attribute_count = $form_attributes
->groupBy('type')
->map(function ($attrs) {
return $attrs->count();
});

$types_to_fetch = $types_to_fetch_with_attribute_count->keys()->toArray();

/* Intersect with requested types */
if (count($this->include_value_types) > 0) {
$types_to_fetch = array_intersect($types_to_fetch, $this->include_value_types);
}

/* drop types that we have already fetched
BUT only if there is a SINGLE attribute of that type in the form,
since the SQL JOINs that we have run previously are only good for fetching
a SINGLE value of each type for each post.
If we don't do this, we would be leaving out the values of the second
and subsequent attributes defined with that type */
$already_obtained_types = collect($already_obtained_types)
->filter(function ($type) use ($types_to_fetch_with_attribute_count) {
return $types_to_fetch_with_attribute_count->get($type) < 2;
})->toArray();
$types_to_fetch = array_diff($types_to_fetch, $already_obtained_types);
}
}
Expand Down