1- const { ReturnDocument } = require ( "mongodb" ) ;
1+
22
33async function findUserByEmail ( email , dbconnection ) {
44
55 const db = await dbconnection ( )
66 try {
77 const result = await db . collection ( 'users' ) . find ( { email } )
8- // const found = await result.toArray()
9- // console.log(" result from DB checking", result)
10- return ( await result . toArray ( ) ) . map ( ( { _id : id , ...found } ) => ( {
11- id,
12- ...found
13- } ) )
8+ const res = ( await result . toArray ( ) ) . map ( ( { _id : id , ...found } ) => ( {
9+ id : id . toString ( ) ,
10+ email : found . email ,
11+ firstName : found . firstName ,
12+ lastName : found . lastName ,
13+ mobile : found . mobile
14+ } ) ) ;
15+
16+ console . log ( "result from store find register" , res ) ;
17+ return res ;
18+ } catch ( error ) {
19+ console . log ( "error checking for thexistence of user in DB" , error ) ;
20+ return null ;
21+ }
22+ }
23+
24+ async function findUserByEmailForLogin ( email , dbconnection ) {
25+
26+ const db = await dbconnection ( )
27+ try {
28+ // const result = await db.collection('users').find({ email })
29+
30+ // const res = (await result.toArray()).map(({ _id: id, ...found }) => ({
31+ // id: id.toString(),
32+ // password: found.password,
33+ // }));
34+ console . log ( "email from store" , email ) ;
35+ const result = await db . collection ( 'users' ) . find ( { email } )
36+ const res = ( await result . toArray ( ) ) . map ( ( { _id : id , ...found } ) => ( {
37+ id : id . toString ( ) ,
38+ ...found ,
39+ } ) ) ;
40+
41+ console . log ( "result from store for login" , res ) ;
42+ return res ;
1443 } catch ( error ) {
1544 console . log ( "error checking for thexistence of user in DB" , error ) ;
1645 return null ;
@@ -21,54 +50,61 @@ async function registerUser(userData, dbconnection) {
2150
2251 const db = await dbconnection ( )
2352 try {
53+ //check if the user already exist
54+ const existingUser = await findUserByEmail ( userData . email , dbconnection ) ;
55+ if ( existingUser ) {
56+ console . log ( "user already exists: " , existingUser ) ;
57+ existingUser ;
58+ }
2459 //insert document and return the inserted document
2560 const result = await db
26- . collection ( 'users' )
27- . insertOne ( { ...userData } ) ;
61+ . collection ( 'users' )
62+ . insertOne ( { ...userData } ) ;
2863 return result
29-
64+
3065 } catch ( error ) {
31-
66+
3267 console . log ( "error registering the user to DB: " , error ) ;
3368 if ( error . code === 11000 ) {
3469 throw new UniqueConstraintError ( error . message )
35- }
70+ }
3671 return null ;
3772 }
38-
73+
3974}
4075
41- // async function findAllUsers() {
42- // const db = await dbconnection()
43- // const result = await db.collection('users').find({})
44- // return (await result.toArray()).map(({ _id: id, ...found }) => ({
45- // id,
46- // ...found
47- // }))
48- // }
76+ // async function findAllUsers() {
77+ // const db = await dbconnection()
78+ // const result = await db.collection('users').find({})
79+ // return (await result.toArray()).map(({ _id: id, ...found }) => ({
80+ // id,
81+ // ...found
82+ // }))
83+ // }
4984
5085
51- // async function updateUser({ id: _id, userData }) {
52- // const db = await dbconnection()
53- // const result = await db
54- // .collection('users')
55- // .updateOne({ _id }, { $set: { ...userData } })
56- // return result.modifiedCount > 0 ? { id: _id, ...userData } : null
57- // }
86+ // async function updateUser({ id: _id, userData }) {
87+ // const db = await dbconnection()
88+ // const result = await db
89+ // .collection('users')
90+ // .updateOne({ _id }, { $set: { ...userData } })
91+ // return result.modifiedCount > 0 ? { id: _id, ...userData } : null
92+ // }
5893
59- // async function deleteUser({ id: _id }) {
60- // const db = await dbconnection()
61- // const result = await db.collection('users').deleteOne({ _id })
62- // return result.deletedCount
63- // }
94+ // async function deleteUser({ id: _id }) {
95+ // const db = await dbconnection()
96+ // const result = await db.collection('users').deleteOne({ _id })
97+ // return result.deletedCount
98+ // }
6499
65100
66101module . exports = function makeUserdb ( { dbconnection } ) {
67102
68103 return Object . freeze ( {
69104 // findAllUsers,
70- findUserByEmail : async ( { email } ) => findUserByEmail ( email , dbconnection ) ,
71- registerUser : async ( userData ) => registerUser ( userData , dbconnection ) ,
105+ findUserByEmail : async ( { email } ) => findUserByEmail ( email , dbconnection ) ,
106+ registerUser : async ( userData ) => registerUser ( userData , dbconnection ) ,
107+ findUserByEmailForLogin : async ( { email } ) => findUserByEmailForLogin ( email , dbconnection ) ,
72108 // updateUser,
73109 // deleteUser
74110 } )
0 commit comments