Skip to content

Commit

Permalink
feat: add scripts for adding custom fields to returnitem and transact…
Browse files Browse the repository at this point in the history
…ion (#281)
  • Loading branch information
NoyanAziz authored Oct 18, 2024
1 parent 30d452b commit 8cba004
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
12 changes: 12 additions & 0 deletions commerce_coordinator/apps/commercetools/catalog_info/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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())}")
Original file line number Diff line number Diff line change
@@ -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())}")

0 comments on commit 8cba004

Please sign in to comment.