improved connection to supabase DB
Some checks failed
Build and deploy the backend to staging / Build and push image (pull_request) Failing after 2m8s
Build and deploy the backend to staging / Deploy to staging (pull_request) Has been skipped
Run linting on the backend code / Build (pull_request) Failing after 2m22s
Run testing on the backend code / Build (pull_request) Failing after 2m28s
Some checks failed
Build and deploy the backend to staging / Build and push image (pull_request) Failing after 2m8s
Build and deploy the backend to staging / Deploy to staging (pull_request) Has been skipped
Run linting on the backend code / Build (pull_request) Failing after 2m22s
Run testing on the backend code / Build (pull_request) Failing after 2m28s
This commit is contained in:
@@ -7,10 +7,10 @@ class BasketItem(BaseModel):
|
||||
Represents a single item in the user's basket.
|
||||
|
||||
Attributes:
|
||||
item_id (str): The unique identifier for the item.
|
||||
item_id (int): The unique identifier for the item.
|
||||
quantity (int): The number of units of the item.
|
||||
"""
|
||||
item_id: str
|
||||
item_id: int
|
||||
quantity: int
|
||||
|
||||
|
||||
@@ -19,12 +19,12 @@ class Item(BaseModel):
|
||||
Represents an item available in the shop.
|
||||
|
||||
Attributes:
|
||||
item_id (str): The unique identifier for the item.
|
||||
item_id (int): The unique identifier for the item.
|
||||
name (str): The name of the item.
|
||||
description (str): The description of the item.
|
||||
unit_price (float): The unit price of the item.
|
||||
"""
|
||||
item_id: str
|
||||
item_id: int
|
||||
name: str
|
||||
description: str
|
||||
unit_price: float
|
||||
|
||||
@@ -175,46 +175,40 @@ class SupabaseClient:
|
||||
Returns an Item pydantic model.
|
||||
"""
|
||||
# First, validate the currency
|
||||
self.validate_currency(currency=currency)
|
||||
|
||||
try:
|
||||
ok = self.validate_currency(currency=currency)
|
||||
# Fetch from items table
|
||||
base_item = (
|
||||
self.supabase
|
||||
.table("items")
|
||||
.select("*")
|
||||
.eq("item_id", item_id)
|
||||
.single()
|
||||
.execute()
|
||||
).data
|
||||
except Exception as e:
|
||||
self.logger.error(e)
|
||||
raise Exception from e
|
||||
self.logger.error(f'Failed to fetch item {item_id}: {e}')
|
||||
raise ValueError(f"Failed to fetch item {item_id}: {e}")
|
||||
|
||||
# Fetch from items table
|
||||
item_res = (
|
||||
self.supabase
|
||||
.table("items")
|
||||
.select("*")
|
||||
.eq("id", item_id)
|
||||
.single()
|
||||
.execute()
|
||||
)
|
||||
|
||||
if item_res.data is None:
|
||||
raise ValueError(f"Item {item_id} does not exist.")
|
||||
|
||||
base_item = item_res.data
|
||||
|
||||
# Fetch price for this currency and item_id
|
||||
price_res = (
|
||||
self.supabase
|
||||
.table("item_prices")
|
||||
.select("*")
|
||||
.eq("item_id", item_id)
|
||||
.eq("currency", currency.upper())
|
||||
.single()
|
||||
.execute()
|
||||
)
|
||||
|
||||
if price_res.data is None:
|
||||
raise ValueError(f"Price for item {item_id} in {currency} does not exist.")
|
||||
|
||||
price = price_res.data
|
||||
try:
|
||||
# Fetch price for this currency and item_id
|
||||
price = (
|
||||
self.supabase
|
||||
.table("item_prices")
|
||||
.select("*")
|
||||
.eq("item_id", item_id)
|
||||
.eq("currency", currency.upper())
|
||||
.single()
|
||||
.execute()
|
||||
).data
|
||||
except Exception as e:
|
||||
self.logger.error(f'Failed to fetch price for {item_id} in {currency}: {e}')
|
||||
raise ValueError(f"Failed to fetch price for {item_id} in {currency}: {e}")
|
||||
|
||||
# Return Item model
|
||||
return Item(
|
||||
id=base_item["id"],
|
||||
item_id=base_item["item_id"],
|
||||
name=base_item["name"],
|
||||
description=base_item["description"],
|
||||
unit_price=price["unit_price"],
|
||||
@@ -223,7 +217,7 @@ class SupabaseClient:
|
||||
)
|
||||
|
||||
|
||||
def validate_currency(self, currency: str) -> dict:
|
||||
def validate_currency(self, currency: str) -> bool:
|
||||
"""
|
||||
Validates that a currency exists in the available_currencies table
|
||||
and is active.
|
||||
@@ -237,20 +231,20 @@ class SupabaseClient:
|
||||
Raises:
|
||||
ValueError: If currency does not exist or is inactive.
|
||||
"""
|
||||
try:
|
||||
result = (
|
||||
self.supabase
|
||||
.table("available_currencies")
|
||||
.select("*")
|
||||
.eq("currency", currency.upper())
|
||||
.single()
|
||||
.execute()
|
||||
)
|
||||
except Exception as exc:
|
||||
self.logger.error(f'Unable to validate currency {currency}, most likely not supported yet: {exc}')
|
||||
raise ValueError(f"Could not validate '{currency}', most likely not supported yet: {exc}")
|
||||
|
||||
result = (
|
||||
self.supabase
|
||||
.table("available_currencies")
|
||||
.select("*")
|
||||
.eq("currency", currency.upper())
|
||||
.single()
|
||||
.execute()
|
||||
)
|
||||
|
||||
if result.data is None:
|
||||
raise ValueError(f"Currency '{currency}' is not supported yet.")
|
||||
|
||||
if result.data.get("active") is not True:
|
||||
if result.data['active'] is not True:
|
||||
raise ValueError(f"Currency '{currency}' is currently not supported.")
|
||||
|
||||
return True
|
||||
return True
|
||||
|
||||
Reference in New Issue
Block a user