added favorate supplier ,edit and delete

master^2
Varun 5 months ago
parent f77dac00f8
commit 46d0a258a1

@ -890,6 +890,102 @@ exports.deleteTeamMember = 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";
// Send response
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);
throw boom.boomify(err);
}
};
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;

@ -62,6 +62,7 @@ const userSchema = new mongoose.Schema(
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,82 @@ 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",

Loading…
Cancel
Save