supplier- user connection

master
Bhaskara Kishore 3 years ago
parent 542b9203cf
commit c8c50556a2

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

@ -94,8 +94,8 @@ const supplierSchema = new mongoose.Schema(
// }) // })
const friendRequestSchema = new mongoose.Schema({ const friendRequestSchema = new mongoose.Schema({
sender: { type: mongoose.Schema.Types.ObjectId, ref: 'user' }, customerId: { type: String, default: null },
receiver: { type: mongoose.Schema.Types.ObjectId, ref: 'supplier' }, supplierId: { type: String, default: null },
status: { type: String, default: "pending" }, status: { type: String, default: "pending" },
timestamp: { type: Date, default: Date.now } timestamp: { type: Date, default: Date.now }
}); });

@ -16,8 +16,8 @@ module.exports = function (fastify, opts, next) {
type: "object", type: "object",
//required: ["customerId"], //required: ["customerId"],
properties: { properties: {
senderId: { type: "string" }, customerId: { type: "string" },
receiverId : { type : "string"} supplierId : { type : "string"}
}, },
}, },
security: [ security: [
@ -40,7 +40,7 @@ module.exports = function (fastify, opts, next) {
body: { body: {
type: "object", type: "object",
properties: { properties: {
friendRequestId: { type: "string" }, supplierId: { type: "string" },
}, },
}, },
@ -63,7 +63,7 @@ module.exports = function (fastify, opts, next) {
body: { body: {
type: "object", type: "object",
properties: { properties: {
friendRequestId: { type: "string" }, supplierId: { type: "string" },
}, },
}, },
@ -75,6 +75,8 @@ module.exports = function (fastify, opts, next) {
}, },
handler: validationHandler.friendRequestReject handler: validationHandler.friendRequestReject
}); });
next(); next();
} }

Loading…
Cancel
Save