Files
EMI-Backend/dbTools/profile.js
Adolfo Reyna 9f5f23364b groups working
2021-08-19 10:19:10 -07:00

72 lines
2.0 KiB
JavaScript

const DBName = "EMI_SOCIAL";
let userProfileCache = {};
userDB = (DB) => {
DB.profileCols = DB.db.db(DBName).collection("profiles");
DB.newProfile = (profileObj) => {
return DB.profileCols.insertOne(profileObj.toObj()).catch((err) => {
console.log(err);
return false;
});
}
DB.getProfile = async (profileId) => {
if (userProfileCache[profileId]) return userProfileCache[profileId];
const _id = DB.ObjectID(profileId);
let r = await DB.profileCols.findOne({ _id }).catch((err) => {
console.log(err);
return false;
});
if (r) userProfileCache[profileId] = r;
return r;
}
DB.getUserProfiles = async (userId) => {
const userid = DB.ObjectID(userId);
return await DB.profileCols.find({ userid }).toArray().catch((err) => {
console.log(err);
return false;
});
}
DB.latestProfile = async (userId) => {
const userid = DB.ObjectID(userId);
let r = await DB.profileCols.find({ userid })
.sort({ lastUpdate: -1 }).limit(1)
.toArray().catch((err) => {
console.log(err);
return false;
});
if (r[0]) userProfileCache[r[0].id] = r[0];
return r[0];
}
//Groups
DB.getGroups = async () => {
let r = await DB.profileCols.find({isGroup: true})
.sort({ lastUpdate: -1 }).limit(10)
.toArray().catch((err) => {
console.log(err);
return false;
});
return r;
}
DB.getGroup = async (groupid) => {
const _id = DB.ObjectID(groupid);
if(userProfileCache[groupid]) return userProfileCache[groupid];
let r = await DB.profileCols.findOne({_id, isGroup: true})
.toArray().catch((err) => {
console.log(err);
return false;
});
if (r) userProfileCache[r.id] = r;
return r;
}
}
module.exports = userDB;