Some checks failed
Phase 1 of the loyalty production launch plan: config & security hardening, dropped-data fix, DB integrity guards, rate limiting, and constant-time auth compare. 362 tests pass. - 1.4 Persist customer birth_date (new column + migration). Enrollment form was collecting it but the value was silently dropped because create_customer_for_enrollment never received it. Backfills existing customers without overwriting. - 1.1 LOYALTY_GOOGLE_SERVICE_ACCOUNT_JSON validated at startup (file must exist and be readable; ~ expanded). Adds is_google_wallet_enabled and is_apple_wallet_enabled derived flags. Prod path documented as ~/apps/orion/google-wallet-sa.json. - 1.5 CHECK constraints on loyalty_cards (points_balance, stamp_count non-negative) and loyalty_programs (min_purchase, points_per_euro, welcome_bonus non-negative; stamps_target >= 1). Mirrored as CheckConstraint in models. Pre-flight scan showed zero violations. - 1.3 @rate_limit on store mutating endpoints: stamp 60/min, redeem/points-earn 30-60/min, void/adjust 20/min, pin unlock 10/min. - 1.2 Constant-time hmac.compare_digest for Apple Wallet auth token (pulled forward from Phase 9 — code is safe whenever Apple ships). See app/modules/loyalty/docs/production-launch-plan.md for the full launch plan and remaining phases. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
91 lines
3.0 KiB
Python
91 lines
3.0 KiB
Python
# app/modules/customers/models/customer.py
|
|
"""
|
|
Customer database models.
|
|
|
|
Provides Customer and CustomerAddress models for store-scoped
|
|
customer management.
|
|
"""
|
|
|
|
from sqlalchemy import (
|
|
JSON,
|
|
Boolean,
|
|
Column,
|
|
Date,
|
|
ForeignKey,
|
|
Integer,
|
|
String,
|
|
)
|
|
from sqlalchemy.orm import relationship
|
|
|
|
from app.core.database import Base
|
|
from models.database.base import SoftDeleteMixin, TimestampMixin
|
|
|
|
|
|
class Customer(Base, TimestampMixin, SoftDeleteMixin):
|
|
"""Customer model with store isolation."""
|
|
|
|
__tablename__ = "customers"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
store_id = Column(Integer, ForeignKey("stores.id"), nullable=False)
|
|
email = Column(
|
|
String(255), nullable=False, index=True
|
|
) # Unique within store scope
|
|
hashed_password = Column(String(255), nullable=False)
|
|
first_name = Column(String(100))
|
|
last_name = Column(String(100))
|
|
phone = Column(String(50))
|
|
birth_date = Column(Date, nullable=True)
|
|
customer_number = Column(
|
|
String(100), nullable=False, index=True
|
|
) # Store-specific ID
|
|
preferences = Column(JSON, default=dict)
|
|
marketing_consent = Column(Boolean, default=False)
|
|
is_active = Column(Boolean, default=True, nullable=False)
|
|
|
|
# Language preference (NULL = use store storefront_language default)
|
|
# Supported: en, fr, de, lb
|
|
preferred_language = Column(String(5), nullable=True)
|
|
|
|
# Relationships
|
|
store = relationship("Store", back_populates="customers")
|
|
addresses = relationship("CustomerAddress", back_populates="customer")
|
|
orders = relationship("Order", back_populates="customer")
|
|
|
|
def __repr__(self):
|
|
return f"<Customer(id={self.id}, store_id={self.store_id}, email='{self.email}')>"
|
|
|
|
@property
|
|
def full_name(self):
|
|
if self.first_name and self.last_name:
|
|
return f"{self.first_name} {self.last_name}"
|
|
return self.email
|
|
|
|
|
|
class CustomerAddress(Base, TimestampMixin):
|
|
"""Customer address model for shipping and billing addresses."""
|
|
|
|
__tablename__ = "customer_addresses"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
store_id = Column(Integer, ForeignKey("stores.id"), nullable=False)
|
|
customer_id = Column(Integer, ForeignKey("customers.id"), nullable=False)
|
|
address_type = Column(String(50), nullable=False) # 'billing', 'shipping'
|
|
first_name = Column(String(100), nullable=False)
|
|
last_name = Column(String(100), nullable=False)
|
|
company = Column(String(200))
|
|
address_line_1 = Column(String(255), nullable=False)
|
|
address_line_2 = Column(String(255))
|
|
city = Column(String(100), nullable=False)
|
|
postal_code = Column(String(20), nullable=False)
|
|
country_name = Column(String(100), nullable=False)
|
|
country_iso = Column(String(5), nullable=False)
|
|
is_default = Column(Boolean, default=False)
|
|
|
|
# Relationships
|
|
store = relationship("Store")
|
|
customer = relationship("Customer", back_populates="addresses")
|
|
|
|
def __repr__(self):
|
|
return f"<CustomerAddress(id={self.id}, customer_id={self.customer_id}, type='{self.address_type}')>"
|