-
Notifications
You must be signed in to change notification settings - Fork 227
FOUR-16001 Implement resources and apiClient to use v1.1 in charts #6959
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2469a74
Enable apiClient 1.1
caleeli e0f1746
Add resources version 1.1
caleeli 405b140
Fix issues
caleeli 9469f0a
Fix code review issues
caleeli da6aa2f
Fix code review observations
caleeli 521374d
Use constant for prefix
caleeli 7e054a2
Improve the code to setup multiple version
caleeli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,174 @@ | ||
<?php | ||
|
||
namespace ProcessMaker\Http\Resources\V1_1; | ||
|
||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
use Illuminate\Database\Eloquent\Relations\HasOne; | ||
use Illuminate\Database\Eloquent\Relations\Relation; | ||
use Illuminate\Http\Request; | ||
use ProcessMaker\Http\Resources\ApiResource; | ||
use ProcessMaker\Http\Resources\ScreenVersion as ScreenVersionResource; | ||
use ProcessMaker\Http\Resources\Task; | ||
use ProcessMaker\Models\ProcessRequestToken; | ||
use ProcessMaker\ProcessTranslations\ProcessTranslation; | ||
use ProcessMaker\Traits\TaskResourceIncludes; | ||
use ProcessMaker\Traits\TaskScreenResourceTrait; | ||
|
||
class TaskResource extends ApiResource | ||
{ | ||
use TaskResourceIncludes; | ||
|
||
const INCLUDE_PREFIX = 'include'; | ||
|
||
protected static $includeMethods = [ | ||
'data', | ||
'user', | ||
'requestor', | ||
'processRequest', | ||
'draft', | ||
'component', | ||
'screen', | ||
'requestData', | ||
'loopContext', | ||
'definition', | ||
'bpmnTagName', | ||
'interstitial', | ||
'userRequestPermission', | ||
'process', | ||
]; | ||
|
||
protected static $defaultFields = [ | ||
'id', | ||
'element_name', | ||
'element_id', | ||
'due_at', | ||
]; | ||
|
||
protected static $defaultIncludes = [ | ||
'process', | ||
'advanceStatus', | ||
]; | ||
|
||
protected static $defaultFieldsFor = [ | ||
'user' => ['id', 'firstname', 'lastname', 'email', 'username', 'avatar'], | ||
'requestor' => ['id', 'first_name', 'last_name', 'email'], | ||
'processRequest' => ['id', 'process_id', 'status'], | ||
'draft' => ['id', 'task_id', 'data'], | ||
'screen' => ['id', 'config'], | ||
'process' => ['id', 'name'], | ||
]; | ||
|
||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @param \Illuminate\Http\Request | ||
* @return array | ||
*/ | ||
public function toArray($request) | ||
{ | ||
$array = [ | ||
'id' => $this->id, | ||
'element_name' => $this->element_name, | ||
'element_id' => $this->element_id, | ||
'due_at' => $this->due_at, | ||
'advancedStatus' => $this->advanceStatus, | ||
]; | ||
|
||
return $this->processInclude($request, $array); | ||
} | ||
|
||
private static function addRelationshipKeyColumn(Builder $query, string $relationship): bool | ||
{ | ||
$model = $query->getModel(); | ||
if (!method_exists($model, $relationship)) { | ||
return false; | ||
} | ||
$relationshipObject = $model->$relationship(); | ||
if (!($relationshipObject instanceof Relation)) { | ||
return false; | ||
} | ||
|
||
if ($relationshipObject instanceof BelongsTo) { | ||
$relationshipKey = $relationshipObject->getForeignKeyName(); | ||
$query->addSelect($relationshipKey); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private static function addRelationship(ProcessRequestToken $model, string $relationship): bool | ||
{ | ||
if (!method_exists($model, $relationship)) { | ||
return false; | ||
} | ||
$relationshipObject = $model->$relationship(); | ||
if (!($relationshipObject instanceof Relation)) { | ||
return false; | ||
} | ||
|
||
$relationshipColumns = self::$defaultFieldsFor[$relationship] ?? ['id']; | ||
$model->$relationship = $relationshipObject->select($relationshipColumns)->get(); | ||
|
||
return true; | ||
} | ||
|
||
public static function preprocessInclude(Request $request, Builder $query): self | ||
{ | ||
foreach (self::$defaultFields as $field) { | ||
$query->addSelect($field); | ||
} | ||
|
||
$include = $request->query('include', []); | ||
if ($include) { | ||
$include = explode(',', $include); | ||
} | ||
$include = array_merge($include, self::$defaultIncludes); | ||
|
||
if (in_array('data', $include)) { | ||
$query->addSelect('process_request_id'); | ||
} | ||
|
||
foreach (self::$includeMethods as $key) { | ||
if (!in_array($key, $include)) { | ||
continue; | ||
} | ||
|
||
self::addRelationshipKeyColumn($query, $key); | ||
} | ||
|
||
$model = $query->first(); | ||
foreach (self::$includeMethods as $key) { | ||
if (!in_array($key, $include)) { | ||
continue; | ||
} | ||
|
||
self::addRelationship($model, $key); | ||
} | ||
|
||
return new static($model); | ||
} | ||
|
||
private function processInclude(Request $request, array $array) | ||
{ | ||
$include = $request->query('include', []); | ||
if ($include) { | ||
$include = explode(',', $include); | ||
} | ||
|
||
foreach (self::$includeMethods as $key) { | ||
if (!in_array($key, $include)) { | ||
continue; | ||
} | ||
|
||
$method = self::INCLUDE_PREFIX . ucfirst($key); | ||
if (method_exists($this, $method)) { | ||
$attributes = $this->$method(); | ||
$array = array_merge($array, $attributes); | ||
} else { | ||
$array[$key] = $this->$key; | ||
} | ||
} | ||
return $array; | ||
} | ||
} |
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use here the new constant
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed