0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-10 13:32:45 +00:00

update deletion to delete brews when the google brew cannot be found

This commit is contained in:
Charlie Humphreys
2022-09-06 22:50:51 -05:00
parent 746e8f8a6a
commit af1821e697

View File

@@ -15,9 +15,7 @@ const { nanoid } = require('nanoid');
// }); // });
// }; // };
const getBrew = (accessType)=>{ const getId = (req)=>{
// 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. // 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; let id = req.params.id, googleId = req.body?.googleId;
@@ -26,6 +24,15 @@ const getBrew = (accessType)=>{
googleId = id.slice(0, -12); googleId = id.slice(0, -12);
id = id.slice(-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)=>{
// Get relevant IDs for the brew
const { id, googleId } = getId(req);
// Try to find the document in the Homebrewery database -- if it doesn't exist, that's fine. // 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 }) let stub = await HomebrewModel.get(accessType === 'edit' ? { editId: id } : { shareId: id })
.catch((err)=>{ .catch((err)=>{
@@ -259,7 +266,16 @@ const deleteGoogleBrew = async (account, id, editId, res)=>{
return true; 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; let brew = req.brew;
const { googleId, editId } = brew; const { googleId, editId } = brew;
const account = req.account; const account = req.account;
@@ -312,8 +328,8 @@ const deleteBrew = async (req, res)=>{
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')), asyncHandler(updateBrew));
router.put('/api/update/: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.delete('/api/:id', asyncHandler(deleteBrew));
router.get('/api/remove/:id', asyncHandler(getBrew('edit')), asyncHandler(deleteBrew)); router.get('/api/remove/:id', asyncHandler(deleteBrew));
module.exports = { module.exports = {
homebrewApi : router, homebrewApi : router,