Created
February 25, 2021 18:33
-
-
Save pablohdzvizcarra/10babb56bb2090bab6c8df4da685af98 to your computer and use it in GitHub Desktop.
Data Access Object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Collection, MongoClient } from 'mongodb' | |
import User from '../lib/User' | |
import * as bcrypt from 'bcrypt' | |
import aggregateRooms from '../Room/Shared/mongodb/aggregateRooms' | |
let users: Collection | |
export type UserFromDB = { | |
_id: string | |
name: string | |
email: string | |
password: string | |
color: string | |
socket_id: string | |
room: string | |
list_chat_rooms: [] | |
chat_rooms_created: [] | |
} | |
type ResultUserDAO = { | |
error?: string | |
document?: UserFromDB | |
} | |
type ReturnDAO = { | |
error?: string | |
document?: UserFromDB | |
} | |
function checkPasswordsAreEqual(password: string, passwordHash: string) { | |
return bcrypt.compareSync(password, passwordHash) | |
} | |
class UsersDAO { | |
static async injectDB(conn: MongoClient): Promise<void> { | |
if (users) { | |
return | |
} | |
try { | |
users = await conn.db().collection('users') | |
} catch (e) { | |
console.error(`Unable to establish collection handle is usersDAO: ${e}`) | |
} | |
} | |
static async addUser( | |
name: string, | |
password: string, | |
color: string, | |
email: string | |
): Promise<{ error: string } | void> { | |
try { | |
const user = new User(name, email, password, color) | |
user.hashPassword() | |
const { insertedCount } = await users.insertOne(user.toJson()) | |
if (insertedCount !== 1) { | |
return { | |
error: 'the document was not inserted in the database' | |
} | |
} | |
return | |
} catch (error) { | |
if (error.code === 11000) | |
return { | |
error: `el email ${email} ya ha sido registrado por otro usuario` | |
} | |
return { | |
error: error | |
} | |
} | |
} | |
static async getUser( | |
email: string, | |
password: string | |
): Promise<ResultUserDAO> { | |
try { | |
const cursor = users.aggregate([ | |
{ | |
$match: { | |
email: email | |
} | |
}, | |
{ | |
$lookup: { | |
from: 'chat_rooms', | |
localField: 'email', | |
foreignField: 'user_creator', | |
as: 'chat_rooms_created' | |
} | |
} | |
]) | |
const userFromDB = await cursor.next() | |
if (!userFromDB) { | |
return { error: 'the email is not valid' } | |
} | |
if (!checkPasswordsAreEqual(password, userFromDB.password)) { | |
return { error: `the password in wrong` } | |
} | |
return { document: userFromDB } | |
} catch (e) { | |
return { error: e.text } | |
} | |
} | |
static async addRoomToUser( | |
email: string, | |
room: Record<string, unknown> | |
): Promise<ResultUserDAO> { | |
try { | |
const userFromDB = await users.findOneAndUpdate( | |
{ email: email }, | |
{ $push: { chat_rooms_created: room } }, | |
{ returnOriginal: false } | |
) | |
if (!userFromDB.value) return { error: 'document could not be found' } | |
return { document: userFromDB.value } | |
} catch (e) { | |
return { error: 'e' } | |
} | |
} | |
static async setCurrentRoomToUser( | |
user_email: string, | |
chat_room: string | |
): Promise<ReturnDAO> { | |
try { | |
const { value, lastErrorObject } = await users.findOneAndUpdate( | |
{ email: user_email }, | |
{ $set: { room: chat_room } }, | |
{ returnOriginal: false } | |
) | |
if (!lastErrorObject.updatedExisting && !value) { | |
return { error: 'dont update the document' } | |
} | |
const userFromDB = await aggregateRooms(users, user_email) | |
return { document: userFromDB } | |
} catch (e) { | |
return { error: e } | |
} | |
} | |
static async setSocketIDToUser( | |
username: string, | |
socket_id: string | |
): Promise<ResultUserDAO> { | |
const result = await users.findOneAndUpdate( | |
{ name: username }, | |
{ $set: { socket_id: socket_id } }, | |
{ returnOriginal: false } | |
) | |
if (!result.value || !result.lastErrorObject.updatedExisting) { | |
return { error: `the username ${username} is not exists` } | |
} | |
return { document: result.value } | |
} | |
} | |
export default UsersDAO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment