# app/modules/loyalty/services/loyalty_widgets.py """ Loyalty dashboard widget provider. Provides storefront dashboard cards for loyalty-related data. Implements get_storefront_dashboard_cards from DashboardWidgetProviderProtocol. """ import logging from sqlalchemy.orm import Session from app.modules.contracts.widgets import ( DashboardWidget, StorefrontDashboardCard, WidgetContext, ) logger = logging.getLogger(__name__) class LoyaltyWidgetProvider: """Widget provider for loyalty module.""" @property def widgets_category(self) -> str: return "loyalty" def get_store_widgets( self, db: Session, store_id: int, context: WidgetContext | None = None, ) -> list[DashboardWidget]: return [] def get_platform_widgets( self, db: Session, platform_id: int, context: WidgetContext | None = None, ) -> list[DashboardWidget]: return [] def get_storefront_dashboard_cards( self, db: Session, store_id: int, customer_id: int, context: WidgetContext | None = None, ) -> list[StorefrontDashboardCard]: """Provide the Loyalty Rewards card for the customer dashboard.""" from app.modules.loyalty.models.loyalty_card import LoyaltyCard from app.utils.i18n import translate card = ( db.query(LoyaltyCard) .filter( LoyaltyCard.customer_id == customer_id, LoyaltyCard.is_active.is_(True), ) .first() ) points = card.points_balance if card else None lang = context.language if context else None subtitle_key = ( "loyalty.widget.rewards.subtitle_member" if card else "loyalty.widget.rewards.subtitle_join" ) return [ StorefrontDashboardCard( key="loyalty.rewards", icon="gift", title=translate("loyalty.widget.rewards.title", lang), subtitle=translate(subtitle_key, lang), route="account/loyalty", value=points, value_label=( translate("loyalty.widget.rewards.value_label", lang) if points is not None else None ), order=30, ), ] loyalty_widget_provider = LoyaltyWidgetProvider()