Skip to content
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

Chunk of refactors and error handling #1345

Merged
merged 14 commits into from
May 14, 2024
7 changes: 0 additions & 7 deletions apps/smtp/src/lib/is-in-iframe.ts

This file was deleted.

19 changes: 0 additions & 19 deletions apps/smtp/src/lib/no-ssr-wrapper.tsx

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { AuthData } from "@saleor/app-sdk/APL";
import { SendEventMessagesUseCase } from "./send-event-messages.use-case";
import { SmtpEmailSender } from "../smtp/services/smtp-email-sender";
import { EmailCompiler } from "../smtp/services/email-compiler";
import { HandlebarsTemplateCompiler } from "../smtp/services/handlebars-template-compiler";
import { HtmlToTextCompiler } from "../smtp/services/html-to-text-compiler";
import { MjmlCompiler } from "../smtp/services/mjml-compiler";
import { SmtpConfigurationService } from "../smtp/configuration/smtp-configuration.service";
import { FeatureFlagService } from "../feature-flag-service/feature-flag-service";
import { SmtpMetadataManager } from "../smtp/configuration/smtp-metadata-manager";
import { createSettingsManager } from "../../lib/metadata-manager";
import { createInstrumentedGraphqlClient } from "../../lib/create-instrumented-graphql-client";

export class SendEventMessagesUseCaseFactory {
createFromAuthData(authData: AuthData): SendEventMessagesUseCase {
const client = createInstrumentedGraphqlClient({
saleorApiUrl: authData.saleorApiUrl,
token: authData.token,
});

return new SendEventMessagesUseCase({
emailSender: new SmtpEmailSender(),
emailCompiler: new EmailCompiler(
new HandlebarsTemplateCompiler(),
new HtmlToTextCompiler(),
new MjmlCompiler(),
),
smtpConfigurationService: new SmtpConfigurationService({
featureFlagService: new FeatureFlagService({ client }),
metadataManager: new SmtpMetadataManager(
createSettingsManager(client, authData.appId),
authData.saleorApiUrl,
),
}),
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { describe, expect, it } from "vitest";
import { SendEventMessagesUseCase } from "./send-event-messages.use-case";
import { SendEventMessagesUseCaseFactory } from "./send-event-messages.use-case.factory";

describe("SendEventMessagesUseCase", () => {
describe("Factory", () => {
it("Build with default dependencies from AuthData", () => {
const instance = new SendEventMessagesUseCaseFactory().createFromAuthData({
saleorApiUrl: "https://demo.saleor.cloud/graphql/",
token: "foo",
appId: "1",
});

expect(instance).toBeDefined();
});
});

describe("sendEventMessages method", () => {
it.todo("Returns error when no active configurations are available for selected channel");

describe("Multiple configurations assigned for the same event", () => {
it.todo("Calls SMTP service to send email for each configuration");
});

describe("Single configuration assigned for the event", () => {
it.todo("Does nothing (?) if config is missing for this event");

it.todo("Does nothing (?) if event is set to not active");

it.todo("Does nothing (?) if configuration sender name is missing");

it.todo("Does nothing (?) if configuration sender email is missing");

it.todo("Does nothing (?) if email compilation fails");

it.todo("Calls SMTP service to send email");
});
});
});
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { SmtpConfigurationService } from "../smtp/configuration/smtp-configuration.service";
import { IEmailCompiler } from "../smtp/email-compiler";
import { IEmailCompiler } from "../smtp/services/email-compiler";
import { MessageEventTypes } from "./message-event-types";
import { createLogger } from "../../logger";
import { ISMTPEmailSender, SendMailArgs } from "../smtp/smtp-email-sender";
import { ISMTPEmailSender, SendMailArgs } from "../smtp/services/smtp-email-sender";

/*
* todo test
* todo: how this service should handle error for one config and success for another?
*/
export class SendEventMessagesUseCase {
private logger = createLogger("SendEventMessagesUseCase");

Expand All @@ -21,8 +25,8 @@ export class SendEventMessagesUseCase {
recipientEmail,
channelSlug,
}: {
channelSlug: string; // todo: id or slug
payload: any; // todo can be narrowed?
channelSlug: string;
payload: unknown;
recipientEmail: string;
event: MessageEventTypes;
}) {
Expand All @@ -42,15 +46,43 @@ export class SendEventMessagesUseCase {
*/
for (const smtpConfiguration of availableSmtpConfigurations.value) {
try {
const preparedEmail = this.deps.emailCompiler.compile({
const eventSettings = smtpConfiguration.events.find((e) => e.eventType === event);

if (!eventSettings) {
/*
* Config missing, ignore
* todo log
*/
return;
}

if (!eventSettings.active) {
/**
* Config found, but set as disabled, ignore.
* todo: log
*/
return;
}

if (!smtpConfiguration.senderName || !smtpConfiguration.senderEmail) {
/**
* TODO: check if this should be allowed
*/
return;
}

const preparedEmailResult = this.deps.emailCompiler.compile({
event: event,
payload: payload,
recipientEmail: recipientEmail,
smtpConfiguration,
bodyTemplate: eventSettings.template,
subjectTemplate: eventSettings.subject,
senderEmail: smtpConfiguration.senderEmail,
senderName: smtpConfiguration.senderName,
});

if (!preparedEmail) {
return; // todo log
if (preparedEmailResult.isErr()) {
return; // todo log + what should we do?
}

const smtpSettings: SendMailArgs["smtpSettings"] = {
Expand All @@ -68,7 +100,7 @@ export class SendEventMessagesUseCase {

try {
await this.deps.emailSender.sendEmailWithSmtp({
mailData: preparedEmail,
mailData: preparedEmailResult.value,
smtpSettings,
});
} catch (e) {
Expand Down
43 changes: 0 additions & 43 deletions apps/smtp/src/modules/shop-info/shop-info-fetcher.ts

This file was deleted.

22 changes: 0 additions & 22 deletions apps/smtp/src/modules/smtp/compile-handlebars-template.ts

This file was deleted.

17 changes: 0 additions & 17 deletions apps/smtp/src/modules/smtp/compile-mjml.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SmtpConfigurationService } from "./smtp-configuration.service";
import { router } from "../../trpc/trpc-server";
import { z } from "zod";
import { compileMjml } from "../compile-mjml";
import { MjmlCompiler } from "../services/mjml-compiler";
import Handlebars from "handlebars";
import { TRPCError } from "@trpc/server";
import {
Expand Down Expand Up @@ -162,10 +162,10 @@ export const smtpConfigurationRouter = router({
const compiledSubjectTemplate = Handlebars.compile(input.template);
const templatedEmail = compiledSubjectTemplate(payload);

const { html: rawHtml } = compileMjml(templatedEmail);
const compilationResult = new MjmlCompiler().compile(templatedEmail);

if (rawHtml) {
renderedEmail = rawHtml;
if (compilationResult.isOk()) {
renderedEmail = compilationResult.value;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { filterConfigurations } from "../../app-configuration/filter-configurati
import { FeatureFlagService } from "../../feature-flag-service/feature-flag-service";
import { createLogger } from "../../../logger";
import { BaseError } from "../../../errors";
import { err, errAsync, fromAsyncThrowable, ok, okAsync, Result, ResultAsync } from "neverthrow";
import { err, errAsync, fromAsyncThrowable, ok, okAsync, ResultAsync } from "neverthrow";

const logger = createLogger("SmtpConfigurationService");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SettingsManager } from "@saleor/app-sdk/settings-manager";
import { SmtpConfig } from "./smtp-config-schema";
import { fromAsyncThrowable, fromThrowable, ok, okAsync, Result, ResultAsync } from "neverthrow";
import { fromAsyncThrowable, fromThrowable, ok, ResultAsync } from "neverthrow";
import { BaseError } from "../../../errors";

export class SmtpMetadataManager {
Expand Down
Loading
Loading