|
|
|
@ -14,10 +14,10 @@ exports.friendRequest = async (request, reply) => {
|
|
|
|
|
const { senderId, receiverId } = request.body;
|
|
|
|
|
|
|
|
|
|
// Check if the sender and receiver exist in the database
|
|
|
|
|
const sender = await User.findOne(senderId);
|
|
|
|
|
const receiver = await Supplier.findOne(receiverId);
|
|
|
|
|
console.log("sender" , sender.customerId)
|
|
|
|
|
console.log("receiver" , receiver.supplierId)
|
|
|
|
|
const sender = await User.findById(senderId);
|
|
|
|
|
const receiver = await Supplier.findById(receiverId);
|
|
|
|
|
console.log("sender" , sender)
|
|
|
|
|
console.log("receiver" , receiver)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!sender || !receiver) {
|
|
|
|
@ -25,21 +25,22 @@ exports.friendRequest = async (request, reply) => {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if a friend request already exists between the two users
|
|
|
|
|
const existingRequest = await FriendRequest.findOne({ customerId: sender.customerId, supplierId: receiver.supplierId});
|
|
|
|
|
|
|
|
|
|
console.log(existingRequest, " .. exist")
|
|
|
|
|
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({ customerId: sender.customerId, supplierId: receiver.supplierId});
|
|
|
|
|
const friendRequest = new FriendRequest({
|
|
|
|
|
sender: senderId,
|
|
|
|
|
receiver: receiverId
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await friendRequest.save();
|
|
|
|
|
console.log("friendRequest", friendRequest)
|
|
|
|
|
|
|
|
|
|
reply.send({ message: 'Friend request sent' });
|
|
|
|
|
reply.send({ message: 'Friend request sent' });
|
|
|
|
|
} catch (err) {
|
|
|
|
|
reply.status(400).send({ error: err.message });
|
|
|
|
|
}
|
|
|
|
@ -48,10 +49,10 @@ exports.friendRequest = async (request, reply) => {
|
|
|
|
|
// Handle friend request acceptance
|
|
|
|
|
exports.friendRequestAccept = async (request, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const {supplierId} = request.body;
|
|
|
|
|
const {friendRequestId} = request.body;
|
|
|
|
|
|
|
|
|
|
// Update the friend request status to 'accepted'
|
|
|
|
|
const friendRequest = await FriendRequest.findOneAndUpdate({supplierId}, { status: 'accepted' });
|
|
|
|
|
const friendRequest = await FriendRequest.findByIdAndUpdate(friendRequestId, { status: 'accepted' });
|
|
|
|
|
console.log("friendRequest....---", friendRequest)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -69,10 +70,10 @@ exports.friendRequestAccept = async (request, reply) => {
|
|
|
|
|
// Handle friend request rejection
|
|
|
|
|
exports.friendRequestReject = async (request, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const {supplierId} = request.body;
|
|
|
|
|
const {friendRequestId} = request.body;
|
|
|
|
|
|
|
|
|
|
// Update the friend request status to 'reject'
|
|
|
|
|
const friendRequest = await FriendRequest.findOneAndUpdate({supplierId}, { status: 'rejected' });
|
|
|
|
|
// Update the friend request status to 'rejected'
|
|
|
|
|
const friendRequest = await FriendRequest.findByIdAndUpdate(friendRequestId, { status: 'rejected' });
|
|
|
|
|
console.log("friendRequest....---", friendRequest)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|