|
| 1 | +// controllers/productController.js |
| 2 | + |
| 3 | +import HistoryModel from "../models/history_model.js"; |
| 4 | +import LocationModel from "../models/locations_models.js"; |
| 5 | +import Product from "../models/product_model.js"; |
| 6 | + |
| 7 | +export const createProduct = async (req, res) => { |
| 8 | + if (req.user.role !== "admin") { |
| 9 | + return res.status(403).json({ message: "not authorized" }); |
| 10 | + } |
| 11 | + try { |
| 12 | + const { |
| 13 | + locationId, |
| 14 | + status, |
| 15 | + title, |
| 16 | + description, |
| 17 | + serialNo, |
| 18 | + rackMountable, |
| 19 | + isPart, |
| 20 | + manufacturer, |
| 21 | + model, |
| 22 | + warrantyMonths, |
| 23 | + dateOfPurchase, |
| 24 | + user, |
| 25 | + } = req.body; |
| 26 | + |
| 27 | + const history = new HistoryModel({ |
| 28 | + location: locationId, |
| 29 | + |
| 30 | + status: [ |
| 31 | + { |
| 32 | + name: status, |
| 33 | + }, |
| 34 | + ], |
| 35 | + }); |
| 36 | + |
| 37 | + await history.save(); |
| 38 | + |
| 39 | + const product = new Product({ |
| 40 | + title, |
| 41 | + description, |
| 42 | + serialNo, |
| 43 | + dateOfPurchase, |
| 44 | + createdBy: req.user._id, |
| 45 | + rackMountable, |
| 46 | + isPart, |
| 47 | + manufacturer, |
| 48 | + model, |
| 49 | + warrantyMonths, |
| 50 | + user, |
| 51 | + history: [history._id], |
| 52 | + }); |
| 53 | + |
| 54 | + await product.save(); |
| 55 | + res.status(201).json(product); |
| 56 | + } catch (error) { |
| 57 | + res.status(400).json({ error: error.message }); |
| 58 | + } |
| 59 | +}; |
| 60 | +export const getAllProducts = async (req, res) => { |
| 61 | + try { |
| 62 | + const { |
| 63 | + page = 1, |
| 64 | + itemsperpage = 10, |
| 65 | + search = "", |
| 66 | + manufacturer, |
| 67 | + } = req.query; |
| 68 | + |
| 69 | + let regex = new RegExp(search, "i"); |
| 70 | + |
| 71 | + const query = { |
| 72 | + $or: [ |
| 73 | + { title: regex }, |
| 74 | + { description: regex }, |
| 75 | + { serialNo: regex }, |
| 76 | + { model: regex }, |
| 77 | + ], |
| 78 | + }; |
| 79 | + |
| 80 | + if (manufacturer) { |
| 81 | + query.manufacturer = mongoose.Types.ObjectId(manufacturer); |
| 82 | + } |
| 83 | + |
| 84 | + const skipItems = (page - 1) * itemsperpage; |
| 85 | + |
| 86 | + const totalCount = await Product.countDocuments(query); |
| 87 | + const pages_count = Math.ceil(totalCount / itemsperpage); |
| 88 | + |
| 89 | + const products = await Product.find(query) |
| 90 | + .skip(skipItems) |
| 91 | + .limit(parseInt(itemsperpage)) |
| 92 | + .populate({ |
| 93 | + path: "history", |
| 94 | + populate: { |
| 95 | + path: "location", |
| 96 | + }, |
| 97 | + }) |
| 98 | + .populate("manufacturer"); |
| 99 | + |
| 100 | + res.status(200).json({ |
| 101 | + data: products, |
| 102 | + pages_count, |
| 103 | + currentPage: page, |
| 104 | + }); |
| 105 | + } catch (error) { |
| 106 | + res.status(500).json({ error: error.message }); |
| 107 | + } |
| 108 | +}; |
| 109 | + |
| 110 | +// Controller to get a single product by ID |
| 111 | +export const getProductById = async (req, res) => { |
| 112 | + try { |
| 113 | + const product = await Product.findById(req.params.id).populate("createdBy"); |
| 114 | + console.log("product", product); |
| 115 | + if (!product) { |
| 116 | + return res.status(404).json({ error: "Product not found" }); |
| 117 | + } |
| 118 | + res.status(200).json(product); |
| 119 | + } catch (error) { |
| 120 | + res.status(500).json({ error: error.message }); |
| 121 | + } |
| 122 | +}; |
| 123 | +export const getProductHistroy = async (req, res) => { |
| 124 | + try { |
| 125 | + const product = await Product.findById(req.params.id) |
| 126 | + .populate("createdBy") |
| 127 | + .populate({ |
| 128 | + path: "history", |
| 129 | + populate: { |
| 130 | + path: "location", |
| 131 | + }, |
| 132 | + }) |
| 133 | + .populate("manufacturer"); |
| 134 | + |
| 135 | + console.log("product", product); |
| 136 | + if (!product) { |
| 137 | + return res.status(404).json({ error: "Product not found" }); |
| 138 | + } |
| 139 | + res.status(200).json(product); |
| 140 | + } catch (error) { |
| 141 | + res.status(500).json({ error: error.message }); |
| 142 | + } |
| 143 | +}; |
| 144 | + |
| 145 | +// Controller to update a product by ID |
| 146 | +export const updateProductById = async (req, res) => { |
| 147 | + const productId = req.params.id; |
| 148 | + try { |
| 149 | + const { |
| 150 | + locationId, |
| 151 | + status, |
| 152 | + title, |
| 153 | + description, |
| 154 | + serialNo, |
| 155 | + createdBy, |
| 156 | + rackMountable, |
| 157 | + isPart, |
| 158 | + manufacturer, |
| 159 | + model, |
| 160 | + warrantyMonths, |
| 161 | + user, |
| 162 | + } = req.body; |
| 163 | + |
| 164 | + if (locationId) { |
| 165 | + // Create a new history entry for the location change |
| 166 | + const history = new HistoryModel({ |
| 167 | + location: locationId, |
| 168 | + status: [ |
| 169 | + { |
| 170 | + name: status, |
| 171 | + }, |
| 172 | + ], |
| 173 | + }); |
| 174 | + |
| 175 | + await history.save(); |
| 176 | + |
| 177 | + // Update product information and push the new history entry |
| 178 | + await Product.findByIdAndUpdate(productId, { |
| 179 | + $set: { |
| 180 | + title, |
| 181 | + description, |
| 182 | + serialNo, |
| 183 | + createdBy, |
| 184 | + rackMountable, |
| 185 | + isPart, |
| 186 | + manufacturer, |
| 187 | + model, |
| 188 | + warrantyMonths, |
| 189 | + user, |
| 190 | + }, |
| 191 | + $push: { |
| 192 | + history: history._id, |
| 193 | + }, |
| 194 | + }); |
| 195 | + } else if (status) { |
| 196 | + // Add a new status entry to the existing history |
| 197 | + const productRes = await Product.findById(productId); |
| 198 | + if (!productRes) { |
| 199 | + return res.status(404).json({ error: "Product not found" }); |
| 200 | + } |
| 201 | + const historyId = productRes.history[0]; |
| 202 | + const his = await HistoryModel.findById(historyId); |
| 203 | + |
| 204 | + // Push the new status object into the history |
| 205 | + his.status.push({ |
| 206 | + name: status, |
| 207 | + }); |
| 208 | + |
| 209 | + await his.save(); |
| 210 | + } else { |
| 211 | + console.log("Unhandled condition"); |
| 212 | + } |
| 213 | + |
| 214 | + res.status(200).json({ message: "success" }); |
| 215 | + } catch (error) { |
| 216 | + res.status(400).json({ error: error.message }); |
| 217 | + } |
| 218 | +}; |
| 219 | + |
| 220 | +// Controller to delete a product by ID |
| 221 | +export const deleteProductById = async (req, res) => { |
| 222 | + try { |
| 223 | + const product = await Product.findByIdAndDelete(req.params.id); |
| 224 | + if (!product) { |
| 225 | + return res.status(404).json({ error: "Product not found" }); |
| 226 | + } |
| 227 | + res.json({ message: "Product deleted successfully" }); |
| 228 | + } catch (error) { |
| 229 | + res.status(500).json({ error: error.message }); |
| 230 | + } |
| 231 | +}; |
0 commit comments