|
| 1 | +import logging |
| 2 | +import os |
| 3 | +import pathlib |
| 4 | + |
| 5 | +from dependency_injector.wiring import inject |
| 6 | + |
| 7 | +from mpt_api_client import AsyncMPTClient |
| 8 | +from mpt_api_client.resources.accounts.licensees import Licensee |
| 9 | +from seed.context import Context |
| 10 | +from seed.defaults import DEFAULT_CONTEXT, DEFAULT_MPT_CLIENT |
| 11 | + |
| 12 | +logger = logging.getLogger(__name__) |
| 13 | + |
| 14 | +icon = pathlib.Path(__file__).parent / "logo.png" |
| 15 | + |
| 16 | + |
| 17 | +@inject |
| 18 | +async def get_licensee( |
| 19 | + context: Context = DEFAULT_CONTEXT, |
| 20 | + mpt_client: AsyncMPTClient = DEFAULT_MPT_CLIENT, |
| 21 | +) -> Licensee | None: |
| 22 | + """Get licensee from context or fetch from API.""" |
| 23 | + licensee_id = context.get_string("accounts.licensee.id") |
| 24 | + if not licensee_id: |
| 25 | + return None |
| 26 | + try: |
| 27 | + licensee = context.get_resource("accounts.licensee", licensee_id) |
| 28 | + except ValueError: |
| 29 | + licensee = None |
| 30 | + if not isinstance(licensee, Licensee): |
| 31 | + licensee = await mpt_client.accounts.licensees.get(licensee_id) |
| 32 | + context.set_resource("accounts.licensee", licensee) |
| 33 | + context["accounts.licensee.id"] = licensee.id |
| 34 | + return licensee |
| 35 | + return licensee |
| 36 | + |
| 37 | + |
| 38 | +@inject |
| 39 | +def build_licensee_data( |
| 40 | + context: Context = DEFAULT_CONTEXT, |
| 41 | +) -> dict[str, object]: |
| 42 | + """Get licensee data dictionary for creation.""" |
| 43 | + account_id = os.getenv("CLIENT_ACCOUNT_ID") |
| 44 | + seller_id = context.get_string("accounts.seller.id") |
| 45 | + buyer_id = context.get_string("accounts.buyer.id") |
| 46 | + group = context.get_resource("accounts.user_group") |
| 47 | + licensee_type = "Client" |
| 48 | + return { |
| 49 | + "name": "E2E Seeded Licensee", |
| 50 | + "address": { |
| 51 | + "addressLine1": "123 Main St", |
| 52 | + "city": "Los Angeles", |
| 53 | + "state": "CA", |
| 54 | + "postCode": "67890", |
| 55 | + "country": "US", |
| 56 | + }, |
| 57 | + "useBuyerAddress": False, |
| 58 | + "seller": {"id": seller_id}, |
| 59 | + "buyer": {"id": buyer_id}, |
| 60 | + "account": {"id": account_id}, |
| 61 | + "eligibility": {"client": True, "partner": False}, |
| 62 | + "groups": [{"id": group.id}], |
| 63 | + "type": licensee_type, |
| 64 | + "status": "Enabled", |
| 65 | + "defaultLanguage": "en-US", |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | +@inject |
| 70 | +async def init_licensee( |
| 71 | + context: Context = DEFAULT_CONTEXT, |
| 72 | + mpt_client: AsyncMPTClient = DEFAULT_MPT_CLIENT, |
| 73 | +) -> Licensee: |
| 74 | + """Get or create licensee.""" |
| 75 | + licensee = await get_licensee(context=context, mpt_client=mpt_client) |
| 76 | + if licensee is None: |
| 77 | + licensee_data = build_licensee_data(context=context) |
| 78 | + logger.debug("Creating licensee ...") |
| 79 | + with pathlib.Path.open(icon, "rb") as icon_file: |
| 80 | + created = await mpt_client.accounts.licensees.create(licensee_data, file=icon_file) |
| 81 | + if isinstance(created, Licensee): |
| 82 | + context.set_resource("accounts.licensee", created) |
| 83 | + context["accounts.licensee.id"] = created.id |
| 84 | + logger.info("Licensee created: %s", created.id) |
| 85 | + return created |
| 86 | + logger.warning("Licensee creation failed") |
| 87 | + raise ValueError("Licensee creation failed") |
| 88 | + logger.info("Licensee found: %s", licensee.id) |
| 89 | + return licensee |
| 90 | + |
| 91 | + |
| 92 | +@inject |
| 93 | +async def seed_licensee() -> None: |
| 94 | + """Seed licensee.""" |
| 95 | + logger.debug("Seeding licensee ...") |
| 96 | + await init_licensee() |
| 97 | + logger.info("Seeding licensee completed.") |
0 commit comments