-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
120 lines (102 loc) · 3.57 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Load environment variables from .env file
require('dotenv').config({ path: './.auth' });
// Importing necessary libraries and modules
const express = require('express');
const path = require("path");
const hbs = require('hbs');
const bcrypt = require('bcrypt');
const session = require('express-session');
const cron = require('node-cron');
const Chart = require('chart.js/auto');
// Import transporter from config.js
const transporter = require('./config/config');
// Import connectDB function from db.js
const connectDB = require('./db/db');
// Create an Express application
const app = express();
const fs = require('fs');
app.use(express.static('public'));
app.set('view engine', 'hbs');
// Default port from environment or fallback to 4000
const PORT = process.env.PORT || 4000;
// Session middleware configuration
app.use(session({
secret: 'your session secret',
resave: false,
saveUninitialized: true,
cookie: {
secure: false,
httpOnly: true,
maxAge: 24 * 60 * 60 * 1000
}
}));
// Set the views directory
app.set('views', path.join(__dirname, 'views'));
// Import User model
const User = require('./models/user');
// Import UserTaxDetails model
const UserTaxDetails = require('./models/userTaxDetails');
// Middleware to parse JSON bodies
app.use(express.json());
// Middleware to parse URL-encoded bodies
app.use(express.urlencoded({ extended: true }));
// Middleware to serve static files
app.use(express.static('public'));
// Asynchronously fetch all users from the database
async function fetchAllUsers() {
try {
// Retrieve all user records
const users = await User.find({});
// For each user, send a reminder email
users.forEach(user => {
sendReminderEmail(user.email, user.name);
});
} catch (err) {
console.error('Error fetching users:', err);
}
}
// Function to send a reminder email to a specific user
function sendReminderEmail(userEmail, userName) {
// Defining email options
const mailOptions = {
from: process.env.EMAIL_USERNAME,
to: userEmail,
subject: 'Tax Filing Deadline Reminder',
text: `Dear ${userName},\n\nThis is a reminder that the tax filing deadline is approaching. Please ensure your documents are submitted in time.\n\nBest regards,\nYour Tax Filing Team`
};
// Send the email using the transporter
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.error('Error sending email:', error);
} else {
console.log('Email sent:', info.response);
}
});
}
// Connect to the database
connectDB();
// Schedule cron job to fetch all users and send reminder emails
let job = cron.schedule('46 19 * * *', function() {
fetchAllUsers();
}, null, true, 'Your_Timezone');
job.start();
// Import routes
const indexRoutes = require('./routes/indexRoutes');
const authRoutes = require('./routes/authRoutes');
const dashboardRoutes = require('./routes/dashboardRoutes');
const taxCalculationRoutes = require('./routes/taxCalculationRoutes');
const calculationRoutes = require('./routes/calculationRoutes');
const suggestionsRoutes = require('./routes/suggestionsRoutes');
const taxAnalyticsRoutes = require('./routes/taxAnalyticsRoutes');
// Use routes
app.use('/', indexRoutes);
app.use(authRoutes);
app.use(dashboardRoutes);
app.use(taxCalculationRoutes);
app.use('/', calculationRoutes);
app.use('/', suggestionsRoutes);
app.use('/', taxAnalyticsRoutes);
// Start the server
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});