Skip to content

Commit

Permalink
fix(medusa-plugin-sendgrid): Inject GiftCardService to eliminate `u…
Browse files Browse the repository at this point in the history
…ndefined` error (#2941)
  • Loading branch information
RegisHubelia committed Jan 30, 2023
1 parent e581d3b commit 2551fe0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-eggs-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"medusa-plugin-sendgrid": minor
---

fix(medusa-plugin-sendgrid): Inject GiftCardService to eliminate undefined error
5 changes: 3 additions & 2 deletions docs/content/add-plugins/sendgrid.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3608,8 +3608,9 @@ You don’t have to create a template for every type in the reference. You can s
```json noReport
Object {
"code": Any<String>,
"value": 4,
"value": 100,
"balance": 100,
"display_value": "100.00",
"region": Object {
"automatic_taxes": true,
"created_at": Any<Date>,
Expand Down Expand Up @@ -3937,7 +3938,7 @@ const plugins = [
order_placed_template: process.env.SENDGRID_ORDER_PLACED_ID,
localization: {
"de-DE": { // locale key
order_placed_template:
order_placed_template:
process.env.SENDGRID_ORDER_PLACED_ID_LOCALIZED,
},
},
Expand Down
16 changes: 7 additions & 9 deletions packages/medusa-plugin-sendgrid/src/services/sendgrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class SendGridService extends NotificationService {
fulfillmentProviderService,
totalsService,
productVariantService,
giftCardService,
},
options
) {
Expand All @@ -49,6 +50,7 @@ class SendGridService extends NotificationService {
this.fulfillmentService_ = fulfillmentService
this.totalsService_ = totalsService
this.productVariantService_ = productVariantService
this.giftCardService_ = giftCardService

SendGrid.setApiKey(options.api_key)
}
Expand Down Expand Up @@ -579,20 +581,16 @@ class SendGridService extends NotificationService {
const giftCard = await this.giftCardService_.retrieve(id, {
relations: ["region", "order"],
})

if (!giftCard.order) {
return
}

const taxRate = giftCard.region.tax_rate / 100

const locale = await this.extractLocale(order)
const locale = giftCard.order ? await this.extractLocale(order) : null;
const email = giftCard.order ? giftCard.order.email : giftCard.metadata.email;

return {
...giftCard,
locale,
email: giftCard.order.email,
display_value: giftCard.value * (1 + taxRate),
email,
display_value: `${this.humanPrice_((giftCard.value * 1+ taxRate), giftCard.region.currency_code)} ${giftCard.region.currency_code}`,
message: giftCard.metadata?.message || giftCard.metadata?.personal_message
}
}

Expand Down

0 comments on commit 2551fe0

Please sign in to comment.