luluhoc / medusa-plugin-sendgrid-typescript

Modified version of the original SendGrid plugin with Typescript and with extra features.
7 stars 3 forks source link

Emails not sent #2

Closed NicolasGorga closed 1 month ago

NicolasGorga commented 3 months ago

Hello,

First of all, thanks for the adaptation to typescript, i haven't been able to send emails with the original plugin on Medusa 1.20.9, since it seems there was a compatibility issue with new subscribers architecture.

Following the README.md of this plugin i am not able to receive an email when an order is placed.

This is my medusa-config.js plugin config:

image

I have verified its not an issue on Sendgrid, since i correctly sent a test message with my Single Verified Sender, for the dynamic template i configured for the order_placed_template.

Seeing the backend logs, it seems that the subscriber is registered correctly, since i see:

Info: Processing order.placed which has 3 subscribers

luluhoc commented 3 months ago

Can you try sending the message manually through API endpoint using plugin service?

NicolasGorga commented 3 months ago

Tried:

Still same outcome.

Testing API endpoint now

luluhoc commented 3 months ago

Tried:

  • Adding '#'
  • Removing subject altogether

Still same outcome.

Testing API endpoint now

The # wouldn't change anything I simply forgot that the template in this repo isn't using # in template but only {}

luluhoc commented 3 months ago

also on which version are you on?

NicolasGorga commented 3 months ago

Reading the code didn't think it would change anything, just as per your first suggestion.

Well, the API endpoint works correctly.

image image

I am using Medusa 1.20.9 on top of MercurJS, which is just basically a Medusa project, with some customizations on models and subscribers to adapt it to a multi vendor marketplace ecosystem.

luluhoc commented 3 months ago

@NicolasGorga Can you try troubleshooting by creating custom API endpoint like this and calling it?

import type {
  MedusaRequest,
  MedusaResponse
} from "@medusajs/medusa";
import SendGridService from "../../../services/sendgrid";

export async function GET(req: MedusaRequest, res: MedusaResponse) {
  try {
    const sendgridService: SendGridService = req.scope.resolve("sendgridService")

  const data = await sendgridService.sendNotification("order.placed", {
    id: "123",
  })
  res.status(200).json(data)
  } catch (error) {
    console.log(error)
  }

}
NicolasGorga commented 3 months ago

`import { MedusaRequest, MedusaResponse, NotificationService } from "@medusajs/medusa"; import { SendGridService } from 'medusa-plugin-sendgrid-typescript/dist/services/sendgrid'

export async function GET(req: MedusaRequest, res: MedusaResponse) { try { const sendgridService = req.scope.resolve("sendgridService")

    const data = await sendgridService.sendNotification("order.placed", {
        id: "order_01J4SAN8R01FQNVCH1582V0SAC",
    })
    res.status(200).json(data)
} catch (error) {
  console.log(error)
}

}`

That works:

"to": "nicogorga11@gmail.com", "status": "sent", "data": { "template_id": "d-86c8d6c2a1d44dc194e698a39d8119a1", "from": "nicolas.gorga@esuniversal.uy", "to": "nicogorga11@gmail.com", "dynamic_template_data": { "object": "order", "id": "order_01J4SAN8R01FQNVCH1582V0SAC", "created_at": "2024-08-08T15:45:29.308Z", "updated_at": "2024-08-08T15:45:29.308Z", "status": "pending", "fulfillment_status": "not_fulfilled", "payment_status": "awaiting", "display_id": 13, "cart_id": "cart_01J4SAMKT8S9XMNFW1BQ2HW9RV", "customer_id": "cus_01J4HPPHHGC0EBFCXGY55SB5DC", "email": "nicogorga11@gmail.com", "billing_address_id": "addr_01J4SAN440GH9PF86EJ034E8GZ", "shipping_address_id": "addr_01J4SAMKT8EBE1R3Q31BRCJ2EN", "region_id": "reg_01J478MPM6HBPZ6D5N8YVA3SQF", "currency_code": "uyu", "tax_rate": null, "draft_order_id": null, "canceled_at": null, "metadata": {}, "no_notification": null, "idempotency_key": null, "external_id": null, "store_id": null, "parent_id": null, "sales_channel_id": "sc_01J477ZVFN7ZAYBAC8XS1VSK1F", "billing_address": { "id": "addr_01J4SAN440GH9PF86EJ034E8GZ", "created_at": "2024-08-08T15:45:24.571Z", "updated_at": "2024-08-08T15:45:24.571Z", "deleted_at": null, "customer_id": null, "company": "", "first_name": "Nicolas", "last_name": "Gorga", "address_1": "Gregorio Suarez 2712", "address_2": "", "city": "Montevideo", "country_code": "uy", "province": "", "postal_code": "11300", "phone": "", "metadata": null }, "claims": [], "customer": { "id": "cus_01J4HPPHHGC0EBFCXGY55SB5DC", "created_at": "2024-08-05T16:41:58.557Z", "updated_at": "2024-08-05T16:41:58.557Z", "deleted_at": null, "email": "nicogorga11@gmail.com", "first_name": null, "last_name": null, "billing_address_id": null, "phone": null, "has_account": false, "metadata": null }, "discounts": [], "fulfillments": [], "gift_card_transactions": [], "gift_cards": [], "items": [ { "id": "item_01J4SAMKXCCENDD1BN0R71SDHA", "created_at": "2024-08-08T15:45:07.989Z", "updated_at": "2024-08-08T15:45:29.308Z", "cart_id": "cart_01J4SAMKT8S9XMNFW1BQ2HW9RV", "order_id": "order_01J4SAN8R01FQNVCH1582V0SAC", "swap_id": null, "claim_order_id": null, "original_item_id": null, "order_edit_id": null, "title": "Botas Dr Marten", "description": "Negras", "thumbnail": "https://centro-mktplace-files.s3.sa-east-1.amazonaws.com/039e1c2a09f3bdf731cb13548042141f-1722710493341.jpg", "is_return": false, "is_giftcard": false, "should_merge": true, "allow_discounts": true, "has_shipping": false, "unit_price": 450000, "variant_id": "variant_01J4CSDG10BJNSPHG2HECD1JPD", "quantity": 1, "fulfilled_quantity": null, "returned_quantity": null, "shipped_quantity": null, "metadata": {}, "adjustments": [], "tax_lines": [ { "id": "litl_01J4SAN8NHTXRHZ154EMRRC5RX", "created_at": "2024-08-08T15:45:29.249Z", "updated_at": "2024-08-08T15:45:29.308Z", "rate": 0, "name": "default", "code": "default", "metadata": null, "item_id": "item_01J4SAMKXCCENDD1BN0R71SDHA" } ], "variant": { "id": "variant_01J4CSDG10BJNSPHG2HECD1JPD", "created_at": "2024-08-03T18:53:15.670Z", "updated_at": "2024-08-08T15:45:41.928Z", "deleted_at": null, "title": "Negras", "product_id": "prod_01J4CRR2M5JEWNPVRRA9J6NW0J", "sku": null, "barcode": null, "ean": null, "upc": null, "variant_rank": 0, "inventory_quantity": 5, "allow_backorder": false, "manage_inventory": true, "hs_code": null, "origin_country": null, "mid_code": null, "material": null, "weight": null, "length": null, "height": null, "width": null, "metadata": {}, "product": { "id": "prod_01J4CRR2M5JEWNPVRRA9J6NW0J", "created_at": "2024-08-03T18:41:33.815Z", "updated_at": "2024-08-03T18:41:33.815Z", "deleted_at": null, "title": "Botas Dr Marten", "subtitle": "Botas Dr Marten Brillosas", "description": "Par de Botas Dr Marten brillosas, para toda la vida!", "handle": "botas-dr-marten", "is_giftcard": false, "status": "published", "thumbnail": "https://centro-mktplace-files.s3.sa-east-1.amazonaws.com/039e1c2a09f3bdf731cb13548042141f-1722710493341.jpg", "weight": null, "length": null, "height": null, "width": null, "hs_code": null, "origin_country": null, "mid_code": null, "material": null, "collection_id": null, "type_id": null, "discountable": true, "external_id": null, "metadata": null, "store_id": "store_01J4CHSRJQBZWBY0QMQSZC28AP", "profiles": [ { "id": "sp_01J477ZVF7AYTJ4JPV12ZYMJ68", "created_at": "2024-08-01T15:12:30.661Z", "updated_at": "2024-08-01T15:12:30.661Z", "deleted_at": null, "name": "Default Shipping Profile", "type": "default", "metadata": null } ], "profile": { "id": "sp_01J477ZVF7AYTJ4JPV12ZYMJ68", "created_at": "2024-08-01T15:12:30.661Z", "updated_at": "2024-08-01T15:12:30.661Z", "deleted_at": null, "name": "Default Shipping Profile", "type": "default", "metadata": null }, "profile_id": "sp_01J477ZVF7AYTJ4JPV12ZYMJ68" } }, "subtotal": 450000, "discount_total": 0, "total": 450000, "original_total": 450000, "original_tax_total": 0, "tax_total": 0, "raw_discount_total": 0, "totals": { "unit_price": 450000, "quantity": 1, "subtotal": 450000, "discount_total": 0, "total": 450000, "original_total": 450000, "original_tax_total": 0, "tax_total": 0, "tax_lines": [ { "id": "litl_01J4SAN8NHTXRHZ154EMRRC5RX", "created_at": "2024-08-08T15:45:29.249Z", "updated_at": "2024-08-08T15:45:29.308Z", "rate": 0, "name": "default", "code": "default", "metadata": null, "item_id": "item_01J4SAMKXCCENDD1BN0R71SDHA" } ], "raw_discount_total": 0 }, "discounted_price": "4500.00 UYU", "price": "4500.00 UYU" } ], "payments": [ { "id": "pay_01J4SAN8PHGE57N8E1S2ATEXNP", "created_at": "2024-08-08T15:45:29.270Z", "updated_at": "2024-08-08T15:45:29.308Z", "swap_id": null, "cart_id": "cart_01J4SAMKT8S9XMNFW1BQ2HW9RV", "order_id": "order_01J4SAN8R01FQNVCH1582V0SAC", "amount": 490000, "currency_code": "uyu", "amount_refunded": 0, "provider_id": "manual", "data": { "status": "authorized" }, "captured_at": null, "canceled_at": null, "metadata": null, "idempotency_key": null, "parent_id": null } ], "refunds": [], "region": { "id": "reg_01J478MPM6HBPZ6D5N8YVA3SQF", "created_at": "2024-08-01T15:23:53.751Z", "updated_at": "2024-08-02T20:51:13.528Z", "deleted_at": null, "name": "UY", "currency_code": "uyu", "tax_rate": 0, "tax_code": null, "gift_cards_taxable": true, "automatic_taxes": true, "tax_provider_id": null, "metadata": {} }, "returns": [], "shipping_address": { "id": "addr_01J4SAMKT8EBE1R3Q31BRCJ2EN", "created_at": "2024-08-08T15:45:07.903Z", "updated_at": "2024-08-08T15:45:24.571Z", "deleted_at": null, "customer_id": null, "company": "", "first_name": "Nicolas", "last_name": "Gorga", "address_1": "Gregorio Suarez 2712", "address_2": "", "city": "Montevideo", "country_code": "uy", "province": "", "postal_code": "11300", "phone": "", "metadata": null }, "shipping_methods": [ { "id": "sm_01J4SAN5DWB2DPW004TF09B8CX", "shipping_option_id": "so_01J4CSE11Q4B1H1VFHGPVSXPQM", "order_id": "order_01J4SAN8R01FQNVCH1582V0SAC", "claim_order_id": null, "cart_id": "cart_01J4SAMKT8S9XMNFW1BQ2HW9RV", "swap_id": null, "return_id": null, "price": 40000, "data": { "store_id": "store_01J4CHSRJQBZWBY0QMQSZC28AP" }, "shipping_option": { "id": "so_01J4CSE11Q4B1H1VFHGPVSXPQM", "created_at": "2024-08-03T18:53:33.108Z", "updated_at": "2024-08-03T18:53:33.108Z", "deleted_at": null, "name": "Pick Up", "region_id": "reg_01J478MPM6HBPZ6D5N8YVA3SQF", "profile_id": "sp_01J477ZVF7AYTJ4JPV12ZYMJ68", "provider_id": "manual", "price_type": "flat_rate", "amount": 40000, "is_return": false, "admin_only": false, "data": { "id": "manual-fulfillment" }, "metadata": null, "store_id": "store_01J4CHSRJQBZWBY0QMQSZC28AP" }, "tax_lines": [ { "id": "smtl_01J4SAN8NH186B8RT3S5KYPYM8", "created_at": "2024-08-08T15:45:29.249Z", "updated_at": "2024-08-08T15:45:29.308Z", "rate": 0, "name": "default", "code": "default", "metadata": null, "shipping_method_id": "sm_01J4SAN5DWB2DPW004TF09B8CX" } ] } ], "swaps": [], "shipping_total": "400.00 UYU", "discount_total": "0.00 UYU", "tax_total": "0.00 UYU", "refunded_total": 0, "gift_card_total": "0.00 UYU", "gift_card_tax_total": 0, "subtotal": "4500.00 UYU", "total": "4900.00 UYU", "locale": null, "has_discounts": 0, "has_gift_cards": 0, "date": "Thu Aug 08 2024", "subtotal_ex_tax": "4500.00 UYU" }, "has_attachments": false } }

NicolasGorga commented 3 months ago

I created a Medusa project from scratch, using 'create-medusa-app' and same problem, so also confirmed it's not an issue of MercurJS.

liho00 commented 2 months ago

for my case, order.placed was triggered, but not for order_shipment_created_template and order_shipped_template

info: Processing order.fulfillment_created which has 0 subscribers

 {
    resolve: `medusa-plugin-sendgrid-typescript`,
    /** @type {import('medusa-plugin-sendgrid-typescript').PluginOptions} */
    options: {
      api_key: process.env.SENDGRID_API_KEY,
      from: process.env.SENDGRID_FROM,
      templates: {
        order_placed_template: {
          id: process.env.SENDGRID_ORDER_PLACED_TEMPLATE,
          // You can add dynamic data to the template by using {variable_name}
          subject: "Thank you for your order #{display_id}!",
        },
        order_shipment_created_template: {
          id: "d-1f57bbf44c0146d8a8dd0bfa7e7943ab",
          subject: "Your order #{display_id} has been shipped!",
        },
        order_shipped_template: {
          id: "d-1f57bbf44c0146d8a8dd0bfa7e7943ab",
          subject: "Your order #{display_id} has been shipped!",
        },
        claim_shipment_created_template: {
          id: process.env.SENDGRID_CLAIM_SHIPMENT_CREATED_TEMPLATE,
          subject: "Your claim shipment has been created!",
        },
      },
    },
  },
NicolasGorga commented 2 months ago

After replacing local-event-bus with redis-event-bus, the order placed event is triggered correctly, haven't tested the others yet. From talking with someone at Medusa, it seems local-event-bus doesn't play nicely with NotificationService.

Are you using local-event-bus? What version of @medusajs/medusa?

luluhoc commented 2 months ago

for my case, order.placed was triggered, but not for order_shipment_created_template and order_shipped_template

info: Processing order.fulfillment_created which has 0 subscribers

 {
    resolve: `medusa-plugin-sendgrid-typescript`,
    /** @type {import('medusa-plugin-sendgrid-typescript').PluginOptions} */
    options: {
      api_key: process.env.SENDGRID_API_KEY,
      from: process.env.SENDGRID_FROM,
      templates: {
        order_placed_template: {
          id: process.env.SENDGRID_ORDER_PLACED_TEMPLATE,
          // You can add dynamic data to the template by using {variable_name}
          subject: "Thank you for your order #{display_id}!",
        },
        order_shipment_created_template: {
          id: "d-1f57bbf44c0146d8a8dd0bfa7e7943ab",
          subject: "Your order #{display_id} has been shipped!",
        },
        order_shipped_template: {
          id: "d-1f57bbf44c0146d8a8dd0bfa7e7943ab",
          subject: "Your order #{display_id} has been shipped!",
        },
        claim_shipment_created_template: {
          id: process.env.SENDGRID_CLAIM_SHIPMENT_CREATED_TEMPLATE,
          subject: "Your claim shipment has been created!",
        },
      },
    },
  },

@liho00 Could you let me know if you managed to resolve It? The order_shipped_template doesn't exist you should use order_shipment_created_template.

liho00 commented 2 months ago

for my case, order.placed was triggered, but not for order_shipment_created_template and order_shipped_template info: Processing order.fulfillment_created which has 0 subscribers

 {
    resolve: `medusa-plugin-sendgrid-typescript`,
    /** @type {import('medusa-plugin-sendgrid-typescript').PluginOptions} */
    options: {
      api_key: process.env.SENDGRID_API_KEY,
      from: process.env.SENDGRID_FROM,
      templates: {
        order_placed_template: {
          id: process.env.SENDGRID_ORDER_PLACED_TEMPLATE,
          // You can add dynamic data to the template by using {variable_name}
          subject: "Thank you for your order #{display_id}!",
        },
        order_shipment_created_template: {
          id: "d-1f57bbf44c0146d8a8dd0bfa7e7943ab",
          subject: "Your order #{display_id} has been shipped!",
        },
        order_shipped_template: {
          id: "d-1f57bbf44c0146d8a8dd0bfa7e7943ab",
          subject: "Your order #{display_id} has been shipped!",
        },
        claim_shipment_created_template: {
          id: process.env.SENDGRID_CLAIM_SHIPMENT_CREATED_TEMPLATE,
          subject: "Your claim shipment has been created!",
        },
      },
    },
  },

@liho00 Could you let me know if you managed to resolve It? The order_shipped_template doesn't exist you should use order_shipment_created_template.

bro, i used both, i guess you overlooked it....

NicolasGorga commented 2 months ago

I guess in order to close the issue an update to the Readme would be needed, to indicate that a Redis setup is needed and that it doesn't work with local-event-bus module

luluhoc commented 1 month ago

@NicolasGorga You can make a PR.