mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-09 15:52:43 +00:00
adjust getBrew to check authorship, change update method to not perform a get
This commit is contained in:
@@ -43,6 +43,9 @@ const getBrew = (accessType, fetchGoogle = true)=>{
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
stub = stub?.toObject();
|
stub = stub?.toObject();
|
||||||
|
if(stub?.authors && !stub?.authors.includes(req.account.username)) {
|
||||||
|
throw 'Current logged in user does not have access to this brew.';
|
||||||
|
}
|
||||||
|
|
||||||
// If there is a google id, try to find the google brew
|
// If there is a google id, try to find the google brew
|
||||||
if(fetchGoogle && (googleId || stub?.googleId)) {
|
if(fetchGoogle && (googleId || stub?.googleId)) {
|
||||||
@@ -59,14 +62,14 @@ const getBrew = (accessType, fetchGoogle = true)=>{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If after all of that we still don't have a brew, throw an exception
|
// If after all of that we still don't have a brew, throw an exception
|
||||||
if(!stub) {
|
if(!stub && fetchGoogle) {
|
||||||
throw 'Brew not found in Homebrewery database or Google Drive';
|
throw 'Brew not found in Homebrewery database or Google Drive';
|
||||||
}
|
}
|
||||||
|
|
||||||
if(typeof stub.tags === 'string') {
|
if(typeof stub?.tags === 'string') {
|
||||||
stub.tags = [];
|
stub.tags = [];
|
||||||
}
|
}
|
||||||
req.brew = stub;
|
req.brew = stub || {};
|
||||||
|
|
||||||
next();
|
next();
|
||||||
};
|
};
|
||||||
@@ -234,30 +237,31 @@ const updateBrew = async (req, res)=>{
|
|||||||
if(req.account) {
|
if(req.account) {
|
||||||
brew.authors = _.uniq(_.concat(brew.authors, req.account.username));
|
brew.authors = _.uniq(_.concat(brew.authors, req.account.username));
|
||||||
}
|
}
|
||||||
|
// we need the tag type change in both getBrew and here to handle the case where we don't have a stub on which to perform the modification
|
||||||
// Fetch the brew from the database again (if it existed there to begin with), and assign the existing brew to it
|
if(typeof brew.tags === 'string') {
|
||||||
brew = _.assign(await HomebrewModel.findOne({ _id: brew._id }), brew);
|
brew.tags = [];
|
||||||
|
|
||||||
if(!brew.markModified) {
|
|
||||||
// If it wasn't in the database, create a new db brew
|
|
||||||
brew = new HomebrewModel(brew);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
brew.markModified('authors');
|
// define a function to catch our save errors
|
||||||
brew.markModified('systems');
|
const saveError = (err)=>{
|
||||||
|
console.error(err);
|
||||||
// Save the database brew
|
res.status(err.status || 500).send(err.message || 'Unable to save brew to Homebrewery database');
|
||||||
const saved = await brew.save()
|
};
|
||||||
.catch((err)=>{
|
let saved;
|
||||||
console.error(err);
|
if(!brew._id) {
|
||||||
res.status(err.status || 500).send(err.message || 'Unable to save brew to Homebrewery database');
|
// if the brew does not have a stub id, create and save it, then write the new value back to the brew.
|
||||||
});
|
saved = await new HomebrewModel(brew).save().catch(saveError);
|
||||||
|
brew = saved.toObject();
|
||||||
|
} else {
|
||||||
|
// if the brew does have a stub id, update it using the stub id as the key.
|
||||||
|
saved = await HomebrewModel.updateOne({ _id: brew._id }, brew).catch(saveError);
|
||||||
|
}
|
||||||
if(!saved) return;
|
if(!saved) return;
|
||||||
// Call and wait for afterSave to complete
|
// Call and wait for afterSave to complete
|
||||||
const after = await afterSave();
|
const after = await afterSave();
|
||||||
if(!after) return;
|
if(!after) return;
|
||||||
|
|
||||||
res.status(200).send(saved);
|
res.status(200).send(brew);
|
||||||
};
|
};
|
||||||
|
|
||||||
const deleteGoogleBrew = async (account, id, editId, res)=>{
|
const deleteGoogleBrew = async (account, id, editId, res)=>{
|
||||||
|
|||||||
Reference in New Issue
Block a user