"""Work with graphql requests for getting/creating vendor/subaccount.""" import config from src.constants import general, graphql_queries def get_vendor_for_rep_owner_code( logger, sm_context) -> list: """Lookup Orchard vendor_id for the rep owner code.""" logger.info('Reading vendor from GraphQL for given rep owner code.') parent_repertoire_owner_code = (sm_context.product. parent_repertoire_owner_code) vendors = config.ows_client.graphql_query( service_name=config.GRAPHQL_SERVICE, query=graphql_queries.get_vendor, variables={ 'externalIdentifier': parent_repertoire_owner_code, 'owner': config.OWNER}, **config.GRAPHQL_HEADERS, correlation_id=sm_context.correlation_id ).json()['data']['vendorsByExternalIdentifier'] return vendors def get_subaccount_for_vendor( logger, sm_context, vendor_id: int) -> list: """Lookup Orchard subaccount_id for the rep owner code.""" logger.info( 'Reading subaccount from GraphQL for vendor_id and repertoire_owner.') all_subaccounts = config.ows_client.graphql_query( service_name=config.GRAPHQL_SERVICE, query=graphql_queries.get_subaccounts, variables={ 'limit': config.LIMIT_SUBACCOUNTS, 'vendorId': vendor_id}, **config.GRAPHQL_HEADERS, correlation_id=sm_context.correlation_id, ).json()['data']['vendor']['subaccounts']['subaccounts'] subaccount_name = sm_context.product.repertoire_owner_name subaccounts = list( filter( lambda s: s['name'] == subaccount_name, all_subaccounts )) return subaccounts def get_product_by_upc( logger, sm_context) -> dict: """Check if product already exists .""" logger.info('Reading product from GraphQL for given upc.') product = config.ows_client.graphql_query( service_name=config.GRAPHQL_SERVICE, query=graphql_queries.get_product, variables={ 'upc': sm_context.product.upc}, **config.GRAPHQL_HEADERS, correlation_id=sm_context.correlation_id ).json()['data']['productByUpc'] return product def create_new_vendor( logger, sm_context) -> dict: """Create vendor in GraphQL.""" logger.info('Creating vendor in GraphQL.') vendor = config.ows_client.graphql_query( service_name=config.GRAPHQL_SERVICE, query=graphql_queries.create_vendor, variables={ 'input': { 'name': sm_context.product.parent_repertoire_owner_name, 'companyBrandName': general.COMPANY_BRAND_NAME, 'currency': general.CURRENCY, 'owner': general.OWNER, 'serviceTierUuid': general.SERVICE_TIER_UUID, 'isDistributor': general.IS_DISTRIBUTOR, } }, **config.GRAPHQL_HEADERS, correlation_id=sm_context.correlation_id ).json()['data']['createVendor'] return vendor def update_external_identifier_1( logger, sm_context, vendor_uuid: str) -> dict: """Update external identifier 1 in GraphQL.""" logger.info('Updating external_identifier_1 in GraphQL.') externalIdentifier1 = sm_context.product.parent_repertoire_owner_code vendor = config.ows_client.graphql_query( service_name=config.GRAPHQL_SERVICE, query=graphql_queries.update_external_identifier_1, variables={ 'uuid': vendor_uuid, 'externalIdentifier1': externalIdentifier1 }, **config.GRAPHQL_HEADERS, correlation_id=sm_context.correlation_id ).json()['data']['updateVendorExternalIdentifier1'] return vendor def update_vendor_country_id( logger, country_id: int, vendor_uuid: str) -> dict: """Update vendor country id in GraphQL.""" logger.info( f'Updating country id in GraphQL. vendor_uuid {vendor_uuid}' f'country_id {country_id}') vendor = config.ows_client.graphql_query( service_name=config.GRAPHQL_SERVICE, query=graphql_queries.update_country_id, variables={ 'uuid': vendor_uuid, 'countryId': country_id }, **config.GRAPHQL_HEADERS ).json()['data'] return vendor def create_new_subaccount(logger, sm_context, vendor_id: int) -> dict: """Create subaccount in GraphQL.""" logger.info('Creating subaccount in GraphQL.') subaccount = config.ows_client.graphql_query( service_name=config.GRAPHQL_SERVICE, query=graphql_queries.create_subaccount, variables={ 'input': { 'subaccountName': sm_context.product.repertoire_owner_name, 'vendorId': vendor_id, 'description': sm_context.product.repertoire_owner_code, } }, **config.GRAPHQL_HEADERS, correlation_id=sm_context.correlation_id ).json()['data']['createSubaccount'] return subaccount