Skip to content

Commit

Permalink
Merge pull request #56 from Skill-Sync/SS-1.4
Browse files Browse the repository at this point in the history
Ss 1.4
  • Loading branch information
MAES-Pyramids authored Sep 14, 2023
2 parents 9648517 + 10f449e commit 363a17e
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 30 deletions.
94 changes: 90 additions & 4 deletions src/controllers/friend.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,93 @@ const AppError = require('../utils/appErrorsClass');
const FriendShip = require('../models/friendship.model');
const catchAsyncError = require('../utils/catchAsyncErrors');
// ---------- User Operations ---------//
exports.getFriend = catchAsyncError(async (req, res, next) => {});
exports.getMyFriends = catchAsyncError(async (req, res, next) => {});
exports.deleteFriend = catchAsyncError(async (req, res, next) => {});
exports.acceptAsFriend = catchAsyncError(async (req, res, next) => {});
exports.getMyFriends = catchAsyncError(async (req, res, next) => {
const userId = res.locals.userId;

const friendships = await FriendShip.find({
$or: [{ user_1: userId }, { user_2: userId }],
status: 'accepted'
})
.populate('user_1')
.populate('user_2');

const friends = friendships.map(friendship => {
return friendship.user_1._id.toString() === userId
? friendship.user_2
: friendship.user_1;
});

res.status(res.locals.statusCode || 200).json({
status: 'success',
result: friends.length,
data: {
friends
}
});
});
exports.createEditFriendship = catchAsyncError(async (req, res, next) => {
const userId = res.locals.userId;
const friendId = req.params.id;
let { status } = req.body;

if (userId === friendId)
return next(new AppError('You cannot be friends with yourself', 400));

let friendship = await FriendShip.findOne({
$or: [
{ user_1: userId, user_2: friendId },
{ user_1: friendId, user_2: userId }
]
});

if (!friendship) {
status = status == 'rejected' ? 'rejected' : 'pending';
const newFriendship = await FriendShip.create({
user_1: userId,
user_2: friendId,
status
});
if (!newFriendship)
return next(new AppError('Could not create friendship', 400));
res.status(res.locals.statusCode || 200).json({ newFriendship });
}

if (friendship.status === 'accepted')
return next(new AppError('You are already friends', 400));

if (friendship.status === 'rejected') {
return res.status(res.locals.statusCode || 200).json({ friendship });
}

if (status === 'accepted') {
friendship.status = 'accepted';
await friendship.save();
} else if (status === 'rejected') {
friendship.status = 'rejected';
await friendship.save();
}

res.status(res.locals.statusCode || 200).json({ friendship });
});

exports.deleteFriend = catchAsyncError(async (req, res, next) => {
const userId = res.locals.userId;
const friendId = req.params.id;

const friendship = await FriendShip.findOne({
$or: [
{ user_1: userId, user_2: friendId },
{ user_1: friendId, user_2: userId }
]
});

if (!friendship)
return next(new AppError('Could not find friendship', 400));

await FriendShip.findByIdAndDelete(friendship._id);

res.status(res.locals.statusCode || 204).json({
status: 'success',
data: null
});
});
41 changes: 21 additions & 20 deletions src/models/friendship.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,28 @@ const mongoose = require('mongoose');
// Please note that we intentionally did not create a 'friends' model with a user and an array of his friends. This decision aligns with our app's logic, where becoming friends with someone is based on mutual consent without requiring any requests or approvals. we know this approach may not be the best for scalability, but it is just a way to go for now."

const friendshipsSchema = new mongoose.Schema({
user_1: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
user_2: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
status: {
type: String,
enum: ['notStarted', 'pending', 'accepted', 'rejected'],
default: 'notStarted'
},
createdAt: {
type: Date,
default: Date.now()
},
updatedAt: {
type: Date
}
user_1: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
user_2: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
status: {
type: String,
enum: ['pending', 'accepted', 'rejected'],
default: 'pending'
},
createdAt: {
type: Date,
default: Date.now()
},
updatedAt: {
type: Date
}
});

//-------------------------Export-----------------------//
const FriendShip = mongoose.model('FriendShip', friendshipsSchema);
module.exports = FriendShip;
9 changes: 5 additions & 4 deletions src/routes/friend.routes.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
const express = require('express');
const userController = require('../controllers/user.controller');
const friendsController = require('../controllers/friend.controller');
//------------------------------------------//
const router = express.Router({ mergeParams: true });
//-------------Users Routes-----------------//
router.get('/', friendsController.getMyFriends);

router
.route('/:id')
.get(friendsController.getFriend)
.post(friendsController.acceptAsFriend)
.delete(friendsController.deleteFriend);
.route('/:id')
.get(userController.getUser)
.delete(friendsController.deleteFriend)
.post(friendsController.createEditFriendship);
//-------------------------------------------//
module.exports = router;
3 changes: 1 addition & 2 deletions src/routes/user.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ router.patch(
userController.getMe,
userController.UpdateMe
);
// router.patch('/updatePassword', authController.updatePassword);
//---------------Admin Routes---------------//
// router.use(authController.restrictTo('admin'));
router.use(authController.restrictTo('admin'));
router.get('/', userController.getAllUsers);
router
.route('/:id')
Expand Down

0 comments on commit 363a17e

Please sign in to comment.