import express from "express"; import Store from "../models/Store.model.js"; import { protectVendor } from "../middleware/authMiddleware.js"; // Protect the vendor import mongoose from "mongoose"; // Import mongoose to use ObjectId const router = express.Router(); /** * 📌 Get Shipping Options for Vendor's Store * @route GET /api/shipping-options * @access Private (Vendors only) */ router.get("/", protectVendor, async (req, res) => { try { if (!req.user.storeId) { return res .status(400) .json({ message: "Store not found for this vendor" }); } const store = await Store.findOne({ vendorId: req.user._id }).select( "shippingOptions" ); if (!store) { return res.status(404).json({ message: "Store not found" }); } res.json(store.shippingOptions); } catch (error) { res .status(500) .json({ message: "Failed to fetch shipping options", error }); } }); /** * 📌 Add New Shipping Option for Vendor's Store * @route POST /api/shipping-options * @access Private (Vendors only) */ router.post("/", protectVendor, async (req, res) => { const { name, price } = req.body; if (!name || !price) { return res.status(400).json({ message: "Missing required fields" }); } try { // Find the store by vendorId (user) const store = await Store.findOne({ vendorId: req.user._id }); if (!store) { return res.status(404).json({ message: "Store not found" }); } // Add the new shipping option to the store's shippingOptions array store.shippingOptions.push({ name, price }); // Save the store with the new shipping option await store.save(); res.status(201).json(store.shippingOptions); } catch (error) { res.status(500).json({ message: "Failed to add shipping option", error }); } }); /** * 📌 Delete Shipping Option for Vendor's Store * @route DELETE /api/shipping-options/:id * @access Private (Vendors only) */ router.delete("/:id", protectVendor, async (req, res) => { const { id } = req.params; try { const store = await Store.findOne({ vendorId: req.user._id }); if (!store) { return res.status(404).json({ message: "Store not found" }); } // Use find to find the shipping option by its 'id' field const shippingOption = store.shippingOptions.find( (option) => option.id.toString() === id ); if (!shippingOption) { return res.status(404).json({ message: "Shipping option not found" }); } // Remove the shipping option const index = store.shippingOptions.indexOf(shippingOption); store.shippingOptions.splice(index, 1); // Remove the shipping option from the array await store.save(); // Save the updated store res.status(204).json({ message: "Shipping option deleted successfully" }); } catch (error) { console.error(error); res .status(500) .json({ message: "Failed to delete shipping option", error }); } }); /** * 📌 Edit Shipping Option for Vendor's Store * @route PUT /api/shipping-options/:id * @access Private (Vendors only) */ router.put("/:id", protectVendor, async (req, res) => { const { name, price } = req.body; const { id } = req.params; if (!name || !price) { return res.status(400).json({ message: "Missing required fields" }); } try { const store = await Store.findOne({ vendorId: req.user._id }); if (!store) { return res.status(404).json({ message: "Store not found" }); } // Use find to find the shipping option by its 'id' field const shippingOption = store.shippingOptions.find( (option) => option.id.toString() === id ); if (!shippingOption) { return res.status(404).json({ message: "Shipping option not found" }); } shippingOption.name = name; // Update the name shippingOption.price = price; // Update the price await store.save(); // Save the updated store res.json(shippingOption); // Return the updated shipping option } catch (error) { console.error(error); res .status(500) .json({ message: "Failed to update shipping option", error }); } }); export default router;