-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
100 lines (81 loc) · 2.8 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
import express from 'express';
import mongoose from 'mongoose';
import dotenv from 'dotenv';
import userSchema from './models/userSchema.js';
import Mail from './api/mail.js';
import cors from 'cors';
const app = express();
dotenv.config();
app.use(express.json());
app.use(cors());
const port = process.env.PORT || 3000; // Use PORT from environment if available
mongoose.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
dbName: 'Challan'
}).then(() => {
console.log("Connected to MongoDB");
}).catch(err => console.error("MongoDB connection error:", err));
app.post('/api', async (req, res) => {
try {
const { email, vehicle } = req.body;
const formattedVehicle = vehicle.toUpperCase();
if (formattedVehicle.length !== 10) {
res.status(405).json({ message: 'Invalid vehicle number length', code: 405 });
return;
}
const existingData = await userSchema.findOne({ email, vehicle: formattedVehicle });
if (!existingData) {
const newData = await userSchema.create({ email, vehicle: formattedVehicle });
const response = await Mail(formattedVehicle, email);
if (response === 'Email sent successfully!') {
res.status(201).json({ message: 'Email sent successfully!', code: 201 });
} else if (response === 'No pending challans.') {
res.status(202).json({ message: 'No pending challans.', code: 202 });
} else if (response === 'Challan details not found.') {
res.status(203).json({ message: 'Challan details not found.', code: 203 });
} else {
res.status(301).json({ message: 'Failed to send email.', code: 301 });
}
} else {
res.status(403).json({ message: 'User and vehicle already exist.', code: 403 });
}
} catch (e) {
console.error('Error:', e);
res.status(500).json({ message: 'Internal server error', code: 500 });
}
});
// health check
app.get('/api', async (req, res) => {
res.sendStatus(200);
});
async function myFunction() {
try {
const data = await userSchema.find();
console.log("Function executed!");
for (const item of data) {
console.log(item.vehicle, item.email);
await Mail(item.vehicle, item.email);
}
} catch (error) {
console.error("Error fetching data:", error);
}
}
function scheduleFunction(intervalInHours, asyncFunc) {
const millisecondsPerHour = 60 * 60 * 1000;
const intervalInMilliseconds = intervalInHours * millisecondsPerHour;
async function wrapper() {
try {
await asyncFunc();
} catch (error) {
console.error("Error in scheduled function:", error);
}
}
wrapper();
setInterval(wrapper, intervalInMilliseconds);
}
const intervalHours = 24;
scheduleFunction(intervalHours, myFunction);
app.listen(port, () => {
console.log(`Server is listening on port ${port}`);
});