Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [SONIC-724] Add scripts for adding custom fields to returnitem and transaction #281

Merged
merged 4 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())}")
Loading