parent
bda814b7b3
commit
78670ce712
22 changed files with 73 additions and 79 deletions
5
.changeset/witty-plants-stare.md
Normal file
5
.changeset/witty-plants-stare.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
"saleor-app-emails-and-messages": patch
|
||||
---
|
||||
|
||||
Changed formatting in the debugging logs to be more concise.
|
|
@ -29,7 +29,7 @@ export const sendEventMessages = async ({
|
|||
client,
|
||||
}: SendEventMessagesArgs) => {
|
||||
const logger = createLogger({
|
||||
fn: "sendEventMessages",
|
||||
name: "sendEventMessages",
|
||||
});
|
||||
|
||||
logger.debug("Function called");
|
||||
|
|
|
@ -5,7 +5,7 @@ import { sendgridTransformV1toV2 } from "./sendgrid-transform-v1-to-v2";
|
|||
import { createLogger } from "@saleor/apps-shared";
|
||||
|
||||
const logger = createLogger({
|
||||
fn: "sendgridConfigMigrationV1ToV2",
|
||||
name: "sendgridConfigMigrationV1ToV2",
|
||||
});
|
||||
|
||||
interface SendgridConfigMigrationV1ToV1Args {
|
||||
|
|
|
@ -12,7 +12,7 @@ import { SendgridPrivateMetadataManager } from "./sendgrid-metadata-manager";
|
|||
import { FeatureFlagService } from "../../feature-flag-service/feature-flag-service";
|
||||
|
||||
const logger = createLogger({
|
||||
service: "SendgridConfigurationService",
|
||||
name: "SendgridConfigurationService",
|
||||
});
|
||||
|
||||
export type SendgridConfigurationServiceErrorType =
|
||||
|
|
|
@ -5,7 +5,7 @@ import { sendgridConfigMigrationV1ToV2 } from "./migrations/sendgrid-config-migr
|
|||
import { createLogger } from "@saleor/apps-shared";
|
||||
|
||||
const logger = createLogger({
|
||||
fn: "SendgridPrivateMetadataManager",
|
||||
name: "SendgridPrivateMetadataManager",
|
||||
});
|
||||
|
||||
export class SendgridPrivateMetadataManager {
|
||||
|
|
|
@ -24,7 +24,7 @@ export const sendSendgrid = async ({
|
|||
sendgridConfiguration,
|
||||
}: SendSendgridArgs) => {
|
||||
const logger = createLogger({
|
||||
fn: "sendSendgrid",
|
||||
name: "sendSendgrid",
|
||||
event,
|
||||
});
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@ import Handlebars from "handlebars";
|
|||
import { createLogger } from "@saleor/apps-shared";
|
||||
|
||||
const logger = createLogger({
|
||||
fn: "compileHandlebarsTemplate",
|
||||
name: "compileHandlebarsTemplate",
|
||||
});
|
||||
|
||||
export const compileHandlebarsTemplate = (template: string, variables: any) => {
|
||||
|
|
|
@ -2,7 +2,7 @@ import mjml2html from "mjml";
|
|||
import { createLogger } from "@saleor/apps-shared";
|
||||
|
||||
const logger = createLogger({
|
||||
fn: "compileMjml",
|
||||
name: "compileMjml",
|
||||
});
|
||||
|
||||
export const compileMjml = (mjml: string) => {
|
||||
|
|
|
@ -1,15 +1,11 @@
|
|||
import { AppConfigPrivateMetadataManager } from "../../../app-configuration/app-config-metadata-manager";
|
||||
import { AppConfig } from "../../../app-configuration/app-config-schema";
|
||||
import { ChannelConfiguration } from "../../../channels/channel-configuration-schema";
|
||||
import { SettingsManager } from "@saleor/app-sdk/settings-manager";
|
||||
import { MjmlPrivateMetadataManager } from "../mjml-metadata-manager";
|
||||
import { SmtpConfigV2 } from "./smtp-config-schema-v2";
|
||||
import { getChannelsAssignedToConfigId } from "../../../app-configuration/migrations/get-channels-assigned-to-config-id";
|
||||
import { smtpTransformV1toV2 } from "./smtp-transform-v1-to-v2";
|
||||
import { createLogger } from "@saleor/apps-shared";
|
||||
|
||||
const logger = createLogger({
|
||||
fn: "smtpConfigMigrationV1ToV2",
|
||||
name: "smtpConfigMigrationV1ToV2",
|
||||
});
|
||||
|
||||
interface SmtpConfigMigrationV1ToV1Args {
|
||||
|
|
|
@ -8,7 +8,7 @@ import { filterConfigurations } from "../../app-configuration/filter-configurati
|
|||
import { FeatureFlagService } from "../../feature-flag-service/feature-flag-service";
|
||||
|
||||
const logger = createLogger({
|
||||
service: "SmtpConfigurationService",
|
||||
name: "SmtpConfigurationService",
|
||||
});
|
||||
|
||||
export type SmtpConfigurationServiceErrorType =
|
||||
|
|
|
@ -5,7 +5,7 @@ import { smtpConfigMigrationV1ToV2 } from "./migrations/smtp-config-migration-v1
|
|||
import { createLogger } from "@saleor/apps-shared";
|
||||
|
||||
const logger = createLogger({
|
||||
fn: "SmtpPrivateMetadataManager",
|
||||
name: "SmtpPrivateMetadataManager",
|
||||
});
|
||||
|
||||
export class SmtpPrivateMetadataManager {
|
||||
|
|
|
@ -2,7 +2,7 @@ import { convert } from "html-to-text";
|
|||
import { createLogger } from "@saleor/apps-shared";
|
||||
|
||||
const logger = createLogger({
|
||||
fn: "htmlToPlaintext",
|
||||
name: "htmlToPlaintext",
|
||||
});
|
||||
|
||||
export const htmlToPlaintext = (html: string) => {
|
||||
|
|
|
@ -3,7 +3,7 @@ import { createLogger } from "@saleor/apps-shared";
|
|||
import { SmtpEncryptionType } from "./configuration/migrations/mjml-config-schema-v1";
|
||||
|
||||
const logger = createLogger({
|
||||
fn: "sendEmailWithSmtp",
|
||||
name: "sendEmailWithSmtp",
|
||||
});
|
||||
|
||||
export interface SendMailArgs {
|
||||
|
|
|
@ -27,7 +27,7 @@ export const sendSmtp = async ({
|
|||
smtpConfiguration,
|
||||
}: SendSmtpArgs) => {
|
||||
const logger = createLogger({
|
||||
fn: "sendSmtp",
|
||||
name: "sendSmtp",
|
||||
event,
|
||||
});
|
||||
|
||||
|
@ -35,24 +35,12 @@ export const sendSmtp = async ({
|
|||
|
||||
if (!eventSettings) {
|
||||
logger.debug("No active settings for this event, skipping");
|
||||
return {
|
||||
errors: [
|
||||
{
|
||||
message: "No active settings for this event",
|
||||
},
|
||||
],
|
||||
};
|
||||
return;
|
||||
}
|
||||
|
||||
if (!eventSettings.active) {
|
||||
logger.debug("Event settings are not active, skipping");
|
||||
return {
|
||||
errors: [
|
||||
{
|
||||
message: "Event settings are not active",
|
||||
},
|
||||
],
|
||||
};
|
||||
return;
|
||||
}
|
||||
|
||||
logger.debug("Sending an email using MJML");
|
||||
|
@ -64,8 +52,6 @@ export const sendSmtp = async ({
|
|||
payload
|
||||
);
|
||||
|
||||
logger.warn(`email subject ${emailSubject} ${subject}`);
|
||||
|
||||
if (handlebarsSubjectErrors?.length) {
|
||||
logger.error("Error during the handlebars subject template compilation");
|
||||
return {
|
||||
|
@ -80,6 +66,8 @@ export const sendSmtp = async ({
|
|||
};
|
||||
}
|
||||
|
||||
logger.debug({ emailSubject }, "Subject compiled");
|
||||
|
||||
const { template: mjmlTemplate, errors: handlebarsErrors } = compileHandlebarsTemplate(
|
||||
rawTemplate,
|
||||
payload
|
||||
|
@ -99,6 +87,8 @@ export const sendSmtp = async ({
|
|||
};
|
||||
}
|
||||
|
||||
logger.debug("Handlebars template compiled");
|
||||
|
||||
const { html: emailBodyHtml, errors: mjmlCompilationErrors } = compileMjml(mjmlTemplate);
|
||||
|
||||
if (mjmlCompilationErrors.length) {
|
||||
|
@ -120,6 +110,8 @@ export const sendSmtp = async ({
|
|||
};
|
||||
}
|
||||
|
||||
logger.debug("MJML template compiled");
|
||||
|
||||
const { plaintext: emailBodyPlaintext } = htmlToPlaintext(emailBodyHtml);
|
||||
|
||||
if (!emailBodyPlaintext || !emailBodyPlaintext?.length) {
|
||||
|
@ -129,6 +121,8 @@ export const sendSmtp = async ({
|
|||
};
|
||||
}
|
||||
|
||||
logger.debug("Email body converted to plaintext");
|
||||
|
||||
const sendEmailSettings: SendMailArgs = {
|
||||
mailData: {
|
||||
text: emailBodyPlaintext,
|
||||
|
@ -156,5 +150,5 @@ export const sendSmtp = async ({
|
|||
if (smtpErrors?.length) {
|
||||
return { errors: smtpErrors };
|
||||
}
|
||||
logger.debug(response?.response);
|
||||
logger.debug(response?.response, "Email sent");
|
||||
};
|
||||
|
|
|
@ -57,15 +57,15 @@ export const giftCardSentWebhook = new SaleorAsyncWebhook<GiftCardSentWebhookPay
|
|||
subscriptionQueryAst: GiftCardSentGraphqlSubscription,
|
||||
});
|
||||
|
||||
const logger = createLogger({
|
||||
name: giftCardSentWebhook.webhookPath,
|
||||
});
|
||||
|
||||
const handler: NextWebhookApiHandler<GiftCardSentWebhookPayloadFragment> = async (
|
||||
req,
|
||||
res,
|
||||
context
|
||||
) => {
|
||||
const logger = createLogger({
|
||||
webhook: giftCardSentWebhook.name,
|
||||
});
|
||||
|
||||
logger.debug("Webhook received");
|
||||
|
||||
const { payload, authData } = context;
|
||||
|
|
|
@ -43,15 +43,15 @@ export const invoiceSentWebhook = new SaleorAsyncWebhook<InvoiceSentWebhookPaylo
|
|||
subscriptionQueryAst: InvoiceSentGraphqlSubscription,
|
||||
});
|
||||
|
||||
const logger = createLogger({
|
||||
name: invoiceSentWebhook.name,
|
||||
});
|
||||
|
||||
const handler: NextWebhookApiHandler<InvoiceSentWebhookPayloadFragment> = async (
|
||||
req,
|
||||
res,
|
||||
context
|
||||
) => {
|
||||
const logger = createLogger({
|
||||
webhook: invoiceSentWebhook.name,
|
||||
});
|
||||
|
||||
logger.debug("Webhook received");
|
||||
|
||||
const { payload, authData } = context;
|
||||
|
|
|
@ -4,6 +4,15 @@ import { createLogger, createGraphQLClient } from "@saleor/apps-shared";
|
|||
import { sendEventMessages } from "../../../modules/event-handlers/send-event-messages";
|
||||
import { MessageEventTypes } from "../../../modules/event-handlers/message-event-types";
|
||||
|
||||
// Notify webhook event groups multiple event types under the one webhook. We need to map it to events recognized by the App
|
||||
const notifyEventMapping: Record<string, MessageEventTypes> = {
|
||||
account_confirmation: "ACCOUNT_CONFIRMATION",
|
||||
account_delete: "ACCOUNT_DELETE",
|
||||
account_password_reset: "ACCOUNT_PASSWORD_RESET",
|
||||
account_change_email_request: "ACCOUNT_CHANGE_EMAIL_REQUEST",
|
||||
account_change_email_confirm: "ACCOUNT_CHANGE_EMAIL_CONFIRM",
|
||||
};
|
||||
|
||||
/*
|
||||
* Notify event handles multiple event types which are recognized based on payload field `notify_event`.
|
||||
* Handler recognizes if event is one of the supported typed and sends appropriate message.
|
||||
|
@ -62,11 +71,11 @@ export const notifyWebhook = new SaleorAsyncWebhook<NotifySubscriptionPayload>({
|
|||
query: "{}", // We are using the default payload instead of subscription
|
||||
});
|
||||
|
||||
const handler: NextWebhookApiHandler<NotifySubscriptionPayload> = async (req, res, context) => {
|
||||
const logger = createLogger({
|
||||
webhook: notifyWebhook.name,
|
||||
name: notifyWebhook.webhookPath,
|
||||
});
|
||||
|
||||
const handler: NextWebhookApiHandler<NotifySubscriptionPayload> = async (req, res, context) => {
|
||||
logger.debug("Webhook received");
|
||||
|
||||
const { payload, authData } = context;
|
||||
|
@ -80,22 +89,12 @@ const handler: NextWebhookApiHandler<NotifySubscriptionPayload> = async (req, re
|
|||
.json({ error: "Email recipient has not been specified in the event payload." });
|
||||
}
|
||||
|
||||
// Notify webhook event groups multiple event types under the one webhook. We need to map it to events recognized by the App
|
||||
const notifyEventMapping: Record<string, MessageEventTypes> = {
|
||||
account_confirmation: "ACCOUNT_CONFIRMATION",
|
||||
account_delete: "ACCOUNT_DELETE",
|
||||
account_password_reset: "ACCOUNT_PASSWORD_RESET",
|
||||
account_change_email_request: "ACCOUNT_CHANGE_EMAIL_REQUEST",
|
||||
account_change_email_confirm: "ACCOUNT_CHANGE_EMAIL_CONFIRM",
|
||||
};
|
||||
|
||||
const event = notifyEventMapping[payload.notify_event];
|
||||
|
||||
if (!event) {
|
||||
logger.error(`The type of received notify event (${payload.notify_event}) is not supported.`);
|
||||
return res
|
||||
.status(200)
|
||||
.json({ error: "Email recipient has not been specified in the event payload." });
|
||||
// NOTIFY webhook sends multiple events to the same endpoint. The app supports only a subset of them.
|
||||
logger.debug(`The type of received notify event (${payload.notify_event}) is not supported.`);
|
||||
return res.status(200).json({ message: `${payload.notify_event} event is not supported.` });
|
||||
}
|
||||
|
||||
const client = createGraphQLClient({
|
||||
|
|
|
@ -34,15 +34,15 @@ export const orderCancelledWebhook = new SaleorAsyncWebhook<OrderCancelledWebhoo
|
|||
subscriptionQueryAst: OrderCancelledGraphqlSubscription,
|
||||
});
|
||||
|
||||
const logger = createLogger({
|
||||
name: orderCancelledWebhook.webhookPath,
|
||||
});
|
||||
|
||||
const handler: NextWebhookApiHandler<OrderCancelledWebhookPayloadFragment> = async (
|
||||
req,
|
||||
res,
|
||||
context
|
||||
) => {
|
||||
const logger = createLogger({
|
||||
webhook: orderCancelledWebhook.name,
|
||||
});
|
||||
|
||||
logger.debug("Webhook received");
|
||||
|
||||
const { payload, authData } = context;
|
||||
|
|
|
@ -35,15 +35,15 @@ export const orderConfirmedWebhook = new SaleorAsyncWebhook<OrderConfirmedWebhoo
|
|||
subscriptionQueryAst: OrderConfirmedGraphqlSubscription,
|
||||
});
|
||||
|
||||
const logger = createLogger({
|
||||
name: orderConfirmedWebhook.webhookPath,
|
||||
});
|
||||
|
||||
const handler: NextWebhookApiHandler<OrderConfirmedWebhookPayloadFragment> = async (
|
||||
req,
|
||||
res,
|
||||
context
|
||||
) => {
|
||||
const logger = createLogger({
|
||||
webhook: orderConfirmedWebhook.name,
|
||||
});
|
||||
|
||||
logger.debug("Webhook received");
|
||||
|
||||
const { payload, authData } = context;
|
||||
|
|
|
@ -32,15 +32,15 @@ export const orderCreatedWebhook = new SaleorAsyncWebhook<OrderCreatedWebhookPay
|
|||
subscriptionQueryAst: OrderCreatedGraphqlSubscription,
|
||||
});
|
||||
|
||||
const logger = createLogger({
|
||||
name: orderCreatedWebhook.webhookPath,
|
||||
});
|
||||
|
||||
const handler: NextWebhookApiHandler<OrderCreatedWebhookPayloadFragment> = async (
|
||||
req,
|
||||
res,
|
||||
context
|
||||
) => {
|
||||
const logger = createLogger({
|
||||
webhook: orderCreatedWebhook.name,
|
||||
});
|
||||
|
||||
logger.debug("Webhook received");
|
||||
|
||||
const { payload, authData } = context;
|
||||
|
|
|
@ -35,15 +35,15 @@ export const orderFulfilledWebhook = new SaleorAsyncWebhook<OrderFulfilledWebhoo
|
|||
subscriptionQueryAst: OrderFulfilledGraphqlSubscription,
|
||||
});
|
||||
|
||||
const logger = createLogger({
|
||||
name: orderFulfilledWebhook.webhookPath,
|
||||
});
|
||||
|
||||
const handler: NextWebhookApiHandler<OrderFulfilledWebhookPayloadFragment> = async (
|
||||
req,
|
||||
res,
|
||||
context
|
||||
) => {
|
||||
const logger = createLogger({
|
||||
webhook: orderFulfilledWebhook.name,
|
||||
});
|
||||
|
||||
logger.debug("Webhook received");
|
||||
|
||||
const { payload, authData } = context;
|
||||
|
|
|
@ -35,15 +35,15 @@ export const orderFullyPaidWebhook = new SaleorAsyncWebhook<OrderFullyPaidWebhoo
|
|||
subscriptionQueryAst: OrderFullyPaidGraphqlSubscription,
|
||||
});
|
||||
|
||||
const logger = createLogger({
|
||||
name: orderFullyPaidWebhook.webhookPath,
|
||||
});
|
||||
|
||||
const handler: NextWebhookApiHandler<OrderFullyPaidWebhookPayloadFragment> = async (
|
||||
req,
|
||||
res,
|
||||
context
|
||||
) => {
|
||||
const logger = createLogger({
|
||||
webhook: orderFullyPaidWebhook.name,
|
||||
});
|
||||
|
||||
logger.debug("Webhook received");
|
||||
|
||||
const { payload, authData } = context;
|
||||
|
|
Loading…
Reference in a new issue