From e7cdd263cebb6d568b7f9f29520391207d4d512b Mon Sep 17 00:00:00 2001 From: Bhaskara Kishore Date: Fri, 24 Mar 2023 16:30:12 +0530 Subject: [PATCH] friend-request --- src/handlers/friendRequestHandler.js | 43 +++++++++++++++++----------- src/models/supplier.js | 4 +-- src/routes/friendRequestRoute.js | 8 +++--- 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/handlers/friendRequestHandler.js b/src/handlers/friendRequestHandler.js index 3c09331b..916395f0 100644 --- a/src/handlers/friendRequestHandler.js +++ b/src/handlers/friendRequestHandler.js @@ -14,28 +14,35 @@ exports.friendRequest = async (request, reply) => { 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) + // const sender = await User.findById(senderId); + // const receiver = await Supplier.findById(receiverId); + // console.log("sender" , sender) + // console.log("receiver" , receiver) + const sender = await User.findOne(senderId); + const receiver = await Supplier.findOne(receiverId); + console.log("sender" , sender.customerId) + console.log("receiver" , receiver.supplierId) 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 }); + // const existingRequest = await FriendRequest.findOne({ sender: senderId, receiver: receiverId }); + const existingRequest = await FriendRequest.findOne({ customerId: sender.customerId, supplierId: receiver.supplierId}); + + console.log(existingRequest, " .. exist") if (existingRequest) { throw new Error('Friend request already sent'); } - - // Create a new friend request - const friendRequest = new FriendRequest({ - sender: senderId, - receiver: receiverId - }); + const friendRequest = new FriendRequest({ customerId: sender.customerId, supplierId: receiver.supplierId}); + // // Create a new friend request + // const friendRequest = new FriendRequest({ + // sender: senderId, + // receiver: receiverId + // }); await friendRequest.save(); console.log("friendRequest", friendRequest) @@ -49,10 +56,11 @@ exports.friendRequest = async (request, reply) => { // Handle friend request acceptance exports.friendRequestAccept = async (request, reply) => { try { - const {friendRequestId} = request.body; - + //const {friendRequestId} = request.body; + const {supplierId} = request.body; // Update the friend request status to 'accepted' - const friendRequest = await FriendRequest.findByIdAndUpdate(friendRequestId, { status: 'accepted' }); + // const friendRequest = await FriendRequest.findByIdAndUpdate(friendRequestId, { status: 'accepted' }); + const friendRequest = await FriendRequest.findOneAndUpdate({supplierId}, { status: 'accepted' }); console.log("friendRequest....---", friendRequest) @@ -70,10 +78,11 @@ exports.friendRequestAccept = async (request, reply) => { // Handle friend request rejection exports.friendRequestReject = async (request, reply) => { try { - const {friendRequestId} = request.body; - + //const {friendRequestId} = request.body; + const {supplierId} = request.body; // Update the friend request status to 'rejected' - const friendRequest = await FriendRequest.findByIdAndUpdate(friendRequestId, { status: 'rejected' }); + // const friendRequest = await FriendRequest.findByIdAndUpdate(friendRequestId, { status: 'rejected' }); + const friendRequest = await FriendRequest.findOneAndUpdate({supplierId}, { status: 'rejected' }); console.log("friendRequest....---", friendRequest) diff --git a/src/models/supplier.js b/src/models/supplier.js index e52d28b0..45bb9eaf 100644 --- a/src/models/supplier.js +++ b/src/models/supplier.js @@ -94,8 +94,8 @@ const supplierSchema = new mongoose.Schema( // }) const friendRequestSchema = new mongoose.Schema({ - sender: { type: mongoose.Schema.Types.ObjectId, ref: 'user' }, - receiver: { type: mongoose.Schema.Types.ObjectId, ref: 'supplier' }, + customerId: { type: String, default: null }, + supplierId: { type: String, default: null }, status: { type: String, default: "pending" }, timestamp: { type: Date, default: Date.now } }); diff --git a/src/routes/friendRequestRoute.js b/src/routes/friendRequestRoute.js index b92a2455..ec45ad4f 100644 --- a/src/routes/friendRequestRoute.js +++ b/src/routes/friendRequestRoute.js @@ -16,8 +16,8 @@ module.exports = function (fastify, opts, next) { type: "object", //required: ["customerId"], properties: { - senderId: { type: "string" }, - receiverId : { type : "string"} + customerId: { type: "string" }, + supplierId : { type : "string"} }, }, security: [ @@ -40,7 +40,7 @@ module.exports = function (fastify, opts, next) { body: { type: "object", properties: { - friendRequestId: { type: "string" }, + supplierId: { type: "string" } }, }, @@ -63,7 +63,7 @@ module.exports = function (fastify, opts, next) { body: { type: "object", properties: { - friendRequestId: { type: "string" }, + supplierId: { type: "string" }, }, },