-
Notifications
You must be signed in to change notification settings - Fork 0
/
seeder.js
69 lines (62 loc) · 1.53 KB
/
seeder.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
import mongoose from 'mongoose'
import User from './models/User.js'
import csv from 'csv-parser'
import qrCode from 'qrcode'
import nodemailer from 'nodemailer'
import fs from 'fs'
import { connect } from './database/db.js'
import 'dotenv/config'
// var counter = 0
// ;(async function connectDb() {
// await connect(process.env.MONGO_URI)
// })()
function readFile(req, res) {
fs.createReadStream('data.csv')
.pipe(csv({}))
.on('data', (data) => {
create_user(data)
// test(data)
})
.on('end', () => {
console.log('CSV file successfully processed')
})
}
async function create_user(data) {
const userExists = await User.findOne({ email: data.email })
if (userExists) {
return
}
const user = new User({
name: data.name,
email: data.email,
rollNo: data.rollNo,
slot: data.slot,
})
await user.save()
//qrcode
const qr = await qrCode.toDataURL(user._id.toString())
let transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: '#', // generated ethereal user
pass: '#', // generated ethereal password
},
})
let info = await transporter.sendMail({
from: '"Owasp" <[email protected]>',
to: user.email,
subject: 'Test Mail',
text: 'You have been invited to the event',
attachments: [
// File Stream attachment
{
filename: 'invite.png',
path: qr,
},
],
})
}
// readFile()
export { readFile }