-
Notifications
You must be signed in to change notification settings - Fork 466
pkp/pkp-lib#11779 added API endpoint to create task templates #11807
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
Conversation
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.
Thanks! I left a couple of comments
$payload['dateDue'] = $illuminateRequest->input('dateDue'); | ||
} | ||
|
||
// validation |
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.
You can use the formRequest for validation https://github.com/pkp/pkp-lib/blob/main/api/v1/reviewers/suggestions/formRequests/AddReviewerSuggestion.php
Then, type hint it as an argument in the Controller method https://github.com/pkp/pkp-lib/blob/main/api/v1/reviewers/suggestions/ReviewerSuggestionController.php#L155
$template->userGroups()->sync($payload['userGroupIds']); | ||
|
||
// store extra template config in settings (non-localized) | ||
DB::table('edit_task_template_settings')->insert([ |
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.
I don't think we have got any settings for the Task Template
return $template->getKey(); | ||
}); | ||
|
||
// serialize with TaskResource |
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.
Regarding Resources, it should have its own resource. You can use task resource as an example: https://github.com/pkp/pkp-lib/blob/main/api/v1/submissions/resources/TaskResource.php
95e3d45
to
d01209f
Compare
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.
Thanks! Only 2 minor comments, otherwise looks good!
$contextId = Application::get()->getRequest()->getContext()->getId(); | ||
|
||
return [ | ||
'stageId' => ['required', 'integer', 'min:1'], |
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.
you can check if the application has the given stage by Application::getApplicationStages()
'stageId' => ['required', 'integer', 'min:1'], | ||
'title' => ['required', 'string', 'max:255'], | ||
'include' => ['sometimes', 'boolean'], | ||
'emailTemplateId' => ['sometimes', 'nullable', 'integer', Rule::exists('email_templates', 'email_id')], |
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.
Maybe specifying the key would be better?
And then we can validate with a custom rule: https://laravel.com/docs/12.x/validation#using-rule-objects. Trying to find all templates by their keys in the given context:
$templates = Repo::emailTemplate()->getCollector()->getMany();
d01209f
to
883a7ac
Compare
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.
Thanks! A couple of additional comments
$emailTemplateId = null; | ||
if (!empty($validated['emailTemplateKey'])) { | ||
$et = Repo::emailTemplate()->getByKey($context->getId(), $validated['emailTemplateKey']); | ||
$emailTemplateId = $et?->getId(); |
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.
I think we should make an additional migration and reference from this column email template key, which also is unique. That's how later we can get either the default template, the customised one or a brand new, created specifically for the task
$template = DB::transaction(function () use ($validated, $context) { | ||
$emailTemplateId = null; | ||
if (!empty($validated['emailTemplateKey'])) { | ||
$et = Repo::emailTemplate()->getByKey($context->getId(), $validated['emailTemplateKey']); |
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.
I think we can use one of the validation hooks instead of the controller for such actions. Not sure but it looks like we can create a method in the class that extends FormRequest
for this:
protected function passedValidation()
{
// transform data after validation is passed
}
]); | ||
} | ||
|
||
public function validated($key = null, $default = null) |
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.
Can you double check if we need to override this method here? I think we can avoid that...
'userGroupIds' => $this->whenLoaded('userGroups', fn () => $this->userGroups->pluck('user_group_id')->values()->all()), | ||
'userGroups' => $this->whenLoaded('userGroups', fn () => $this->userGroups->map(fn ($ug) => [ | ||
'id' => (int) $ug->user_group_id, | ||
'name' => method_exists($ug, 'getLocalizedName') ? $ug->getLocalizedName() : ($ug->name ?? null), |
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.
Can you double check if we need to return an assoc array with data in all locales or just a localized string?
883a7ac
to
df710af
Compare
df710af
to
f25b7a9
Compare
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.
Just minor comments, looks good, thanks!
use Illuminate\Foundation\Http\FormRequest; | ||
use Illuminate\Validation\Rule; | ||
|
||
class AddEditTaskTemplate extends FormRequest |
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.
It can be renamed just to the AddTaskTemplate
to avoid confusion
public function up(): void | ||
{ | ||
Schema::table('edit_task_templates', function (Blueprint $table) { | ||
if (!Schema::hasColumn('edit_task_templates', 'email_template_key')) { |
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.
Personally, I'd avoid using such checks. That's because of something like this goes wrong, it's better to end the migration and give user the ability to restart it from the backup
|
||
// drop FK and old column | ||
Schema::table('edit_task_templates', function (Blueprint $table) { | ||
try { $table->dropForeign(['email_template_id']); } catch (\Throwable $e) {} |
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.
The same as above
for #11779