Skip to content

Commit

Permalink
MEGA COMMIT!
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaloney111 committed Jun 7, 2024
1 parent 00c7e4a commit 8959ee9
Show file tree
Hide file tree
Showing 17 changed files with 241 additions and 144 deletions.
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"axios": "^1.6.8",
"bcrypt": "^5.1.1",
"buffer": "^6.0.3",
"chart.js": "^4.4.3",
"dotenv": "^16.4.5",
"eslint-config-google": "^0.14.0",
"js-cookie": "^3.0.5",
Expand Down
26 changes: 20 additions & 6 deletions packages/express-backend/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ app.post("/users/profile", userService.authenticateUser, (req, res) => {
});
});

app.get("/user-stats", userService.authenticateUser, (req, res) => {
const id = req.userID;
userService.getProductOrderCounts(id).then(result => {
res.send(result)
})
})

app.get("/users", userService.authenticateUser, (req, res) => {
const id = req.userID;
userService
Expand Down Expand Up @@ -294,13 +301,13 @@ app.get("/orders/:find", userService.authenticateUser, (req, res) => {
});

app.post("/orders", userService.authenticateUser, (req, res) => {
//console.log(req.body);
const orderToAdd = req.body;
console.log(orderToAdd);
orderService
.addOrder(orderToAdd)
.then((result) => {
const UserID = req.userID;
userService.addOrderToUser(UserID, result.id);
userService.addOrderToUser(UserID, result);
console.log(result);
res.status(201).send(result);
})
Expand Down Expand Up @@ -359,10 +366,17 @@ app.post("/order-units", userService.authenticateUser, (req, res) => {
console.log(orderToAdd);
userService.hasProduct(UserID, orderToAdd.product).then(has => {
if (has) {
orderUnitService
.addOrder(orderToAdd)
.then((result) => {
res.status(201).send(result);
userService.quantityCheck(UserID, orderToAdd.product, orderToAdd.quantity).then(good => {
if (good == true) {
orderUnitService
.addOrder(orderToAdd)
.then((result) => {
res.status(201).send(result);
})
}
else {
res.status(400).send(good.toString());
}
})
} else {
res.status(204).send(orderToAdd.product);
Expand Down
1 change: 0 additions & 1 deletion packages/express-backend/services/order-unit-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ function removeOrder(id) {
}

function addOrder(order_unit) {
//console.log(order_unit);
const OrderToAdd = new OrderUnitModel(order_unit);
const promise = OrderToAdd.save();
return promise;
Expand Down
3 changes: 0 additions & 3 deletions packages/express-backend/services/product-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ function addProduct(product) {
$set: { price: product.price }
};

console.log(product.quantity);
console.log(product);
// Only include $inc for quantity if it is not null
if (product.quantity !== null && product.quantity !== undefined && product.quantity !== "") {
updateObject.$inc = { quantity: product.quantity };
}
Expand Down
72 changes: 63 additions & 9 deletions packages/express-backend/services/user-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,22 +199,58 @@ function removeProductFromUserID(userID, productIDToRemove) {
return UserModel.updateOne({ _id: userID }, { $pull: { products: productIDToRemove } });
}

async function addOrderToUser(id, orderId) {
async function addOrderToUser(id, order) {
try {
const user = await UserModel.findById(id);
const user = await UserModel.findById(id).populate('products');
if (!user) {
throw new Error("User not found");
}
for (const item of order.items) {
const product = user.products.find(prod => prod.product === item.product);

user.orders.push(orderId);
if (!product) {
throw new Error(`Product ${item.product} not found for user ${id}`);
}
product.quantity -= item.quantity;
await product.save();
}
user.orders.push(order.id);
await user.save();

console.log(`Product ${orderId} added to user ${id}`);
console.log(`Order ${order.id} added to user ${id}`);
} catch (error) {
console.error("Error adding product to user:", error);
console.error("Error adding order to user:", error);
}
}

async function getProductOrderCounts(userId) {
try {
const user = await UserModel.findById(userId).populate('products orders');

if (!user) {
throw new Error("User not found");
}


const productOrderCounts = {};
for (const product of user.products) {
let productCount = 0;
for (const order of user.orders) {
const orderContainsProduct = order.items.some(item => item.product === product.product);
if (orderContainsProduct) {
productCount++;
}
}
productOrderCounts[product.product] = productCount;
}
return productOrderCounts;
} catch (error) {
console.error("Error getting product order counts:", error);
throw error;
}
}


function removeOrderFromUserID(userID, orderIDToRemove) {
return UserModel.updateOne({ _id: userID }, { $pull: { orders: orderIDToRemove } });
}
Expand All @@ -224,12 +260,28 @@ async function hasProduct(userID, productName) {
if (!user || !user.products) {
return false;
}
console.log("delapaz")
console.log(user.products)
console.log(productName)
return user.products.some(product => product.product === productName);
}

async function quantityCheck(userID, productName, desiredQuantity) {
const user = await findUserById(userID).populate("products");
if (!user || !user.products) {
return false;
}
let product = null;
for (let i = 0; i < user.products.length; i++) {
if (user.products[i].product === productName) {
product = user.products[i];
break;
}
}
if (product.quantity < desiredQuantity) {
return product.quantity;
}
return true;
}


export default {
addUser,
removeUser,
Expand All @@ -246,5 +298,7 @@ export default {
removeProductFromUserID,
addOrderToUser,
removeOrderFromUserID,
hasProduct
hasProduct,
quantityCheck,
getProductOrderCounts
};
32 changes: 30 additions & 2 deletions packages/react-frontend/src/Components/Navbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import ProductPage from "../Views/ProductPage";
import AboutUs from "../Views/AboutUs";
import TermsAndConds from "../Views/TermsAndConds";
import EditProfile from "../Views/EditProfile";

import "../Styles/Navbar.css";

function App() {
Expand Down Expand Up @@ -73,7 +74,7 @@ function NavBar() {
<Link to="/manage-orders">Manage Orders</Link>
</li>
<li className="dropdown-item">
<Link to="/add-orders">&nbsp;&nbsp;&nbsp;Add Orders</Link>
<Link to="/add-orders">Add Orders</Link>
</li>
</ul>
</li>
Expand All @@ -100,6 +101,7 @@ function NavBar() {
);
}


function Footer() {
return (
<footer className="footer">
Expand All @@ -117,7 +119,33 @@ function Footer() {
}

function HomePage() {
return <h1 className="title">Home page</h1>;
return (
<div>
<h1 className="title">Home page</h1>
<div className="widgets">
<div className="widget-square">
<Link to="/inventory">
<h3>Inventory</h3>
</Link>
</div>
<div className="widget-square">
<Link to="/manage-orders">
<h3>Manage Orders</h3>
</Link>
</div>
<div className="widget-square">
<Link to="/add-orders">
<h3>Add Orders</h3>
</Link>
</div>
<div className="widget-square">
<Link to="/statistics">
<h3>Order Statistics</h3>
</Link>
</div>
</div>
</div>
);
}

export default App;
25 changes: 25 additions & 0 deletions packages/react-frontend/src/Styles/Navbar.css
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,29 @@
border-radius: 8px;
background-color: #f9f9f9;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.widgets {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-gap: 20px;
padding-bottom: 100px;
margin-top: -150px;
}

.widget-square {
background-color: #2980b9;
color: white;
text-align: center;
padding: 20px;
border-radius: 10px;
cursor: pointer;
}

.widget-square:hover {
background-color: #3498db;
}

.widget-square h3 {
margin: 0;
}
32 changes: 10 additions & 22 deletions packages/react-frontend/src/Views/AddOrders.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,18 @@ function AddOrders() {
if (res.status === 201) {
return res.json();
} else if (res.status === 204) {
alert(`Invalid order: ${order.product}`);
alert(`Invalid order for non-existent product: ${order.product}`);
return;
} else if (res.status === 400) {
res.json().then(quantity => {
alert(`There's not enough ${order.product} in stock! There's only ${quantity} left`)
})
return;
} else {
return;
}
})
.then((res) => {
// res = JSON.stringify(res).split(",");
// const item_count = Number(
// res[res.length - 3].slice(res[res.length - 3].indexOf('"item_count":') + 13)
// );
// let temp_list = res;
// for (let i = 1; i < 3 * item_count; i++) {
// let combo = temp_list[0] + ", " + temp_list[1];
// temp_list = temp_list.slice(2);
// temp_list.unshift(combo);
// }
// res = temp_list;
// let temp = res[0].slice(1);
// let temp2 = res[1];
// res[0] = "{" + res[2];
// res[1] = temp;
// res[2] = temp2;
// res = res.slice(1).reduce((accumulator, cur_val) => accumulator + ", " + cur_val, res[0]);
if (res !== undefined) {
setOrders([...orders, res]);
}
Expand All @@ -80,13 +68,13 @@ function AddOrders() {
}

function fetchOrders() {
return fetch("http://localhost:8000/order-units", {
return fetch("https://safehavenapp.azurewebsites.net//order-units", {
headers: addAuthHeader()
});
}

function postOrderUnit(order) {
return fetch("http://localhost:8000/order-units", {
return fetch("https://safehavenapp.azurewebsites.net//order-units", {
method: "POST",
headers: addAuthHeader({
"Content-Type": "application/json"
Expand All @@ -96,7 +84,7 @@ function AddOrders() {
}

function deleteOrder(id) {
const uri = `http://localhost:8000/order-units/${id}`;
const uri = `https://safehavenapp.azurewebsites.net//order-units/${id}`;
return fetch(uri, {
method: "DELETE",
headers: addAuthHeader({
Expand All @@ -106,7 +94,7 @@ function AddOrders() {
}

function runPost(order_str) {
return fetch("http://localhost:8000/orders", {
return fetch("https://safehavenapp.azurewebsites.net//orders", {
method: "POST",
headers: addAuthHeader({
"Content-Type": "application/json"
Expand Down
2 changes: 1 addition & 1 deletion packages/react-frontend/src/Views/EditProfile.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useNavigate } from "react-router-dom";
function EditProfile() {
const navigate = useNavigate();
function changeProfile(profile){
fetch("http://localhost:8000/users/profile", {
fetch("https://safehavenapp.azurewebsites.net//users/profile", {
method: "POST",
headers: addAuthHeader({
"Content-Type": "application/json"
Expand Down
Loading

0 comments on commit 8959ee9

Please sign in to comment.