Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: removed dead code and marked unused code #150

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion client/src/api/products/products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export async function getProductByProductId(
});
return response.data.products[0] as Product;
} catch (error: unknown) {
return errorResponse(error, 'api.products.getProductById') as MessageResponse | ErrorResponse;
return errorResponse(error, 'api.products.getProductByProductId') as
| MessageResponse
| ErrorResponse;
}
}
33 changes: 14 additions & 19 deletions client/src/components/Cart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,26 +120,21 @@ function Cart() {
};

const handleBuy = async () => {
const userres = await getUserById(user_Email);
// console.log('User:', user.user.user_address);
const user = (await getUserById(user_Email)) as User;
const { store_id } = product_details[cart.carts[0].product_id];
const storeres = await getStoreById(store_id);
if ('user' in userres && 'store' in storeres) {
const user = userres.user as User;
const store = storeres.store as Store;
const orderData = await {
amount_paid: Total,
delivery_address: user.user_address,
delivery_status: true,
email_id: user_Email,
product_list: cart.carts,
seller_id: store.seller_id,
store_id,
store_name: store.store_name,
user_id: user.user_id,
};
handlePayment(orderData as Omit<Order, 'order_id'>);
}
const store = (await getStoreById(store_id)) as Store;
const orderData = await {
amount_paid: Total,
delivery_address: user.user_address,
delivery_status: true,
email_id: user_Email,
product_list: cart.carts,
seller_id: store.seller_id,
store_id,
store_name: store.store_name,
user_id: user.user_id,
};
handlePayment(orderData as Omit<Order, 'order_id'>);
};

useEffect(() => {
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/Landing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export default function Landing() {

if ('product' in response) {
const productWithDiscount: Product & { discount?: number } = {
...(response.product as Product),
...(response as Product),
discount: offer.discount,
};

Expand Down
16 changes: 8 additions & 8 deletions client/src/components/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,20 @@ function Navbar() {
if ('error' in userResponse && 'error' in sellerResponse) {
navigate(`/createuser/${user.email}`);
} else if ('user' in userResponse && 'seller' in sellerResponse) {
appDispatch(setCartDataAsync(userResponse.user.user_id as string));
dispatch(authenticated(userResponse.user.user_id));
appDispatch(setCartDataAsync(userResponse.user_id as string));
dispatch(authenticated(userResponse.user_id));

dispatch(setUserEmail(userResponse.user.user_email));
dispatch(setUserEmail(userResponse.user_email));
dispatch(sellerAuthenticated());
dispatch(setSellerId(sellerResponse.seller.seller_id));
dispatch(setSellerId(sellerResponse.seller_id));
} else if ('user' in userResponse) {
appDispatch(setCartDataAsync(userResponse.user.user_id as string));
dispatch(authenticated(userResponse.user.user_id));
dispatch(setUserEmail(userResponse.user.user_email));
appDispatch(setCartDataAsync(userResponse.user_id as string));
dispatch(authenticated(userResponse.user_id));
dispatch(setUserEmail(userResponse.user_email));
navigate('/');
} else if ('seller' in sellerResponse) {
dispatch(sellerAuthenticated());
dispatch(setSellerId(sellerResponse.seller.seller_id));
dispatch(setSellerId(sellerResponse.seller_id));
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/ProductPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default function ProductPage() {
setErrorProduct(true);
setIsLoading(false);
} else if ('product' in response) {
setProduct(response.product as Product);
setProduct(response as Product);
setIsLoading(false);
}
}
Expand Down
11 changes: 4 additions & 7 deletions client/src/store/cart/cartSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,10 @@ export default cartSlice.reducer;
export const setCartDataAsync = (user_id: string) => async (dispatch: AppDispatch) => {
try {
const cart = await getUserCart(user_id);
if ('cart' in cart) {
const cartData = cart.cart;
if (cartData) {
dispatch(set(cartData as Cart));
} else {
errorResponse('Invalid cart data received', 'store.cart.setCartDataAsync');
}
if (cart) {
dispatch(set(cart as Cart));
} else {
errorResponse('Invalid cart data received', 'store.cart.setCartDataAsync');
}
} catch (error) {
errorResponse(Error.toString(), 'store.cart.setCartDataAsync');
Expand Down
13 changes: 0 additions & 13 deletions server/campus_hub/controllers/authentication.py
Original file line number Diff line number Diff line change
@@ -1,13 +0,0 @@
def register_user(request_data):
# Placeholder logic for user registration
return {"message": "User registered successfully"}


def login_user(request_data):
# Placeholder logic for user login
return {"message": "User logged in successfully"}


def forgot_password(request_data):
# Placeholder logic for password recovery
return {"message": "Password recovery initiated"}
11 changes: 0 additions & 11 deletions server/campus_hub/controllers/notifications.py
Original file line number Diff line number Diff line change
@@ -1,11 +0,0 @@
def get_notifications():
# Placeholder logic to get notifications for a user
return [
{"id": 1, "message": "Notification 1"},
{"id": 2, "message": "Notification 2"},
]


def get_notification_by_id(notification_id):
# Placeholder logic to get details of a specific notification by ID
return {"id": notification_id, "message": "Notification"}
7 changes: 1 addition & 6 deletions server/campus_hub/controllers/offers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@
from collections import Counter


def add_offer(request_data):
# Placeholder logic to add a new service
return {"message": "Offer added successfully"}


def get_offers() -> APIResponse:
def get_offers() -> APIResponse:
"""
Get a list of all offers from the MongoDB database.
Returns:
Expand Down
10 changes: 3 additions & 7 deletions server/campus_hub/controllers/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ def add_order() -> APIResponse:
)


def get_all_orders():
# TODO: Add the following function to the API spec with filters and pagination
def get_all_orders(): #! NOT IN API.YAML
"""
fetches all orders

Expand Down Expand Up @@ -162,13 +163,8 @@ def get_order_by_id(order_id):
Status.BAD_REQUEST, **message(f"Invalid order data: {str(ve)}")
)

return response(Status.SUCCESS, order.model_dump())
return response(Status.SUCCESS, **order.model_dump())
except PyMongoError as e:
return response(
Status.INTERNAL_SERVER_ERROR, **message(f"Internal Server Error: {str(e)}")
)


def cancel_order(order_id):
# Placeholder logic to cancel an order by ID
return {"message": "Order canceled successfully"}
1 change: 1 addition & 0 deletions server/campus_hub/controllers/payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def add_payment() -> APIResponse:
)


# TODO: Why do we need this method?
def verify_payment(request_data):
# Placeholder logic to verify payment status
return {"message": "Payment status verified successfully"}
98 changes: 2 additions & 96 deletions server/campus_hub/controllers/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def get_products() -> APIResponse:
)


def get_reviews_by_product_id(product_id) -> APIResponse:
def get_reviews_by_product_id(product_id) -> APIResponse: #
"""
Get reviews of a product by its product_id from the MongoDB database.
Args:
Expand Down Expand Up @@ -121,50 +121,7 @@ def get_reviews_by_product_id(product_id) -> APIResponse:
)


def get_product_cost(product_id) -> APIResponse:
"""
Get cost of a product by its product_id from the MongoDB database.
Args:
product_id (str): The product_id of the product to be fetched
Returns:
Flask response: JSON response containing the list of products.
"""

products_collection_name = "products"

# Query without including _id field in the result
query: dict = {}
query["product_id"] = product_id

try:
_products = db_connector.query_data(products_collection_name, query)

# If there are no products, return 404 error
if not _products or len(_products) == 0:
return response(
Status.NOT_FOUND, **message("No products found in the database.")
)

try:
products = Product(**_products[0])
except Exception as e:
return response(
Status.INTERNAL_SERVER_ERROR,
**message(f"Invalid product data in DB: {str(e)}"),
)

cost: float = products.product_cost

# If products are found, return a JSON response
return response(Status.SUCCESS, cost=cost)
except Exception as e:
return response(
Status.INTERNAL_SERVER_ERROR,
**message(f"Error retrieving product from MongoDB: {e}"),
)


def get_range_of_cost_in_store(store_id) -> APIResponse:
def get_range_of_cost_in_store(store_id) -> APIResponse: #! NOT IN API.YAML
"""
Get maximum and minimum cost of products in a store by its store_id from the MongoDB database.
Args:
Expand Down Expand Up @@ -194,54 +151,3 @@ def get_range_of_cost_in_store(store_id) -> APIResponse:
Status.INTERNAL_SERVER_ERROR,
**message(f"Error retrieving product from MongoDB: {e}"),
)


def search_products(str_query, service_id) -> APIResponse:
"""
Search for products in the MongoDB database based on string query.
Args:
str_query (str): The string query to be searched
service_id (str): The service_id of the product to be fetched
Returns:
Flask response: JSON response containing the list of products.
"""

products_collection_name = "products"

# Convert the input string query to a regular expression with case-insensitive option
regex_pattern = re.escape(" " + str_query) # Escape special characters in the query
regex_query = re.compile(f"^{regex_pattern}", re.IGNORECASE)

# Query without including _id field in the result
query: dict = {}
query["product_name"] = {"$regex": regex_query}
if service_id:
query["service_id"] = service_id
projection = {"_id": False}

try:
_products = db_connector.query_data(products_collection_name, query, projection)

# If there are no products, return 404 error
if not _products or len(_products) == 0:
return response(
Status.NOT_FOUND, **message("No products found in the database.")
)

try:
products = [Product(**product) for product in _products]
except Exception as e:
return response(
Status.INTERNAL_SERVER_ERROR,
**message(f"Invalid product data in DB: {str(e)}"),
)

# If products are found, return a JSON response
return response(
Status.SUCCESS, products=[product.model_dump() for product in products]
)
except Exception as e:
return response(
Status.INTERNAL_SERVER_ERROR,
**message(f"Error retrieving product from MongoDB: {e}"),
)
2 changes: 1 addition & 1 deletion server/campus_hub/controllers/sellers.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def get_seller_by_id(seller_email: str) -> APIResponse:
Status.BAD_REQUEST, **message(f"Invalid seller data in DB: {str(e)}")
)

return response(Status.SUCCESS, seller=seller.model_dump())
return response(Status.SUCCESS, **seller.model_dump())
except Exception as e:
return response(
Status.INTERNAL_SERVER_ERROR,
Expand Down
20 changes: 0 additions & 20 deletions server/campus_hub/controllers/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,6 @@ def add_service() -> APIResponse:
)


def get_service_by_id(service_id):
# Placeholder logic to get details of a specific service by ID
return {"id": service_id, "name": "Service", "description": "Description"}


def update_service(service_id: str) -> APIResponse:
"""
Updates a service to the MongoDB database.
Expand Down Expand Up @@ -253,26 +248,11 @@ def get_stores_by_service_id(service_id: str) -> APIResponse:
)


def update_store_by_service_id(service_id, request_data):
# Placeholder logic to update a service by ID
return {"message": "Service updated successfully"}


def delete_store_by_service_id(service_id):
# Placeholder logic to delete a service by ID
return {"message": "Service deleted successfully"}


def get_products_by_service_id(service_id):
# Placeholder logic to get details of a specific service by ID
return {"id": service_id, "name": "Service", "description": "Description"}


def add_store_by_service_id(service_id):
# Placeholder logic to get details of a specific service by ID
return {"id": service_id, "name": "Service", "description": "Description"}


def get_offers_by_service_id(service_id: str) -> APIResponse:
"""
Get offers by service id from the MongoDB database.
Expand Down
4 changes: 2 additions & 2 deletions server/campus_hub/controllers/stores.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def get_store_by_id(store_id) -> APIResponse:
)

# If stores are found, return a JSON response
return response(Status.SUCCESS, store=store.model_dump())
return response(Status.SUCCESS, **store.model_dump())
except Exception as e:
return response(
Status.INTERNAL_SERVER_ERROR,
Expand Down Expand Up @@ -566,7 +566,7 @@ def get_product_by_id(store_id, product_id) -> APIResponse:
)

# If products are found, return a JSON response
return response(Status.SUCCESS, product=product.model_dump())
return response(Status.SUCCESS, **product.model_dump())
except Exception as e:
return response(
Status.INTERNAL_SERVER_ERROR,
Expand Down
4 changes: 2 additions & 2 deletions server/campus_hub/controllers/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def get_user_by_id(user_email: str) -> APIResponse:
Status.BAD_REQUEST, **message(f"Invalid user data in DB: {str(e)}")
)

return response(Status.SUCCESS, user=user.model_dump())
return response(Status.SUCCESS, **user.model_dump())
except Exception as e:
return response(
Status.INTERNAL_SERVER_ERROR,
Expand Down Expand Up @@ -234,7 +234,7 @@ def get_cart_by_id(user_id: str) -> APIResponse:
Status.BAD_REQUEST, **message(f"Invalid cart data: {str(e)}")
)

return response(Status.SUCCESS, cart=cart.model_dump())
return response(Status.SUCCESS, **cart.model_dump())
except Exception as e:
return response(
Status.INTERNAL_SERVER_ERROR,
Expand Down
Loading