refactor: move transaction management from services to API endpoints

- Services now use db.flush() instead of db.commit() for database operations
- API endpoints handle transaction commit after service calls
- Remove db.rollback() from services (let exception handlers manage this)
- Ensures consistent transaction boundaries at API layer

This pattern gives API endpoints full control over when to commit,
allowing for better error handling and potential multi-operation transactions.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-06 18:34:41 +01:00
parent 5d40551d98
commit 3520bcb069
33 changed files with 118 additions and 119 deletions

View File

@@ -62,7 +62,7 @@ class InventoryService:
old_qty = existing.quantity
existing.quantity = inventory_data.quantity
existing.updated_at = datetime.now(UTC)
db.commit()
db.flush()
db.refresh(existing)
logger.info(
@@ -79,7 +79,7 @@ class InventoryService:
gtin=product.marketplace_product.gtin, # Optional reference
)
db.add(new_inventory)
db.commit()
db.flush()
db.refresh(new_inventory)
logger.info(
@@ -143,7 +143,7 @@ class InventoryService:
gtin=product.marketplace_product.gtin,
)
db.add(new_inventory)
db.commit()
db.flush()
db.refresh(new_inventory)
logger.info(
@@ -165,7 +165,7 @@ class InventoryService:
existing.quantity = new_qty
existing.updated_at = datetime.now(UTC)
db.commit()
db.flush()
db.refresh(existing)
logger.info(
@@ -227,7 +227,7 @@ class InventoryService:
# Reserve inventory
inventory.reserved_quantity += reserve_data.quantity
inventory.updated_at = datetime.now(UTC)
db.commit()
db.flush()
db.refresh(inventory)
logger.info(
@@ -287,7 +287,7 @@ class InventoryService:
inventory.reserved_quantity -= reserve_data.quantity
inventory.updated_at = datetime.now(UTC)
db.commit()
db.flush()
db.refresh(inventory)
logger.info(
@@ -353,7 +353,7 @@ class InventoryService:
0, inventory.reserved_quantity - reserve_data.quantity
)
inventory.updated_at = datetime.now(UTC)
db.commit()
db.flush()
db.refresh(inventory)
logger.info(
@@ -507,7 +507,7 @@ class InventoryService:
inventory.location = self._validate_location(inventory_update.location)
inventory.updated_at = datetime.now(UTC)
db.commit()
db.flush()
db.refresh(inventory)
logger.info(f"Updated inventory {inventory_id}")
@@ -535,7 +535,7 @@ class InventoryService:
raise InventoryNotFoundException(f"Inventory {inventory_id} not found")
db.delete(inventory)
db.commit()
db.flush()
logger.info(f"Deleted inventory {inventory_id}")
return True