diff --git a/src/handlers/friendRequestHandler.js b/src/handlers/friendRequestHandler.js new file mode 100644 index 00000000..3c09331b --- /dev/null +++ b/src/handlers/friendRequestHandler.js @@ -0,0 +1,89 @@ +const { Supplier, FriendRequest } = require('../models/supplier'); +const { User } = require('../models/User') +const boom = require("boom"); +const fastify = require("fastify")({ + logger: true, +}); + + + + +// Handle friend request creation +exports.friendRequest = async (request, reply) => { + try { + 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) + + + 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 }); + + if (existingRequest) { + throw new Error('Friend request already sent'); + } + + // Create a new friend request + const friendRequest = new FriendRequest({ + sender: senderId, + receiver: receiverId + }); + + await friendRequest.save(); + console.log("friendRequest", friendRequest) + + reply.send({ message: 'Friend request sent' }); + } catch (err) { + reply.status(400).send({ error: err.message }); + } +}; + +// Handle friend request acceptance +exports.friendRequestAccept = async (request, reply) => { + try { + const {friendRequestId} = request.body; + + // Update the friend request status to 'accepted' + const friendRequest = await FriendRequest.findByIdAndUpdate(friendRequestId, { status: 'accepted' }); + console.log("friendRequest....---", friendRequest) + + + if (!friendRequest) { + throw new Error('Friend request not found'); + } + + reply.send({ message: 'Friend request accepted' }); + } catch (err) { + reply.status(400).send({ error: err.message }); + } +}; + + +// Handle friend request rejection +exports.friendRequestReject = async (request, reply) => { + try { + const {friendRequestId} = request.body; + + // Update the friend request status to 'rejected' + const friendRequest = await FriendRequest.findByIdAndUpdate(friendRequestId, { status: 'rejected' }); + console.log("friendRequest....---", friendRequest) + + + if (!friendRequest) { + throw new Error('Friend request not found'); + } + + reply.send({ message: 'Friend request rejected' }); + } catch (err) { + reply.status(400).send({ error: err.message }); + } +}; + diff --git a/src/index.js b/src/index.js index 728230b1..31dda7cb 100644 --- a/src/index.js +++ b/src/index.js @@ -337,7 +337,7 @@ fastify.register(require("./routes/createConnectionsRoute")); fastify.register(require("./routes/tankersRoute.js")); fastify.register(require("./routes/supplierRoute")); fastify.register(require("./routes/supplierOrdersRoutes")); - +fastify.register(require("./routes/friendRequestRoute")); // Testing route allows for retrieving a user by phone so one can see what is the phone verification code sent for a given user's phone // Also allows deletion of a user with a given phone number diff --git a/src/models/supplier.js b/src/models/supplier.js index a9215d58..f2c270b5 100644 --- a/src/models/supplier.js +++ b/src/models/supplier.js @@ -80,8 +80,30 @@ const supplierSchema = new mongoose.Schema( ); + // const deliveryAgent = new mongoose.Schema({ + // deliveryboyId : { type: String, default: null }, + // deliveryboyname : { type: String }, + // vechilenumber : { type: String, default: null }, + // bookingid : { type: String, default: null }, + // status: { + // type: String, + // enum: ['complete', 'pending'], + // default: 'pending' + // }, + + // }) + + const friendRequestSchema = new mongoose.Schema({ + sender: { type: mongoose.Schema.Types.ObjectId, ref: 'user' }, + receiver: { type: mongoose.Schema.Types.ObjectId, ref: 'supplier' }, + status: { type: String, default: "pending" }, + timestamp: { type: Date, default: Date.now } + }); const Supplier = mongoose.model("Supplier", supplierSchema); +//const DeliveryAgent = mongoose.model("DeliveryAgent", deliveryAgent); +const FriendRequest = mongoose.model('FriendRequest', friendRequestSchema); + +module.exports = { Supplier, generateSupplierId, FriendRequest} -module.exports = { Supplier, generateSupplierId} diff --git a/src/routes/friendRequestRoute.js b/src/routes/friendRequestRoute.js new file mode 100644 index 00000000..b92a2455 --- /dev/null +++ b/src/routes/friendRequestRoute.js @@ -0,0 +1,81 @@ +const fastify = require("fastify"); +const validationHandler = require("../handlers/friendRequestHandler"); + + + +module.exports = function (fastify, opts, next) { + + fastify.route({ + method: "POST", + url: "/api/users/friend-request", + schema: { + tags: ["Friend-Request"], + description: "This is for User Friend Request", + summary: "This is for User Friend Request", + body: { + type: "object", + //required: ["customerId"], + properties: { + senderId: { type: "string" }, + receiverId : { type : "string"} + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + handler: validationHandler.friendRequest, + }); + + + fastify.route({ + method: "PUT", + url: "/api/friend-request/:id/accept", + schema: { + tags: ["Friend-Request"], + description: "This is for supplier accept the friend request", + summary: "This is for supplier accept the friend request", + body: { + type: "object", + properties: { + friendRequestId: { type: "string" }, + + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + handler: validationHandler.friendRequestAccept + }); + fastify.route({ + method: "PUT", + url: "/api/friend-request/:id/reject", + schema: { + tags: ["Friend-Request"], + description: "This is for supplier reject the friend request", + summary: "This is for supplier reject the friend request", + + body: { + type: "object", + properties: { + friendRequestId: { type: "string" }, + + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + handler: validationHandler.friendRequestReject + }); + + next(); +} +