-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongobd.js
188 lines (172 loc) · 5.86 KB
/
mongobd.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
const { json } = require('express');
const {MongoClient, ObjectId} = require('mongodb');
const mongoErrorHandler = require('./mongoErrorHandler');
const {emailSender} = require("./mailSender")
const {jwtGenerate} = require("./jwtToken")
const {mongodbUrl, dbName, serverUrl} = require("./config");
const {apiResponse} = require("./apiResponse");
const customVerification = require('./customVerification');
const client = new MongoClient(mongodbUrl);
async function createUser(data){
const userMail = data.email;
const userName = data.username;
try {
const finduser = await client.db(dbName).collection('auth').findOne({email: data.email})
if (finduser) {
return {message: "Email account exist", err: true}
}
const data1 = await client.db(dbName).collection("auth").insertOne(data)
const token = jwtGenerate(data1)
const url = `http://${serverUrl}/verifyemail?token=${token}`
const emailBody = customVerification(userName,url)
await emailSender(userMail, emailBody, "Verification Email");
//console.log(data1);
return {message: "Account creation successfull", err: false, id: data1.insertedId}
} catch (error) {
return mongoErrorHandler(error)
}
}
async function UpdateUserById(userData) {
const {id, data, collection} = userData;
if (!id || !data) {
return (apiResponse("Data or Id missing", true, 406))
}
try {
const updateComplete = await client.db(dbName).collection(collection).updateOne({_id: new ObjectId(id)}, {$set:data});
if (updateComplete) {
return (apiResponse("Update successfully", false, 204))
}
return (apiResponse("Id does not exist", true, 404))
} catch (error) {
console.log(error);
}
}
async function findById(data) {
const {id, collection} = data
if (!id || !collection) {
return undefined
}
try {
const userData = await client.db(dbName).collection(collection).findOne({_id: new ObjectId(id)})
if (userData) {
return userData;
}
return undefined;
} catch (error) {
console.log(error);
}
}
async function findByUserId(data) {
const {id, collection} = data
console.log(`user ${id}, collection ${collection}`);
if (!id || !collection) {
return undefined
}
try {
const userData = await client.db(dbName).collection(collection).findOne({id: new ObjectId(id)})
console.log(userData);
if (userData) {
return userData;
}
return undefined;
} catch (error) {
console.log(error);
}
}
async function findByEmail(em) {
const {email, collection} = em
//console.log(em);
if (!email || !collection) {
return undefined
}
try {
const userData = await client.db(dbName).collection(collection).findOne({email: email})
if (userData) {
return userData;
}
return undefined;
} catch (error) {
console.log(error);
}
}
async function findByEmailMany(em) {
const {email, collection} = em
//console.log(em);
if (!email || !collection) {
return undefined
}
try {
const userData = await client.db(dbName).collection(collection).find({email: email}).toArray()
if (userData) {
return userData;
}
return undefined;
} catch (error) {
console.log(error);
}
}
async function createOrUpdateField(userData) {
const {id, data, collection} = userData;
if (!id || !data) {
return (apiResponse("Data or Id missing", true, 406))
}
try {
const updateComplete = await client.db(dbName).collection(collection).updateOne({_id: new ObjectId(id)}, {$set:data}, {upsert:true});
if (updateComplete) {
return (apiResponse("Update successfully", false, 204))
}
return (apiResponse("Id does not exist", true, 404))
} catch (error) {
console.log(error);
}
}
async function insertData(data){
try {
const insertDa = await client.db(dbName).collection("usersInfo").insertOne(data)
if (insertDa) {
return (apiResponse("Update successfully", false, 204))
}
return (apiResponse("Id does not exist", true, 404))
} catch (error) {
console.log(error);
}
}
async function insertDataEmail(data){
try {
const insertDa = await client.db(dbName).collection("usersAttandence").insertOne(data)
if (insertDa) {
return (apiResponse("Update successfully", false, 204))
}
return (apiResponse("Id does not exist", true, 404))
} catch (error) {
console.log(error);
}
}
async function deleteField(userData) {
const {id, data, collection} = userData;
if (!id || !data) {
return (apiResponse("Data or Id missing", true, 406))
}
try {
const updateComplete = await client.db(dbName).collection(collection).updateOne({_id: new ObjectId(id)}, {$unset:data});
if (updateComplete) {
return (apiResponse("Update successfully", false, 204))
}
return (apiResponse("Id does not exist", true, 404))
} catch (error) {
console.log(error);
}
}
async function updateDataUsingAny(userData, findby, collection ){
try {
const updateComplete = await client.db(dbName).collection(collection).updateOne(findby, {$set:userData});
console.log(updateComplete);
if (updateComplete) {
return (apiResponse("Update successfully", false, 204))
}
return (apiResponse("Id does not exist", true, 404))
} catch (error) {
console.log(error);
}
}
module.exports = {updateDataUsingAny,findByEmailMany,insertDataEmail,findByUserId, createUser, UpdateUserById, findByEmail, findById, createOrUpdateField, deleteField, insertData}