0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-25 22:42:41 +00:00

fix deletion

This commit is contained in:
Víctor Losada Hernández
2025-01-11 23:03:26 +01:00
parent 2d3ebfd6bf
commit e0d4eafe17

View File

@@ -377,30 +377,43 @@ app.put('/api/user/rename', async (req, res)=>{
});
//Delete brews based on author
app.delete('/api/user/delete', async (req, res)=>{
app.delete('/api/user/delete', async (req, res) => {
const { username } = req.body;
console.log(username);
const ownAccount = req.account && (req.account.username == username);
if(!username)
return res.status(400).json({ error: 'Username and newUsername are required.' });
if(!ownAccount)
return res.status(403).json({ error: 'Must be logged in to change your username' });
// if(!ownAccount) return res.status(403).json({ error: 'Must be logged in to change your username' });
try {
const brews = await HomebrewModel.getByUser(username, true, ['authors']);
await brews.forEach((brew)=>{
request.delete(`/api/${brew.googleId ?? ''}${brew.editId}`).send().end((err, res)=>{
if (err) reportError(err);
});
});
console.log(brews);
for (let brew of brews) {
//attaching the brew to the request for the deleteBrew method to work
req.brew = brew;
await new Promise((resolve, reject) => {
api.deleteBrew(req, res, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
console.log('all brews should be deleted');
return res.json({ success: true, message: `All brews for ${username} have been deleted.` });
return res.json({ success: true, message: `Brews for ${username} renamed to ${newUsername}.` });
} catch (error) {
console.error('Error renaming brews:', error);
return res.status(500).json({ error: 'Failed to rename brews.' });
console.error('Error deleting brews:', error);
if (!res.headersSent) {
return res.status(500).json({ error: 'Failed to delete the brews.' });
}
}
});
//Edit Page
app.get('/edit/:id', asyncHandler(getBrew('edit')), asyncHandler(async(req, res, next)=>{
req.brew = req.brew.toObject ? req.brew.toObject() : req.brew;