-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·472 lines (372 loc) · 13.6 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
const { cloudinary } = require('./utils/cloudinary');
const nodemailer = require('nodemailer')
const handlebars = require("handlebars");
const multer = require("multer");
const path = require('path');
const fs = require('fs');
require('dotenv').config();
const connectDB = require('./utils/db.js');
const User = require('./Models/memberModel.js');
const Review = require('./Models/reviewModel.js');
const Insta = require('./Models/instaModel.js');
const express = require('express');
const app = express();
const axios = require('axios');
const cron = require('node-cron');
const Instagram = require('node-instagram').default;
var randomstring = require("randomstring");
var cors = require('cors');
app.use(express.static('public'));
app.use(express.json({ limit: '50mb' }));
app.use(express.urlencoded({ limit: '50mb', extended: true }));
app.use(cors());
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*")
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
next()
})
connectDB()
//instragram
cron.schedule('0 */3 * * *', async () => {
try {
console.log('running a task every three hours');
const token = await axios.get(process.env.INSTA_API_FETCH)
let instaAccessToken = token.data; // get from DB
console.log(token.data)
let resp = await axios.get(`https://graph.instagram.com/me/media?fields=media_type,permalink,media_url&access_token=${instaAccessToken}`);
resp = resp.data;
let instaPhotos = resp.data.filter(d => d.media_type === "IMAGE").map(d => d.media_url);
// Got insta photos
await Insta.deleteMany({});
resp.data.forEach(async d => {
await Insta.create(d)
})
} catch (e) {
console.log(e.response.data.error);
}
});
app.get('/api/insta', async (req, res) => {
const data = await Insta.find({});
res.send(data);
})
//Date Format
function padTo2Digits(num) {
return num.toString().padStart(2, '0');
}
function formatDate() {
return [
padTo2Digits(new Date().getDate() + 2),
padTo2Digits(new Date().getMonth() + 1),
new Date().getFullYear(),
].join('/');
}
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.GMAIL_ID,
pass: process.env.GMAIL_PASSWORD
}
})
let generatedOtp;
let verifiedEmail = '';
app.get('/api/images', async (req, res) => {
const members = await User.find({});
console.log(members)
res.json(members)
});
app.get('/bro', (req, res) => {
// res.download('./templete/Broucher/ScalantBrochure.pdf'))
var data = fs.readFileSync('./templete/Broucher/ScalantBrochure.pdf');
res.contentType("application/pdf");
res.send(data);
})
app.get('/brodownload', (req, res) => {
res.download('./templete/Broucher/ScalantBrochure.pdf')
})
app.post('/api/getotp', async (req, res) => {
try {
generatedOtp = Math.floor(100000 + Math.random() * 900000);
const { email } = req.body
console.log(email, 'hello')
if (email === '') {
return res.send({ status: false, info: 'Please enter email' })
}
let mailOptions = {
from: process.env.USER_ID,
to: email,
subject: 'Let\' get you verified!!',
html: `Your OTP For Verification is ${generatedOtp}`,
}
await transporter.sendMail(mailOptions, function (err, info) {
if (err) {
return res.json(err)
} else {
res.status(201).json({ status: true, info: 'Otp Send' })
}
})
verifiedEmail = email
}
catch (err) {
console.log(err)
res.status(404).json(err)
}
})
app.post('/api/verifyotp', async (req, res) => {
try {
const { otp } = req.body
if (otp === '') {
return res.send({ status: false, info: 'Please enter otp' })
}
if (+otp === generatedOtp) {
return res.send({ status: true, info: 'Otp Verified' })
} else {
return res.send({ status: false, info: 'Otp Not Verified' })
}
}
catch (err) {
console.log(err)
res.status(404).json(err)
}
})
app.post('/api/upload', async (req, res) => {
try {
const { name, email, domain, linkedin, github, twitter, instagram, phoneNumber, profileImage } = req.body
const user = await User.findOne({ email })
if (user) {
return res.send({ status: false, info: 'User Already Exist' })
}
const member = await User.create({ photo: profileImage, name, email, phoneNumber, domain, linkedin, github, twitter, instagram })
const filePath = path.join(__dirname, './templete/email.html');
const source = fs.readFileSync(filePath, 'utf-8').toString();
const template = handlebars.compile(source);
const replacements = {
username: name.toUpperCase()
};
const htmlToSend = template(replacements);
let mailOptions = {
from: process.env.USER_ID,
to: email,
subject: 'Welcome to the Community!',
html: htmlToSend
}
await transporter.sendMail(mailOptions, function (err, info) {
if (err) {
return res.json(err)
} else {
console.log(member)
res.status(201).json({ status: true, member })
}
})
} catch (err) {
console.error(err);
res.status(500).json({ err: 'Something went wrong' });
}
});
app.get('/api/user/:id', async (req, res) => {
const id = req.params.id;
const user = await User.findById(id);
res.status(200).json({ user })
})
app.put('/api/user/:id', async (req, res) => {
const id = req.params.id;
const { name, email, domain, phoneNumber, linkedin, github, twitter, instagram, isTeamMember } = req.body
const updatedUser = await User.findByIdAndUpdate(id, { name, email, domain, phoneNumber, linkedin, github, twitter, instagram, isTeamMember }, {
new: true
})
res.json({ updatedUser });
})
app.use('/images', express.static(path.join(__dirname, './images')))
app.get('/files/:name', (req, res) => {
const fileName = req.params.name;
const directoryPath = __dirname + "/images/";
res.download(directoryPath + fileName, fileName, (err) => {
if (err) {
res.status(500).send({
message: "Could not download the file. " + err,
});
}
});
})
app.get('/api/review', async (req, res) => {
const users = await User.find({})
users.map(async (u) => {
if (u.isTeamMember) {
const filePath = path.join(__dirname, './templete/review/SCALANT.html');
const source = fs.readFileSync(filePath, 'utf-8').toString();
const template = handlebars.compile(source);
const replacements = {
username: u.name.toUpperCase(),
linktoform: `https://review.scalant.in/review/${u._id}`,
};
const htmlToSend = template(replacements);
let mailOptions = {
from: process.env.USER_ID,
to: u.email,
subject: 'Review Form',
html: htmlToSend,
}
await transporter.sendMail(mailOptions, function (err, info) {
if (err) {
console.log(err)
} else {
console.log('done')
}
})
}
})
res.send(users)
})
app.get('/api/offer', async (req, res) => {
const users = await User.find({})
users.map(async (u) => {
if (u.email === '[email protected]') {
console.log(u)
const filePath = path.join(__dirname, './templete/offerletter/template/offer.html');
const source = fs.readFileSync(filePath, 'utf-8').toString();
const template = handlebars.compile(source);
const replacements = {
linktoform: `https://scalant.in/tnc`,
date: formatDate()
};
const htmlToSend = template(replacements);
let mailOptions = {
from: process.env.USER_ID,
to: u.email,
subject: `Welcome Letter from Scalant`,
html: htmlToSend,
attachments: [
{
filename: `${u.name.split(' ')[0]}.pdf`,
path: path.join(__dirname, `./templete/offerletter/${u.name.split(' ')[0]}.pdf`),
contentType: 'application/pdf',
},
]
}
await transporter.sendMail(mailOptions, function (err, info) {
if (err) {
console.log(err)
} else {
console.log(`email sent to- ${u.name} on ${u.email}`)
}
})
}
})
res.send(users)
})
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, "./images/");
},
filename: (req, file, cb) => {
cb(null, `${file.fieldname}-${Date.now()}${path.extname(file.originalname)}`);
}
});
const checkFileType = (file, cb) => {
const filetypes = /jpeg|jpg|png|gif/;
const extname = filetypes.test(path.extname(file.originalname).toLowerCase());
const mimetype = filetypes.test(file.mimetype);
if (mimetype && extname) {
return cb(null, true);
} else {
cb("Error: Images Only!");
}
}
const upload = multer({
storage,
fileFilter: (req, file, cb) => {
checkFileType(file, cb);
}
});
app.post('/api/imgupload', upload.single('image'), (req, res) => {
const url = req.protocol + '://' + req.get("host");
res.send(`https://apiscalant.live//${req.file.path}`)
})
// app.post('/api/imgupload', async (req, res) => {
// console.log(req.headers)
// const baseurl = `https://apiscalant.live/files`
// const { data, format } = req.body
// const name = randomstring.generate(7) + '.' + format;
// let base64Data = data.replace(/^data:image\/png;base64,/, "");
// base64Data += base64Data.replace('+', ' ');
// binaryData = new Buffer(base64Data, 'base64').toString('binary');
// await fs.writeFile(`./images/${name}`, binaryData, "binary", function (err) {
// console.log(err); // writes out file without error, but it's not a valid image
// });
// res.send(baseurl + `/${name}`);
// })
app.post('/api/contact', async (req, res) => {
const { name, email, message } = req.body
let mailOptions = {
from: process.env.USER_ID,
to: '[email protected]',
subject: `Contact Mail`,
text: `Name: ${name}\nEmail: ${email}\nMessage: ${message}`,
}
await transporter.sendMail(mailOptions, function (err, info) {
if (err) {
console.log(err)
res.status(500).json({ err });
} else {
console.log(`email sent`)
res.status(201).json({ success: true, info: 'Mail Sent' })
}
})
})
app.get('/api/review/:id', async (req, res) => {
const id = req.params.id;
try {
const user = await User.findById(id);
res.send(user)
}
catch (error) {
console.log('error')
res.json({ error: 'user not found' })
}
})
app.post('/api/review', async (req, res) => {
const { name, email, past, future, issue, improvement } = req.body
const memberReview = await Review.create({ name, email, past, future, issue, improvement })
res.send(memberReview)
})
app.delete('/api/user/:id', async (req, res) => {
const id = req.params.id;
const deletedUser = await User.findByIdAndDelete(id)
res.json({ deletedUser })
})
app.get('/api/allreviews', async (req, res) => {
const reviews = await Review.find({})
let reviewUsers = reviews.map(r => r.name);
const reviewSet = new Set([...reviewUsers]);
console.log(reviewSet);
reviewUsers = [...reviewSet]
res.send({ reviewUsers });
})
// //whatsapp bot
// const client = new Client({ puppeteer: { headless: true, args: ['--no-sandbox', '--disable-setuid-sandbox'] }, authStrategy: new LocalAuth() });
// client.on("qr", (qr) => {
// console.log(qr)
// qrcode.generate(qr, { small: true });
// });
// client.on("ready", () => {
// console.log("Client is ready!");
// client.sendMessage('[email protected]', 'hello')
// });
// app.post('/api/whatsappOtp', async (req, res) => {
// const { phoneNumber } = req.body
// generatedWhatsappOtp = Math.floor(100000 + Math.random() * 900000);
// const sanitized_number = phoneNumber.toString().replace(/[- )(]/g, ""); // remove unnecessary chars from the number
// const final_number = `91${sanitized_number.substring(sanitized_number.length - 10)}`; // add 91 before the number here 91 is country code of India
// const number_details = await client.getNumberId(final_number); // get mobile number details
// if (number_details) {
// const sendMessageData = await client.sendMessage(number_details._serialized, `Otp For Whatsapp Verification is ${generatedWhatsappOtp}`); // send message
// } else {
// console.log(final_number, "Mobile number is not registered");
// return res.status(401).json({ err: 'Mobile number not registered', phoneNumber: final_number });
// }
// res.json({ status: 'complete' })
// })
// client.initialize();
const port = process.env.PORT || 3001;
app.listen(port, () => {
console.log('listening on 3001');
});