How I can pass settimeout function inside of if condition in node js api?

65 views Asked by At

I am working on mail sending api using node js and sendinblue, here i wrote my api inside of if condition is satisfied after 1 day only mail should send to user how i can pass here settimeout function here. and can u give some suggestion after 1 day one mail should send and after second day another mail should send automatically if user not clicked confirmation link. if any idea that one also can give how can i implement here.

const loginemail = async (req: Request) => {
    const connection = await createConnection()
    try {
        const [dashboardJobCount]: any = await connection.query(`select account_id,datediff(now(),created_date) as days,username,verified from account where verified=false;`)
      
        const client = Sib.ApiClient.instance
        
        const apiKey = client.authentications['api-key']
        apiKey.apiKey = process.env.SIB_API_KEY

        const tranEmailApi = new Sib.TransactionalEmailsApi()
      
        const sender = {
            email: '[email protected]',
            name: 'Anjan',
        }
        const receivers = [
            {
                email: '[email protected]',
            },
        ]
        const firstMail = {
            sender,
            to: receivers,
            subject: 'Testing mail 1st day',
            textContent: `
        Cules Coding will teach you how to become {{params.role}} a developer.
        `,
        templateId: 1,
       
            params: {
                role: 'Frontend',
            },
        }
        const secondMail = {
            sender,
            to: receivers,
            subject: 'Testing mail 2nd day',
            textContent: `
        Cules Coding will teach you how to become {{params.role}} a developer.
        `,
        templateId: 2,
       
            params: {
                role: 'Frontend',
            },
        }
        const thirdMail = {
            sender,
            to: receivers,
            subject: 'Testing mail 4th day',
            textContent: `
        Cules Coding will teach you how to become {{params.role}} a developer.
        `,
        templateId: 2,
       
            params: {
                role: 'Frontend',
            },
        }
        for (let i = 0; i < dashboardJobCount.length; i++) {
            if([1,93].includes(dashboardJobCount[i].days)){
          
             const msg = await tranEmailApi.sendTransacEmail(firstMail)
             return msg
            }
            else if([4,2,94].includes(dashboardJobCount[i].days)){
                const msg = await tranEmailApi.sendTransacEmail(secondMail)
                console.log("kkkk", dashboardJobCount[i].days)
                return msg
            }
            else if([6].includes(dashboardJobCount[i].days)){
                const msg = await tranEmailApi.sendTransacEmail(secondMail)
                console.log("kkkk", dashboardJobCount[i].days)
                return msg
            }
            else{
                console.log("your account has deleted")
            }
            
         }
        
       
    } catch (error) {
        await connection.end()
        throw error
    }
}
1

There are 1 answers

3
lpizzinidev On BEST ANSWER

I'd just return a flag to indicate if the user has created the account today or yesterday and send the correct email based on that:

const loginemail = async (req: Request) => {
  const connection = await createConnection();
  try {
    const [dashboardJobCount]: any = await connection.query(`
    select 
        account_id, 
        IF (DATE(created_date) = CURDATE(), TRUE, FALSE) AS created_today
        username, 
        verified 
    from 
        account 
    where 
        verified=false
        AND DATE(created_date) = CURDATE()
        OR DATE(created_date) = DATE_SUB(CURDATE(), INTERVAL 1 DAY)
    `);

    const client = Sib.ApiClient.instance;

    const apiKey = client.authentications['api-key'];
    apiKey.apiKey = process.env.SIB_API_KEY;

    const tranEmailApi = new Sib.TransactionalEmailsApi();

    const sender = {
      email: '[email protected]',
      name: 'Anjan',
    };
    const receivers = [
      {
        email: '[email protected]',
      },
    ];
    const firstMail = {
      sender,
      to: receivers,
      subject: 'Testing mail 1st day',
      textContent: `
        Cules Coding will teach you how to become {{params.role}} a developer.
        `,
      templateId: 1,

      params: {
        role: 'Frontend',
      },
    };
    const secondMail = {
      sender,
      to: receivers,
      subject: 'Testing mail 2nd day',
      textContent: `
        Cules Coding will teach you how to become {{params.role}} a developer.
        `,
      templateId: 2,

      params: {
        role: 'Frontend',
      },
    };
    const thirdMail = {
      sender,
      to: receivers,
      subject: 'Testing mail 4th day',
      textContent: `
        Cules Coding will teach you how to become {{params.role}} a developer.
        `,
      templateId: 2,

      params: {
        role: 'Frontend',
      },
    };
    for (let i = 0; i < dashboardJobCount.length; i++) {
      const mailToSend = dashboardJobCount[i].created_today
        ? firstMail
        : secondMail;
      await tranEmailApi.sendTransacEmail(mailToSend);
    }
  } catch (error) {
    await connection.end();
    throw error;
  }
};