async-labs / builderbook

Open source web application to learn JS stack: React, Material-UI, Next.js, Node.js, Express.js, Mongoose, MongoDB database.
https://builderbook.org
MIT License
3.77k stars 894 forks source link

Problem with main repo app: book purchase #87

Closed johnking closed 6 years ago

johnking commented 6 years ago

After deploying the app both in local env and production env (zeit), I found: a regular user could not open the book and read the chapter even though:

  1. it shows the book on the list of 'Your books' after going through the purchasing procedure.

screen shot 2018-08-19 at 18 12 12

  1. After trying open it, it show the TOC, and big blue 'Buy button' screen shot 2018-08-19 at 18 13 53

  1. but it worked when I follow the instructions and code of chapter-8 book/8-end

  2. I am using macPro, chrome.

thanks

klyburke commented 6 years ago

@johnking thanks for opening up the issue.

Can you clarify which app you are running and having a problem? I'm guessing it is either:

You can share your actual code in a public repo for us to review.


If the book is successfully purchased by a user, you should see the following data in your database:

Do you see these data?

johnking commented 6 years ago

Hi @klyburke

The issue is related to the main repository, not about the tutorial code (book chapter-8 code).

the only changes in my clone are

  1. the ROOT_URL
  2. all kinds of env variables

-John

klyburke commented 6 years ago

Thanks @johnking

To help figure out the problem, can you confirm if you see the data I mentioned in the previous comment?

  • a Purchase document with proper userId and bookId parameters
  • a parameter purchasedBookIds in the User document with proper value (id of purchased book)
johnking commented 6 years ago

Hi @klyburke

I tried a clean deployment locally with my own secrets, and completed the purchase process with the demo-book (forked from your demo-book repository), I checked the database:

let me know if you need more information to resolve it.

thanks

-John

tima101 commented 6 years ago

@johnking Thanks for your response about DB.

@klyburke Thanks for narrowing down on potential problem.

johnking commented 6 years ago

Hi @tima101 @klyburke ,

I repeated the steps based on your instructions, but I did not find any log errors related to AWS SES or Mailchimp, and I did not receive the purchase notification email either. I could receive the notification email based on chapter 8-end code.

screen shot 2018-08-20 at 12 03 41

both the console logs and in-application message give me a not found error message.

The DB has the same status, there is no Purchase document created inside the purchase collection.

tima101 commented 6 years ago

@johnking Alright, this helps to narrow down problem even further. You did not get purchase email via AWS SES. Was your email address added to Mailchimp list?

Please comment out these 2 blocks of code and test purchase again (remember to clear relevant data in DB):

    const template = await getEmailTemplate('purchased', {
      userName: user.displayName,
      bookTitle: book.name,
      bookUrl: `https://builderbook.org/books/${book.slug}/introduction`,
    });

    try {
      await sendEmail({
        from: `Kelly from builderbook.org <${process.env.EMAIL_SUPPORT_FROM_ADDRESS}>`,
        to: [user.email],
        subject: template.subject,
        body: template.message,
      });
    } catch (error) {
      logger.error('Email sending error:', error);
    }

    try {
      await subscribe({
        email: user.email,
        listName: 'purchased',
      });
    } catch (error) {
      logger.error('Mailchimp error:', error);

The goal is to create Purchase doc successfully. After that we will investigate why purchase email was not sent by AWS SES.

Thanks.

johnking commented 6 years ago

Hi @tima101 ,

The purchase flow is successful this time by commenting out the lines as specified above!

I double checked that my second gmail address IS in the mailchimp MAILCHIMP_PURCHASED_LIST_ID.

let me know what I should test next.

thanks

tima101 commented 6 years ago

@johnking Great. This means that Mailchimp works as expected but not email sending block of code.

Please comment out only this block of code to confirm our conclusion:

    const template = await getEmailTemplate('purchased', {
      userName: user.displayName,
      bookTitle: book.name,
      bookUrl: `https://builderbook.org/books/${book.slug}/introduction`,
    });

    try {
      await sendEmail({
        from: `Kelly from builderbook.org <${process.env.EMAIL_SUPPORT_FROM_ADDRESS}>`,
        to: [user.email],
        subject: template.subject,
        body: template.message,
      });
    } catch (error) {
      logger.error('Email sending error:', error);
    }

If purchase works then problem is within above code block.

I may have found the problem, try fixing, replace purchased with purchase inside: const template = await getEmailTemplate('purchased', {

I think your DB has 2 docs in EmailTemplates collection, one has name welcome and second has name purchase (not purchased). Thus error.

This means that third-party APIs work fine and you created/added proper env variables. Problem is in wrong name for email template. template variable is likely undefined in:

    const template = await getEmailTemplate('purchased', {
      userName: user.displayName,
      bookTitle: book.name,
      bookUrl: `https://builderbook.org/books/${book.slug}/introduction`,
    });

You can confirm this by uncommenting all code and adding console.log(template); under above code block. Then monitor output in your terminal.

If you confirm all above, please create pull request for fix. Thanks for running code and debugging, I could not run app since my env variable are on a different machine.

johnking commented 6 years ago

Yes, it works now. 👍

I will create a pull request soon.

thanks again.

klyburke commented 6 years ago

@johnking Have you seen our SaaS boilerplate: https://github.com/async-labs/saas ?

If you happen to run/test/use our SaaS app, you are welcome to create pull requests in that repo as well.

Thanks.

johnking commented 6 years ago

hi @klyburke ,

thanks for your invitation, I will try your SaaS boilerplate later.