Patch to put: Template API

This commit is contained in:
Matej Bačo
2026-05-20 12:58:53 +02:00
parent a899d8b61d
commit 159127ced5
2 changed files with 24 additions and 34 deletions
@@ -34,7 +34,7 @@ class Update extends Action
public function __construct()
{
$this
->setHttpMethod(Action::HTTP_REQUEST_METHOD_PATCH) // Should be PUT
->setHttpMethod(Action::HTTP_REQUEST_METHOD_PATCH) // Behaves as PUT
->setHttpPath('/v1/project/smtp')
->httpAlias('/v1/projects/:projectId/smtp')
->desc('Update project SMTP configuration')
@@ -16,7 +16,6 @@ use Utopia\Emails\Validator\Email;
use Utopia\Platform\Action;
use Utopia\Platform\Scope\HTTP;
use Utopia\System\System;
use Utopia\Validator\Nullable;
use Utopia\Validator\Text;
use Utopia\Validator\WhiteList;
@@ -31,7 +30,7 @@ class Update extends Action
public function __construct()
{
$this->setHttpMethod(Action::HTTP_REQUEST_METHOD_PATCH)
$this->setHttpMethod(Action::HTTP_REQUEST_METHOD_PATCH) // Should be PUT
->setHttpPath('/v1/project/templates/email')
->httpAlias('/v1/projects/:projectId/templates/email')
->httpAlias('/v1/projects/:projectId/templates/email/:templateId/:locale')
@@ -57,13 +56,13 @@ class Update extends Action
]
))
->param('templateId', '', new WhiteList(Config::getParam('locale-templates')['email'] ?? [], true), 'Custom email template type. Can be one of: '.\implode(', ', Config::getParam('locale-templates')['email'] ?? []))
->param('locale', '', fn ($localeCodes) => new WhiteList($localeCodes), 'Custom email template locale. If left empty, the fallback locale (en) will be used.', optional: true, injections: ['localeCodes'])
->param('subject', null, new Nullable(new Text(255)), 'Subject of the email template. Can be up to 255 characters.', optional: true)
->param('message', null, new Nullable(new Text(10485760)), 'Plain or HTML body of the email template message. Can be up to 10MB of content.', optional: true)
->param('senderName', null, new Nullable(new Text(255, 0)), 'Name of the email sender.', optional: true)
->param('senderEmail', null, new Nullable(new Email()), 'Email of the sender.', optional: true)
->param('replyToEmail', null, new Nullable(new Email()), 'Reply to email.', optional: true)
->param('replyToName', null, new Nullable(new Text(255, 0)), 'Reply to name.', optional: true)
->param('locale', '', fn ($localeCodes) => new WhiteList($localeCodes), 'Custom email template locale. If left empty, the fallback locale (en) will be used.', injections: ['localeCodes'])
->param('subject', null, new Text(255), 'Subject of the email template. Can be up to 255 characters.')
->param('message', null, new Text(10485760), 'Plain or HTML body of the email template message. Can be up to 10MB of content.')
->param('senderName', null, new Text(255, 0), 'Name of the email sender.')
->param('senderEmail', null, new Email(), 'Email of the sender.')
->param('replyToEmail', null, new Email(), 'Reply to email.')
->param('replyToName', null, new Text(255, 0), 'Reply to name.')
->inject('response')
->inject('queueForEvents')
->inject('dbForPlatform')
@@ -75,12 +74,12 @@ class Update extends Action
public function action(
string $templateId,
string $locale,
?string $subject,
?string $message,
?string $senderName,
?string $senderEmail,
?string $replyToEmail,
?string $replyToName,
string $subject,
string $message,
string $senderName,
string $senderEmail,
string $replyToEmail,
string $replyToName,
Response $response,
QueueEvent $queueForEvents,
Database $dbForPlatform,
@@ -95,24 +94,15 @@ class Update extends Action
throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'SMTP must be enabled on the project to configure custom email templates.');
}
// Fetch current configuration
$templates = $project->getAttribute('templates', []);
$template = $templates['email.' . $templateId . '-' . $locale] ?? [];
$template = [
'subject' => $subject,
'message' => $message,
'senderName' => $senderName,
'senderEmail' => $senderEmail,
'replyToEmail' => $replyToEmail,
'replyToName' => $replyToName,
];
// Apply changes
$keys = ['senderName', 'senderEmail', 'replyToEmail', 'replyToName', 'message', 'subject'];
foreach ($keys as $key) {
if (!\is_null(${$key})) {
$template[$key] = ${$key};
}
}
// Backwards compatibility
if (!\is_null($template['replyTo'] ?? null)) {
$template['replyToEmail'] = $template['replyToEmail'] ?? $template['replyTo'] ?? '';
}
// Ensure required fields are set
$requiredKeys = ['subject', 'message'];
foreach ($requiredKeys as $key) {
if (empty($template[$key])) {
@@ -120,7 +110,7 @@ class Update extends Action
}
}
// Save configuration
$templates = $project->getAttribute('templates', []);
$templates['email.' . $templateId . '-' . $locale] = $template;
$updates = new Document([
'templates' => $templates,