You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
2.4 KiB
90 lines
2.4 KiB
|
3 years ago
|
const { Supplier, FriendRequest } = require('../models/supplier');
|
||
|
|
const { User } = require('../models/User')
|
||
|
|
const boom = require("boom");
|
||
|
|
const fastify = require("fastify")({
|
||
|
|
logger: true,
|
||
|
|
});
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
// Handle friend request creation
|
||
|
|
exports.friendRequest = async (request, reply) => {
|
||
|
|
try {
|
||
|
|
const { senderId, receiverId } = request.body;
|
||
|
|
|
||
|
|
// Check if the sender and receiver exist in the database
|
||
|
|
const sender = await User.findById(senderId);
|
||
|
|
const receiver = await Supplier.findById(receiverId);
|
||
|
|
console.log("sender" , sender)
|
||
|
|
console.log("receiver" , receiver)
|
||
|
|
|
||
|
|
|
||
|
|
if (!sender || !receiver) {
|
||
|
|
throw new Error('Sender or receiver not found');
|
||
|
|
}
|
||
|
|
|
||
|
|
// Check if a friend request already exists between the two users
|
||
|
|
const existingRequest = await FriendRequest.findOne({ sender: senderId, receiver: receiverId });
|
||
|
|
|
||
|
|
if (existingRequest) {
|
||
|
|
throw new Error('Friend request already sent');
|
||
|
|
}
|
||
|
|
|
||
|
|
// Create a new friend request
|
||
|
|
const friendRequest = new FriendRequest({
|
||
|
|
sender: senderId,
|
||
|
|
receiver: receiverId
|
||
|
|
});
|
||
|
|
|
||
|
|
await friendRequest.save();
|
||
|
|
console.log("friendRequest", friendRequest)
|
||
|
|
|
||
|
|
reply.send({ message: 'Friend request sent' });
|
||
|
|
} catch (err) {
|
||
|
|
reply.status(400).send({ error: err.message });
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
// Handle friend request acceptance
|
||
|
|
exports.friendRequestAccept = async (request, reply) => {
|
||
|
|
try {
|
||
|
|
const {friendRequestId} = request.body;
|
||
|
|
|
||
|
|
// Update the friend request status to 'accepted'
|
||
|
|
const friendRequest = await FriendRequest.findByIdAndUpdate(friendRequestId, { status: 'accepted' });
|
||
|
|
console.log("friendRequest....---", friendRequest)
|
||
|
|
|
||
|
|
|
||
|
|
if (!friendRequest) {
|
||
|
|
throw new Error('Friend request not found');
|
||
|
|
}
|
||
|
|
|
||
|
|
reply.send({ message: 'Friend request accepted' });
|
||
|
|
} catch (err) {
|
||
|
|
reply.status(400).send({ error: err.message });
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
|
||
|
|
// Handle friend request rejection
|
||
|
|
exports.friendRequestReject = async (request, reply) => {
|
||
|
|
try {
|
||
|
|
const {friendRequestId} = request.body;
|
||
|
|
|
||
|
|
// Update the friend request status to 'rejected'
|
||
|
|
const friendRequest = await FriendRequest.findByIdAndUpdate(friendRequestId, { status: 'rejected' });
|
||
|
|
console.log("friendRequest....---", friendRequest)
|
||
|
|
|
||
|
|
|
||
|
|
if (!friendRequest) {
|
||
|
|
throw new Error('Friend request not found');
|
||
|
|
}
|
||
|
|
|
||
|
|
reply.send({ message: 'Friend request rejected' });
|
||
|
|
} catch (err) {
|
||
|
|
reply.status(400).send({ error: err.message });
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|