Skip to content

Commit d2c9785

Browse files
committed
fix : cron job
1 parent e595d02 commit d2c9785

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717
"dotenv": "^16.4.5",
1818
"express": "^4.19.2",
1919
"jsonwebtoken": "^9.0.2",
20-
"mongoose": "^8.6.0"
20+
"mongoose": "^8.6.0",
21+
"node-cron": "^3.0.3",
22+
"nodemailer": "^6.9.15"
2123
},
2224
"devDependencies": {
2325
"@types/bcryptjs": "^2.4.6",
2426
"@types/express": "^4.17.21",
2527
"@types/jsonwebtoken": "^9.0.6",
28+
"@types/node-cron": "^3.0.11",
2629
"nodemon": "^3.1.4",
2730
"ts-node": "^10.9.2",
2831
"typescript": "^5.5.4"

src/configs/email-template.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const EMAIL_TEMPLATE = `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html dir="ltr" xmlns="http://www.w3.org/1999/xhtml" xmlns:o="urn:schemas-microsoft-com:office:office" lang="EN" > <head> <meta charset="UTF-8" /> <meta content="width=device-width, initial-scale=1" name="viewport" /> <meta name="x-apple-disable-message-reformatting" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta content="telephone=no" name="format-detection" /> <title>Diwali-2</title> <style type="text/css"> #outlook a { padding: 0; } .es-button { mso-style-priority: 100 !important; text-decoration: none !important; } a[x-apple-data-detectors] { color: inherit !important; text-decoration: none !important; font-size: inherit !important; font-family: inherit !important; font-weight: inherit !important; line-height: inherit !important; } .es-desk-hidden { display: none; float: left; overflow: hidden; width: 0; max-height: 0; line-height: 0; mso-hide: all; } @media only screen and (max-width: 600px) { p, ul li, ol li, a { line-height: 150% !important; } h1, h2, h3, h1 a, h2 a, h3 a { line-height: 120% !important; } h1 { font-size: 30px !important; text-align: left; } h2 { font-size: 24px !important; text-align: left; } h3 { font-size: 20px !important; text-align: left; } .es-header-body h1 a, .es-content-body h1 a, .es-footer-body h1 a { font-size: 30px !important; text-align: left; } .es-header-body h2 a, .es-content-body h2 a, .es-footer-body h2 a { font-size: 24px !important; text-align: left; } .es-header-body h3 a, .es-content-body h3 a, .es-footer-body h3 a { font-size: 20px !important; text-align: left; } .es-menu td a { font-size: 14px !important; } .es-header-body p, .es-header-body ul li, .es-header-body ol li, .es-header-body a { font-size: 14px !important; } .es-content-body p, .es-content-body ul li, .es-content-body ol li, .es-content-body a { font-size: 14px !important; } .es-footer-body p, .es-footer-body ul li, .es-footer-body ol li, .es-footer-body a { font-size: 14px !important; } .es-infoblock p, .es-infoblock ul li, .es-infoblock ol li, .es-infoblock a { font-size: 12px !important; } *[class="gmail-fix"] { display: none !important; } .es-m-txt-c, .es-m-txt-c h1, .es-m-txt-c h2, .es-m-txt-c h3 { text-align: center !important; } .es-m-txt-r, .es-m-txt-r h1, .es-m-txt-r h2, .es-m-txt-r h3 { text-align: right !important; } .es-m-txt-l, .es-m-txt-l h1, .es-m-txt-l h2, .es-m-txt-l h3 { text-align: left !important; } .es-m-txt-r img, .es-m-txt-c img, .es-m-txt-l img { display: inline !important; } .es-button-border { display: inline-block !important; } a.es-button, button.es-button { font-size: 18px !important; display: inline-block !important; } .es-adaptive table, .es-left, .es-right { width: 100% !important; } .es-content table, .es-header table, .es-footer table, .es-content, .es-footer, .es-header { width: 100% !important; max-width: 600px !important; } .es-adapt-td { display: block !important; width: 100% !important; } .adapt-img { width: 100% !important; height: auto !important; } .es-m-p0 { padding: 0 !important; } .es-m-p0r { padding-right: 0 !important; } .es-m-p0l { padding-left: 0 !important; } .es-m-p0t { padding-top: 0 !important; } .es-m-p0b { padding-bottom: 0 !important; } .es-m-p20b { padding-bottom: 20px !important; } .es-mobile-hidden, .es-hidden { display: none !important; } tr.es-desk-hidden, td.es-desk-hidden, table.es-desk-hidden { width: auto !important; overflow: visible !important; float: none !important; max-height: inherit !important; line-height: inherit !important; } tr.es-desk-hidden { display: table-row !important; } table.es-desk-hidden { display: table !important; } td.es-desk-menu-hidden { display: table-cell !important; } .es-menu td { width: 1% !important; } table.es-table-not-adapt, .esd-block-html table { width: auto !important; } table.es-social { display: inline-block !important; } table.es-social td { display: inline-block !important; } .es-desk-hidden { display: table-row !important; width: auto !important; overflow: visible !important; max-height: inherit !important; } .es-m-p5 { padding: 5px !important; } .es-m-p5t { padding-top: 5px !important; } .es-m-p5b { padding-bottom: 5px !important; } .es-m-p5r { padding-right: 5px !important; } .es-m-p5l { padding-left: 5px !important; } .es-m-p10 { padding: 10px !important; } .es-m-p10t { padding-top: 10px !important; } .es-m-p10b { padding-bottom: 10px !important; } .es-m-p10r { padding-right: 10px !important; } .es-m-p10l { padding-left: 10px !important; } .es-m-p15 { padding: 15px !important; } .es-m-p15t { padding-top: 15px !important; } .es-m-p15b { padding-bottom: 15px !important; } .es-m-p15r { padding-right: 15px !important; } .es-m-p15l { padding-left: 15px !important; } .es-m-p20 { padding: 20px !important; } .es-m-p20t { padding-top: 20px !important; } .es-m-p20r { padding-right: 20px !important; } .es-m-p20l { padding-left: 20px !important; } .es-m-p25 { padding: 25px !important; } .es-m-p25t { padding-top: 25px !important; } .es-m-p25b { padding-bottom: 25px !important; } .es-m-p25r { padding-right: 25px !important; } .es-m-p25l { padding-left: 25px !important; } .es-m-p30 { padding: 30px !important; } .es-m-p30t { padding-top: 30px !important; } .es-m-p30b { padding-bottom: 30px !important; } .es-m-p30r { padding-right: 30px !important; } .es-m-p30l { padding-left: 30px !important; } .es-m-p35 { padding: 35px !important; } .es-m-p35t { padding-top: 35px !important; } .es-m-p35b { padding-bottom: 35px !important; } .es-m-p35r { padding-right: 35px !important; } .es-m-p35l { padding-left: 35px !important; } .es-m-p40 { padding: 40px !important; } .es-m-p40t { padding-top: 40px !important; } .es-m-p40b { padding-bottom: 40px !important; } .es-m-p40r { padding-right: 40px !important; } .es-m-p40l { padding-left: 40px !important; } .h-auto { height: auto !important; } @media only screen and (max-width: 600px) { .es-m-txt-c, .es-m-txt-c h1, .es-m-txt-c h2, .es-m-txt-c h3 { text-align: left !important; } } } </style> </head> <body data-new-gr-c-s-loaded="14.1136.0" style=" width: 100%; font-family: arial, 'helvetica neue', helvetica, sans-serif; -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; padding: 0; margin: 0; " > <div dir="ltr" class="es-wrapper-color" lang="EN" style="background-color: #f6f6f6" > <!--[if gte mso 9 ]><v:background xmlns:v="urn:schemas-microsoft-com:vml" fill="t"> <v:fill type="tile" color="#f6f6f6"></v:fill> </v:background ><![endif]--> <table class="es-wrapper" width="100%" cellspacing="0" cellpadding="0" role="none" style=" mso-table-lspace: 0pt; mso-table-rspace: 0pt; border-collapse: collapse; border-spacing: 0px; padding: 0; margin: 0; width: 100%; height: 100%; background-repeat: repeat; background-position: center top; background-color: #f6f6f6; " > <tr> <td valign="top" style="padding: 0; margin: 0"> <table class="es-content" cellspacing="0" cellpadding="0" align="center" role="none" style=" mso-table-lspace: 0pt; mso-table-rspace: 0pt; border-collapse: collapse; border-spacing: 0px; table-layout: fixed !important; width: 100%; " > <tr> <td align="center" style="padding: 0; margin: 0"> <table class="es-content-body" cellspacing="0" cellpadding="0" bgcolor="#ffffff" align="center" role="none" style=" mso-table-lspace: 0pt; mso-table-rspace: 0pt; border-collapse: collapse; border-spacing: 0px; background-color: #ffffff; width: 600px; " > <tr> <td align="left" style=" padding: 0; margin: 0; padding-top: 20px; padding-left: 20px; padding-right: 20px; " > <table width="100%" cellspacing="0" cellpadding="0" role="none" style=" mso-table-lspace: 0pt; mso-table-rspace: 0pt; border-collapse: collapse; border-spacing: 0px; " > <tr> <td valign="top" align="center" style="padding: 0; margin: 0; width: 560px" > <table width="100%" cellspacing="0" cellpadding="0" role="presentation" style=" mso-table-lspace: 0pt; mso-table-rspace: 0pt; border-collapse: collapse; border-spacing: 0px; " > <tr> <td align="center" style=" padding: 0; margin: 0; padding-bottom: 40px; padding-top: 10px; font-size: 0px; " > <a href="#" style=" -webkit-text-size-adjust: none; -ms-text-size-adjust: none; mso-line-height-rule: exactly; text-decoration: underline; color: #2cb543; font-size: 14px; " ><img class="adapt-img" src="https://res.cloudinary.com/dlroruqsz/image/upload/v1725447307/vfllao3ctaqyt7ydpcdj.png" alt="email-hero" style=" display: block; border: 0; outline: none; text-decoration: none; -ms-interpolation-mode: bicubic; " width="550" height="835" /></a> </td> </tr> </table> </td> </tr> </table> </td> </tr> <tr> <td align="left" style=" padding: 0; margin: 0; padding-top: 20px; padding-left: 20px; padding-right: 20px; " > <table cellpadding="0" cellspacing="0" width="100%" role="none" style=" mso-table-lspace: 0pt; mso-table-rspace: 0pt; border-collapse: collapse; border-spacing: 0px; " > <tr> <td align="center" valign="top" style="padding: 0; margin: 0; width: 560px" > <table cellpadding="0" cellspacing="0" width="100%" role="presentation" style=" mso-table-lspace: 0pt; mso-table-rspace: 0pt; border-collapse: collapse; border-spacing: 0px; " > </table> </td> </tr> </table> </td> </tr> </td> </tr> </table> </td> </tr> </table> <table cellpadding="0" cellspacing="0" class="es-header" align="center" role="none" style=" mso-table-lspace: 0pt; mso-table-rspace: 0pt; border-collapse: collapse; border-spacing: 0px; table-layout: fixed !important; width: 100%; background-color: transparent; background-repeat: repeat; background-position: center top; " > <tr> <td align="center" style="padding: 0; margin: 0"> <table bgcolor="#ffffff" class="es-header-body" align="center" cellpadding="0" cellspacing="0" role="none" style=" mso-table-lspace: 0pt; mso-table-rspace: 0pt; border-collapse: collapse; border-spacing: 0px; background-color: #ffffff; width: 600px; " > <tr> <td style=" margin: 0; padding-top: 20px; padding-bottom: 20px; padding-left: 20px; padding-right: 20px; background-color: transparent; " bgcolor="transparent" align="left" > <table width="100%" cellspacing="0" cellpadding="0" role="none" style=" mso-table-lspace: 0pt; mso-table-rspace: 0pt; border-collapse: collapse; border-spacing: 0px; " > <tr> <td valign="top" align="center" style="padding: 0; margin: 0; width: 560px" > <table width="100%" cellspacing="0" cellpadding="0" role="presentation" style=" mso-table-lspace: 0pt; mso-table-rspace: 0pt; border-collapse: collapse; border-spacing: 0px; " > <tr> <td align="center" style=" padding: 0; margin: 0; padding-bottom: 25px; font-size: 0px; " > <img class="adapt-img" src="https://res.cloudinary.com/dlroruqsz/image/upload/v1725447306/cb12ts6vxg5orcn7cycj.png" alt="image-footer" style=" display: block; border: 0; outline: none; text-decoration: none; -ms-interpolation-mode: bicubic; " width="550" height="125" /> </td> </tr> <tr> <td align="center" style=" padding: 0; margin: 0; padding-top: 40px; padding-bottom: 10px; font-size: 0px; " > <table class="es-table-not-adapt es-social" cellspacing="0" cellpadding="0" role="presentation" style=" mso-table-lspace: 0pt; mso-table-rspace: 0pt; border-collapse: collapse; border-spacing: 0px; " > <tr> <td valign="top" align="center" style=" padding: 0; margin: 0; padding-right: 18px; " > <a href="#"> <img title="Twitter" src="https://res.cloudinary.com/dlroruqsz/image/upload/v1725447306/htgk9loclnzuwcxm2zor.png" alt="Tw" width="auto" height="24" style=" display: block; border: 0; outline: none; text-decoration: none; -ms-interpolation-mode: bicubic; " /></a> </td> <td valign="top" align="center" style=" padding: 0; margin: 0; padding-right: 18px; padding-left: 3px; " > <a href="#"> <img title="Linkend" src="https://res.cloudinary.com/dlroruqsz/image/upload/v1725447306/uybotzg88c68xh5dnuvz.png" alt="Linked" width="auto" height="24" style=" display: block; border: 0; outline: none; text-decoration: none; -ms-interpolation-mode: bicubic; " /></a> </td> <td valign="top" align="center" style=" padding: 0; margin: 0; padding-right: 18px; " > <a href="#"> <img title="fb" src="https://res.cloudinary.com/dlroruqsz/image/upload/v1725447306/yllutcbj60vmr8t4lpt7.png" alt="fb" width="auto" height="24" style=" display: block; border: 0; outline: none; text-decoration: none; -ms-interpolation-mode: bicubic; " /></a> </td> </tr> </table> </td> </tr> </table> </td> </tr> </table> </td> </tr> </table> </td> </tr> </table> </td> </tr> </table> </div> </body> </html>
2+
`;

src/configs/email.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { EMAIL_TEMPLATE } from "./email-template";
2+
3+
const nodemailer = require("nodemailer");
4+
5+
export const transporter = (email: string, paasword: string) =>
6+
nodemailer.createTransport({
7+
host: "smtp.gmail.com",
8+
service: "gmail",
9+
port: 465,
10+
secure: true,
11+
auth: {
12+
user: email,
13+
pass: paasword,
14+
},
15+
});
16+
17+
// Email options
18+
export const mailOptions = {
19+
from: "dileep.r@webllisto.com",
20+
to: "ddr.dileepraloti@gmail.com,",
21+
subject: "Node.js Email Test",
22+
html: EMAIL_TEMPLATE,
23+
};

src/index.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ import { config } from "dotenv";
33
import { databaseConnection } from "./configs/db-config";
44
import rootRouter from "./routers";
55
import { apiErrorResponse } from "./configs/api/api.response.config";
6+
import { mailOptions, transporter } from "./configs/email";
7+
import cron from "node-cron";
68

79
// env and app configuration
810
config();
911
databaseConnection();
1012
const app = express();
1113
app.use(express.json());
1214
const port: string = process.env.APPLICATION_PROT || "8000";
15+
const SENDER_EMAIL = process.env.SENDER_EMAIL;
16+
const SENDER_EMAIL_SECURITY = process.env.SENDER_EMAIL_SECURITY;
1317

1418
app.use("/api/v1", rootRouter); // root router
1519

@@ -20,6 +24,23 @@ app.use((req: Request, res: Response) => {
2024
.json(apiErrorResponse("not-found", "Route or method not found"));
2125
});
2226

27+
function logMessage() {
28+
transporter(SENDER_EMAIL!, SENDER_EMAIL_SECURITY!).sendMail(
29+
mailOptions,
30+
async (error: any, info: any) => {
31+
if (error) {
32+
return console.log(`Error: ${error}`);
33+
}
34+
console.log(`Message Sent: ${info.response}`);
35+
}
36+
);
37+
}
38+
39+
// every-day 10 am mon-fri
40+
cron.schedule("0 10 * * 1-5", () => {
41+
logMessage();
42+
});
43+
2344
app.listen(port, () => {
2445
console.log(`Server is running on http://localhost:${port}`);
2546
});

0 commit comments

Comments
 (0)