From d65aa2d7f080919ec8351366697f1772c80e3417 Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Wed, 21 Oct 2026 19:47:57 +0000 Subject: [PATCH 1/5] Unwind stub/nostub saving logic --- server/homebrew.api.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/server/homebrew.api.js b/server/homebrew.api.js index 2fd81e4f2..8f18543dc 100644 --- a/server/homebrew.api.js +++ b/server/homebrew.api.js @@ -447,16 +447,14 @@ const api = { console.error(err); res.status(err.status || 500).send(err.message || 'Unable to save brew to Homebrewery database'); }; - 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); - } else { - // if the brew does have a stub id, update it using the stub id as the key. + + if(brew._id) // if the brew has a stub id, fetch and merge into Mongo stub document brew = _.assign(await HomebrewModel.findOne({ _id: brew._id }), brew); - saved = await brew.save() - .catch(saveError); - } + else // if not, create a new Mongo document + brew = new HomebrewModel(brew); + + let saved = await brew.save().catch(saveError); + if(!saved) return; // Call and wait for afterSave to complete const after = await afterSave(); From e1ddac766ff798711a5f9fd956eb6a75869bb0d2 Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Wed, 21 Oct 2026 20:07:45 +0000 Subject: [PATCH 2/5] Unwind google deletion logic --- server/homebrew.api.js | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/server/homebrew.api.js b/server/homebrew.api.js index 8f18543dc..77a2dff6d 100644 --- a/server/homebrew.api.js +++ b/server/homebrew.api.js @@ -398,20 +398,12 @@ const api = { brew.description = brew.description.trim() || ''; brew.text = api.mergeBrewText(brew); + //=====----- Update to or remove from Google Drive -----=========// const googleId = brew.googleId; const { saveToGoogle, removeFromGoogle } = req.query; - let afterSave = async ()=>true; if(brew.googleId && removeFromGoogle) { - // If the google id exists and we're removing it from google, set afterSave to delete the google brew and mark the brew's google id as undefined - afterSave = async ()=>{ - return await api.deleteGoogleBrew(req.account, googleId, brew.editId, res) - .catch((err)=>{ - console.error(err); - res.status(err?.status || err?.response?.status || 500).send(err.message || err); - }); - }; - + // If the google id exists and we're removing it from google, mark the brew's google id as undefined brew.googleId = undefined; } else if(!brew.googleId && saveToGoogle) { // If we don't have a google id and the user wants to save to google, create the google brew and set the google id on the brew @@ -425,8 +417,9 @@ const api = { if(!updated) return; } + //=====----- Update to Mongo -----=====// if(brew.googleId) { - // If the google id exists after all those actions, exclude the props that are stored in google and aren't needed for rendering the brew items + // If google id exists after above actions, Mongo will store only a stub; exclude non-stub properties from update brew = api.excludeStubProps(brew); } else { // Compress brew text to binary before saving @@ -454,11 +447,17 @@ const api = { brew = new HomebrewModel(brew); let saved = await brew.save().catch(saveError); - if(!saved) return; - // Call and wait for afterSave to complete - const after = await afterSave(); - if(!after) return; + + if(googleId && removeFromGoogle) { + const deleted = await api.deleteGoogleBrew(req.account, googleId, brew.editId, res) + .catch((err)=>{ + console.error(err); + res.status(err?.status || err?.response?.status || 500).send(err.message || err); + }); + + if(!deleted) return; + } saved.textBin = undefined; // Remove textBin from the saved object to save bandwidth From a8312ba3ff7f50881a0030db41307c9509f19b52 Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Wed, 21 Oct 2026 20:08:41 +0000 Subject: [PATCH 3/5] Remove redundant excludeGoogleProps from newGoogleBrew call --- server/homebrew.api.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/homebrew.api.js b/server/homebrew.api.js index 77a2dff6d..5715378ae 100644 --- a/server/homebrew.api.js +++ b/server/homebrew.api.js @@ -407,7 +407,7 @@ const api = { brew.googleId = undefined; } else if(!brew.googleId && saveToGoogle) { // If we don't have a google id and the user wants to save to google, create the google brew and set the google id on the brew - brew.googleId = await api.newGoogleBrew(req.account, api.excludeGoogleProps(brew), res); + brew.googleId = await api.newGoogleBrew(req.account, brew, res); if(!brew.googleId) return; } else if(brew.googleId) { From 7bd6e7b7ab998391b8535389528a3603e957bc68 Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Wed, 21 Oct 2026 20:24:33 +0000 Subject: [PATCH 4/5] Group brew-normalizing steps together --- server/homebrew.api.js | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/server/homebrew.api.js b/server/homebrew.api.js index 5715378ae..53f3c6e11 100644 --- a/server/homebrew.api.js +++ b/server/homebrew.api.js @@ -397,23 +397,26 @@ const api = { brew.title = brew.title.trim(); brew.description = brew.description.trim() || ''; brew.text = api.mergeBrewText(brew); + brew.updatedAt = new Date(); + brew.version = (brew.version || 1) + 1; + if(req.account) { + brew.authors = _.uniq(_.concat(brew.authors, req.account.username)); + brew.invitedAuthors = _.uniq(_.filter(brew.invitedAuthors, (a)=>req.account.username !== a)); + } //=====----- Update to or remove from Google Drive -----=========// const googleId = brew.googleId; const { saveToGoogle, removeFromGoogle } = req.query; - if(brew.googleId && removeFromGoogle) { - // If the google id exists and we're removing it from google, mark the brew's google id as undefined + if(brew.googleId && removeFromGoogle) { // If removing from google, remove the brew's googleId brew.googleId = undefined; - } else if(!brew.googleId && saveToGoogle) { - // If we don't have a google id and the user wants to save to google, create the google brew and set the google id on the brew + } + else if(!brew.googleId && saveToGoogle) { // If adding to google, create new google document and record the googleId brew.googleId = await api.newGoogleBrew(req.account, brew, res); - if(!brew.googleId) return; - } else if(brew.googleId) { - // If the google id exists and no other actions are being performed, update the google brew + } + else if(brew.googleId) { // If already on google, update the google brew const updated = await GoogleActions.updateGoogleBrew(api.excludeGoogleProps(brew), req.ip); - if(!updated) return; } @@ -427,13 +430,6 @@ const api = { // Delete the non-binary text field since it's not needed anymore brew.text = undefined; } - brew.updatedAt = new Date(); - brew.version = (brew.version || 1) + 1; - - if(req.account) { - brew.authors = _.uniq(_.concat(brew.authors, req.account.username)); - brew.invitedAuthors = _.uniq(_.filter(brew.invitedAuthors, (a)=>req.account.username !== a)); - } // define a function to catch our save errors const saveError = (err)=>{ From a430a5c3ef9dad0c2a7f9de935764ff136973636 Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Wed, 21 Oct 2026 20:40:04 +0000 Subject: [PATCH 5/5] Inline the "saveError" function --- server/homebrew.api.js | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/server/homebrew.api.js b/server/homebrew.api.js index 53f3c6e11..6bcd293b0 100644 --- a/server/homebrew.api.js +++ b/server/homebrew.api.js @@ -223,7 +223,7 @@ const api = { return _.omit(brew, ['_id', 'views', 'lastViewed']); }, excludeGoogleProps : (brew)=>{ //Remove props that should not be saved to Google (only stored to Homebrewery stub) - return _.omit(brew, ['version', 'tags', 'systems', 'published', 'authors', 'owner', 'views', 'thumbnail']); + return _.omit(brew, ['version', 'tags', 'systems', 'published', 'authors', 'invitedAuthors', 'owner', 'views', 'thumbnail']); }, excludeStubProps : (brew)=>{ //Remove props that should not be saved to stub-only brews brew.text = brew.textBin = undefined; // Can't use _.omit because this brew is a Mongoose object @@ -345,7 +345,6 @@ const api = { return res.status(200).send(returnObj); }, updateBrew : async (req, res)=>{ - // Initialize brew from request and body, destructure query params, and set the initial value for the after-save method const brewFromClient = api.excludePropsFromUpdate(req.body); const brewFromServer = req.brew; @@ -405,7 +404,7 @@ const api = { } //=====----- Update to or remove from Google Drive -----=========// - const googleId = brew.googleId; + const originalGoogleId = brew.googleId; const { saveToGoogle, removeFromGoogle } = req.query; if(brew.googleId && removeFromGoogle) { // If removing from google, remove the brew's googleId @@ -431,22 +430,21 @@ const api = { brew.text = undefined; } - // define a function to catch our save errors - const saveError = (err)=>{ - console.error(err); - res.status(err.status || 500).send(err.message || 'Unable to save brew to Homebrewery database'); - }; - if(brew._id) // if the brew has a stub id, fetch and merge into Mongo stub document brew = _.assign(await HomebrewModel.findOne({ _id: brew._id }), brew); else // if not, create a new Mongo document brew = new HomebrewModel(brew); - let saved = await brew.save().catch(saveError); + let 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(googleId && removeFromGoogle) { - const deleted = await api.deleteGoogleBrew(req.account, googleId, brew.editId, res) + if(originalGoogleId && removeFromGoogle) { + const deleted = await api.deleteGoogleBrew(req.account, originalGoogleId, brew.editId, res) .catch((err)=>{ console.error(err); res.status(err?.status || err?.response?.status || 500).send(err.message || err);