Skip to content

Commit

Permalink
Merge branch 'main' into component-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jhagendoornCP authored Jun 3, 2024
2 parents 51a621a + 109360b commit 4819a78
Show file tree
Hide file tree
Showing 28 changed files with 619 additions and 351 deletions.
31 changes: 30 additions & 1 deletion backend/routes/basketRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ router.get(
console.error("Error fetching basket:", error); // Log the error for debugging
res.status(500).send("Internal Server Error");
}
},
}
);

router.post("/", authenticateUser, async (req: Request, res: Response) => {
Expand Down Expand Up @@ -106,6 +106,35 @@ router.patch("/:id", authenticateUser, async (req: Request, res: Response) => {
}
});

router.patch(
"/:bid/removeitem",
authenticateUser,
async (req: Request, res: Response) => {
const { bid } = req.params;
const { item } = req.body;
try {
const b: IBasket | null = await Basket.findById(bid);
if (!b) return res.status(404).send("Basket not found.");

const newItemList = b.items.filter((id) => id.toString() !== item);
if (newItemList?.length === b?.items.length) {
return res.status(404).send("Item not found");
}

const partial: Partial<IBasket> = { items: newItemList };
const updatedBasket = await Basket.findByIdAndUpdate(bid, partial, {
new: true,
runValidators: true,
}).lean();

res.status(200).send(updatedBasket);
} catch (error) {
console.error("Error removing an item from the Basket:", error);
res.status(500).send("Internal Server Error");
}
}
);

router.delete("/:id", authenticateUser, async (req: Request, res: Response) => {
connectDB();
const { id } = req.params;
Expand Down
2 changes: 1 addition & 1 deletion backend/routes/groupRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ router.get(
res.status(500).send("Internal Server Error");
}
}
},
}
);

router.post("/", authenticateUser, async (req: Request, res: Response) => {
Expand Down
11 changes: 4 additions & 7 deletions backend/routes/itemRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,15 @@ router.get(
async (req: Request, res: Response) => {
// Ensure the database connection
connectDB();

try {
console.log("Here");

// Use findById correctly with the id parameter from the request
const itemById = await Item.findById(req.params.itemid);

// Check if group is null or undefined
// Check if item is null or undefined
if (!itemById) {
return res.status(404).send("No item found"); // Use return to exit the function after sending the response
}
// Send the found user
// Send the found item
res.send(itemById);
console.log("Sent item");
} catch (error) {
Expand All @@ -49,15 +46,15 @@ router.get(
return res.status(404).send("No items found"); // Use return to exit the function after sending the response
}

// Send the found user
// Send the found item
res.send(itemsByName);
console.log("Sent items");
} catch (error) {
console.error("Error fetching group:", error); // Log the error for debugging
res.status(500).send("Internal Server Error");
}
}
},
}
);

router.post("/", authenticateUser, async (req: Request, res: Response) => {
Expand Down
32 changes: 15 additions & 17 deletions backend/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
{
"compilerOptions": {
"lib": [
"es6"
],
"target": "es6",
"module": "ESNext",
"moduleResolution": "node",
"outDir": "dist",
"resolveJsonModule": true,
"emitDecoratorMetadata": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"sourceMap": true
},
"include": ["**/*.ts"],
"exclude": ["node_modules", "**/*.spec.ts"]
}
"compilerOptions": {
"lib": ["es6"],
"target": "es6",
"module": "ESNext",
"moduleResolution": "node",
"outDir": "dist",
"resolveJsonModule": true,
"emitDecoratorMetadata": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"sourceMap": true
},
"include": ["**/*.ts"],
"exclude": ["node_modules", "**/*.spec.ts"]
}
57 changes: 43 additions & 14 deletions frontend/lib/deletes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const handleDeleteAllBasketsAndItems = async (groupId: string) => {
} catch (error) {
console.error(
"There was an error deleting baskets and items in the group",
error,
error
);
}
};
Expand Down Expand Up @@ -81,7 +81,7 @@ export const handleDeleteBasket = async (basketId: string) => {
};
export const handleDeleteGroupFromUsers = async (
groupId: string,
userIds: string[],
userIds: string[]
) => {
try {
// Iterate over each userId
Expand All @@ -90,8 +90,7 @@ export const handleDeleteGroupFromUsers = async (
headers: {
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
}
);
});
if (response.ok) {
const user = await response.json();
const userGroups = user.groups;
Expand All @@ -112,7 +111,7 @@ export const handleDeleteGroupFromUsers = async (
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
body: JSON.stringify({ groups: updatedGroups }),
},
}
);

if (updateResponse.ok) {
Expand All @@ -131,22 +130,21 @@ export const handleDeleteGroupFromUsers = async (

export const handleDeleteBasketFromGroup = async (
groupId: string,
basketId: string,
basketId: string
) => {
try {
const response = await fetch(`${vite_backend_url}/groups/${groupId}`, {
headers: {
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
}
);
});
if (response.ok) {
const group = await response.json();
const groupBaskets = group.baskets;

// Remove the basketId from the group's baskets
const updatedBaskets = groupBaskets.filter(
(id: string) => id !== basketId,
(id: string) => id !== basketId
);

// Update the group object
Expand All @@ -162,7 +160,7 @@ export const handleDeleteBasketFromGroup = async (
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
body: JSON.stringify({ baskets: updatedBaskets }),
},
}
);

if (updateResponse.ok) {
Expand Down Expand Up @@ -206,7 +204,7 @@ export const handleDeleteAllItemsInBasket = async (basketId: string) => {

export const removeFriendFromUserByFriendId = async (
friendId: string,
userId: string,
userId: string
) => {
try {
const response = await fetch(
Expand All @@ -218,7 +216,7 @@ export const removeFriendFromUserByFriendId = async (
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
body: JSON.stringify({ friendId: friendId }),
},
}
);
if (!response.ok) {
throw new Error(`Error: ${response.statusText}`);
Expand All @@ -231,7 +229,7 @@ export const removeFriendFromUserByFriendId = async (

export const removeItemFromBasketAndDelete = async (
baskets: IBasket[],
item: IItem,
item: IItem
) => {
try {
baskets.forEach(async (basket) => {
Expand Down Expand Up @@ -259,7 +257,38 @@ export const removeItemFromBasketAndDelete = async (
} catch (error) {
console.error(
"There was an error removing the item from the basket",
error,
error
);
}
};

export const deleteItemWithBasketString = (item: IItem, bid: string = "") => {
if (!item.basket && bid === "") throw "Missing basket id.";
fetch(
`${vite_backend_url}/baskets/${item?.basket ? item.basket : bid}/removeitem`,
{
method: "PATCH",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
body: JSON.stringify({ item: item._id }),
}
)
.then((res) => {
if (res.status === 200) {
fetch(`${vite_backend_url}/items/${item._id}`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
}).then((response) => {
console.log("Response: ", response);
});
} else Promise.reject("failed to remove item from basket");
})
.catch((error) => {
console.log(error);
});
};
Loading

0 comments on commit 4819a78

Please sign in to comment.