-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fixed issues in the create group and join group APIs
- Loading branch information
1 parent
3d605ed
commit ca6f023
Showing
7 changed files
with
449 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Helper function to generate the invite code | ||
const crypto = require('crypto'); | ||
|
||
const generateInviteCode = () => { | ||
return crypto.randomBytes(6).toString('hex'); | ||
}; | ||
|
||
// validate email helper function | ||
const validateEmail = (email) => { | ||
return validator.isEmail(email) && validator.normalizeEmail(email); | ||
}; | ||
|
||
// helper function to verify if user is admin | ||
const isGroupAdmin = async (req, res, next) => { | ||
try { | ||
const group = await groupModel.findById(req.params.groupId); | ||
if (!group) { | ||
return res.status(400).json({ message: 'Group not found' }); | ||
} | ||
const memeberRecord = group.members.find((mem) => { | ||
mem.user.toString() === req.user.id && m.role === 'admin'; | ||
}); | ||
if (!memeberRecord) { | ||
return res.status(403).json({ message: 'Only group admins can perform this action' }); | ||
} | ||
req.group = group; | ||
next(); | ||
} catch (error) { | ||
logger.error('Error in isGroupAdmin middleware:', error); | ||
res.status(500).json({ message: 'Internal server error' }); | ||
} | ||
}; | ||
|
||
module.exports = (generateInviteCode, validateEmail); |
Oops, something went wrong.