fix(medusa-plugin-sendgrid): Support custom templates (#5833)

This commit is contained in:
Govind authored and GitHub committed 2024-01-24 10:17:22 +01:00
1 parent ddc0c9dfff
commit eb498c500e
3 files changed
+31 -35

No files matched your search

@@ -13,3 +13,5 @@ node_modules
/models /models
/subscribers /subscribers
*.tgz
@@ -193,4 +193,23 @@ describe("SendGridService", () => {
}) })
expect(mockedSendGrid.send).toBeCalled() expect(mockedSendGrid.send).toBeCalled()
}) })
it("should send message to non predefined template", async () => {
sendGridService = new SendGridService({}, { "send-otp": "test-template" })
await sendGridService.sendNotification("send-otp", {
otp: "test",
validity: "12-01-2020",
})
expect(mockedSendGrid.send).toBeCalled()
expect(mockedSendGrid.send).toHaveBeenCalledWith(
expect.objectContaining({
template_id: "test-template",
dynamic_template_data: {
otp: "test",
validity: "12-01-2020",
},
})
)
})
}) })
@@ -138,7 +138,7 @@ class SendGridService extends NotificationService {
case "order.refund_created": case "order.refund_created":
return this.orderRefundCreatedData(eventData, attachmentGenerator) return this.orderRefundCreatedData(eventData, attachmentGenerator)
default: default:
return {} return eventData
} }
} }
@@ -184,40 +184,15 @@ class SendGridService extends NotificationService {
} }
getTemplateId(event) { getTemplateId(event) {
switch (event) { const templates = Object.keys(this.options_ ?? {})
case "order.return_requested": const normalizedEvent = event.toLowerCase().replaceAll(".", "_")
return this.options_.order_return_requested_template const key = templates.find((template) => {
case "swap.shipment_created": return (
return this.options_.swap_shipment_created_template normalizedEvent === template ||
case "claim.shipment_created": `${normalizedEvent}_template` === template
return this.options_.claim_shipment_created_template )
case "order.items_returned": })
return this.options_.order_items_returned_template return this.options_[key] ?? key
case "swap.received":
return this.options_.swap_received_template
case "swap.created":
return this.options_.swap_created_template
case "gift_card.created":
return this.options_.gift_card_created_template
case "order.gift_card_created":
return this.options_.gift_card_created_template
case "order.placed":
return this.options_.order_placed_template
case "order.shipment_created":
return this.options_.order_shipped_template
case "order.canceled":
return this.options_.order_canceled_template
case "user.password_reset":
return this.options_.user_password_reset_template
case "customer.password_reset":
return this.options_.customer_password_reset_template
case "restock-notification.restocked":
return this.options_.medusa_restock_template
case "order.refund_created":
return this.options_.order_refund_created_template
default:
return null
}
} }
async sendNotification(event, eventData, attachmentGenerator) { async sendNotification(event, eventData, attachmentGenerator) {