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 .licensees import Licensee
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_licensee (
17- context : Context = Provide [Container .context ],
18- mpt_client : AsyncMPTClient = Provide [Container .mpt_client ],
19- ) -> Licensee | None :
20- """Get licensee from context or fetch from API."""
21- licensee_id = context .get_string ("accounts.licensee.id" )
22- if not licensee_id :
23- return None
24- try :
25- licensee = context .get_resource ("accounts.licensee" , licensee_id )
26- except ValueError :
27- licensee = None
28- if not isinstance (licensee , Licensee ):
29- licensee = await mpt_client .accounts .licensees .get (licensee_id )
30- context .set_resource ("accounts.licensee" , licensee )
31- context ["accounts.licensee.id" ] = licensee .id
32- return licensee
33- return licensee
34-
35-
3615@inject
3716def build_licensee_data ( # noqa: WPS238
3817 context : Context = Provide [Container .context ],
3918) -> dict [str , object ]:
4019 """Get licensee data dictionary for creation."""
41- account_id = os .getenv ("CLIENT_ACCOUNT_ID" )
42- if not account_id :
43- raise ValueError ("CLIENT_ACCOUNT_ID environment variable is required" )
44- seller_id = context .get_string ("accounts.seller.id" )
45- if not seller_id :
46- raise ValueError ("Seller ID is required in context" )
47- buyer_id = context .get_string ("accounts.buyer.id" )
48- if not buyer_id :
49- raise ValueError ("Buyer ID is required in context" )
50- group = context .get_resource ("accounts.user_group" )
51- if group is None :
52- raise ValueError ("User group is required in context" )
20+ account_id = require_context_id (context , "accounts.account.id" , "creating licensee" )
21+ seller_id = require_context_id (context , "accounts.seller.id" , "create licensee" )
22+ buyer_id = require_context_id (context , "accounts.buyer.id" , "create licensee" )
23+ user_group_id = require_context_id (context , "accounts.user_group.id" , "create licensee" )
24+
5325 licensee_type = "Client"
5426 return {
5527 "name" : "E2E Seeded Licensee" ,
@@ -65,39 +37,23 @@ def build_licensee_data( # noqa: WPS238
6537 "buyer" : {"id" : buyer_id },
6638 "account" : {"id" : account_id },
6739 "eligibility" : {"client" : True , "partner" : False },
68- "groups" : [{"id" : group . id }],
40+ "groups" : [{"id" : user_group_id }],
6941 "type" : licensee_type ,
7042 "status" : "Enabled" ,
7143 "defaultLanguage" : "en-US" ,
7244 }
7345
7446
7547@inject
76- async def init_licensee (
77- context : Context = Provide [Container .context ],
78- mpt_client : AsyncMPTClient = Provide [Container .mpt_client ],
79- ) -> Licensee :
80- """Get or create licensee."""
81- licensee = await get_licensee (context = context , mpt_client = mpt_client )
82- if licensee is None :
83- licensee_data = build_licensee_data (context = context )
84- logger .debug ("Creating licensee ..." )
85- with ICON .open ("rb" ) as icon_file :
86- created = await mpt_client .accounts .licensees .create (licensee_data , file = icon_file )
87- if isinstance (created , Licensee ):
88- context .set_resource ("accounts.licensee" , created )
89- context ["accounts.licensee.id" ] = created .id
90- logger .info ("Licensee created: %s" , created .id )
91- return created
92- logger .warning ("Licensee creation failed" ) # type: ignore[unreachable]
93- raise ValueError ("Licensee creation failed" )
94- logger .info ("Licensee found: %s" , licensee .id )
95- return licensee
48+ async def create_licensee (mpt_client : AsyncMPTClient = Provide [Container .mpt_client ]) -> Licensee :
49+ """Create licensee."""
50+ licensee_data = build_licensee_data ()
51+ with ICON .open ("rb" ) as icon_fd :
52+ return await mpt_client .accounts .licensees .create (licensee_data , file = icon_fd )
9653
9754
98- @inject
9955async def seed_licensee () -> None :
10056 """Seed licensee."""
10157 logger .debug ("Seeding licensee ..." )
102- await init_licensee ( )
58+ await init_resource ( "accounts.licensee.id" , create_licensee )
10359 logger .info ("Seeding licensee completed." )
0 commit comments