pevey / medusa-plugin-ses

Notifications plugin for Medusa ecommerce server that sends transactional emails via AWS SES (Simple Email Service).
https://pevey.com/medusa-plugin-ses
GNU General Public License v3.0
20 stars 7 forks source link

Gift Card Created Email - this.giftCardService_ is undefined on ses.js #4

Closed Douglas-Pontes closed 10 months ago

Douglas-Pontes commented 10 months ago

When I test to send the email via postman It works like a charm, but when I go to the admin dashboard and create a gift card it tries to send the email but I get this problem Cannot read properties of undefined (reading 'retrieve') at ses.js file on line 589 const giftCard = await this.giftCardService.retrieve(id, {, that means this.giftCardService is undefined. I console log the id and its right, I think the problem is the dependency injection of this service

image
PACKAGE JSON
"dependencies": {
    "@medusajs/admin": "^7.1.3",
    "@medusajs/cache-inmemory": "^1.8.8",
    "@medusajs/cache-redis": "^1.8.8",
    "@medusajs/event-bus-local": "^1.9.6",
    "@medusajs/event-bus-redis": "^1.8.9",
    "@medusajs/icons": "^1.0.1",
    "@medusajs/inventory": "^1.10.0",
    "@medusajs/medusa": "^1.17.0",
    "@medusajs/stock-location": "^1.10.0",
    "@medusajs/ui": "^2.0.0",
    "@tanstack/react-query": "4.22.0",
    "babel-preset-medusa-package": "^1.1.19",
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "dotenv": "16.0.3",
    "express": "^4.17.2",
    "medusa-file-gcs": "^0.1.0",
    "medusa-fulfillment-manual": "^1.1.38",
    "medusa-interfaces": "^1.3.7",
    "medusa-payment-manual": "^1.0.24",
    "medusa-payment-paypal": "^6.0.2",
    "medusa-payment-stripe": "^6.0.5",
    "medusa-plugin-meilisearch": "^2.0.10",
    "medusa-plugin-ses": "^2.2.3",
    "medusa-plugin-variant-images": "^1.0.3",
    "prism-react-renderer": "^2.0.4",
    "typeorm": "^0.3.16"
  },
  "devDependencies": {
    "@babel/cli": "^7.14.3",
    "@babel/core": "^7.14.3",
    "@babel/preset-typescript": "^7.21.4",
    "@medusajs/medusa-cli": "^1.3.21",
    "@types/express": "^4.17.13",
    "@types/jest": "^27.4.0",
    "@types/node": "^17.0.8",
    "babel-preset-medusa-package": "^1.1.19",
    "cross-env": "^7.0.3",
    "eslint": "^6.8.0",
    "jest": "^27.3.1",
    "mongoose": "^5.13.14",
    "rimraf": "^3.0.2",
    "ts-jest": "^27.0.7",
    "ts-loader": "^9.2.6",
    "typescript": "^4.5.2"
  },

MEDUSA CONFIG PLUGIN
{
    resolve: "medusa-plugin-ses",
    options: {
      access_key_id: process.env.SES_ACCESS_KEY_ID,
      secret_access_key: process.env.SES_SECRET_ACCESS_KEY,
      region: process.env.SES_REGION,
      from: process.env.SES_FROM,
      template_path: process.env.SES_TEMPLATE_PATH,
      partial_path: process.env.SES_PARTIAL_PATH,
      enable_endpoint: process.env.SES_ENABLE_ENDPOINT,
      enable_sim_mode: process.env.SES_ENABLE_SIM_MODE,
      gift_card_created_template: 'gift_card_created',
      // order_placed_template: 'order_placed',
      // order_shipped_template: 'order_shipped',
      // customer_password_reset_template: 'customer_password_reset',
      //order_canceled_template: 'order_canceled',
      //order_refund_created_template: 'order_refund_created',
      //order_return_requested_template: 'order_return_requested',
      //order_items_returned_template: 'order_items_returned',
      //swap_created_template: 'swap_created',
      //swap_shipment_created_template: 'swap_shipment_created',
      //swap_received_template: 'swap_received',
      //claim_shipment_created_template: 'claim_shipment_created',
      //user_password_reset_template: 'user_password_reset',
      //medusa_restock_template: 'medusa_restock',
    }
  },
pevey commented 10 months ago

The gift card service was missing from the ses class constructor. It has been fixed in development, but not officially released yet because the changes for the new version are extensive and still in testing.

Could I get you to try updating to version "3.0.1-canary.5" to see if that resolved the issue?

pevey commented 10 months ago

With the new version, you don't need to specify all the templates in your config. Just specify your template and partials folders. The templates must be named according to the default names to be recognized. But the major benefit is that now a LOT more events are enabled for notifications by default, if a valid template is present.

Douglas-Pontes commented 10 months ago

Now we have the gift card service, but Im creating an gift card without an order related, so order is coming null, but in your code at ses.js:71 the email is required, but in this case you should be getting the email at metadata, like I believe we will only have an order related if someone uses the gift card right?

image image image image
Douglas-Pontes commented 10 months ago

With the new version, you don't need to specify all the templates in your config. Just specify your template and partials folders. The templates must be named according to the default names to be recognized. But the major benefit is that now a LOT more events are enabled for notifications by default, if a valid template is present.

Thats great nice job

pevey commented 10 months ago

I think I know how to fix it. I'll let you know when I post a new canary version.

Douglas-Pontes commented 10 months ago

Actually I configured your plugin and the first event I tried to test was gift card, from what I understood medusa only sends an email when you create a custom gift card which you send to an specific email, which later when customer uses the gift card it will be checked if the customer has the same email, in this case at notification-data at line 93 you should get the email from metadata which will be the email you want to send the gift card to and also order can will be null at the time the email is sent

But I'm very new to medusa aswell, I can be wrong

pevey commented 10 months ago

I just published 3.0.1-canary.6. Could you try it and see if it resolves your error?

Douglas-Pontes commented 10 months ago

not yet, you fixed the problem of email, but now its not finding the template I debbuged with console.log

for some reason in the templatePath we have node_modules before /data/templates in my project thats wrong

image image

SES_TEMPLATE_PATH="data/templates"

Douglas-Pontes commented 10 months ago

If I change the template path to be the whole path it works I got the email! SES_TEMPLATE_PATH="/Users/douglas/Desktop/PmdTech Solutions/ecommerce/my-medusa-store/data/templates"

but you should fix that because in production thats bad

pevey commented 10 months ago

Hmm, this is a strange one. Nothing about the template path was changed.

When I develop locally and use yarn link to install or install from a local folder, I have to use the full path (because of sym-linking weirdness. But when I install from npm, I can use the relative path. I'm not sure what might be causing this for you.

pevey commented 10 months ago

I will definitely test it more before 3.0 goes live.

Douglas-Pontes commented 10 months ago

Yeah me neither, I'll test all the events, and I'll keep opening issues if I find something else okay? for now I'll use .6 version and the whole path

pevey commented 10 months ago

Ah, I think I might know what has happened. Thank you so much for surfacing this.

With 3.0, it was converted to typescript. As part of that, the build process was changed to put everything in the plugin's 'dist' folder instead of the top level folder like it was before. This extra level is causing the issue. I will fix it.

Douglas-Pontes commented 10 months ago

Ah, I think I might know what has happened. Thank you so much for surfacing this.

With 3.0, it was converted to typescript. As part of that, the build process was changed to put everything in the 'dist' folder instead of the top level folder like it was before. This extra level is causing the issue. I will fix it.

Oh yeah, thats it, well done!!

So I'll get back with other issues if I find more okay? thank you for the quick answers, I hope I can help you more

pevey commented 10 months ago

I just pushed 3.0.1-canary.7

Douglas-Pontes commented 10 months ago

Nice @pevey It working with data/templates

Thanky youu