-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
233 lines (179 loc) · 5.53 KB
/
index.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
const express = require('express');
const cors = require('cors');
// import dotenv from "dotenv";
require('dotenv').config();
// dotenv.config();
require('./db/conn.js')
const mongoose = require('mongoose');
const DATABASE = process.env.DATABASE;
const EMAIL = process.env.EMAIL;
const PASSWORD = process.env.PASSWORD;
const { Subscribe } = require('./models/userSchema.js')
// import nodemailer from "nodemailer";
const nodemailer = require('nodemailer')
const userrouter = require('./routes/user.js');
const dictrouter = require('./routes/dictionary.js');
const { storesentdata } = require('./controllers/antosynocontroller.js')
// import {getallsentwords} from "./controllers/wordcontroller.js";
const app = express();
app.use(express.json());
// app.use(express.urlencoded());
app.use(cors());
app.use(userrouter);
app.use(dictrouter);
// let startword=0;
const timeInterval = 60 * 1000;
// setInterval(() => {
// sendmail();
// // getallsentwords()
// }, timeInterval);
const sendmail = async () => {
const lastIndex = await Index.find();
let startword = lastIndex[lastIndex.length - 1]?.num;
// let startword = 0;
try {
console.log("sendmail called");
const recipients = await Subscribe.find();
// console.log(recipients);
// console.log(antData[0].email)
// const allwords =await allData.find();
const db = mongoose.connection.db;
const allDataCollection = db.collection('allData');
const allData = await allDataCollection.find().toArray();
// console.log('All data from the collection:', allData);
// const allsynData = await AdSynonym.find();
// console.log(allwords);
// console.log(startword);
// if (
// startword + 2 > allwords.length
// ) {
// startword = 0;
// }
// console.log(startword)
const filtereddata = allData.slice(startword, startword + 2);
storesentdata(filtereddata[0]);
storesentdata(filtereddata[1]);
// console.log(filtereddata);
const emailHTML = `
<html>
<body>
<h2> Here are 2 word of the day</h2>
<h4>Word:${filtereddata[0]?.word}</h4>
<h4>Meaning:${filtereddata[0]?.meaning}</h4>
<h4>Antonym:${filtereddata[0]?.antonym}</h4>
<h4>Synonym:${filtereddata[0]?.synonym}</h4>
<h4>Example:${filtereddata[0]?.example}</h4>
<br/>
<h4>Word:${filtereddata[1]?.word}</h4>
<h4>Meaning:${filtereddata[1]?.meaning}</h4>
<h4>Antonym:${filtereddata[1]?.antonym}</h4>
<h4>Synonym:${filtereddata[1]?.synonym}</h4>
<h4>Example:${filtereddata[1]?.example}</h4>
</body>
</html>
`;
const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: EMAIL,
pass: PASSWORD,
},
});
for (let i = 0; i < recipients.length; i++) {
const recipient = recipients[i].email;
const mailOptions = {
from: "[email protected]",
to: recipient,
subject: "Word of the Day",
html: emailHTML,
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log("error", error);
} else {
console.log("Email sent" + info.response);
// res.status(201).json({ status: 201, info });
}
});
}
}
catch (error) {
console.log(" error occured in email send function", error);
}
startword += 2;
getIndex(startword);
};
const indexSchema = new mongoose.Schema({
num: Number,
});
const Index = new mongoose.model("Index", indexSchema);
const getIndex = (currval) => {
const index = new Index({ num: currval });
index.save((error, savedUser) => {
if (error) {
console.error(error);
} else {
console.log("index set successfully:");
}
});
};
// // adding antonyms
app.get("/", (req, res) => {
res.json("server getting started on localhost");
});
// // admin
// const antomymAdminSchema = new mongoose.Schema({
// word: String,
// antonym: String,
// });
// const AdAntonym = new mongoose.model("AdAntonym", antomymAdminSchema);
// app.post("/addallantonyms", (req, res) => {
// const { word, antonym } = req.body;
// console.log(word, antonym);
// const Adantonym = new AdAntonym({
// word,
// antonym,
// });
// Adantonym.save((err) => {
// if (err) res.send(err);
// else res.send({ message: "New Word Added" });
// });
// });
// app.get("/getallantonyms", async (req, res) => {
// try {
// const allantonymData = await AdAntonym.find();
// res.status(201).json(allantonymData);
// // console.log(allantonymData);
// } catch (error) {
// res.status(404).json(error);
// }
// });
// const synonymAdminSchema = new mongoose.Schema({
// word: String,
// synonym: String,
// });
// const AdSynonym = new mongoose.model("AdSynonym", synonymAdminSchema);
// app.post("/addallsynonyms", (req, res) => {
// const { word, synonym } = req.body;
// // console.log(word,antonym);
// const Adsynonym = new AdSynonym({
// word,
// synonym,
// });
// Adsynonym.save((err) => {
// if (err) res.send(err);
// else res.send({ message: "New Word Added" });
// });
// });
// app.get("/getallsynonyms", async (req, res) => {
// try {
// const allsynonymData = await AdSynonym.find();
// res.status(201).json(allsynonymData);
// // console.log(allantonymData);
// } catch (error) {
// res.status(404).json(error);
// }
// });
app.listen(process.env.PORT || 9000, () => {
console.log("app is running on 9000");
});