Fix profile update persistence and stale cache
This commit is contained in:
@@ -23,7 +23,9 @@ userDB = (DB) => {
|
|||||||
|
|
||||||
DB.updateProfile = async (profileid, profileObj) => {
|
DB.updateProfile = async (profileid, profileObj) => {
|
||||||
let tempProfile = profileObj.toObj();
|
let tempProfile = profileObj.toObj();
|
||||||
const query = { _id: profileid };
|
if (!DB.ObjectID.isValid(profileid)) return false;
|
||||||
|
const _id = DB.ObjectID(profileid);
|
||||||
|
const query = { _id };
|
||||||
const update = {
|
const update = {
|
||||||
$set: {
|
$set: {
|
||||||
profile: tempProfile.profile,
|
profile: tempProfile.profile,
|
||||||
@@ -34,6 +36,7 @@ userDB = (DB) => {
|
|||||||
console.log(err);
|
console.log(err);
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
if (userProfileCache[profileid]) delete userProfileCache[profileid];
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -760,16 +760,28 @@ DB.getDB.then((DB) => {
|
|||||||
* type: string
|
* type: string
|
||||||
*/
|
*/
|
||||||
router.post("/myProfile", async (req, res) => {
|
router.post("/myProfile", async (req, res) => {
|
||||||
|
try {
|
||||||
let profile = {
|
let profile = {
|
||||||
userid: getUserId(req),
|
userid: getUserId(req),
|
||||||
profile: req.body.profile,
|
profile: req.body.profile,
|
||||||
data: req.body.data
|
data: req.body.data
|
||||||
};
|
};
|
||||||
let profileObj = new Profile(profile); //validates profile
|
let profileObj = new Profile(profile); //validates profile
|
||||||
DB.updateProfile(getProfileId(req), profileObj);
|
const updateRes = await DB.updateProfile(getProfileId(req), profileObj);
|
||||||
|
if (!updateRes || !updateRes.matchedCount) {
|
||||||
|
return res.status(400).json({
|
||||||
|
status: "Could not update profile"
|
||||||
|
});
|
||||||
|
}
|
||||||
return res.json({
|
return res.json({
|
||||||
status: "ok"
|
status: "ok"
|
||||||
});
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error updating myProfile", error);
|
||||||
|
return res.status(500).json({
|
||||||
|
status: "Internal server error"
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user