feat(arch): implement soft delete for business-critical models
Adds SoftDeleteMixin (deleted_at + deleted_by_id) with automatic query
filtering via do_orm_execute event. Soft-deleted records are invisible
by default; bypass with execution_options={"include_deleted": True}.
Models: User, Merchant, Store, StoreUser, Customer, Order, Product,
LoyaltyProgram, LoyaltyCard.
Infrastructure:
- SoftDeleteMixin in models/database/base.py
- Auto query filter registered on SessionLocal and test sessions
- soft_delete(), restore(), soft_delete_cascade() in app/core/soft_delete.py
- Alembic migration adding columns to 9 tables
- Partial unique indexes on users.email/username, stores.store_code/subdomain
Service changes:
- admin_service: delete_user, delete_store → soft_delete/soft_delete_cascade
- merchant_service: delete_merchant → soft_delete_cascade (stores→children)
- store_team_service: remove_team_member → soft_delete (fixes is_active bug)
- product_service: delete_product → soft_delete
- program_service: delete_program → soft_delete_cascade
Admin API:
- include_deleted/only_deleted query params on admin list endpoints
- PUT restore endpoints for users, merchants, stores
Tests: 9 unit tests for soft-delete infrastructure.
Docs: docs/backend/soft-delete.md + follow-up proposals.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -148,6 +148,8 @@ class MerchantService:
|
||||
search: str | None = None,
|
||||
is_active: bool | None = None,
|
||||
is_verified: bool | None = None,
|
||||
include_deleted: bool = False,
|
||||
only_deleted: bool = False,
|
||||
) -> tuple[list[Merchant], int]:
|
||||
"""
|
||||
Get paginated list of merchants with optional filters.
|
||||
@@ -159,15 +161,25 @@ class MerchantService:
|
||||
search: Search term for merchant name
|
||||
is_active: Filter by active status
|
||||
is_verified: Filter by verified status
|
||||
include_deleted: Include soft-deleted merchants
|
||||
only_deleted: Show only soft-deleted merchants (trash view)
|
||||
|
||||
Returns:
|
||||
Tuple of (merchants list, total count)
|
||||
"""
|
||||
exec_opts = {}
|
||||
if include_deleted or only_deleted:
|
||||
exec_opts["include_deleted"] = True
|
||||
|
||||
query = select(Merchant).options(
|
||||
joinedload(Merchant.stores),
|
||||
joinedload(Merchant.owner),
|
||||
)
|
||||
|
||||
# Soft-delete filter
|
||||
if only_deleted:
|
||||
query = query.where(Merchant.deleted_at.isnot(None))
|
||||
|
||||
# Apply filters
|
||||
if search:
|
||||
query = query.where(Merchant.name.ilike(f"%{search}%"))
|
||||
@@ -178,13 +190,13 @@ class MerchantService:
|
||||
|
||||
# Get total count
|
||||
count_query = select(func.count()).select_from(query.subquery())
|
||||
total = db.execute(count_query).scalar()
|
||||
total = db.execute(count_query, execution_options=exec_opts).scalar()
|
||||
|
||||
# Apply pagination and order
|
||||
query = query.order_by(Merchant.name).offset(skip).limit(limit)
|
||||
|
||||
# Use unique() when using joinedload with collections to avoid duplicate rows
|
||||
merchants = list(db.execute(query).scalars().unique().all())
|
||||
merchants = list(db.execute(query, execution_options=exec_opts).scalars().unique().all())
|
||||
|
||||
return merchants, total
|
||||
|
||||
@@ -228,11 +240,19 @@ class MerchantService:
|
||||
Raises:
|
||||
MerchantNotFoundException: If merchant not found
|
||||
"""
|
||||
from app.core.soft_delete import soft_delete_cascade
|
||||
|
||||
merchant = self.get_merchant_by_id(db, merchant_id)
|
||||
|
||||
# Due to cascade="all, delete-orphan", associated stores will be deleted
|
||||
db.delete(merchant)
|
||||
db.flush()
|
||||
MERCHANT_CASCADE = [
|
||||
("stores", [
|
||||
("products", []),
|
||||
("customers", []),
|
||||
("orders", []),
|
||||
("store_users", []),
|
||||
]),
|
||||
]
|
||||
soft_delete_cascade(db, merchant, deleted_by_id=None, cascade_rels=MERCHANT_CASCADE)
|
||||
logger.info(f"Deleted merchant ID {merchant_id} and associated stores")
|
||||
|
||||
def toggle_verification(
|
||||
|
||||
Reference in New Issue
Block a user