mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
Merge pull request #12696 from appwrite/fix/email-template-update-race
Fix flaky email template and usage tests
This commit is contained in:
2 files changed
+52
-34
No files matched your search
@@ -91,45 +91,63 @@ class Update extends Action
|
||||
) {
|
||||
$locale = $locale ?: System::getEnv('_APP_LOCALE', 'en');
|
||||
|
||||
// Prevent template update if custom SMTP is not configured
|
||||
$smtp = $project->getAttribute('smtp', []);
|
||||
if (($smtp['enabled'] ?? false) !== true) {
|
||||
throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'SMTP must be enabled on the project to configure custom email templates.');
|
||||
}
|
||||
$templateKey = "email.{$templateId}-{$locale}";
|
||||
$changes = [
|
||||
'senderName' => $senderName,
|
||||
'senderEmail' => $senderEmail,
|
||||
'replyToEmail' => $replyToEmail,
|
||||
'replyToName' => $replyToName,
|
||||
'message' => $message,
|
||||
'subject' => $subject,
|
||||
];
|
||||
|
||||
// Fetch current configuration
|
||||
$templates = $project->getAttribute('templates', []);
|
||||
$template = $templates['email.' . $templateId . '-' . $locale] ?? [];
|
||||
$project = $dbForPlatform->withTransaction(function () use ($dbForPlatform, $authorization, $project, $templateKey, $changes) {
|
||||
$project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId(), forUpdate: true));
|
||||
|
||||
// Apply changes — null means "not provided, keep existing".
|
||||
// Empty string explicitly clears a previously-set value.
|
||||
$keys = ['senderName', 'senderEmail', 'replyToEmail', 'replyToName', 'message', 'subject'];
|
||||
foreach ($keys as $key) {
|
||||
if (!\is_null(${$key})) {
|
||||
$template[$key] = ${$key};
|
||||
if ($project->isEmpty()) {
|
||||
throw new Exception(Exception::PROJECT_NOT_FOUND);
|
||||
}
|
||||
}
|
||||
|
||||
// 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])) {
|
||||
throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Param "' . $key . '" is not optional.');
|
||||
// Prevent template update if custom SMTP is not configured
|
||||
$smtp = $project->getAttribute('smtp', []);
|
||||
if (($smtp['enabled'] ?? false) !== true) {
|
||||
throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'SMTP must be enabled on the project to configure custom email templates.');
|
||||
}
|
||||
}
|
||||
|
||||
// Save configuration
|
||||
$templates['email.' . $templateId . '-' . $locale] = $template;
|
||||
$updates = new Document([
|
||||
'templates' => $templates,
|
||||
]);
|
||||
// Fetch current configuration from the locked row so concurrent updates
|
||||
// to other locale/template entries are preserved.
|
||||
$templates = $project->getAttribute('templates', []);
|
||||
$template = $templates[$templateKey] ?? [];
|
||||
|
||||
$project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates));
|
||||
// Apply changes: null means "not provided, keep existing".
|
||||
// Empty string explicitly clears a previously-set value.
|
||||
foreach ($changes as $key => $value) {
|
||||
if ($value !== null) {
|
||||
$template[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
// Backwards compatibility
|
||||
if (($template['replyTo'] ?? null) !== null) {
|
||||
$template['replyToEmail'] ??= $template['replyTo'] ?? '';
|
||||
}
|
||||
|
||||
// Ensure required fields are set
|
||||
$requiredKeys = ['subject', 'message'];
|
||||
foreach ($requiredKeys as $key) {
|
||||
if (empty($template[$key])) {
|
||||
throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Param "' . $key . '" is not optional.');
|
||||
}
|
||||
}
|
||||
|
||||
$templates[$templateKey] = $template;
|
||||
|
||||
return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([
|
||||
'templates' => $templates,
|
||||
])));
|
||||
});
|
||||
|
||||
$template = $project->getAttribute('templates', [])[$templateKey] ?? [];
|
||||
|
||||
$queueForEvents->setParam('templateId', $templateId);
|
||||
|
||||
|
||||
@@ -1869,7 +1869,7 @@ final class UsageTest extends Scope
|
||||
$this->validateDates($response['body']['executions']);
|
||||
$this->assertEquals($executionTime, $response['body']['executionsTime'][array_key_last($response['body']['executionsTime'])]['value']);
|
||||
$this->validateDates($response['body']['executionsTime']);
|
||||
});
|
||||
}, 60000, 500);
|
||||
|
||||
$this->assertEventually(function () use ($executions, $executionTime) {
|
||||
$response = $this->client->call(
|
||||
@@ -1901,7 +1901,7 @@ final class UsageTest extends Scope
|
||||
$this->validateDates($response['body']['executionsTime']);
|
||||
$this->assertGreaterThan(0, $response['body']['buildsTime'][array_key_last($response['body']['buildsTime'])]['value']);
|
||||
$this->validateDates($response['body']['buildsTime']);
|
||||
});
|
||||
}, 60000, 500);
|
||||
}
|
||||
|
||||
public function testCustomDomainsFunctionStats(): void
|
||||
|
||||
Reference in new issue
Block a user