feat(loyalty): implement Phase 2 - company-wide points system

Complete implementation of loyalty module Phase 2 features:

Database & Models:
- Add company_id to LoyaltyProgram for chain-wide loyalty
- Add company_id to LoyaltyCard for multi-location support
- Add CompanyLoyaltySettings model for admin-controlled settings
- Add points expiration, welcome bonus, and minimum redemption fields
- Add POINTS_EXPIRED, WELCOME_BONUS transaction types

Services:
- Update program_service for company-based queries
- Update card_service with enrollment and welcome bonus
- Update points_service with void_points for returns
- Update stamp_service for company context
- Update pin_service for company-wide operations

API Endpoints:
- Admin: Program listing with stats, company detail views
- Vendor: Terminal operations, card management, settings
- Storefront: Customer card/transactions, self-enrollment

UI Templates:
- Admin: Programs dashboard, company detail, settings
- Vendor: Terminal, cards list, card detail, settings, stats, enrollment
- Storefront: Dashboard, history, enrollment, success pages

Background Tasks:
- Point expiration task (daily, based on inactivity)
- Wallet sync task (hourly)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-05 22:10:27 +01:00
parent 3bdf1695fd
commit d8f3338bc8
54 changed files with 7252 additions and 186 deletions

View File

@@ -2,7 +2,12 @@
"""
Loyalty program database model.
Defines the vendor's loyalty program configuration including:
Company-based loyalty program configuration:
- Program belongs to Company (one program per company)
- All vendors under a company share the same loyalty program
- Customers earn and redeem points at any location (vendor) within the company
Defines:
- Program type (stamps, points, hybrid)
- Stamp configuration (target, reward description)
- Points configuration (per euro rate, rewards catalog)
@@ -41,9 +46,13 @@ class LoyaltyType(str, enum.Enum):
class LoyaltyProgram(Base, TimestampMixin):
"""
Vendor's loyalty program configuration.
Company's loyalty program configuration.
Each vendor can have one loyalty program that defines:
Program belongs to Company (chain-wide shared points).
All vendors under a company share the same loyalty program.
Customers can earn and redeem at any vendor within the company.
Each company can have one loyalty program that defines:
- Program type and mechanics
- Stamp or points configuration
- Anti-fraud rules
@@ -54,19 +63,20 @@ class LoyaltyProgram(Base, TimestampMixin):
id = Column(Integer, primary_key=True, index=True)
# Vendor association (one program per vendor)
vendor_id = Column(
# Company association (one program per company)
company_id = Column(
Integer,
ForeignKey("vendors.id", ondelete="CASCADE"),
ForeignKey("companies.id", ondelete="CASCADE"),
unique=True,
nullable=False,
index=True,
comment="Company that owns this program (chain-wide)",
)
# Program type
loyalty_type = Column(
String(20),
default=LoyaltyType.STAMPS.value,
default=LoyaltyType.POINTS.value,
nullable=False,
)
@@ -96,9 +106,9 @@ class LoyaltyProgram(Base, TimestampMixin):
# =========================================================================
points_per_euro = Column(
Integer,
default=10,
default=1,
nullable=False,
comment="Points earned per euro spent",
comment="Points earned per euro spent (1 euro = X points)",
)
points_rewards = Column(
JSON,
@@ -107,6 +117,38 @@ class LoyaltyProgram(Base, TimestampMixin):
comment="List of point rewards: [{id, name, points_required, description}]",
)
# Points expiration and bonus settings
points_expiration_days = Column(
Integer,
nullable=True,
comment="Days of inactivity before points expire (None = never expire)",
)
welcome_bonus_points = Column(
Integer,
default=0,
nullable=False,
comment="Bonus points awarded on enrollment",
)
minimum_redemption_points = Column(
Integer,
default=100,
nullable=False,
comment="Minimum points required for any redemption",
)
minimum_purchase_cents = Column(
Integer,
default=0,
nullable=False,
comment="Minimum purchase amount (cents) to earn points (0 = no minimum)",
)
# Future tier configuration (Bronze/Silver/Gold)
tier_config = Column(
JSON,
nullable=True,
comment='Future: Tier thresholds {"bronze": 0, "silver": 1000, "gold": 5000}',
)
# =========================================================================
# Anti-Fraud Settings
# =========================================================================
@@ -151,7 +193,7 @@ class LoyaltyProgram(Base, TimestampMixin):
logo_url = Column(
String(500),
nullable=True,
comment="URL to vendor logo for card",
comment="URL to company logo for card",
)
hero_image_url = Column(
String(500),
@@ -210,7 +252,7 @@ class LoyaltyProgram(Base, TimestampMixin):
# =========================================================================
# Relationships
# =========================================================================
vendor = relationship("Vendor", back_populates="loyalty_program")
company = relationship("Company", backref="loyalty_program")
cards = relationship(
"LoyaltyCard",
back_populates="program",
@@ -224,11 +266,11 @@ class LoyaltyProgram(Base, TimestampMixin):
# Indexes
__table_args__ = (
Index("idx_loyalty_program_vendor_active", "vendor_id", "is_active"),
Index("idx_loyalty_program_company_active", "company_id", "is_active"),
)
def __repr__(self) -> str:
return f"<LoyaltyProgram(id={self.id}, vendor_id={self.vendor_id}, type='{self.loyalty_type}')>"
return f"<LoyaltyProgram(id={self.id}, company_id={self.company_id}, type='{self.loyalty_type}')>"
# =========================================================================
# Properties