Bhaskar 5 months ago
commit 2e5d6fbb84

@ -731,6 +731,46 @@ exports.changePassword = async (req, reply) => {
} }
}; };
exports.addingfavoratesupplier = async (req, reply) => {
try {
const { customerId } = req.params;
const { supplierId } = req.query;
if (!supplierId) {
return reply.code(400).send({ status_code: 400, message: "supplierId is required" });
}
// Find user
const user = await User.findOne({ customerId });
if (!user) {
return reply.code(404).send({ status_code: 404, message: "User not found" });
}
// Add supplierId to favorate_suppliers if not already there
if (!user.favorate_suppliers.includes(supplierId)) {
user.favorate_suppliers.push(supplierId);
await user.save();
}
// Fetch FriendRequest status
const friendRequest = await FriendRequest.findOne({ customerId, supplierId });
const status = friendRequest ? friendRequest.status : "not_requested";
reply.send({
status_code: 200,
message: "Supplier added to favorites successfully",
data: {
customerId,
supplierId,
favorate_suppliers: user.favorate_suppliers,
status,
},
});
} catch (err) {
console.error(err);
reply.status(500).send({ status_code: 500, message: err.message });
}
};
exports.forgotPasswordSupplier = async (req, reply) => { exports.forgotPasswordSupplier = async (req, reply) => {
@ -890,6 +930,65 @@ exports.deleteTeamMember = async (req, reply) => {
} }
}; };
exports.editFavoriteSupplier = async (req, reply) => {
try {
const { customerId } = req.params;
const { oldSupplierId, newSupplierId } = req.query;
const user = await User.findOne({ customerId });
if (!user) return reply.code(404).send({ status_code: 404, message: "User not found" });
const index = user.favorate_suppliers.indexOf(oldSupplierId);
if (index === -1)
return reply.code(400).send({ status_code: 400, message: "Old supplier not found in favorites" });
user.favorate_suppliers[index] = newSupplierId;
await user.save();
reply.send({
status_code: 200,
message: "Favorite supplier updated",
data: user.favorate_suppliers,
});
} catch (err) {
throw boom.boomify(err);
}
};
exports.deleteFavoriteSupplier = async (req, reply) => {
try {
const { customerId } = req.params;
const { supplierId } = req.query;
const user = await User.findOne({ customerId });
if (!user) return reply.code(404).send({ status_code: 404, message: "User not found" });
const initialLength = user.favorate_suppliers.length;
user.favorate_suppliers = user.favorate_suppliers.filter(id => id !== supplierId);
if (user.favorate_suppliers.length === initialLength)
return reply.code(400).send({ status_code: 400, message: "Supplier not found in favorites" });
await user.save();
reply.send({
status_code: 200,
message: "Favorite supplier removed",
data: user.favorate_suppliers,
});
} catch (err) {
throw boom.boomify(err);
}
};
exports.updateTeamMember = async (req, reply) => { exports.updateTeamMember = async (req, reply) => {
try { try {
var customerId = req.params.customerId; var customerId = req.params.customerId;
@ -1046,63 +1145,37 @@ exports.blockStaff = async (request, reply) => {
exports.addFavoriteSupplier = async (req, reply) => {
const { customerId, supplierId } = req.body;
try {
const user = await User.findOne({ customerId });
if (!user) {
return reply.status(404).send({ message: "User not found" });
}
if (!user.favorate_suppliers.includes(supplierId)) {
user.favorate_suppliers.push(supplierId);
await user.save();
}
reply.send({ message: "Supplier added to favorites", data: user.favorate_suppliers });
} catch (err) {
reply.status(500).send({ message: err.message });
}
};
exports.getFavoriteSuppliers = async (req, reply) => { exports.getFavoriteSuppliers = async (req, reply) => {
const { customerId } = req.params; const { customerId } = req.params;
try { try {
const user = await User.findOne({ customerId }) // Find the user by customerId
.populate("favorate_suppliers") // If you want to get full supplier details
.exec();
if (!user) {
return reply.status(404).send({ message: "User not found" });
}
reply.send({ data: user.favorate_suppliers });
} catch (err) {
reply.status(500).send({ message: err.message });
}
};
exports.removeFavoriteSupplier = async (req, reply) => {
const { customerId, supplierId } = req.params;
try {
const user = await User.findOne({ customerId }); const user = await User.findOne({ customerId });
if (!user) { if (!user) {
return reply.status(404).send({ message: "User not found" }); return reply.status(404).send({ status_code: 404, message: "User not found" });
} }
user.favorate_suppliers = user.favorate_suppliers.filter( const supplierIds = user.favorate_suppliers || [];
(id) => id.toString() !== supplierId
);
await user.save(); // Get full supplier details for those supplierIds
const suppliers = await Supplier.find({
supplierId: { $in: supplierIds }
}).exec();
reply.send({
status_code: 200,
data: suppliers,
count: suppliers.length
});
reply.send({ message: "Supplier removed from favorites", data: user.favorate_suppliers });
} catch (err) { } catch (err) {
reply.status(500).send({ message: err.message }); console.error("Error fetching favorite suppliers:", err);
reply.status(500).send({ status_code: 500, message: "Internal server error" });
} }
}; };

@ -1021,17 +1021,22 @@ exports.getConnectedSuppliers = async (req, reply) => {
const limit = parseInt(req.query.limit) || 100; const limit = parseInt(req.query.limit) || 100;
const page = parseInt(req.query.page) || 1; const page = parseInt(req.query.page) || 1;
const startindex = (page - 1) * limit; const startindex = (page - 1) * limit;
const customerId = req.params.customerId; // Assuming you have already authenticated the user and stored their ID in the request object const customerId = req.params.customerId;
try { try {
// Get user's favorite suppliers
const user = await User.findOne({ customerId }, 'favorate_suppliers');
const favorateSuppliers = user?.favorate_suppliers || [];
// Get accepted friend requests
const friendRequests = await FriendRequest.find({ const friendRequests = await FriendRequest.find({
customerId, customerId,
status: "accepted", status: "accepted",
}); });
const supplierIdsToInclude = friendRequests.map( const supplierIdsToInclude = friendRequests.map(req => req.supplierId);
(request) => request.supplierId
);
// Get suppliers
const suppliers = await Supplier.find({ const suppliers = await Supplier.find({
supplierId: { $in: supplierIdsToInclude } supplierId: { $in: supplierIdsToInclude }
}) })
@ -1039,19 +1044,24 @@ exports.getConnectedSuppliers = async (req, reply) => {
.skip(startindex) .skip(startindex)
.exec(); .exec();
const supplierIds = suppliers.map((supplier) => supplier.supplierId); const supplierIds = suppliers.map(s => s.supplierId);
// Get profile pictures
const profilePictures = await profilePictureSupplier.find({ const profilePictures = await profilePictureSupplier.find({
supplierId: { $in: supplierIds } supplierId: { $in: supplierIds }
}).exec(); });
// Construct final response
const data = suppliers.map((supplier) => { const data = suppliers.map((supplier) => {
const profilePicture = profilePictures.find( const profilePicture = profilePictures.find(
(picture) => picture.supplierId === supplier.supplierId (pic) => pic.supplierId === supplier.supplierId
); );
const isFavorate = favorateSuppliers.includes(supplier.supplierId);
return { return {
...supplier.toObject(), ...supplier.toObject(),
picture: profilePicture ? profilePicture.picture : null, picture: profilePicture ? profilePicture.picture : null,
favorate: isFavorate,
}; };
}); });

@ -56,12 +56,14 @@ const userSchema = new mongoose.Schema(
inchargeName: String, inchargeName: String,
phoneVerified: { type: Boolean, default: false }, phoneVerified: { type: Boolean, default: false },
phoneVerificationCode: { type: Number, default: 11111 }, phoneVerificationCode: { type: Number, default: 11111 },
favorate_suppliers: [{ type: mongoose.Schema.Types.ObjectId, ref: "Supplier" }],
passwordResetCode: { type: Number, default: code }, passwordResetCode: { type: Number, default: code },
oneTimePasswordSetFlag: { type: Boolean, default: false }, oneTimePasswordSetFlag: { type: Boolean, default: false },
emails: [{ email: String, verified: { type: Boolean, default: false } }], emails: [{ email: String, verified: { type: Boolean, default: false } }],
services: { password: { bcrypt: String } }, services: { password: { bcrypt: String } },
survey_status:{ type:String,default: "pending" }, survey_status:{ type:String,default: "pending" },
favorate_suppliers: [{ type: String, default: null }],
staff: { staff: {

@ -656,6 +656,8 @@ module.exports = function (fastify, opts, next) {
}); });
fastify.route({ fastify.route({
method: "POST", method: "POST",
url: "/api/sendSms", url: "/api/sendSms",
@ -752,6 +754,85 @@ module.exports = function (fastify, opts, next) {
preHandler: fastify.auth([fastify.authenticate]), preHandler: fastify.auth([fastify.authenticate]),
handler: userController.deleteTeamMember, // Ensure this line points to the handler handler: userController.deleteTeamMember, // Ensure this line points to the handler
}); });
fastify.route({
method: "PUT",
url: "/api/addingfavoratesupplier/:customerId",
schema: {
tags: ["User"],
summary: "This is for adding favorate supplier",
description: "This is for adding favorate supplier",
params: {
required: ["customerId"],
type: "object",
properties: {
customerId: {
type: "string",
description: "customerId",
},
},
},
querystring: {
supplierId: { type: "string" },
},
security: [
{
basicAuth: [],
},
],
},
// preHandler: fastify.auth([fastify.authenticate]),
handler: userController.addingfavoratesupplier, // Ensure this line points to the handler
});
fastify.route({
method: "PUT",
url: "/api/editfavoratesupplier/:customerId",
schema: {
tags: ["User"],
summary: "Edit a favorite supplier",
description: "Replace an existing supplierId with a new one",
params: {
required: ["customerId"],
type: "object",
properties: {
customerId: { type: "string" },
},
},
querystring: {
oldSupplierId: { type: "string" },
newSupplierId: { type: "string" },
},
security: [{ basicAuth: [] }],
},
handler: userController.editFavoriteSupplier,
});
fastify.route({
method: "DELETE",
url: "/api/deletefavoratesupplier/:customerId",
schema: {
tags: ["User"],
summary: "Delete a favorite supplier",
description: "Remove a supplierId from favorite suppliers",
params: {
required: ["customerId"],
type: "object",
properties: {
customerId: { type: "string" },
},
},
querystring: {
supplierId: { type: "string" },
},
security: [{ basicAuth: [] }],
},
handler: userController.deleteFavoriteSupplier,
});
fastify.route({ fastify.route({
method: "PUT", method: "PUT",
@ -938,25 +1019,7 @@ fastify.route({
fastify.route({
method: "POST",
url: "/api/favorites/add",
schema: {
tags: ["User"],
description: "Add a supplier to the customer's favorites",
summary: "Add a supplier to the customer's favorites",
body: {
type: "object",
required: ["customerId", "supplierId"],
properties: {
customerId: { type: "string", description: "Customer ID" },
supplierId: { type: "string", description: "Supplier ID to be added" }
}
},
security: [{ basicAuth: [] }]
},
handler: userController.addFavoriteSupplier
});
fastify.route({ fastify.route({
method: "GET", method: "GET",
@ -977,25 +1040,8 @@ fastify.route({
handler: userController.getFavoriteSuppliers handler: userController.getFavoriteSuppliers
}); });
fastify.route({
method: "DELETE",
url: "/api/favorites/:customerId/:supplierId",
schema: {
tags: ["User"],
description: "Remove a supplier from the customer's favorites",
summary: "Remove a supplier from the customer's favorites",
params: {
type: "object",
required: ["customerId", "supplierId"],
properties: {
customerId: { type: "string", description: "Customer ID" },
supplierId: { type: "string", description: "Supplier ID to be removed" }
}
},
security: [{ basicAuth: [] }]
},
handler: userController.removeFavoriteSupplier
});
next(); next();

Loading…
Cancel
Save