mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-05 12:22:44 +00:00
Merge pull request #2470 from jeddai/remove-google-get-during-update
update getBrew usages to not fetch google brew during updates
This commit is contained in:
@@ -253,7 +253,6 @@ const GoogleActions = {
|
|||||||
text : file.data,
|
text : file.data,
|
||||||
|
|
||||||
description : obj.data.description,
|
description : obj.data.description,
|
||||||
tags : obj.data.properties.tags ? obj.data.properties.tags : '',
|
|
||||||
systems : obj.data.properties.systems ? obj.data.properties.systems.split(',') : [],
|
systems : obj.data.properties.systems ? obj.data.properties.systems.split(',') : [],
|
||||||
authors : [],
|
authors : [],
|
||||||
published : obj.data.properties.published ? obj.data.properties.published == 'true' : false,
|
published : obj.data.properties.published ? obj.data.properties.published == 'true' : false,
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ const getId = (req)=>{
|
|||||||
return { id, googleId };
|
return { id, googleId };
|
||||||
};
|
};
|
||||||
|
|
||||||
const getBrew = (accessType)=>{
|
const getBrew = (accessType, stubOnly = false)=>{
|
||||||
// Create middleware with the accessType passed in as part of the scope
|
// Create middleware with the accessType passed in as part of the scope
|
||||||
return async (req, res, next)=>{
|
return async (req, res, next)=>{
|
||||||
// Get relevant IDs for the brew
|
// Get relevant IDs for the brew
|
||||||
@@ -45,7 +45,7 @@ const getBrew = (accessType)=>{
|
|||||||
stub = stub?.toObject();
|
stub = stub?.toObject();
|
||||||
|
|
||||||
// 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(googleId || stub?.googleId) {
|
if(!stubOnly && (googleId || stub?.googleId)) {
|
||||||
let googleError;
|
let googleError;
|
||||||
const googleBrew = await GoogleActions.getGoogleBrew(googleId || stub?.googleId, id, accessType)
|
const googleBrew = await GoogleActions.getGoogleBrew(googleId || stub?.googleId, id, accessType)
|
||||||
.catch((err)=>{
|
.catch((err)=>{
|
||||||
@@ -59,14 +59,14 @@ const getBrew = (accessType)=>{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 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 && !stubOnly) {
|
||||||
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();
|
||||||
};
|
};
|
||||||
@@ -235,23 +235,22 @@ const updateBrew = async (req, res)=>{
|
|||||||
brew.authors = _.uniq(_.concat(brew.authors, req.account.username));
|
brew.authors = _.uniq(_.concat(brew.authors, req.account.username));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch the brew from the database again (if it existed there to begin with), and assign the existing brew to it
|
// define a function to catch our save errors
|
||||||
brew = _.assign(await HomebrewModel.findOne({ _id: brew._id }), brew);
|
const saveError = (err)=>{
|
||||||
|
console.error(err);
|
||||||
if(!brew.markModified) {
|
res.status(err.status || 500).send(err.message || 'Unable to save brew to Homebrewery database');
|
||||||
// If it wasn't in the database, create a new db brew
|
};
|
||||||
brew = new HomebrewModel(brew);
|
let saved;
|
||||||
|
if(!brew._id) {
|
||||||
|
// 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.findOneAndUpdate({ _id: brew._id }, brew, {
|
||||||
|
returnOriginal : false
|
||||||
|
}).catch(saveError);
|
||||||
}
|
}
|
||||||
|
|
||||||
brew.markModified('authors');
|
|
||||||
brew.markModified('systems');
|
|
||||||
|
|
||||||
// Save the database brew
|
|
||||||
const saved = await brew.save()
|
|
||||||
.catch((err)=>{
|
|
||||||
console.error(err);
|
|
||||||
res.status(err.status || 500).send(err.message || 'Unable to save brew to Homebrewery database');
|
|
||||||
});
|
|
||||||
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();
|
||||||
@@ -327,8 +326,8 @@ const deleteBrew = async (req, res, next)=>{
|
|||||||
};
|
};
|
||||||
|
|
||||||
router.post('/api', asyncHandler(newBrew));
|
router.post('/api', asyncHandler(newBrew));
|
||||||
router.put('/api/:id', asyncHandler(getBrew('edit')), asyncHandler(updateBrew));
|
router.put('/api/:id', asyncHandler(getBrew('edit', true)), asyncHandler(updateBrew));
|
||||||
router.put('/api/update/:id', asyncHandler(getBrew('edit')), asyncHandler(updateBrew));
|
router.put('/api/update/:id', asyncHandler(getBrew('edit', true)), asyncHandler(updateBrew));
|
||||||
router.delete('/api/:id', asyncHandler(deleteBrew));
|
router.delete('/api/:id', asyncHandler(deleteBrew));
|
||||||
router.get('/api/remove/:id', asyncHandler(deleteBrew));
|
router.get('/api/remove/:id', asyncHandler(deleteBrew));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user