diff --git a/routes/group.js b/routes/group.js index d371e4d..7b239bb 100644 --- a/routes/group.js +++ b/routes/group.js @@ -52,4 +52,9 @@ router.get('/leave/:groupId', db.mwUser, async (req, res) => { res.redirect('/user'); }); +router.get('/delete/:groupId', db.mwUser, db.mwGroup, isGroupAdmin, async (req, res) => { + await db.deleteGroup(req.params.groupId); + res.redirect('/user'); +}); + module.exports = router; diff --git a/src/database.js b/src/database.js index 7cfe746..7422069 100644 --- a/src/database.js +++ b/src/database.js @@ -244,20 +244,25 @@ module.exports.getUserGroups = async (userId) => { // User Groups Middleware - depends upon mwUser module.exports.mwUserGroups = async (req, res, next) => { - const userId = req.pageData.userData._id; - if (req.pageData.userData) { - const userGroups = await this.getUserGroups(userId); - req.pageData.userGroups = await async.mapSeries(userGroups, async (group) => { - group.memberNames = await async.mapSeries(group.members, async (member) => { - const userData = await this.getUser(member.userId); - return userData.username; + if (!req.pageData.userData) { + req.pageData.userGroups = []; + next(); + } else { + const userId = req.pageData.userData._id; + if (req.pageData.userData) { + const userGroups = await this.getUserGroups(userId); + req.pageData.userGroups = await async.mapSeries(userGroups, async (group) => { + group.memberNames = await async.mapSeries(group.members, async (member) => { + const userData = await this.getUser(member.userId); + return userData.username; + }); + group.isAdmin = group.adminId === userId; + group.accepted = group.members.find((member) => member.userId === userId).accepted; + return group; }); - group.isAdmin = group.adminId === userId; - group.accepted = group.members.find((member) => member.userId === userId).accepted; - return group; - }); + } + next(); } - next(); }; module.exports.inviteToGroup = async (_id, userId) => { @@ -285,6 +290,15 @@ module.exports.leaveGroup = async (_id, userId) => { } }; +module.exports.deleteGroup = async (_id) => { + try { + return await db.groups.remove({ _id }); + } catch (err) { + console.log(err); + return null; + } +}; + // !!!! Registration !!!! module.exports.createRegistration = async () => {