added favorate suppliers to customer and common get for orders based on orders

master^2
Varun 6 months ago
parent b4e7fcbe51
commit 63356cac31

@ -782,3 +782,44 @@ exports.medicine = async (req, reply) => {
}
};
exports.getOrdersByStatus = async (req, reply) => {
const { customerId, orderStatus } = req.query;
const query = { orderStatus };
if (customerId) {
query.customerId = customerId;
}
try {
const orders = await Tankerbooking.find(query).exec();
let enrichedOrders = orders;
if (orderStatus === "accepted") {
enrichedOrders = await Promise.all(
orders.map(async (order) => {
const deliveryBoy = await DeliveryBoy.findOne({ phone: order.delivery_agent_mobile });
return {
...order.toJSON(),
deliveryBoyLocation: deliveryBoy
? {
deliveryboy_latitude: deliveryBoy.latitude,
deliveryboy_longitude: deliveryBoy.longitude,
deliveryboy_address: deliveryBoy.address
}
: null
};
})
);
}
reply.send({ status_code: 200, data: enrichedOrders, count: enrichedOrders.length });
} catch (err) {
reply.status(400).send({ message: err.message });
}
};

@ -1042,3 +1042,67 @@ exports.blockStaff = async (request, reply) => {
reply.status(500).send({ error: 'An error occurred while blocking staff' });
}
};
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) => {
const { customerId } = req.params;
try {
const user = await User.findOne({ 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 });
if (!user) {
return reply.status(404).send({ message: "User not found" });
}
user.favorate_suppliers = user.favorate_suppliers.filter(
(id) => id.toString() !== supplierId
);
await user.save();
reply.send({ message: "Supplier removed from favorites", data: user.favorate_suppliers });
} catch (err) {
reply.status(500).send({ message: err.message });
}
};

@ -56,6 +56,7 @@ const userSchema = new mongoose.Schema(
inchargeName: String,
phoneVerified: { type: Boolean, default: false },
phoneVerificationCode: { type: Number, default: 11111 },
favorate_suppliers: [{ type: mongoose.Schema.Types.ObjectId, ref: "Supplier" }],
passwordResetCode: { type: Number, default: code },
oneTimePasswordSetFlag: { type: Boolean, default: false },
emails: [{ email: String, verified: { type: Boolean, default: false } }],

@ -583,6 +583,32 @@ module.exports = function (fastify, opts, next) {
});
fastify.route({
method: "GET",
url: "/api/orders",
schema: {
tags: ["Supplier-Order"],
description: "Get orders filtered by status and customerId",
summary: "Get orders filtered by status and customerId",
querystring: {
type: "object",
properties: {
customerId: { type: "string", description: "Customer ID (optional)" },
orderStatus: {
type: "string",
enum: ["accepted", "rejected", "delivered", "pending"],
description: "Order status to filter by"
}
},
required: ["orderStatus"]
},
security: [{ basicAuth: [] }]
},
handler: supplierOrderController.getOrdersByStatus
});
// fastify.route({
// method: "GET",
// url: "/api/billing/:bookingId",

@ -937,5 +937,66 @@ 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({
method: "GET",
url: "/api/favorites/:customerId",
schema: {
tags: ["User"],
description: "Get all favorite suppliers of a customer",
summary: "Get all favorite suppliers of a customer",
params: {
type: "object",
required: ["customerId"],
properties: {
customerId: { type: "string", description: "Customer ID" }
}
},
security: [{ basicAuth: [] }]
},
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();
};

Loading…
Cancel
Save