Skip to content

Commit

Permalink
feat(api-mailer): make sure that default mailer is present
Browse files Browse the repository at this point in the history
  • Loading branch information
brunozoric committed Oct 25, 2022
1 parent 260ba01 commit 3dfdd3e
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 12 deletions.
18 changes: 18 additions & 0 deletions packages/api-mailer/__tests__/transporter.crud.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,22 @@ describe("Mailer Transporter CRUD", () => {
error: null
});
});

it("should use dummy transporter because of non-existing settings", async () => {
delete process.env.WEBINY_MAILER_HOST;
delete process.env.WEBINY_MAILER_USER;
delete process.env.WEBINY_MAILER_PASSWORD;
delete process.env.WEBINY_MAILER_REPLY_TO;
delete process.env.WEBINY_MAILER_FROM;

const context = await handle();

const result = await context.mailer.getTransport();

expect(result).toEqual({
name: "dummy-default",
send: expect.any(Function),
getAllSent: expect.any(Function)
});
});
});
11 changes: 5 additions & 6 deletions packages/api-mailer/src/crud/transporter.crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import {
MailerTransporterContext
} from "~/types";
import { createTopic } from "@webiny/pubsub";
import { attachOnTransportBeforeSend } from "~/crud/mailer/onMailerBeforeSend";
import { attachOnTransportBeforeSend } from "~/crud/transport/onTransportBeforeSend";
import { CreateTransportPlugin } from "~/plugins";
import WebinyError from "@webiny/error";

import { createValidation } from "./settings/validation";

interface BuildMailerParams {
plugins: CreateTransportPlugin[];
settings: TransportSettings;
settings: TransportSettings | null;
context: MailerContext;
}

Expand Down Expand Up @@ -117,10 +117,9 @@ export const createTransporterCrud = async (
}
if (!settings && !defaultSettings) {
console.log(`There are no Mailer transport settings for tenant "${tenant}".`);
return null;
}
const transporter = await buildTransporter({
settings: (settings || defaultSettings) as TransportSettings,
settings: settings || defaultSettings,
plugins,
context
});
Expand All @@ -141,8 +140,8 @@ export const createTransporterCrud = async (
return {
result: null,
error: {
message: "There is no mailer available.",
code: "NO_MAILER_DEFINED"
message: "There is no transport available.",
code: "NO_TRANSPORT_DEFINED"
}
};
}
Expand Down
4 changes: 1 addition & 3 deletions packages/api-mailer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ export const createMailer = (): PluginCollection => {
* Smtp mailer goes into the plugins after the dummy one because plugins are loaded in reverse.
*/
createTransport(async params => {
const plugin = await createSmtpTransport({
...params.settings
});
const plugin = await createSmtpTransport(params.settings);
plugin.name = "smtp-default";
return plugin;
}),
Expand Down
2 changes: 1 addition & 1 deletion packages/api-mailer/src/plugins/CreateTransportPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Plugin as BasePlugin } from "@webiny/plugins";
import { Transport, MailerContext, TransportSettings } from "~/types";

interface TransportParams {
settings: TransportSettings;
settings: TransportSettings | null;
context: MailerContext;
}

Expand Down
6 changes: 4 additions & 2 deletions packages/api-mailer/src/transports/createSmtpTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ export interface SmtpTransport extends Transport {
transporter: Transporter<SMTPTransport.SentMessageInfo>;
}

export const createSmtpTransport = (config?: Partial<SmtpTransportConfig>): SmtpTransport => {
if (!config) {
export const createSmtpTransport = (
config?: Partial<SmtpTransportConfig> | null
): SmtpTransport => {
if (!config || typeof config !== "object" || Object.keys(config).length === 0) {
throw new WebinyError("There is no configuration for the SMTP transport.");
}

Expand Down

0 comments on commit 3dfdd3e

Please sign in to comment.