diff --git a/commerce_coordinator/apps/commercetools/catalog_info/constants.py b/commerce_coordinator/apps/commercetools/catalog_info/constants.py index 70d67c60..62b86e56 100644 --- a/commerce_coordinator/apps/commercetools/catalog_info/constants.py +++ b/commerce_coordinator/apps/commercetools/catalog_info/constants.py @@ -33,6 +33,18 @@ class TwoUKeys: PAYPAL_CONNECTOR_CONTAINER = 'paypal-commercetools-connector' PAYPAL_CONNECTOR_SETTINGS_KEY = 'settings' + # Return Item Types + RETURN_ITEM_CUSTOM_TYPE = 'returnItemCustomType' + + # Return Item Custom Fields + TRANSACTION_ID = 'transactionId' + + # Transaction Types + TRANSACTION_CUSTOM_TYPE = 'transactionCustomType' + + # Transaction Custom Fields + RETURN_ITEM_ID = 'returnItemId' + class EdXFieldNames: """edX Specific field names for use in Commercetools""" diff --git a/commerce_coordinator/apps/commercetools/catalog_info/foundational_types.py b/commerce_coordinator/apps/commercetools/catalog_info/foundational_types.py index 4613b204..215ea38a 100644 --- a/commerce_coordinator/apps/commercetools/catalog_info/foundational_types.py +++ b/commerce_coordinator/apps/commercetools/catalog_info/foundational_types.py @@ -188,6 +188,50 @@ class TwoUCustomTypes: ] ) + RETURN_ITEM_TYPE_DRAFT = TypeDraft( + key=TwoUKeys.RETURN_ITEM_CUSTOM_TYPE, + name=ls({'en': 'Return Item Custom Type'}), + resource_type_ids=[ResourceTypeId.ORDER_RETURN_ITEM], + # ^^^ this cannot be updated, the whole type has to be unassigned, removed and replaced. + field_definitions=[ + # Updating Field Definitions only supports: + # - basic field definitions changes, like label and input_hint, not type or + # - whether it is required or not. + # - It can add new ones. + # If you need something done that can't be, the whole type has to be unassigned, removed and replaced. + + FieldDefinition( + type=CustomFieldStringType(), + name=TwoUKeys.TRANSACTION_ID, + required=False, + label=ls({'en': 'Transaction ID'}), + input_hint=TypeTextInputHint.SINGLE_LINE + ) + ] + ) + + TRANSACTION_TYPE_DRAFT = TypeDraft( + key=TwoUKeys.TRANSACTION_CUSTOM_TYPE, + name=ls({'en': 'Transaction Custom Type'}), + resource_type_ids=[ResourceTypeId.TRANSACTION], + # ^^^ this cannot be updated, the whole type has to be unassigned, removed and replaced. + field_definitions=[ + # Updating Field Definitions only supports: + # - basic field definitions changes, like label and input_hint, not type or + # - whether it is required or not. + # - It can add new ones. + # If you need something done that can't be, the whole type has to be unassigned, removed and replaced. + + FieldDefinition( + type=CustomFieldStringType(), + name=TwoUKeys.RETURN_ITEM_ID, + required=False, + label=ls({'en': 'Return Item ID'}), + input_hint=TypeTextInputHint.SINGLE_LINE + ) + ] + ) + class TwoUCustomObjects: PAYPAL_CUSTOM_OBJECT_DRAFT = CustomObjectDraft( diff --git a/commerce_coordinator/apps/commercetools/management/commands/create_returnitem_custom_type.py b/commerce_coordinator/apps/commercetools/management/commands/create_returnitem_custom_type.py new file mode 100644 index 00000000..216f39eb --- /dev/null +++ b/commerce_coordinator/apps/commercetools/management/commands/create_returnitem_custom_type.py @@ -0,0 +1,25 @@ +import json + +from commercetools import CommercetoolsError + +from commerce_coordinator.apps.commercetools.catalog_info.foundational_types import TwoUCustomTypes +from commerce_coordinator.apps.commercetools.management.commands._ct_api_client_command import ( + CommercetoolsAPIClientCommand +) + + +class Command(CommercetoolsAPIClientCommand): + help = 'Create a custom type for returnItem with field transactionId' + + def handle(self, *args, **options): + current_draft = TwoUCustomTypes.RETURN_ITEM_TYPE_DRAFT + type_key = current_draft.key + + try: + ret = self.ct_api_client.base_client.types.get_by_key(type_key) + print(f"ReturnItem custom type with field transactionId already exists: {json.dumps(ret.serialize())}") + except CommercetoolsError as ex: + ret = self.ct_api_client.base_client.types.create( + TwoUCustomTypes.RETURN_ITEM_TYPE_DRAFT + ) + print(f"Created ReturnItem custom type with field transactionId: {json.dumps(ret.serialize())}") diff --git a/commerce_coordinator/apps/commercetools/management/commands/create_transaction_custom_type.py b/commerce_coordinator/apps/commercetools/management/commands/create_transaction_custom_type.py new file mode 100644 index 00000000..8d99426d --- /dev/null +++ b/commerce_coordinator/apps/commercetools/management/commands/create_transaction_custom_type.py @@ -0,0 +1,25 @@ +import json + +from commercetools import CommercetoolsError + +from commerce_coordinator.apps.commercetools.catalog_info.foundational_types import TwoUCustomTypes +from commerce_coordinator.apps.commercetools.management.commands._ct_api_client_command import ( + CommercetoolsAPIClientCommand +) + + +class Command(CommercetoolsAPIClientCommand): + help = 'Create a custom type for transactions with field returnItemId' + + def handle(self, *args, **options): + current_draft = TwoUCustomTypes.TRANSACTION_TYPE_DRAFT + type_key = current_draft.key + + try: + ret = self.ct_api_client.base_client.types.get_by_key(type_key) + print(f"Transaction custom type with field returnItemId already exists: {json.dumps(ret.serialize())}") + except CommercetoolsError as ex: + ret = self.ct_api_client.base_client.types.create( + TwoUCustomTypes.TRANSACTION_TYPE_DRAFT + ) + print(f"Created Transaction custom type with field returnItemId: {json.dumps(ret.serialize())}")