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

This commit is contained in:
Govind
2024-01-24 14:47:22 +05:30
committed by GitHub
parent ddc0c9dfff
commit eb498c500e
3 changed files with 31 additions and 35 deletions

View File

@@ -13,3 +13,5 @@ node_modules
/models
/subscribers
*.tgz

View File

@@ -193,4 +193,23 @@ describe("SendGridService", () => {
})
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",
},
})
)
})
})

View File

@@ -138,7 +138,7 @@ class SendGridService extends NotificationService {
case "order.refund_created":
return this.orderRefundCreatedData(eventData, attachmentGenerator)
default:
return {}
return eventData
}
}
@@ -184,40 +184,15 @@ class SendGridService extends NotificationService {
}
getTemplateId(event) {
switch (event) {
case "order.return_requested":
return this.options_.order_return_requested_template
case "swap.shipment_created":
return this.options_.swap_shipment_created_template
case "claim.shipment_created":
return this.options_.claim_shipment_created_template
case "order.items_returned":
return this.options_.order_items_returned_template
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
}
const templates = Object.keys(this.options_ ?? {})
const normalizedEvent = event.toLowerCase().replaceAll(".", "_")
const key = templates.find((template) => {
return (
normalizedEvent === template ||
`${normalizedEvent}_template` === template
)
})
return this.options_[key] ?? key
}
async sendNotification(event, eventData, attachmentGenerator) {