repository.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from typing import Protocol
  2. from app.domains.attribution.models import (
  3. LeadAttribution,
  4. OrderAttribution,
  5. PromotionCode,
  6. Salesperson,
  7. )
  8. class AttributionRepository(Protocol):
  9. def save_salesperson(self, salesperson: Salesperson) -> None: ...
  10. def list_salespersons(self) -> list[Salesperson]: ...
  11. def save_promotion_code(self, promotion_code: PromotionCode) -> None: ...
  12. def get_promotion_code(self, code: str) -> PromotionCode | None: ...
  13. def list_promotion_codes(self) -> list[PromotionCode]: ...
  14. def save_lead(self, lead: LeadAttribution) -> None: ...
  15. def get_lead_by_user(self, h5_user_id: str) -> LeadAttribution | None: ...
  16. def list_leads(self) -> list[LeadAttribution]: ...
  17. def save_order_attribution(self, attribution: OrderAttribution) -> None: ...
  18. def get_order_attribution(self, order_id: str) -> OrderAttribution | None: ...
  19. def list_order_attributions(self) -> list[OrderAttribution]: ...
  20. class InMemoryAttributionRepository:
  21. def __init__(self) -> None:
  22. self._salespersons: dict[str, Salesperson] = {}
  23. self._promotion_codes: dict[str, PromotionCode] = {}
  24. self._leads: dict[str, LeadAttribution] = {}
  25. self._orders: dict[str, OrderAttribution] = {}
  26. def save_salesperson(self, salesperson: Salesperson) -> None:
  27. self._salespersons[salesperson.id] = salesperson
  28. def list_salespersons(self) -> list[Salesperson]:
  29. return sorted(self._salespersons.values(), key=lambda item: item.code)
  30. def save_promotion_code(self, promotion_code: PromotionCode) -> None:
  31. self._promotion_codes[promotion_code.code.upper()] = promotion_code
  32. def get_promotion_code(self, code: str) -> PromotionCode | None:
  33. return self._promotion_codes.get(code.upper())
  34. def list_promotion_codes(self) -> list[PromotionCode]:
  35. return sorted(self._promotion_codes.values(), key=lambda item: item.code)
  36. def save_lead(self, lead: LeadAttribution) -> None:
  37. self._leads[lead.h5_user_id] = lead
  38. def get_lead_by_user(self, h5_user_id: str) -> LeadAttribution | None:
  39. return self._leads.get(h5_user_id)
  40. def list_leads(self) -> list[LeadAttribution]:
  41. return list(self._leads.values())
  42. def save_order_attribution(self, attribution: OrderAttribution) -> None:
  43. self._orders[attribution.order_id] = attribution
  44. def get_order_attribution(self, order_id: str) -> OrderAttribution | None:
  45. return self._orders.get(order_id)
  46. def list_order_attributions(self) -> list[OrderAttribution]:
  47. return list(self._orders.values())