Skip to content

Conversation

@sahilphulwani
Copy link
Contributor

No description provided.


exports.getMenu = async (req, res) => {
try {
const menu = await menuService.getMenu(req.params.restaurantId);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

menuService is not imported into the controller file but is used throughout.

return res.status(404).json({ error: result.error });
}
res
.status(500)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The wrong status code of 500 instead of 200 is returned. This will confuse the client into thinking the request failed.

};

async function writeRestaurants(restaurants) {
await fs.writeFile(writePath, restaurants);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Data should be stringified before writing using JSON.stringify, or the file content will be invalid.

try {
const restaurants = await readRestaurants();

const restaurant = restaurants.find((r) => r.id === restaurantId);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use findIndex instead of find to get the correct index. Using find returns the object, causing an error when accessing restaurants[restaurant].menu.

let updatedItem = null;

for (const restaurant of restaurants) {
const menuItem = restaurants.menu?.find((item) => item.itemId === itemId);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: It should be restaurant instead of restaurants. This will result in undefined property access.

for (const restaurant of restaurants) {
const menuItem = restaurants.menu?.find((item) => item.itemId === itemId);
if (menuItem) {
Object.assign(null, updatedData);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using Object.assign with null as the target will throw a TypeError. The first parameter should be menuItem.

};

exports.updateAvailability = async (itemId, availability) => {
return exports.updateMenuItem(itemId, { availability });
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing await for updateMenuItem will return an unresolved Promise.

for (const restaurant of restaurants) {
const index = restaurant.menu.findIndex((item) => item.itemId === itemId);
if (index !== -1) {
restaurant.menu.slice(index, 1);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using slice instead of splice will not modify the original array, so the menu item will not be removed.

if (index !== -1) {
restaurant.menu.slice(index, 1);
await writeRestaurants(restaurants);
return;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning undefined instead of the menu: { menu: restaurant.menu }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants