Skip to content

Commit

Permalink
#4787 Reviewer suggestions api update
Browse files Browse the repository at this point in the history
  • Loading branch information
touhidurabir committed Oct 14, 2024
1 parent 304c66c commit c2c4775
Show file tree
Hide file tree
Showing 21 changed files with 814 additions and 167 deletions.
65 changes: 0 additions & 65 deletions api/v1/reviewerSuggestions/formRequests/AddReviewerSuggestion.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,25 @@
*
*/

namespace PKP\API\v1\reviewerSuggestions;
namespace PKP\API\v1\reviewers\suggestions;

use APP\core\Application;
use PKP\API\v1\reviewerSuggestions\formRequests\AddReviewerSuggestion;
use PKP\API\v1\reviewers\suggestions\resources\ReviewerSuggestionResource;

use PKP\security\authorization\internal\SubmissionIncompletePolicy;
use APP\facades\Repo;
use PKP\API\v1\reviewers\suggestions\formRequests\AddReviewerSuggestion;
use PKP\security\authorization\internal\SubmissionIncompletePolicy;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Route;
use PKP\core\PKPApplication;
use PKP\core\PKPBaseController;
use PKP\core\PKPRequest;
use PKP\db\DAORegistry;
use PKP\log\EmailLogEntry;
use PKP\log\SubmissionEmailLogEventType;
use PKP\security\authorization\ContextAccessPolicy;
use PKP\security\authorization\SubmissionAccessPolicy;
use PKP\security\authorization\UserRolesRequiredPolicy;
use PKP\security\Role;
use PKP\submissionFile\SubmissionFile;
use PKP\submission\reviewer\suggestion\ReviewerSuggestion;

class ReviewerSuggestionController extends PKPBaseController
{
Expand All @@ -43,7 +40,7 @@ class ReviewerSuggestionController extends PKPBaseController
*/
public function getHandlerPath(): string
{
return 'reviewerSuggestions';
return 'submissions/{submissionId}/reviewers/suggestions';
}

/**
Expand Down Expand Up @@ -74,11 +71,11 @@ public function getGroupRoutes(): void
->name('reviewer.suggestions.get')
->whereNumber('suggestionId');

Route::get('submission/{submissionId}', $this->getMany(...))
Route::get('', $this->getMany(...))
->name('reviewer.suggestions.getMany')
->whereNumber('submissionId');

Route::post('submission/{submissionId}', $this->add(...))
Route::post('', $this->add(...))
->name('reviewer.suggestions.add');

Route::put('{suggestionId}', $this->edit(...))
Expand Down Expand Up @@ -114,30 +111,40 @@ public function authorize(PKPRequest $request, array &$args, array $roleAssignme

public function get(Request $illuminateRequest): JsonResponse
{
$request = $this->getRequest();
$context = $request->getContext();
$contextId = $context->getId();
$suggestion = ReviewerSuggestion::find($illuminateRequest->route('suggestionId'));

if (!$suggestion) {
return response()->json([
'error' => __('api.404.resourceNotFound'),
], Response::HTTP_NOT_FOUND);
}

return response()->json([], Response::HTTP_OK);
return response()->json(
(new ReviewerSuggestionResource($suggestion))->toArray($illuminateRequest),
Response::HTTP_OK
);
}

public function getMany(Request $illuminateRequest): JsonResponse
{
$request = $this->getRequest();
$context = $request->getContext();
$contextId = $context->getId();
$suggestions = ReviewerSuggestion::withSubmissionId($illuminateRequest->route('sibmissionId'))->get();

return response()->json([], Response::HTTP_OK);
return response()->json([
'items' => ReviewerSuggestionResource::collection($suggestions),
'itemMax' => $suggestions->count(),
], Response::HTTP_OK);
}

public function add(AddReviewerSuggestion $illuminateRequest): JsonResponse
{
$validateds = $illuminateRequest->validated();
$request = $this->getRequest();
$context = $request->getContext();
$contextId = $context->getId();

$suggestion = ReviewerSuggestion::create($validateds);

return response()->json([], Response::HTTP_OK);
return response()->json(
(new ReviewerSuggestionResource($suggestion))->toArray($illuminateRequest),
Response::HTTP_OK
);
}

public function edit(Request $illuminateRequest): JsonResponse
Expand Down
107 changes: 107 additions & 0 deletions api/v1/reviewers/suggestions/formRequests/AddReviewerSuggestion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

namespace PKP\API\v1\reviewers\suggestions\formRequests;

use APP\core\Application;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class AddReviewerSuggestion extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}

/**
* @copydoc \Illuminate\Foundation\Http\FormRequest::validated()
*/
public function validated($key = null, $default = null)
{
$validated = parent::validated($key, $default);

return collect($validated)->map(
fn($value) => is_array($value) ? array_filter($value) : $value
)->toArray();
}

/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
return [
'submissionId' => [
'required',
'integer',
Rule::exists('submissions', 'submission_id'),
],
'suggestingUserId' => [
'sometimes',
'nullable',
'integer',
Rule::exists('users', 'user_id'),
],
'familyName' => [
'required',
'array',
],
'givenName' => [
'required',
'array',
],
'email' => [
'required',
'email',
],
'affiliation' => [
'required',
'array',
],
'suggestionReason' => [
'required',
'array',
],
];
}

/**
* Get the error messages for the defined validation rules.
*
* @return array<string, string>
*/
public function messages(): array
{
return [
'familyName.required' => 'family name is required',
];
}

/**
* Get custom attributes for validator errors.
*
* @return array<string, string>
*/
public function attributes(): array
{
return [
'familyName' => __('user.familyName'),
'givenName' => __('user.givenName'),
'email' => __('user.email'),
];
}

/**
* Prepare the data for validation.
*/
protected function prepareForValidation(): void
{
$this->merge([
'suggestingUserId' => Application::get()->getRequest()?->getUser()?->getId(),
'submissionId' => $this->route('submissionId'),
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace PKP\API\v1\reviewers\suggestions\resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class ReviewerSuggestionResource extends JsonResource
{
/**
* Transform the resource into an array.
*/
public function toArray(Request $request)
{
return [
'id' => $this->id,
'submissionId' => $this->submissionId,
'suggestingUserId' => $this->suggestingUserId,
'familyName' => $this->familyName,
'givenName' => $this->givenName,
'fullName' => $this->fullname,
'email' => $this->email,
'orcidId' => $this->orcidId,
'affiliation' => $this->affiliation,
'suggestionReason' => $this->suggestionReason,
];
}
}
82 changes: 82 additions & 0 deletions classes/components/forms/publication/ReviewerSuggestionsForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* @file classes/components/form/publication/ReviewerSuggestionsForm.php
*
* Copyright (c) 2024 Simon Fraser University
* Copyright (c) 2024 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class ReviewerSuggestionsForm
*
* @brief
*/

namespace PKP\components\forms\publication;

use APP\submission\Submission;
use PKP\components\forms\FieldOrcid;
use PKP\components\forms\FieldRichTextarea;
use PKP\components\forms\FieldText;
use PKP\components\forms\FormComponent;
use PKP\context\Context;
use PKP\orcid\OrcidManager;

class ReviewerSuggestionsForm extends FormComponent
{
public const FORM_CONTRIBUTOR = 'reviewerSuggestions';

/**
* @copydoc FormComponent::$id
*/
public $id = self::FORM_CONTRIBUTOR;

/**
* @copydoc FormComponent::$method
*/
public $method = 'POST';

public Submission $submission;
public Context $context;

public function __construct(string $action, array $locales, Submission $submission, Context $context)
{
$this->action = $action;
$this->locales = $locales;
$this->submission = $submission;
$this->context = $context;

$this
->addField(new FieldText('givenName', [
'label' => __('user.givenName'),
'isMultilingual' => true,
'isRequired' => true,
]))
->addField(new FieldText('familyName', [
'label' => __('user.familyName'),
'isMultilingual' => true,
'isRequired' => true,
]))
->addField(new FieldText('email', [
'label' => __('user.email'),
'isRequired' => true,
]))
->addField(new FieldText('affiliation', [
'label' => __('user.affiliation'),
'isRequired' => true,
'isMultilingual' => true,
]))
->addField(new FieldRichTextarea('suggestionReason', [
'label' => __('reviewerSuggestion.suggestionReason'),
'description' => __('reviewerSuggestion.suggestionReason.description'),
'isRequired' => true,
'isMultilingual' => true,
]));

if (OrcidManager::isEnabled()) {
$this->addField(new FieldOrcid('orcid', [
'label' => __('user.orcid'),
'tooltip' => __('orcid.about.orcidExplanation'),
]), [FIELD_POSITION_AFTER, 'email']);
}
}
}
Loading

0 comments on commit c2c4775

Please sign in to comment.