Files
EMI-Backend/mongoDB.js
T
2023-05-31 06:11:50 -04:00

127 lines
4.3 KiB
JavaScript

const mongo = require('mongodb');
const MongoClient = mongo.MongoClient;
const ObjectID = mongo.ObjectID;
const DBName = "EMI_SOCIAL";
const mongoUrl = process.env.MONGO_URL;
const postDB = require("./dbTools/post.js");
const profileDB = require("./dbTools/profile.js");
const paymentDB = require("./dbTools/payments.js");
const loggerDB = require("./dbTools/logger.js");
const songsDB = require("./dbTools/songs.js");
const getDB = new Promise((resolve, reject) => {
const DB = {ObjectID: mongo.ObjectID};
MongoClient.connect(mongoUrl, function(err, db) {
if (err) return reject(err);
DB.db = db;
DB.ObjectID = ObjectID;
console.log("Connected to DB!");
DB.usersCol = db.db(DBName).collection("users");
DB.tokensCol = db.db(DBName).collection("tokens");
DB.invitationCol = db.db(DBName).collection("invitation");
DB.checkSessionOnDB = async (session_id, user_sid)=>{
const temp_id = new mongo.ObjectID(session_id);
//We can add cache, like a temporary Redis entry, to avoid calling the DB on each call
const doc = await DB.tokensCol.findOne({"_id":temp_id});
if(doc && doc.uid == user_sid){
const userMongoId = new mongo.ObjectID(user_sid);
const userInfo = await DB.usersCol.findOne({"_id": userMongoId}, {fields: {password: 0}});
return userInfo;
}
return false;
};
DB.getUser = (username)=>{
return DB.usersCol.findOne({ username: username });
}
DB.getAllEmails = () => {
// Change this to paginations chunks
return DB.usersCol.find().project({username: 1}).toArray();
};
DB.resetUserPassword = (username, password)=>{
return DB.usersCol.updateOne({username}, {$set:{password}})
.catch(console.error);
}
DB.setUserCustomerId = (username, customer)=>{
return DB.usersCol.updateOne({username}, {$set:{customer}})
.catch(console.error);
}
DB.getUserById = (userid)=>{
const _id = new mongo.ObjectID(userid);
return DB.usersCol.findOne({ _id });
}
let usernamesCache = {}
DB.getUsernameByIdCache = async (userid)=>{
if(!userid) return {};
if(usernamesCache[userid]) return usernamesCache[userid];
const _id = new mongo.ObjectID(userid);
let user = await DB.usersCol.findOne({ _id });
if(!user) return '';
usernamesCache[userid] = user.username;
return usernamesCache[userid];
}
DB.newInvitation = (userid, name, email)=>{
const invitation = {
responsable: new mongo.ObjectID(userid),
email,
name,
ts: new Date()
}
return DB.invitationCol.insertOne(invitation).catch((err)=>{
if (err.name === 'MongoError' && err.code === 11000) {
// Duplicate invite email
return "This email already has been invited";
}
console.log(err);
return "System Error";
});
};
DB.getInvitation = (email)=>{
return DB.invitationCol.findOne({email}).catch((err)=>{
console.log(err);
return "System Error";
});
};
DB.newUser = (userInformation)=>{
return DB.usersCol.insertOne(userInformation).catch((err)=>{
if (err.name === 'MongoError' && err.code === 11000) {
// Duplicate username
return "User already exists";
}
console.log(err);
return "System Error";
});
};
DB.newSession = (user_id)=>{
return DB.tokensCol.insertOne({uid: user_id});
}
DB.removeSession = (session_id)=>{
const temp_id = new mongo.ObjectID(session_id);
return DB.tokensCol.deleteOne({"_id":temp_id});
}
postDB(DB);
profileDB(DB);
paymentDB(DB);
loggerDB(DB);
songsDB(DB);
resolve(DB);
});
});
exports.getDB = getDB;