1010from pathlib import Path
1111from typing import Any , Dict , List , Optional
1212
13- from ..categories import DEFAULT_CATEGORY_GROUPS
13+ from ..categories import get_effective_category_groups
1414from .base import FinanceBackend
1515
1616
@@ -25,19 +25,21 @@ class AmazonBackend(FinanceBackend):
2525 imported from CSV files exported from Amazon.com.
2626 """
2727
28- def __init__ (self , db_path : Optional [str ] = None ):
28+ def __init__ (self , db_path : Optional [str ] = None , config_dir : Optional [ str ] = None ):
2929 """
3030 Initialize the Amazon backend.
3131
3232 Args:
3333 db_path: Path to SQLite database. Defaults to ~/.moneyflow/amazon.db
34+ config_dir: Config directory for loading categories. Defaults to ~/.moneyflow
3435
3536 Note: Database file is not created until first access (lazy initialization).
3637 """
3738 if db_path is None :
3839 db_path = str (Path .home () / ".moneyflow" / "amazon.db" )
3940
4041 self .db_path = Path (db_path ).expanduser ()
42+ self .config_dir = config_dir
4143 self ._db_initialized = False
4244
4345 def _ensure_db_initialized (self ) -> None :
@@ -282,19 +284,24 @@ async def get_transactions(
282284
283285 async def get_transaction_categories (self ) -> Dict [str , Any ]:
284286 """
285- Fetch all categories from centralized category list .
287+ Fetch all categories from config.yaml (or defaults if not present) .
286288
287- Returns categories from categories.py (not database) to avoid data duplication
288- and allow easy updates via config file.
289+ Returns categories from config.yaml if available (e.g., fetched from Monarch),
290+ otherwise uses built-in defaults. This allows Amazon mode to use the same
291+ category structure as your primary backend.
289292
290293 Returns:
291294 Dictionary containing categories in standard format
292295 """
293296 categories = []
294297 cat_id_counter = 1
295298
296- # Build categories from DEFAULT_CATEGORY_GROUPS
297- for group_name , category_names in DEFAULT_CATEGORY_GROUPS .items ():
299+ # Load category groups from config.yaml if available, otherwise use built-in defaults
300+ # Note: This is NOT a merge - it's one or the other (priority: config.yaml > defaults)
301+ category_groups = get_effective_category_groups (self .config_dir )
302+
303+ # Build categories from loaded category groups
304+ for group_name , category_names in category_groups .items ():
298305 for cat_name in category_names :
299306 cat_id = f"cat_{ cat_name .lower ().replace (' ' , '_' ).replace ('&' , 'and' )} "
300307 categories .append (
@@ -348,11 +355,12 @@ async def update_transaction(
348355 if category_id is not None :
349356 updates .append ("category_id = ?" )
350357 params .append (category_id )
351- # Also update category name from DEFAULT_CATEGORY_GROUPS
358+ # Also update category name from effective category groups
352359 # (group is derived from category by data_manager, not stored)
353360 # Build category_id → category_name lookup
361+ category_groups = get_effective_category_groups (self .config_dir )
354362 category_name = None
355- for group_name , category_names in DEFAULT_CATEGORY_GROUPS .items ():
363+ for group_name , category_names in category_groups .items ():
356364 for cat_name in category_names :
357365 cat_id = f"cat_{ cat_name .lower ().replace (' ' , '_' ).replace ('&' , 'and' )} "
358366 if cat_id == category_id :
0 commit comments