feat(loyalty): refactor analytics into shared template and add merchant stats API
Some checks failed
CI / ruff (push) Successful in 11s
CI / validate (push) Has been cancelled
CI / dependency-scanning (push) Has been cancelled
CI / docs (push) Has been cancelled
CI / deploy (push) Has been cancelled
CI / pytest (push) Has been cancelled

Extract analytics stat cards, points activity, and location breakdown
into a shared partial used by admin, merchant, and store dashboards.
Add merchant stats API endpoint and client-side merchant filter on admin
analytics page. Extend stats schema with new_this_month and
estimated_liability_cents fields.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-11 11:08:16 +01:00
parent 8cf5da6914
commit 6acd783754
11 changed files with 630 additions and 375 deletions

View File

@@ -322,6 +322,52 @@ class ProgramService:
db.query(func.count(func.distinct(LoyaltyProgram.merchant_id))).scalar() or 0
)
# All-time points
total_points_issued = (
db.query(func.sum(LoyaltyTransaction.points_delta))
.filter(LoyaltyTransaction.points_delta > 0)
.scalar()
or 0
)
total_points_redeemed = (
db.query(func.sum(func.abs(LoyaltyTransaction.points_delta)))
.filter(LoyaltyTransaction.points_delta < 0)
.scalar()
or 0
)
# Outstanding points balance
total_points_balance = (
db.query(func.sum(LoyaltyCard.points_balance)).scalar() or 0
)
# New members this month
month_start = datetime.now(UTC).replace(day=1, hour=0, minute=0, second=0, microsecond=0)
new_this_month = (
db.query(func.count(LoyaltyCard.id))
.filter(LoyaltyCard.created_at >= month_start)
.scalar()
or 0
)
# Estimated liability (rough estimate: points / 100 as euros)
points_value_cents = total_points_balance // 100 * 100
# Stamp liability across all programs
stamp_liability = 0
programs = db.query(LoyaltyProgram).all()
for prog in programs:
stamp_value = prog.stamps_reward_value_cents or 0
stamps_target = prog.stamps_target or 1
current_stamps = (
db.query(func.sum(LoyaltyCard.stamp_count))
.filter(LoyaltyCard.program_id == prog.id)
.scalar()
or 0
)
stamp_liability += current_stamps * stamp_value // stamps_target
estimated_liability_cents = stamp_liability + points_value_cents
return {
"total_programs": total_programs,
"active_programs": active_programs,
@@ -331,6 +377,11 @@ class ProgramService:
"transactions_30d": transactions_30d,
"points_issued_30d": points_issued_30d,
"points_redeemed_30d": points_redeemed_30d,
"total_points_issued": total_points_issued,
"total_points_redeemed": total_points_redeemed,
"total_points_balance": total_points_balance,
"new_this_month": new_this_month,
"estimated_liability_cents": estimated_liability_cents,
}
def check_self_enrollment_allowed(self, db: Session, merchant_id: int) -> None:
@@ -843,6 +894,7 @@ class ProgramService:
}
thirty_days_ago = datetime.now(UTC) - timedelta(days=30)
month_start = datetime.now(UTC).replace(day=1, hour=0, minute=0, second=0, microsecond=0)
# Total cards
stats["total_cards"] = (
@@ -920,6 +972,37 @@ class ProgramService:
or 0
)
# New members this month
stats["new_this_month"] = (
db.query(func.count(LoyaltyCard.id))
.filter(
LoyaltyCard.merchant_id == merchant_id,
LoyaltyCard.created_at >= month_start,
)
.scalar()
or 0
)
# Estimated liability (unredeemed value)
current_stamps = (
db.query(func.sum(LoyaltyCard.stamp_count))
.filter(LoyaltyCard.merchant_id == merchant_id)
.scalar()
or 0
)
stamp_value = program.stamps_reward_value_cents or 0
stamps_target = program.stamps_target or 1
current_points = (
db.query(func.sum(LoyaltyCard.points_balance))
.filter(LoyaltyCard.merchant_id == merchant_id)
.scalar()
or 0
)
points_value_cents = current_points // 100 * 100
stats["estimated_liability_cents"] = (
(current_stamps * stamp_value // stamps_target) + points_value_cents
)
# Get all stores for this merchant for location breakdown
stores = store_service.get_stores_by_merchant_id(db, merchant_id)