-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMongo.js
142 lines (118 loc) · 3.2 KB
/
Mongo.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
const { MongoClient , ObjectId } = require('mongodb');
const client = new MongoClient('mongodb://127.0.0.1:27017');
const CryptoJS = require('crypto-js');
ips=[""]
Md5=(str,salt)=>{
return CryptoJS.MD5(str+salt).toString(CryptoJS.enc.Hex);
}
async function login(user="",pass="",ip) {
await client.connect();
const db = client.db("loja");
const collection = db.collection('usuarios');
const result = await collection.findOne({ email: user, password: Md5(pass,"LOJA0xdeadbeef") });
if (result) {
if(result.LastIp!=ip){
// sendMail()
//confirm on mail
}
return {id:result._id.toString(),status:true};
} else {
return "Nome de usuário ou senha incorretos.";
}
}
async function usrData(user = "", ip = "") {
try {
await client.connect();
const db = client.db("loja");
const collection = db.collection('usuarios');
const result = await collection.findOne({ _id: new ObjectId(user.toString()) });
if (result) {
return result;
} else {
return "Null";
}
} catch (err) {
return "Error";
} finally {
await client.close();
}
}
async function IpByTkn(user){
try {
await client.connect();
const db = client.db("loja");
const collection = db.collection('usuarios');
const result = await collection.findOne({ _id: new ObjectId(user.toString()) });
if (result) {
return result.ip;
} else {
return "Null";
}
} catch (err) {
return "Error";
} finally {
await client.close();
}
}
async function updtUsr(id, data) {
try {
await client.connect();
const db = client.db("loja");
const collection = db.collection('usuarios');
const filter = { _id: new ObjectId(id.toString()) };
const updateDoc = {
$set: {
name: data.name,
cpf: data.cpf,
email: data.email,
address: {
country: data.country,
uf: data.uf,
cep: data.cep,
rua: data.rua,
num: data.num,
bairro: data.bairro,
city: data.city,
},
tel: data.tel
}
};
const result = await collection.updateOne(filter, updateDoc);
if (result.matchedCount === 0) {
return "can't find user";
} else {
return "ok";
}
} catch (err) {
console.error("Error updating user:", err);
return "Error";
} finally {
await client.close();
}
}
async function getData(query="{}",type="") {
await client.connect();
const db = client.db("loja");
const collection = db.collection('produtos');
result = await collection.findOne({ _id: new ObjectId ("66c5dcfc17fbcc6cb3269003") });
try{
if(query=="{}"){
return result["categorys"]
}else{
if(type=="category"){
let cats=[];
result["categorys"].forEach(i => {
i["subcat"].forEach(j => {
cats.push(j)
})
})
return cats.filter(x => x["name"] == query).map(x => x["products"]);
}
}
}catch(err){
console.log(err)
}
}
module.exports = {
login,usrData,updtUsr,IpByTkn,getData
}