|
1 |
| -const mongoCollections = require("../config/mongoCollections.js"); |
2 |
| -const user = mongoCollections.user; |
3 |
| -const product = mongoCollections.product; |
4 |
| -let { ObjectId } = require("mongodb"); |
5 |
| - |
6 |
| -const bcrypt = require("bcryptjs"); |
7 |
| - |
8 |
| -const saltRounds = 1; |
9 |
| - |
10 |
| -async function getUser(id) { |
11 |
| - const userCollection = await user(); |
12 |
| - const prodCollection = await product(); |
13 |
| - |
14 |
| - let objectID = ObjectId(id); |
15 |
| - const userModel = await userCollection.findOne({ _id: objectID }); |
16 |
| - const productRes = await prodCollection.find({ sellerID: id }).toArray(); |
17 |
| - let result = { |
18 |
| - user: userModel, |
19 |
| - products: productRes, |
20 |
| - }; |
21 |
| - return result; |
22 |
| -} |
23 |
| - |
24 |
| -function removeObjectFromId(obj) { |
25 |
| - obj["_id"] = obj["_id"].toString(); |
26 |
| - return obj; |
27 |
| -} |
28 |
| - |
29 |
| -async function updateProfile(name, email, Password, address, phone, id) { |
30 |
| - let objectID = ObjectId(id); |
31 |
| - const userCollection = await user(); |
32 |
| - console.log(name + " " + email + " " + Password + " " + address); |
33 |
| - const updatedInfo = await userCollection.updateOne( |
34 |
| - { _id: objectID }, |
35 |
| - { |
36 |
| - $set: { |
37 |
| - name: name, |
38 |
| - address: address, |
39 |
| - phoneNumber: phone, |
40 |
| - email: email, |
41 |
| - password: Password, |
42 |
| - }, |
43 |
| - } |
44 |
| - ); |
45 |
| - if (updatedInfo.modifiedCount === 0) return false; |
46 |
| - return true; |
47 |
| -} |
48 |
| - |
49 |
| -async function createUser(name, address, phoneNumber, email, password) { |
50 |
| - console.log("inside CreateUSer"); |
51 |
| - for (let i of email) { |
52 |
| - if (i == " ") throw `email has empty spaces`; |
53 |
| - } |
54 |
| - for (let i of password) if (i == " ") throw `password has empty spaces`; |
55 |
| - const hashedPassword = await bcrypt.hash(password, saltRounds); |
56 |
| -console.log("inside create user") |
57 |
| - if (await emailExists(email)) { |
58 |
| - return "email already taken"; |
59 |
| - } |
60 |
| - |
61 |
| - const userCollection = await user(); |
62 |
| - let newUser = { |
63 |
| - name: name, |
64 |
| - address: address, |
65 |
| - phoneNumber: phoneNumber, |
66 |
| - email: email, |
67 |
| - hashedPassword: hashedPassword, |
68 |
| - activeCart: [], |
69 |
| - }; |
70 |
| - |
71 |
| - const insertInfo = await userCollection.insertOne(newUser); |
72 |
| - if (insertInfo.insertedCount === 0) throw "Failed to create a new user."; |
73 |
| - |
74 |
| - return { userInserted: true }; |
75 |
| -} |
76 |
| - |
77 |
| -async function emailExists(email) { |
78 |
| - email = email.toLowerCase(); |
79 |
| - |
80 |
| - const loginCollection = await user(); |
81 |
| - |
82 |
| - return (await loginCollection.findOne({ email: email })) !== null; |
83 |
| -} |
84 |
| - |
85 |
| -async function checkUser(email, password) { |
86 |
| - const userCollection = await user(); |
87 |
| - |
88 |
| - const res = await userCollection.findOne({ |
89 |
| - email: email, |
90 |
| - }); |
91 |
| - if (res == null) { |
92 |
| - throw `error`; |
93 |
| - } |
94 |
| - if (await bcrypt.compare(password, res.hashedPassword)) { |
95 |
| - return { userId: removeObjectFromId(res)._id, authenticated: true }; |
96 |
| - } else { |
97 |
| - throw `Password not match`; |
98 |
| - } |
99 |
| -} |
100 |
| -module.exports = { |
101 |
| - updateProfile, |
102 |
| - getUser, |
103 |
| - createUser, |
104 |
| - checkUser, |
105 |
| -}; |
| 1 | +const mongoCollections = require("../config/mongoCollections.js"); |
| 2 | +const user = mongoCollections.user; |
| 3 | +const product = mongoCollections.product; |
| 4 | +let { ObjectId } = require("mongodb"); |
| 5 | + |
| 6 | +const bcrypt = require("bcryptjs"); |
| 7 | + |
| 8 | +const saltRounds = 1; |
| 9 | + |
| 10 | +async function getUser(id) { |
| 11 | + const userCollection = await user(); |
| 12 | + const prodCollection = await product(); |
| 13 | + |
| 14 | + let objectID = ObjectId(id); |
| 15 | + const userModel = await userCollection.findOne({ _id: objectID }); |
| 16 | + const productRes = await prodCollection.find({ sellerID: id }).toArray(); |
| 17 | + let result = { |
| 18 | + user: userModel, |
| 19 | + products: productRes, |
| 20 | + }; |
| 21 | + return result; |
| 22 | +} |
| 23 | + |
| 24 | +function removeObjectFromId(obj) { |
| 25 | + obj["_id"] = obj["_id"].toString(); |
| 26 | + return obj; |
| 27 | +} |
| 28 | + |
| 29 | +async function updateProfile(name, email, Password, address, phone, id) { |
| 30 | + let objectID = ObjectId(id); |
| 31 | + const userCollection = await user(); |
| 32 | + console.log(name + " " + email + " " + Password + " " + address); |
| 33 | + const updatedInfo = await userCollection.updateOne( |
| 34 | + { _id: objectID }, |
| 35 | + { |
| 36 | + $set: { |
| 37 | + name: name, |
| 38 | + address: address, |
| 39 | + phoneNumber: phone, |
| 40 | + email: email, |
| 41 | + password: Password, |
| 42 | + }, |
| 43 | + } |
| 44 | + ); |
| 45 | + if (updatedInfo.modifiedCount === 0) return false; |
| 46 | + return true; |
| 47 | +} |
| 48 | + |
| 49 | +async function createUser(name, address, phoneNumber, email, password) { |
| 50 | + console.log("inside CreateUSer"); |
| 51 | + for (let i of email) { |
| 52 | + if (i == " ") throw `email has empty spaces`; |
| 53 | + } |
| 54 | + for (let i of password) if (i == " ") throw `password has empty spaces`; |
| 55 | + const hashedPassword = await bcrypt.hash(password, saltRounds); |
| 56 | +console.log("inside create user") |
| 57 | + if (await emailExists(email)) { |
| 58 | + return "email already taken"; |
| 59 | + } |
| 60 | + |
| 61 | + const userCollection = await user(); |
| 62 | + let newUser = { |
| 63 | + name: name, |
| 64 | + address: address, |
| 65 | + phoneNumber: phoneNumber, |
| 66 | + email: email, |
| 67 | + hashedPassword: hashedPassword, |
| 68 | + activeCart: [], |
| 69 | + }; |
| 70 | + |
| 71 | + const insertInfo = await userCollection.insertOne(newUser); |
| 72 | + if (insertInfo.insertedCount === 0) throw "Failed to create a new user."; |
| 73 | + |
| 74 | + return { userInserted: true }; |
| 75 | +} |
| 76 | + |
| 77 | +async function emailExists(email) { |
| 78 | + email = email.toLowerCase(); |
| 79 | + |
| 80 | + const loginCollection = await user(); |
| 81 | + |
| 82 | + return (await loginCollection.findOne({ email: email })) !== null; |
| 83 | +} |
| 84 | + |
| 85 | +async function checkUser(email, password) { |
| 86 | + const userCollection = await user(); |
| 87 | + |
| 88 | + const res = await userCollection.findOne({ |
| 89 | + email: email, |
| 90 | + }); |
| 91 | + if (res == null) { |
| 92 | + throw `error`; |
| 93 | + } |
| 94 | + if (await bcrypt.compare(password, res.hashedPassword)) { |
| 95 | + return { userId: removeObjectFromId(res)._id, authenticated: true }; |
| 96 | + } else { |
| 97 | + throw `Password not match`; |
| 98 | + } |
| 99 | +} |
| 100 | +module.exports = { |
| 101 | + updateProfile, |
| 102 | + getUser, |
| 103 | + createUser, |
| 104 | + checkUser, |
| 105 | +}; |
0 commit comments