|
| 1 | +import logging |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +from dependency_injector.wiring import inject |
| 5 | + |
| 6 | +from mpt_api_client import AsyncMPTClient |
| 7 | +from mpt_api_client.resources.catalog.items import Item |
| 8 | +from seed.context import Context |
| 9 | +from seed.defaults import ( |
| 10 | + DEFAULT_CONTEXT, |
| 11 | + DEFAULT_MPT_OPERATIONS, |
| 12 | + DEFAULT_MPT_VENDOR, |
| 13 | +) |
| 14 | + |
| 15 | +logger = logging.getLogger(__name__) |
| 16 | + |
| 17 | + |
| 18 | +@inject |
| 19 | +async def refresh_item( |
| 20 | + context: Context = DEFAULT_CONTEXT, |
| 21 | + mpt_vendor: AsyncMPTClient = DEFAULT_MPT_VENDOR, |
| 22 | +) -> Item | None: |
| 23 | + """Refresh item in context (always fetch).""" |
| 24 | + item_id = context.get_string("catalog.item.id") |
| 25 | + if not item_id: |
| 26 | + return None |
| 27 | + item_resource = await mpt_vendor.catalog.items.get(item_id) |
| 28 | + context["catalog.item.id"] = item_resource.id |
| 29 | + context.set_resource("catalog.item", item_resource) |
| 30 | + return item_resource |
| 31 | + |
| 32 | + |
| 33 | +@inject |
| 34 | +async def get_item( |
| 35 | + context: Context = DEFAULT_CONTEXT, |
| 36 | + mpt_vendor: AsyncMPTClient = DEFAULT_MPT_VENDOR, |
| 37 | +) -> Item | None: |
| 38 | + """Get item from context or fetch from API if not cached.""" |
| 39 | + item_id = context.get_string("catalog.item.id") |
| 40 | + if not item_id: |
| 41 | + return None |
| 42 | + try: |
| 43 | + catalog_item = context.get_resource("catalog.item", item_id) |
| 44 | + except ValueError: |
| 45 | + catalog_item = None |
| 46 | + if not isinstance(catalog_item, Item): |
| 47 | + logger.debug("Loading item: %s", item_id) |
| 48 | + catalog_item = await mpt_vendor.catalog.items.get(item_id) |
| 49 | + context["catalog.item.id"] = catalog_item.id |
| 50 | + context.set_resource("catalog.item", catalog_item) |
| 51 | + return catalog_item |
| 52 | + return catalog_item |
| 53 | + |
| 54 | + |
| 55 | +@inject |
| 56 | +def build_item(context: Context = DEFAULT_CONTEXT) -> dict[str, Any]: |
| 57 | + """Build item data dictionary for creation.""" |
| 58 | + product_id = context.get("catalog.product.id") |
| 59 | + item_group_id = context.get("catalog.item_group.id") |
| 60 | + return { |
| 61 | + "product": {"id": product_id}, |
| 62 | + "parameters": [], |
| 63 | + "name": "Product Item 1", |
| 64 | + "description": "Product Item 1 - Description", |
| 65 | + "group": {"id": item_group_id}, |
| 66 | + "unit": { |
| 67 | + "id": "UNT-1229", |
| 68 | + "name": "<string> 1", |
| 69 | + "revision": 1, |
| 70 | + "description": "<string>TEST", |
| 71 | + "statistics": {"itemCount": 34}, |
| 72 | + }, |
| 73 | + "terms": {"model": "quantity", "period": "1m", "commitment": "1m"}, |
| 74 | + "quantityNotApplicable": False, |
| 75 | + "externalIds": {"vendor": "item_1"}, |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | +@inject |
| 80 | +async def create_item( |
| 81 | + context: Context = DEFAULT_CONTEXT, |
| 82 | + mpt_vendor: AsyncMPTClient = DEFAULT_MPT_VENDOR, |
| 83 | +) -> Item: |
| 84 | + """Create item and cache in context.""" |
| 85 | + item_data = build_item(context=context) |
| 86 | + catalog_item = await mpt_vendor.catalog.items.create(item_data) |
| 87 | + context["catalog.item.id"] = catalog_item.id |
| 88 | + context.set_resource("catalog.item", catalog_item) |
| 89 | + return catalog_item |
| 90 | + |
| 91 | + |
| 92 | +@inject |
| 93 | +async def review_item( |
| 94 | + context: Context = DEFAULT_CONTEXT, |
| 95 | + mpt_vendor: AsyncMPTClient = DEFAULT_MPT_VENDOR, |
| 96 | +) -> Item | None: |
| 97 | + """Review item if in draft status and cache result.""" |
| 98 | + logger.debug("Reviewing catalog.item ...") |
| 99 | + catalog_item = context.get_resource("catalog.item") |
| 100 | + if catalog_item.status != "Draft": |
| 101 | + return catalog_item # type: ignore[return-value] |
| 102 | + catalog_item = await mpt_vendor.catalog.items.review(catalog_item.id) |
| 103 | + context.set_resource("catalog.item", catalog_item) |
| 104 | + return catalog_item |
| 105 | + |
| 106 | + |
| 107 | +@inject |
| 108 | +async def publish_item( |
| 109 | + context: Context = DEFAULT_CONTEXT, |
| 110 | + mpt_operations: AsyncMPTClient = DEFAULT_MPT_OPERATIONS, |
| 111 | +) -> Item | None: |
| 112 | + """Publish item if in reviewing status and cache result.""" |
| 113 | + logger.debug("Publishing catalog.item ...") |
| 114 | + catalog_item = context.get_resource("catalog.item") |
| 115 | + if catalog_item.status != "Reviewing": |
| 116 | + return catalog_item # type: ignore[return-value] |
| 117 | + catalog_item = await mpt_operations.catalog.items.publish(catalog_item.id) |
| 118 | + context.set_resource("catalog.item", catalog_item) |
| 119 | + return catalog_item |
| 120 | + |
| 121 | + |
| 122 | +@inject |
| 123 | +async def seed_items( |
| 124 | + context: Context = DEFAULT_CONTEXT, |
| 125 | + mpt_vendor: AsyncMPTClient = DEFAULT_MPT_VENDOR, |
| 126 | + mpt_operations: AsyncMPTClient = DEFAULT_MPT_OPERATIONS, |
| 127 | +) -> None: |
| 128 | + """Seed catalog items (create/review/publish).""" |
| 129 | + logger.debug("Seeding catalog.item ...") |
| 130 | + existing = await refresh_item(context=context, mpt_vendor=mpt_vendor) |
| 131 | + if not existing: |
| 132 | + await create_item(context=context, mpt_vendor=mpt_vendor) |
| 133 | + await review_item(context=context, mpt_vendor=mpt_vendor) |
| 134 | + await publish_item(context=context, mpt_operations=mpt_operations) |
| 135 | + logger.debug("Seeded catalog.item completed.") |
0 commit comments