Skip to content

Commit 8cba004

Browse files
author
NoyanAziz
authored
feat: add scripts for adding custom fields to returnitem and transaction (#281)
1 parent 30d452b commit 8cba004

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

commerce_coordinator/apps/commercetools/catalog_info/constants.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ class TwoUKeys:
3333
PAYPAL_CONNECTOR_CONTAINER = 'paypal-commercetools-connector'
3434
PAYPAL_CONNECTOR_SETTINGS_KEY = 'settings'
3535

36+
# Return Item Types
37+
RETURN_ITEM_CUSTOM_TYPE = 'returnItemCustomType'
38+
39+
# Return Item Custom Fields
40+
TRANSACTION_ID = 'transactionId'
41+
42+
# Transaction Types
43+
TRANSACTION_CUSTOM_TYPE = 'transactionCustomType'
44+
45+
# Transaction Custom Fields
46+
RETURN_ITEM_ID = 'returnItemId'
47+
3648

3749
class EdXFieldNames:
3850
"""edX Specific field names for use in Commercetools"""

commerce_coordinator/apps/commercetools/catalog_info/foundational_types.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,50 @@ class TwoUCustomTypes:
188188
]
189189
)
190190

191+
RETURN_ITEM_TYPE_DRAFT = TypeDraft(
192+
key=TwoUKeys.RETURN_ITEM_CUSTOM_TYPE,
193+
name=ls({'en': 'Return Item Custom Type'}),
194+
resource_type_ids=[ResourceTypeId.ORDER_RETURN_ITEM],
195+
# ^^^ this cannot be updated, the whole type has to be unassigned, removed and replaced.
196+
field_definitions=[
197+
# Updating Field Definitions only supports:
198+
# - basic field definitions changes, like label and input_hint, not type or
199+
# - whether it is required or not.
200+
# - It can add new ones.
201+
# If you need something done that can't be, the whole type has to be unassigned, removed and replaced.
202+
203+
FieldDefinition(
204+
type=CustomFieldStringType(),
205+
name=TwoUKeys.TRANSACTION_ID,
206+
required=False,
207+
label=ls({'en': 'Transaction ID'}),
208+
input_hint=TypeTextInputHint.SINGLE_LINE
209+
)
210+
]
211+
)
212+
213+
TRANSACTION_TYPE_DRAFT = TypeDraft(
214+
key=TwoUKeys.TRANSACTION_CUSTOM_TYPE,
215+
name=ls({'en': 'Transaction Custom Type'}),
216+
resource_type_ids=[ResourceTypeId.TRANSACTION],
217+
# ^^^ this cannot be updated, the whole type has to be unassigned, removed and replaced.
218+
field_definitions=[
219+
# Updating Field Definitions only supports:
220+
# - basic field definitions changes, like label and input_hint, not type or
221+
# - whether it is required or not.
222+
# - It can add new ones.
223+
# If you need something done that can't be, the whole type has to be unassigned, removed and replaced.
224+
225+
FieldDefinition(
226+
type=CustomFieldStringType(),
227+
name=TwoUKeys.RETURN_ITEM_ID,
228+
required=False,
229+
label=ls({'en': 'Return Item ID'}),
230+
input_hint=TypeTextInputHint.SINGLE_LINE
231+
)
232+
]
233+
)
234+
191235

192236
class TwoUCustomObjects:
193237
PAYPAL_CUSTOM_OBJECT_DRAFT = CustomObjectDraft(
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import json
2+
3+
from commercetools import CommercetoolsError
4+
5+
from commerce_coordinator.apps.commercetools.catalog_info.foundational_types import TwoUCustomTypes
6+
from commerce_coordinator.apps.commercetools.management.commands._ct_api_client_command import (
7+
CommercetoolsAPIClientCommand
8+
)
9+
10+
11+
class Command(CommercetoolsAPIClientCommand):
12+
help = 'Create a custom type for returnItem with field transactionId'
13+
14+
def handle(self, *args, **options):
15+
current_draft = TwoUCustomTypes.RETURN_ITEM_TYPE_DRAFT
16+
type_key = current_draft.key
17+
18+
try:
19+
ret = self.ct_api_client.base_client.types.get_by_key(type_key)
20+
print(f"ReturnItem custom type with field transactionId already exists: {json.dumps(ret.serialize())}")
21+
except CommercetoolsError as ex:
22+
ret = self.ct_api_client.base_client.types.create(
23+
TwoUCustomTypes.RETURN_ITEM_TYPE_DRAFT
24+
)
25+
print(f"Created ReturnItem custom type with field transactionId: {json.dumps(ret.serialize())}")
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import json
2+
3+
from commercetools import CommercetoolsError
4+
5+
from commerce_coordinator.apps.commercetools.catalog_info.foundational_types import TwoUCustomTypes
6+
from commerce_coordinator.apps.commercetools.management.commands._ct_api_client_command import (
7+
CommercetoolsAPIClientCommand
8+
)
9+
10+
11+
class Command(CommercetoolsAPIClientCommand):
12+
help = 'Create a custom type for transactions with field returnItemId'
13+
14+
def handle(self, *args, **options):
15+
current_draft = TwoUCustomTypes.TRANSACTION_TYPE_DRAFT
16+
type_key = current_draft.key
17+
18+
try:
19+
ret = self.ct_api_client.base_client.types.get_by_key(type_key)
20+
print(f"Transaction custom type with field returnItemId already exists: {json.dumps(ret.serialize())}")
21+
except CommercetoolsError as ex:
22+
ret = self.ct_api_client.base_client.types.create(
23+
TwoUCustomTypes.TRANSACTION_TYPE_DRAFT
24+
)
25+
print(f"Created Transaction custom type with field returnItemId: {json.dumps(ret.serialize())}")

0 commit comments

Comments
 (0)