varun 3 years ago
commit 4b1cdbe8c4

@ -4,51 +4,86 @@ const boom = require("boom");
const fastify = require("fastify")({ const fastify = require("fastify")({
logger: true, logger: true,
}); });
const moment = require('moment');
// Handle friend request creation // Handle friend request creation
// exports.friendRequest = async (request, reply) => {
// try {
// // const { senderId, receiverId } = request.body;
// const customerId = request.body.customerId;
// const supplierId = request.body.supplierId;
// // 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({ customerId : customerId});
// const receiver = await Supplier.findOne({ supplierId : supplierId});
// 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({ customerId: sender.customerId, supplierId: receiver.supplierId});
// console.log(existingRequest, " .. exist")
// if (existingRequest) {
// throw new Error('Friend request already sent');
// }
// 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)
// reply.send({ timestamp: friendRequest.timestamp, message: 'Friend request sent' });
// } catch (err) {
// reply.status(400).send({ error: err.message });
// }
// };
exports.friendRequest = async (request, reply) => { exports.friendRequest = async (request, reply) => {
try { try {
// const { senderId, receiverId } = request.body;
const customerId = request.body.customerId; const customerId = request.body.customerId;
const supplierId = request.body.supplierId; const supplierId = request.body.supplierId;
// Check if the sender and receiver exist in the database const sender = await User.findOne({ customerId: customerId });
// const sender = await User.findById(senderId); const receiver = await Supplier.findOne({ supplierId: supplierId });
// const receiver = await Supplier.findById(receiverId);
// console.log("sender" , sender)
// console.log("receiver" , receiver)
const sender = await User.findOne({ customerId : customerId});
const receiver = await Supplier.findOne({ supplierId : supplierId});
console.log("sender" , sender.customerId)
console.log("receiver" , receiver.supplierId)
if (!sender || !receiver) { if (!sender || !receiver) {
throw new Error('Sender or receiver not found'); 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({ customerId: sender.customerId, supplierId: receiver.supplierId}); 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');
} }
const friendRequest = new FriendRequest({ customerId: sender.customerId, supplierId: receiver.supplierId});
// // Create a new friend request const timestamp = moment().format('MM/DD/YYYY, hh:mm:ss A');
// const friendRequest = new FriendRequest({
// sender: senderId, const friendRequest = new FriendRequest({
// receiver: receiverId customerId: sender.customerId,
// }); supplierId: receiver.supplierId,
timestamp: timestamp
});
await friendRequest.save(); await friendRequest.save();
console.log("friendRequest", friendRequest)
reply.send({ timestamp: friendRequest.timestamp, message: 'Friend request sent' }); reply.send({ timestamp: friendRequest.timestamp, message: 'Friend request sent' });
} catch (err) { } catch (err) {
@ -56,6 +91,7 @@ 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 {

@ -10,7 +10,7 @@ const ObjectId = Schema.Types.ObjectId;
const code = Math.floor(100000 + Math.random() * 900000); const code = Math.floor(100000 + Math.random() * 900000);
const RoleSchema = new Schema({ name: String }); const RoleSchema = new Schema({ name: String });
const tanksSchema = new mongoose.Schema({ const tanksSchema = new mongoose.Schema({
hardwareId: { type: String, unique: true }, hardwareId: { type: String },
customerId: { type: String, default: null }, customerId: { type: String, default: null },
tankName: { type: String, default: null }, tankName: { type: String, default: null },
blockName: { type: String, default: null }, blockName: { type: String, default: null },

Loading…
Cancel
Save