From c8c50556a249db90ad55a4fcbdb50e3a07dabc23 Mon Sep 17 00:00:00 2001 From: Bhaskara Kishore Date: Mon, 20 Mar 2023 17:32:57 +0530 Subject: [PATCH 1/2] supplier- user connection --- src/handlers/friendRequestHandler.js | 29 ++++++++++++++-------------- src/models/supplier.js | 4 ++-- src/routes/friendRequestRoute.js | 10 ++++++---- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/handlers/friendRequestHandler.js b/src/handlers/friendRequestHandler.js index 3c09331b..7010ae93 100644 --- a/src/handlers/friendRequestHandler.js +++ b/src/handlers/friendRequestHandler.js @@ -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.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) { @@ -25,22 +25,21 @@ exports.friendRequest = async (request, reply) => { } // 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({ 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}); 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 }); } @@ -49,10 +48,10 @@ exports.friendRequest = async (request, reply) => { // Handle friend request acceptance exports.friendRequestAccept = async (request, reply) => { try { - 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.findOneAndUpdate({supplierId}, { status: 'accepted' }); console.log("friendRequest....---", friendRequest) @@ -70,10 +69,10 @@ exports.friendRequestAccept = async (request, reply) => { // Handle friend request rejection exports.friendRequestReject = async (request, reply) => { try { - const {friendRequestId} = request.body; + const {supplierId} = request.body; - // Update the friend request status to 'rejected' - const friendRequest = await FriendRequest.findByIdAndUpdate(friendRequestId, { status: 'rejected' }); + // Update the friend request status to 'reject' + 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 f2c270b5..a81210e4 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..65a873f2 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" }, }, }, @@ -75,6 +75,8 @@ module.exports = function (fastify, opts, next) { }, handler: validationHandler.friendRequestReject }); + + next(); } From c66f1fcba61a4001446ae0477afd5595514aebb3 Mon Sep 17 00:00:00 2001 From: Bhaskara Kishore Date: Mon, 20 Mar 2023 17:57:58 +0530 Subject: [PATCH 2/2] In tankerschema side add price and changes --- src/controllers/tankersController.js | 2 ++ src/models/tankers.js | 11 +++++++++-- src/routes/tankersRoute.js | 1 + 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/controllers/tankersController.js b/src/controllers/tankersController.js index 1b8540aa..684eb176 100644 --- a/src/controllers/tankersController.js +++ b/src/controllers/tankersController.js @@ -35,6 +35,7 @@ console.log(req.params); alternative_phoneNumber: req.body.alternative_phoneNumber, typeofwater: req.body.typeofwater, capacity: req.body.capacity, + price: req.body.price }; console.log(req.body.typeofwater,req.body.capacity) @@ -59,6 +60,7 @@ console.log(req.params); tankers.capacity = usertobeInserted.capacity; tankers.typeofwater = usertobeInserted.typeofwater; tankers.supplierId = usertobeInserted.supplierId; + tankers.price = usertobeInserted.price; } } const insertedTanker = await tankers.save(); diff --git a/src/models/tankers.js b/src/models/tankers.js index bad820b5..e73e520d 100644 --- a/src/models/tankers.js +++ b/src/models/tankers.js @@ -15,10 +15,17 @@ const tankersSchema = new mongoose.Schema({ tankerName: { type: String, default: null }, phoneNumber: { type: String, default: null }, alternative_phoneNumber: { type: String, default: null }, - typeofwater: { typeofwater: String}, - capacity: { capacity: String}, + typeofwater: { + type: [String], + default: [] + }, + capacity: { type: String}, supplier_address: { type: String, default: null }, supplier_name : { type: String, default: null }, + price: { + type: [String], + default: [] + }, }); diff --git a/src/routes/tankersRoute.js b/src/routes/tankersRoute.js index 53344e6b..6cbf5cd4 100644 --- a/src/routes/tankersRoute.js +++ b/src/routes/tankersRoute.js @@ -33,6 +33,7 @@ module.exports = function (fastify, opts, next) { alternative_phoneNumber: { type: "string"}, typeofwater: { type: "string"}, capacity: { type: "string"}, + price: {type : "string"} }, },