Skip to content

Commit bb36b30

Browse files
committed
initial
0 parents  commit bb36b30

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+12845
-0
lines changed

Backend/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules
2+
.env

Backend/README.md

Whitespace-only changes.

Backend/app.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import express from "express";
2+
import { connectdb } from "./db/user_db.js";
3+
import userRouter from "./routes/user_routes.js";
4+
import dotenv from "dotenv";
5+
import cookieParser from "cookie-parser";
6+
import cors from "cors";
7+
import productRouter from "./routes/productRoutes.js";
8+
import companyRouter from "./routes/companyRoutes.js";
9+
import locationRouter from "./routes/locationRoutes.js";
10+
import analyticsRoutes from "./routes/analyticsRoutes.js";
11+
12+
dotenv.config();
13+
14+
const app = express();
15+
app.use(
16+
cors({
17+
// methods: ["GET", "POST", "PUT", "PATCH", "DELETE"],
18+
// credentials: true,
19+
// preflightContinue: false,
20+
origin: true,
21+
methods: ["GET", "POST", "PUT", "PATCH", "DELETE"],
22+
credentials: true,
23+
})
24+
);
25+
26+
app.use(cookieParser());
27+
app.use(express.json());
28+
app.use(express.urlencoded());
29+
connectdb();
30+
31+
app.use("/api/v1/users", userRouter);
32+
app.use("/api/v1/products", productRouter);
33+
app.use("/api/v1/brands", companyRouter);
34+
app.use("/api/v1/location", locationRouter);
35+
app.use("/api/v1/analytics", analyticsRoutes);
36+
37+
app.use(express.urlencoded({ extended: true }));
38+
39+
// console.log(process.env.FRONTEND_URL);
40+
41+
app.get("/", (req, res) => {
42+
res.send("<h1>working nicely</h1>");
43+
});
44+
45+
app.use((error, req, res, next) => {
46+
console.log(error, error.message);
47+
return res.status(400).json({ message: "internal server error" });
48+
});
49+
50+
app.listen(process.env.PORT, () => {
51+
console.log(
52+
`server is working at port:${process.env.PORT} in ${process.env.NODE_ENV} mode`
53+
);
54+
});

Backend/config.npmrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
proxy=null
2+
https-proxy=null
Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
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

Comments
 (0)