working on users having multiple profiles

This commit is contained in:
Adolfo Reyna
2021-08-17 11:28:45 -07:00
parent f909233533
commit 416c14d03b
9 changed files with 84 additions and 48 deletions

49
dbTools/profile.js Normal file
View File

@@ -0,0 +1,49 @@
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.getProfiles = 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];
}
}
module.exports = userDB;