Skip to content

Commit

Permalink
my messages page fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
singharaj-usai committed Oct 16, 2024
1 parent 6d6fb52 commit e02ca66
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions server/functions/api/routes/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ router.post(
const { recipient, subject, message } = req.body;

try {
const sender = await User.findOne({ username: req.user.username });
const sender = await User.findOne({ user: req.user.username });
if (!sender) {
return res
.status(401)
Expand Down Expand Up @@ -57,7 +57,7 @@ router.post(
// Get received messages
router.get('/received', authenticateToken, async (req, res) => {
try {
const user = await User.findOne({ username: req.user.username });
const user = await User.findOne({ userId: req.user.userId });
const messages = await Message.find({ recipient: user._id })
.populate('sender', 'username')
.sort({ sentAt: -1 });
Expand All @@ -72,7 +72,7 @@ router.get('/received', authenticateToken, async (req, res) => {
// Get sent messages
router.get('/sent', authenticateToken, async (req, res) => {
try {
const user = await User.findOne({ username: req.user.username });
const user = await User.findOne({ userId: req.user.userId });
const messages = await Message.find({ sender: user._id })
.populate('recipient', 'username')
.sort({ sentAt: -1 });
Expand All @@ -93,7 +93,7 @@ router.post('/:id/archive', authenticateToken, async (req, res) => {
return res.status(404).json({ error: 'Message not found' });
}

const user = await User.findOne({ username: req.user.username });
const user = await User.findOne({ userId: req.user.userId });
if (!user) {
return res.status(404).json({ error: 'User not found' });
}
Expand Down Expand Up @@ -125,7 +125,7 @@ router.post('/:id/restore', authenticateToken, async (req, res) => {
return res.status(404).json({ error: 'Message not found' });
}

const user = await User.findOne({ username: req.user.username });
const user = await User.findOne({ userId: req.user.userId });
if (!user) {
return res.status(404).json({ error: 'User not found' });
}
Expand All @@ -151,7 +151,7 @@ router.post('/:id/restore', authenticateToken, async (req, res) => {
// Get archived messages
router.get('/archived', authenticateToken, async (req, res) => {
try {
const user = await User.findOne({ username: req.user.username });
const user = await User.findOne({ userId: req.user.userId });
const messages = await Message.find({
recipient: user._id,
isArchived: true,
Expand All @@ -172,17 +172,17 @@ router.get('/:id', authenticateToken, async (req, res) => {
const messageId = req.params.id;
try {
const message = await Message.findById(messageId)
.populate('sender', 'username')
.populate('recipient', 'username');
.populate('sender', 'userId')
.populate('recipient', 'userId');

if (!message) {
return res.status(404).json({ error: 'Message not found' });
}

// Verify that the requester is either the sender or the recipient
if (
message.sender.username !== req.user.username &&
message.recipient.username !== req.user.username
message.sender.userId !== req.user.userId &&
message.recipient.userId !== req.user.userId
) {
return res.status(403).json({ error: 'Access denied' });
}
Expand Down

0 comments on commit e02ca66

Please sign in to comment.