33 lines
1.2 KiB
Python
33 lines
1.2 KiB
Python
from collections import defaultdict
|
|
import math
|
|
from services.DealsService import DealsService
|
|
from services.ProductService import ProductService
|
|
|
|
|
|
class BasketService:
|
|
baskets = None
|
|
dealsService = None
|
|
productService = None
|
|
|
|
def __init__(self) -> None:
|
|
self.baskets = defaultdict()
|
|
self.dealsService = DealsService()
|
|
self.productService = ProductService()
|
|
|
|
def total(self, sessionID: str) -> int:
|
|
# this function should really be in a separate file but I am already over time
|
|
# this round up function needs to be checked with stakeholders
|
|
# as it would generate millions in profit in a big online shop,
|
|
# but could be considered unfriendly towards consumers
|
|
def _round_up(val: float):
|
|
return math.ceil(val * 100) / 100
|
|
items = self.baskets[sessionID]
|
|
prices = self.dealsService.get_items_with_final_prices(items)
|
|
return _round_up(sum([item.price for item in prices]))
|
|
|
|
def scan(self, session_id: str, itemId) -> None:
|
|
item = self.productService.get_item_from_id(itemId)
|
|
if session_id not in self.baskets:
|
|
self.baskets[session_id] = []
|
|
self.baskets[session_id].append(item)
|