From af1821e69798e8dba9b8ec3947ae4455711ea1e2 Mon Sep 17 00:00:00 2001 From: Charlie Humphreys Date: Tue, 6 Sep 2022 22:50:51 -0500 Subject: [PATCH 1/4] update deletion to delete brews when the google brew cannot be found --- server/homebrew.api.js | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/server/homebrew.api.js b/server/homebrew.api.js index 924e728db..51f6f8f42 100644 --- a/server/homebrew.api.js +++ b/server/homebrew.api.js @@ -15,17 +15,24 @@ const { nanoid } = require('nanoid'); // }); // }; +const getId = (req)=>{ + // Set the id and initial potential google id, where the google id is present on the existing brew. + let id = req.params.id, googleId = req.body?.googleId; + + // If the id is longer than 12, then it's a google id + the edit id. This splits the longer id up. + if(id.length > 12) { + googleId = id.slice(0, -12); + id = id.slice(-12); + } + return { id, googleId }; +}; + const getBrew = (accessType)=>{ // Create middleware with the accessType passed in as part of the scope return async (req, res, next)=>{ - // Set the id and initial potential google id, where the google id is present on the existing brew. - let id = req.params.id, googleId = req.body?.googleId; + // Get relevant IDs for the brew + const { id, googleId } = getId(req); - // If the id is longer than 12, then it's a google id + the edit id. This splits the longer id up. - if(id.length > 12) { - googleId = id.slice(0, -12); - id = id.slice(-12); - } // Try to find the document in the Homebrewery database -- if it doesn't exist, that's fine. let stub = await HomebrewModel.get(accessType === 'edit' ? { editId: id } : { shareId: id }) .catch((err)=>{ @@ -259,7 +266,16 @@ const deleteGoogleBrew = async (account, id, editId, res)=>{ return true; }; -const deleteBrew = async (req, res)=>{ +const deleteBrew = async (req, res, next)=>{ + try { + req.brew = await getBrew('edit')(req, res, next); + } catch (e) { + const { id, googleId } = getId(req); + console.warn(`No google brew found for id ${googleId}, the stub will be deleted.`); + await HomebrewModel.deleteOne({ editId: id }); + return next(); + } + let brew = req.brew; const { googleId, editId } = brew; const account = req.account; @@ -312,8 +328,8 @@ const deleteBrew = async (req, res)=>{ router.post('/api', asyncHandler(newBrew)); router.put('/api/:id', asyncHandler(getBrew('edit')), asyncHandler(updateBrew)); router.put('/api/update/:id', asyncHandler(getBrew('edit')), asyncHandler(updateBrew)); -router.delete('/api/:id', asyncHandler(getBrew('edit')), asyncHandler(deleteBrew)); -router.get('/api/remove/:id', asyncHandler(getBrew('edit')), asyncHandler(deleteBrew)); +router.delete('/api/:id', asyncHandler(deleteBrew)); +router.get('/api/remove/:id', asyncHandler(deleteBrew)); module.exports = { homebrewApi : router, From a234fdbab72dae0cc00c384948b7ca291813db67 Mon Sep 17 00:00:00 2001 From: Charlie Humphreys Date: Thu, 8 Sep 2022 08:15:55 -0500 Subject: [PATCH 2/4] fix getBrew issue --- server/homebrew.api.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/homebrew.api.js b/server/homebrew.api.js index 51f6f8f42..b13c2bb88 100644 --- a/server/homebrew.api.js +++ b/server/homebrew.api.js @@ -268,8 +268,8 @@ const deleteGoogleBrew = async (account, id, editId, res)=>{ const deleteBrew = async (req, res, next)=>{ try { - req.brew = await getBrew('edit')(req, res, next); - } catch (e) { + await getBrew('edit')(req, res, next); + } catch (err) { const { id, googleId } = getId(req); console.warn(`No google brew found for id ${googleId}, the stub will be deleted.`); await HomebrewModel.deleteOne({ editId: id }); From 0625c5782426466250b106bae4049e6ac6333f30 Mon Sep 17 00:00:00 2001 From: Charlie Humphreys Date: Thu, 8 Sep 2022 08:16:49 -0500 Subject: [PATCH 3/4] override next with an empty function --- 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 b13c2bb88..9e7578cda 100644 --- a/server/homebrew.api.js +++ b/server/homebrew.api.js @@ -268,7 +268,7 @@ const deleteGoogleBrew = async (account, id, editId, res)=>{ const deleteBrew = async (req, res, next)=>{ try { - await getBrew('edit')(req, res, next); + await getBrew('edit')(req, res, ()=>{}); } catch (err) { const { id, googleId } = getId(req); console.warn(`No google brew found for id ${googleId}, the stub will be deleted.`); From 3c12f1133eea84fc353c114f870ce1b6aee8f873 Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Thu, 8 Sep 2022 22:38:00 -0400 Subject: [PATCH 4/4] Tweak console warning --- server/homebrew.api.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/server/homebrew.api.js b/server/homebrew.api.js index 9e7578cda..52d8c6c37 100644 --- a/server/homebrew.api.js +++ b/server/homebrew.api.js @@ -267,11 +267,12 @@ const deleteGoogleBrew = async (account, id, editId, res)=>{ }; const deleteBrew = async (req, res, next)=>{ + // Delete an orphaned stub if its Google brew doesn't exist try { await getBrew('edit')(req, res, ()=>{}); } catch (err) { const { id, googleId } = getId(req); - console.warn(`No google brew found for id ${googleId}, the stub will be deleted.`); + console.warn(`No google brew found for id ${googleId}, the stub with id ${id} will be deleted.`); await HomebrewModel.deleteOne({ editId: id }); return next(); }