fix(loyalty): use SQL func.replace() for card number search

list_cards() was calling Python .replace() on a SQLAlchemy column
object instead of SQL func.replace(), causing AttributeError when
searching cards by card number.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-23 21:25:28 +01:00
parent 5a33f68743
commit fcde2d68fc

View File

@@ -393,12 +393,14 @@ class CardService:
query = query.filter(LoyaltyCard.is_active == is_active)
if search:
from sqlalchemy import func
# Normalize search term for card number matching
search_normalized = search.replace("-", "").replace(" ", "")
# Use relationship-based join to avoid direct Customer model import
CustomerModel = LoyaltyCard.customer.property.mapper.class_
query = query.join(LoyaltyCard.customer).filter(
(LoyaltyCard.card_number.replace("-", "").ilike(f"%{search_normalized}%"))
(func.replace(LoyaltyCard.card_number, "-", "").ilike(f"%{search_normalized}%"))
| (CustomerModel.email.ilike(f"%{search}%"))
| (CustomerModel.first_name.ilike(f"%{search}%"))
| (CustomerModel.last_name.ilike(f"%{search}%"))