11import logging
2- import os
32
43from dependency_injector .wiring import Provide , inject
54
65from mpt_api_client import AsyncMPTClient
76from mpt_api_client .resources .accounts .buyers import Buyer
87from seed .container import Container
98from seed .context import Context
9+ from seed .helper import init_resource , require_context_id
1010from seed .static .static import ICON
1111
1212logger = logging .getLogger (__name__ )
1313
1414
15- @inject
16- async def get_buyer (
17- context : Context = Provide [Container .context ],
18- mpt_operations : AsyncMPTClient = Provide [Container .mpt_operations ],
19- ) -> Buyer | None :
20- """Get buyer from context or fetch from API."""
21- buyer_id = context .get_string ("accounts.buyer.id" )
22- if not buyer_id :
23- return None
24- try :
25- buyer = context .get_resource ("accounts.buyer" , buyer_id )
26- except ValueError :
27- buyer = None
28- if not isinstance (buyer , Buyer ):
29- buyer = await mpt_operations .accounts .buyers .get (buyer_id )
30- context .set_resource ("accounts.buyer" , buyer )
31- context ["accounts.buyer.id" ] = buyer .id
32- return buyer
33- return buyer
34-
35-
3615@inject
3716def build_buyer_data (context : Context = Provide [Container .context ]) -> dict [str , object ]:
3817 """Build buyer data dictionary for creation."""
39- buyer_account_id = os .getenv ("CLIENT_ACCOUNT_ID" )
40- if not buyer_account_id :
41- raise ValueError ("CLIENT_ACCOUNT_ID environment variable is required" )
42- seller_id = context .get_string ("accounts.seller.id" )
43- if not seller_id :
44- raise ValueError ("accounts.seller.id missing from context; seed seller before buyer." )
18+ client_account_id = require_context_id (context , "accounts.client_account.id" , "creating buyer" )
19+ seller_id = require_context_id (context , "accounts.seller.id" , "creating buyer" )
20+
4521 return {
4622 "name" : "E2E Seeded Buyer" ,
47- "account" : {"id" : buyer_account_id },
23+ "account" : {"id" : client_account_id },
4824 "sellers" : [{"id" : seller_id }],
4925 "contact" : {
5026 "firstName" : "first" ,
@@ -62,31 +38,19 @@ def build_buyer_data(context: Context = Provide[Container.context]) -> dict[str,
6238
6339
6440@inject
65- async def init_buyer (
41+ async def create_buyer (
6642 context : Context = Provide [Container .context ],
6743 mpt_operations : AsyncMPTClient = Provide [Container .mpt_operations ],
6844) -> Buyer :
69- """Get or create buyer."""
70- buyer = await get_buyer (context = context , mpt_operations = mpt_operations )
71- if buyer is None :
72- buyer_data = build_buyer_data (context = context )
73- logger .debug ("Creating buyer ..." )
74- with ICON .open ("rb" ) as icon_fd :
75- created = await mpt_operations .accounts .buyers .create (buyer_data , file = icon_fd )
76- if isinstance (created , Buyer ):
77- context .set_resource ("accounts.buyer" , created )
78- context ["accounts.buyer.id" ] = created .id
79- logger .info ("Buyer created: %s" , created .id )
80- return created
81- logger .warning ("Buyer creation failed" )
82- raise ValueError ("Buyer creation failed" )
83- logger .info ("Buyer found: %s" , buyer .id )
84- return buyer
45+ """Creates a buyer."""
46+ buyer_data = build_buyer_data (context = context )
47+ logger .debug ("Creating buyer ..." )
48+ with ICON .open ("rb" ) as icon_fd :
49+ return await mpt_operations .accounts .buyers .create (buyer_data , file = icon_fd )
8550
8651
87- @inject
8852async def seed_buyer () -> None :
8953 """Seed buyer."""
9054 logger .debug ("Seeding buyer ..." )
91- await init_buyer ( )
55+ await init_resource ( "accounts.buyer.id" , create_buyer )
9256 logger .debug ("Seeding buyer completed." )
0 commit comments