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,29 +175,25 @@ class SupabaseClient:
Returns an Item pydantic model. Returns an Item pydantic model.
""" """
# First, validate the currency # First, validate the currency
try: self.validate_currency(currency=currency)
ok = self.validate_currency(currency=currency)
except Exception as e:
self.logger.error(e)
raise Exception from e
try:
# Fetch from items table # Fetch from items table
item_res = ( base_item = (
self.supabase self.supabase
.table("items") .table("items")
.select("*") .select("*")
.eq("id", item_id) .eq("item_id", item_id)
.single() .single()
.execute() .execute()
) ).data
except Exception as e:
if item_res.data is None: self.logger.error(f'Failed to fetch item {item_id}: {e}')
raise ValueError(f"Item {item_id} does not exist.") raise ValueError(f"Failed to fetch item {item_id}: {e}")
base_item = item_res.data
try:
# Fetch price for this currency and item_id # Fetch price for this currency and item_id
price_res = ( price = (
self.supabase self.supabase
.table("item_prices") .table("item_prices")
.select("*") .select("*")
@@ -205,16 +201,14 @@ class SupabaseClient:
.eq("currency", currency.upper()) .eq("currency", currency.upper())
.single() .single()
.execute() .execute()
) ).data
except Exception as e:
if price_res.data is None: self.logger.error(f'Failed to fetch price for {item_id} in {currency}: {e}')
raise ValueError(f"Price for item {item_id} in {currency} does not exist.") raise ValueError(f"Failed to fetch price for {item_id} in {currency}: {e}")
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,7 +231,7 @@ class SupabaseClient:
Raises: Raises:
ValueError: If currency does not exist or is inactive. ValueError: If currency does not exist or is inactive.
""" """
try:
result = ( result = (
self.supabase self.supabase
.table("available_currencies") .table("available_currencies")
@@ -246,11 +240,11 @@ class SupabaseClient:
.single() .single()
.execute() .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}")
if result.data is None: if result.data['active'] is not True:
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