mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-04 19:02:38 +00:00
fix deletion
This commit is contained in:
@@ -377,30 +377,43 @@ app.put('/api/user/rename', async (req, res)=>{
|
|||||||
});
|
});
|
||||||
|
|
||||||
//Delete brews based on author
|
//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;
|
const { username } = req.body;
|
||||||
|
console.log(username);
|
||||||
const ownAccount = req.account && (req.account.username == username);
|
const ownAccount = req.account && (req.account.username == username);
|
||||||
|
|
||||||
if(!username)
|
// if(!ownAccount) return res.status(403).json({ error: 'Must be logged in to change your 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' });
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const brews = await HomebrewModel.getByUser(username, true, ['authors']);
|
const brews = await HomebrewModel.getByUser(username, true, ['authors']);
|
||||||
await brews.forEach((brew)=>{
|
console.log(brews);
|
||||||
request.delete(`/api/${brew.googleId ?? ''}${brew.editId}`).send().end((err, res)=>{
|
|
||||||
if (err) reportError(err);
|
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) {
|
} catch (error) {
|
||||||
console.error('Error renaming brews:', error);
|
console.error('Error deleting brews:', error);
|
||||||
return res.status(500).json({ error: 'Failed to rename brews.' });
|
if (!res.headersSent) {
|
||||||
|
return res.status(500).json({ error: 'Failed to delete the brews.' });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
//Edit Page
|
//Edit Page
|
||||||
app.get('/edit/:id', asyncHandler(getBrew('edit')), asyncHandler(async(req, res, next)=>{
|
app.get('/edit/:id', asyncHandler(getBrew('edit')), asyncHandler(async(req, res, next)=>{
|
||||||
req.brew = req.brew.toObject ? req.brew.toObject() : req.brew;
|
req.brew = req.brew.toObject ? req.brew.toObject() : req.brew;
|
||||||
|
|||||||
Reference in New Issue
Block a user