-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MS-754] feat: Imporove logging. WIP logic for sending emails
- Loading branch information
1 parent
6ba5e8d
commit b392f34
Showing
17 changed files
with
10,202 additions
and
9,379 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,8 @@ export const configSchema = z | |
SERVER_PORT: z.number().default(3000), | ||
PROXY_PORT: z.number().default(3001), | ||
STATIC_URL: z.string().url(), | ||
FROM_EMAIL: z.string().email("[email protected]"), | ||
FROM_NAME: z.string().default("Mirumee"), | ||
}) | ||
.and(commonConfigSchema) | ||
.and(appConfigSchema) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,56 @@ | ||
// TODO: Mails sender serverless | ||
import { type Context, type SQSBatchResponse, type SQSEvent } from "aws-lambda"; | ||
import { | ||
type Context, | ||
type SQSBatchResponse, | ||
type SQSEvent, | ||
type SQSRecord, | ||
} from "aws-lambda"; | ||
import { type ComponentType } from "react"; | ||
|
||
import { CONFIG } from "@/config"; | ||
import { EmailParsePayloadError } from "@/lib/emails/errors"; | ||
import { getEmailProvider } from "@/providers/email"; | ||
import { logger } from "@/providers/logger"; | ||
import { getLogger } from "@/providers/logger"; | ||
import { OrderCreatedEmail } from "@/templates/OrderCreatedEmail"; | ||
|
||
import { OrderCreatedEmail } from "./templates/OrderCreatedEmail"; | ||
import { type WebhookEventTypeAsyncEnum } from "./graphql/schema"; | ||
import { type SerializedPayload } from "./lib/emails/events/helpers"; | ||
import { getJSONFormatHeader } from "./lib/saleor/apps/utils"; | ||
|
||
type OrderEvent = { order: { userEmail: string } }; | ||
|
||
const extractEmailFromOrder = (data: OrderEvent) => data.order.userEmail; | ||
|
||
const TEMPLATES_MAP: { | ||
[key in Lowercase<WebhookEventTypeAsyncEnum>]?: { | ||
extractFn: (data: any) => string; | ||
template: ComponentType<any>; | ||
}; | ||
} = { | ||
// order_created: { | ||
// template: OrderCreatedEmail, | ||
// extractFn: extractEmailFromOrder, | ||
// }, | ||
order_updated: { | ||
template: OrderCreatedEmail, | ||
extractFn: extractEmailFromOrder, | ||
}, | ||
}; | ||
|
||
export const logger = getLogger("emails-sender"); | ||
|
||
const parseRecord = (record: SQSRecord) => { | ||
try { | ||
// FIXME: Proxy events has invalid format? Test with real data & localstack. | ||
const data = JSON.parse((record as any).Body); | ||
return data as SerializedPayload; | ||
} catch (error) { | ||
logger.error("Failed to parse record payload.", { record, error }); | ||
|
||
throw new EmailParsePayloadError("Failed to parse record payload.", { | ||
cause: { source: error as Error }, | ||
}); | ||
} | ||
}; | ||
|
||
export const handler = async (event: SQSEvent, context: Context) => { | ||
const failures: string[] = []; | ||
|
@@ -16,23 +61,52 @@ export const handler = async (event: SQSEvent, context: Context) => { | |
/** | ||
* Process event | ||
*/ | ||
logger.info({ message: "Processing record", record }); | ||
logger.debug("Processing record", { record }); | ||
|
||
const sender = getEmailProvider({ | ||
fromEmail: `piotr.grundas+${CONFIG.NAME}@mirumee.com`, | ||
from: CONFIG.RELEASE, | ||
toEmail: "[email protected]", | ||
}); | ||
const { | ||
format, | ||
payload: { data, event }, | ||
} = parseRecord(record); | ||
|
||
const html = await sender.render({ | ||
props: {}, | ||
template: OrderCreatedEmail, | ||
}); | ||
if (format === getJSONFormatHeader({ version: 1, name: CONFIG.NAME })) { | ||
const match = TEMPLATES_MAP[event]; | ||
|
||
await sender.send({ | ||
html, | ||
subject: "Order created", | ||
}); | ||
if (!match) { | ||
return logger.warn("Received payload with unhandled template.", { | ||
format, | ||
data, | ||
event, | ||
}); | ||
} | ||
|
||
const { extractFn, template } = match; | ||
// const toEmail = extractFn(data); | ||
const toEmail = "[email protected]"; | ||
const fromEmail = CONFIG.FROM_EMAIL; | ||
const from = CONFIG.FROM_NAME; | ||
|
||
const sender = getEmailProvider({ | ||
fromEmail, | ||
from, | ||
toEmail, | ||
}); | ||
|
||
const html = await sender.render({ | ||
props: data, | ||
template, | ||
}); | ||
|
||
await sender.send({ | ||
html, | ||
subject: "Order created", | ||
}); | ||
} else { | ||
return logger.warn("Received payload with unsupported format.", { | ||
format, | ||
data, | ||
event, | ||
}); | ||
} | ||
} | ||
|
||
if (failures.length) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ subscription OrderCreatedSubscription { | |
... on OrderCreated { | ||
order { | ||
id | ||
userEmail | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ subscription OrderUpdatedSubscription { | |
... on OrderUpdated { | ||
order { | ||
id | ||
userEmail | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.