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.client_account.id" , "creating licensee" )
21+ seller_id = require_context_id (context , "accounts.seller.id" , "creating licensee" )
22+ buyer_id = require_context_id (context , "accounts.buyer.id" , "creating licensee" )
23+ group_id = require_context_id (context , "accounts.user_group.id" , "creating licensee" )
24+
5325 licensee_type = "Client"
26+
5427 return {
5528 "name" : "E2E Seeded Licensee" ,
5629 "address" : {
@@ -65,39 +38,27 @@ def build_licensee_data( # noqa: WPS238
6538 "buyer" : {"id" : buyer_id },
6639 "account" : {"id" : account_id },
6740 "eligibility" : {"client" : True , "partner" : False },
68- "groups" : [{"id" : group . id }],
41+ "groups" : [{"id" : group_id }],
6942 "type" : licensee_type ,
7043 "status" : "Enabled" ,
7144 "defaultLanguage" : "en-US" ,
7245 }
7346
7447
7548@inject
76- async def init_licensee (
49+ async def create_licensee (
7750 context : Context = Provide [Container .context ],
7851 mpt_client : AsyncMPTClient = Provide [Container .mpt_client ],
7952) -> 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
53+ """Creates a licensee."""
54+ licensee_data = build_licensee_data (context = context )
55+ logger .debug ("Creating licensee ..." )
56+ with ICON .open ("rb" ) as icon_fd :
57+ return await mpt_client .accounts .licensees .create (licensee_data , file = icon_fd )
9658
9759
98- @inject
9960async def seed_licensee () -> None :
10061 """Seed licensee."""
10162 logger .debug ("Seeding licensee ..." )
102- await init_licensee ( )
63+ await init_resource ( "accounts.licensee.id" , create_licensee )
10364 logger .info ("Seeding licensee completed." )
0 commit comments