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

This commit is contained in:
2025-12-03 21:46:27 +01:00
parent 6fbd6a8545
commit 5441861cb3
2 changed files with 47 additions and 53 deletions

View File

@@ -7,10 +7,10 @@ class BasketItem(BaseModel):
Represents a single item in the user's basket. Represents a single item in the user's basket.
Attributes: 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. quantity (int): The number of units of the item.
""" """
item_id: str item_id: int
quantity: int quantity: int
@@ -19,12 +19,12 @@ class Item(BaseModel):
Represents an item available in the shop. Represents an item available in the shop.
Attributes: 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. name (str): The name of the item.
description (str): The description of the item. description (str): The description of the item.
unit_price (float): The unit price of the item. unit_price (float): The unit price of the item.
""" """
item_id: str item_id: int
name: str name: str
description: str description: str
unit_price: float unit_price: float

View File

@@ -175,46 +175,40 @@ class SupabaseClient:
Returns an Item pydantic model. Returns an Item pydantic model.
""" """
# First, validate the currency # First, validate the currency
self.validate_currency(currency=currency)
try: 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: except Exception as e:
self.logger.error(e) self.logger.error(f'Failed to fetch item {item_id}: {e}')
raise Exception from e raise ValueError(f"Failed to fetch item {item_id}: {e}")
# Fetch from items table try:
item_res = ( # Fetch price for this currency and item_id
self.supabase price = (
.table("items") self.supabase
.select("*") .table("item_prices")
.eq("id", item_id) .select("*")
.single() .eq("item_id", item_id)
.execute() .eq("currency", currency.upper())
) .single()
.execute()
if item_res.data is None: ).data
raise ValueError(f"Item {item_id} does not exist.") except Exception as e:
self.logger.error(f'Failed to fetch price for {item_id} in {currency}: {e}')
base_item = item_res.data raise ValueError(f"Failed to fetch price for {item_id} in {currency}: {e}")
# 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
# Return Item model # Return Item model
return Item( return Item(
id=base_item["id"], item_id=base_item["item_id"],
name=base_item["name"], name=base_item["name"],
description=base_item["description"], description=base_item["description"],
unit_price=price["unit_price"], 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 Validates that a currency exists in the available_currencies table
and is active. and is active.
@@ -237,20 +231,20 @@ class SupabaseClient:
Raises: Raises:
ValueError: If currency does not exist or is inactive. 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 = ( if result.data['active'] is not True:
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:
raise ValueError(f"Currency '{currency}' is currently not supported.") raise ValueError(f"Currency '{currency}' is currently not supported.")
return True return True