[CORL-943] Send test emails (#2882)

* add smtp test email

* disable button if email not enabled

* add error handling

* internationalize strings

* sort imports
This commit is contained in:
Tessa Thornton
2020-03-27 11:46:53 -04:00
committed by GitHub
parent 7fd524cd6a
commit 4645ed4cd7
11 changed files with 164 additions and 1 deletions
@@ -14,6 +14,7 @@ import {
regenerateSSOKey,
rotateSSOKey,
rotateWebhookEndpointSecret,
sendSMTPTest,
update,
updateWebhookEndpoint,
} from "coral-server/services/tenant";
@@ -42,6 +43,8 @@ export const Settings = ({
tenant,
config,
now,
mailerQueue,
user,
}: GraphContext) => ({
update: (
input: WithoutMutationID<GQLUpdateSettingsInput>
@@ -101,4 +104,5 @@ export const Settings = ({
input.inactiveIn,
now
),
testSMTP: () => sendSMTPTest(tenant, user!, mailerQueue),
});
@@ -326,4 +326,10 @@ export const Mutation: Required<GQLMutationTypeResolver<void>> = {
endpoint: await ctx.mutators.Settings.rotateWebhookEndpointSecret(input),
clientMutationId,
}),
testSMTP: async (source, { input: { clientMutationId } }, ctx) => {
await ctx.mutators.Settings.testSMTP();
return {
clientMutationId,
};
},
};
@@ -6244,6 +6244,14 @@ type UpdateSitePayload {
site: Site!
}
input TestSMTPInput {
clientMutationId: String!
}
type TestSMTPPayload {
clientMutationId: String!
}
##################
## Mutation
##################
@@ -6713,6 +6721,12 @@ type Mutation {
"""
removeStoryExpert(input: RemoveStoryExpertInput!): RemoveStoryExpertPayload!
@auth(roles: [ADMIN, MODERATOR])
"""
testSMTP sends a test email.
"""
testSMTP(input: TestSMTPInput!): TestSMTPPayload!
@auth(roles: [ADMIN, MODERATOR])
}
##################
+3
View File
@@ -121,3 +121,6 @@ email-template-notificationOnCommentRejected =
# Notification Digest
email-subject-notificationDigest = Your latest comment activity at { $organizationName }
email-subject-testSmtpTest = Test email from Coral
email-template-testSmtpTest = This is a test email sent to { $email }
@@ -3,6 +3,15 @@ interface EmailTemplate<T extends string, U extends {}> {
context: U;
}
type TestContext<T extends string, U extends {}> = EmailTemplate<T, U>;
export type SMTPTestTemplate = TestContext<
"test/smtp-test",
{
email: string;
}
>;
/**
* NotificationContext
*/
@@ -203,6 +212,7 @@ type Templates =
| OnFeaturedTemplate
| DigestTemplate
| OnCommentRejectedTemplate
| SMTPTestTemplate
| OnCommentApprovedTemplate;
export { Templates as EmailTemplate };
@@ -0,0 +1,8 @@
{% extends "layouts/base.html" %} {% block content %}
<div
data-l10n-id="email-template-testSmtpTest"
data-l10n-args="{{ context | dump }}"
>
This is a test email sent to {{ context.email }}
</div>
{% endblock %}
+27
View File
@@ -26,6 +26,8 @@ import {
updateTenantWebhookEndpoint,
UpdateTenantWebhookEndpointInput,
} from "coral-server/models/tenant";
import { User } from "coral-server/models/user";
import { MailerQueue } from "coral-server/queue/tasks/mailer";
import { I18n } from "coral-server/services/i18n";
import {
@@ -498,3 +500,28 @@ export async function deleteAnnouncement(
await cache.update(redis, updated);
return updated;
}
export async function sendSMTPTest(
tenant: Tenant,
user: User,
mailer: MailerQueue
) {
if (user.email) {
if (!tenant.email.enabled) {
throw new Error("Email not enabled");
}
await mailer.add({
tenantID: tenant.id,
message: {
to: user.email,
},
template: {
name: "test/smtp-test",
context: {
email: user.email,
},
},
});
}
return tenant;
}