From 1085d09ef53fb44a1211b0ae6f25f5fd884b1624 Mon Sep 17 00:00:00 2001 From: Taslan Graham Date: Wed, 25 Sep 2024 19:39:49 -0500 Subject: [PATCH] pkp/pkp-lib#10465 Improve GET submissions/{submissionId}/decisions endpoint --- .../submissions/PKPSubmissionController.php | 25 +++++++++++++---- classes/decision/Collector.php | 28 ++++++++++++++++++- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/api/v1/submissions/PKPSubmissionController.php b/api/v1/submissions/PKPSubmissionController.php index 25c11d64a33..633111ee51f 100644 --- a/api/v1/submissions/PKPSubmissionController.php +++ b/api/v1/submissions/PKPSubmissionController.php @@ -906,8 +906,11 @@ public function changeLocale(Request $illuminateRequest): JsonResponse // Convert a form field value to multilingual (if it is not) and merge rest values collect(app()->get('schema')->getMultilingualProps(PKPSchemaService::SCHEMA_PUBLICATION)) - ->each(fn (string $prop) => - $illuminateRequest->whenHas($prop, fn ($value) => + ->each( + fn (string $prop) => + $illuminateRequest->whenHas( + $prop, + fn ($value) => $illuminateRequest->merge([ $prop => array_merge( $publication->getData($prop) ?? [], @@ -943,8 +946,19 @@ public function getDecisions(Request $illuminateRequest): JsonResponse ], Response::HTTP_NOT_FOUND); } - $decisionIterator = Repo::decision()->getCollector() + $decisionTypes = array_map('intval', paramToArray($illuminateRequest->input('decisionTypes') ?? [])); + $editorIds = array_map('intval', paramToArray($illuminateRequest->input('editorIds') ?? [])); + $reviewRoundId = $illuminateRequest->input('reviewRoundId') ? [(int)$illuminateRequest->input('reviewRoundId')] : null; + $stageId = $illuminateRequest->input('stageId') ? [(int)$illuminateRequest->input('stageId')] : null; + + $collector = Repo::decision()->getCollector(); + $decisionIterator = $collector ->filterBySubmissionIds([$submission->getId()]) + ->filterByDecisionTypes(!empty($decisionTypes) ? $decisionTypes : null) + ->filterByEditorIds(!empty($editorIds) ? $editorIds : null) + ->filterByReviewRoundIds($reviewRoundId) + ->filterByStageIds($stageId) + ->orderBy($collector::ORDERBY_DATE_DECIDED, $collector::ORDER_DIR_DESC) ->getMany(); $data = Repo::decision() @@ -2050,7 +2064,8 @@ protected function getSubmissionAndPublicationData(Request $illuminateRequest): /** * Copy author, files, etc. multilingual fields from old to new changed language */ - protected function copyMultilingualData(Submission $submission, string $newLocale): void { + protected function copyMultilingualData(Submission $submission, string $newLocale): void + { $oldLocale = $submission->getData('locale'); $editProps = fn (Author|SubmissionFile $item, array $props): array => collect($props) ->mapWithKeys(fn (string $prop): array => [$prop => ($data = $item->getData($prop)[$oldLocale] ?? null) ? [$newLocale => $data] : null]) @@ -2066,7 +2081,7 @@ protected function copyMultilingualData(Submission $submission, string $newLocal ->filterBySubmissionIds([$submission->getId()]) ->getMany() ->each(fn (SubmissionFile $file) => Repo::submissionFile()->edit($file, $editProps($file, $fileProps))); - + // Contributor $contributorProps = [ 'givenName', diff --git a/classes/decision/Collector.php b/classes/decision/Collector.php index 8f44667198c..a628e508dbc 100644 --- a/classes/decision/Collector.php +++ b/classes/decision/Collector.php @@ -33,6 +33,17 @@ class Collector implements CollectorInterface public ?array $stageIds = null; public ?array $submissionIds = null; + public const ORDERBY_DATE_DECIDED = 'date_decided'; + public const ORDER_DIR_ASC = 'ASC'; + public const ORDER_DIR_DESC = 'DESC'; + + /** The default orderBy value for decision collector */ + private ?string $orderBy = self::ORDERBY_DATE_DECIDED; + + /** The default orderBy direction value for decision collector */ + private ?string $orderDirection = self::ORDER_DIR_ASC; + + public function __construct(DAO $dao) { $this->dao = $dao; @@ -129,6 +140,21 @@ public function filterBySubmissionIds(?array $submissionIds): self return $this; } + /** + * Order the results + * + * Results are ordered by the date_decided column by default. + * + * @param string|null $column Name of column to order results by. + * @param string $direction One of the self::ORDER_DIR_ constants + */ + public function orderBy(?string $column, string $direction = self::ORDER_DIR_ASC): self + { + $this->orderBy = $column; + $this->orderDirection = $direction; + return $this; + } + /** * @copydoc CollectorInterface::getQueryBuilder() * @@ -156,7 +182,7 @@ public function getQueryBuilder(): Builder ->when(!is_null($this->submissionIds), function ($q) { $q->whereIn('submission_id', $this->submissionIds); }) - ->orderBy('date_decided', 'asc'); + ->orderBy($this->orderBy ?? self::ORDERBY_DATE_DECIDED, $this->orderDirection ?? self::ORDER_DIR_ASC); Hook::call('Decision::Collector', [&$qb, $this]);