0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-25 20:33:51 +00:00
This commit is contained in:
Víctor Losada Hernández
2025-04-19 20:30:47 +02:00
parent a1b8a85583
commit 7c100e608d

View File

@@ -36,7 +36,6 @@ import bodyParser from 'body-parser';
import cookieParser from 'cookie-parser';
import forceSSL from './forcessl.mw.js';
const sanitizeBrew = (brew, accessType)=>{
brew._id = undefined;
brew.__v = undefined;
@@ -142,6 +141,7 @@ app.get('/', (req, res, next)=>{
title : 'Homepage',
description : 'Homepage'
};
console.log(req.account);
splitTextStyleAndMetadata(req.brew);
return next();
@@ -374,44 +374,38 @@ 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(!ownAccount) return res.status(403).json({ error: 'Must be logged in to change your username' });
console.log('username: ', username);
if(!username) return res.status(400).json({ error: 'Username is required.' });
//const ownAccount = req.account && (req.account.username == 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']);
//get the relevant fields, not just author you moron!
console.log(brews);
for (let brew of brews) {
//attaching the brew to the request for the deleteBrew method to work
const brews = await HomebrewModel.getByUser(username, true, ['_id', 'googleId', 'editId', 'authors']);
console.log('brews: ', brews);
const deletePromises = brews.map((brew)=>{
req.brew = brew;
await new Promise((resolve, reject) => {
api.deleteBrew(req, res, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
return new Promise((resolve, reject)=>{
api.deleteBrew(req, res, (err)=>err ? reject(err) : resolve());
});
}
console.log('all brews should be deleted');
return res.json({ success: true, message: `All brews for ${username} have been deleted.` });
});
console.log('delete promises: ', deletePromises);
await Promise.all(deletePromises);
return res.json({ success: true, message: `All brews for ${username} have been deleted.` });
} catch (error) {
console.error('Error deleting brews:', error);
if (!res.headersSent) {
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;