feat(loyalty): multi-select categories on transactions
Some checks failed
CI / ruff (push) Successful in 24s
CI / validate (push) Has been cancelled
CI / dependency-scanning (push) Has been cancelled
CI / deploy (push) Has been cancelled
CI / docs (push) Has been cancelled
CI / pytest (push) Has been cancelled

Switch from single category_id to category_ids JSON array on
transactions. Sellers can now select multiple categories (e.g.,
Men + Accessories) when entering stamp/points transactions.

- Migration loyalty_009: drop category_id FK, add category_ids JSON
- Schemas: category_id → category_ids (list[int] | None)
- Services: stamp_service + points_service accept category_ids
- Terminal UI: pills are now multi-select (toggle on/off)
- Transaction response: category_names (list[str]) resolved from IDs
- Recent transactions table: new Category column showing comma-
  separated names

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-19 21:36:49 +02:00
parent 220f7e3a08
commit 29593f4c61
11 changed files with 81 additions and 32 deletions

View File

@@ -836,8 +836,8 @@ class CardService:
"transaction_at": tx.transaction_at.isoformat() if tx.transaction_at else None,
"notes": tx.notes,
"store_name": None,
"category_id": tx.category_id,
"category_name": None,
"category_ids": tx.category_ids,
"category_names": None,
}
if tx.store_id:
@@ -845,15 +845,19 @@ class CardService:
if store_obj:
tx_data["store_name"] = store_obj.name
if tx.category_id:
if tx.category_ids and isinstance(tx.category_ids, list):
from app.modules.loyalty.services.category_service import (
category_service,
)
cat_name = category_service.validate_category_for_store(
db, tx.category_id, tx.store_id or 0
)
tx_data["category_name"] = cat_name
names = []
for cid in tx.category_ids:
name = category_service.validate_category_for_store(
db, cid, tx.store_id or 0
)
if name:
names.append(name)
tx_data["category_names"] = names if names else None
tx_responses.append(tx_data)

View File

@@ -49,7 +49,7 @@ class PointsService:
purchase_amount_cents: int,
order_reference: str | None = None,
staff_pin: str | None = None,
category_id: int | None = None,
category_ids: list[int] | None = None,
ip_address: str | None = None,
user_agent: str | None = None,
notes: str | None = None,
@@ -104,7 +104,7 @@ class PointsService:
raise OrderReferenceRequiredException()
# Category is mandatory when the store has categories configured
if not category_id:
if not category_ids:
from app.modules.loyalty.services.category_service import category_service
store_categories = category_service.list_categories(
@@ -196,7 +196,7 @@ class PointsService:
card_id=card.id,
store_id=store_id,
staff_pin_id=verified_pin.id if verified_pin else None,
category_id=category_id,
category_ids=category_ids,
transaction_type=TransactionType.POINTS_EARNED.value,
points_delta=points_earned,
stamps_balance_after=card.stamp_count,

View File

@@ -46,7 +46,7 @@ class StampService:
qr_code: str | None = None,
card_number: str | None = None,
staff_pin: str | None = None,
category_id: int | None = None,
category_ids: list[int] | None = None,
ip_address: str | None = None,
user_agent: str | None = None,
notes: str | None = None,
@@ -144,7 +144,7 @@ class StampService:
card_id=card.id,
store_id=store_id,
staff_pin_id=verified_pin.id if verified_pin else None,
category_id=category_id,
category_ids=category_ids,
transaction_type=TransactionType.STAMP_EARNED.value,
stamps_delta=1,
stamps_balance_after=card.stamp_count,