From 7dac6300074a8eb98db2e45e7de1d55d231506ba Mon Sep 17 00:00:00 2001 From: Arno Erpenbeck <88486704+arnoerpenbeck@users.noreply.github.com> Date: Fri, 24 May 2024 08:22:16 +0200 Subject: [PATCH 1/2] fix: add field definition for custom type --- shared/src/commercetools/customTypeService.ts | 41 +++++++++++++++---- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/shared/src/commercetools/customTypeService.ts b/shared/src/commercetools/customTypeService.ts index 584ebe5..35ee377 100644 --- a/shared/src/commercetools/customTypeService.ts +++ b/shared/src/commercetools/customTypeService.ts @@ -66,13 +66,14 @@ export async function createCustomOrderType(): Promise { try { const typeKey = await orderCustomTypeKey(); if (typeKey) { + const fieldDefinitions = await requiredFieldDefinitions(); const customType: TypeDraft = { key: typeKey, name: { en: CUSTOM_TYPE_NAME, }, resourceTypeIds: ['order'], - fieldDefinitions: customTypeDefinition.orderCustomTypeFieldDefinitions as FieldDefinition[], + fieldDefinitions, }; const result = await createApiRoot().types().post({ body: customType }).execute(); @@ -97,7 +98,7 @@ export async function updateCustomOrderType(id: string): Promise 0) { const updateActions = missingFields.map((f) => addFieldDefinitionAction(f)); const typeUpdate = updateTypeAction(customType, updateActions); @@ -130,12 +131,38 @@ export async function orderCustomTypeKey(): Promise { return key; } -function listOfMissingFields(existingFields: FieldDefinition[]): FieldDefinition[] { - const requiredFieldNames = customTypeDefinition.orderCustomTypeFieldDefinitions.map((f) => f.name); +async function collectChannelReferenceFieldName(): Promise { + let name: string | undefined; + const configuration = await getConfiguration(); + if (configuration) { + name = configuration.collectChannelReferenceFieldName; + } + return name; +} + +async function requiredFieldDefinitions(): Promise { + const requiredFieldDefinitions = customTypeDefinition.orderCustomTypeFieldDefinitions as FieldDefinition[]; + const fieldName = await collectChannelReferenceFieldName(); + if (fieldName) { + requiredFieldDefinitions.push({ + name: fieldName, + label: { + en: 'FFT Click & Collect Facility', + }, + required: false, + type: { + name: 'String', + }, + }); + } + return requiredFieldDefinitions; +} + +async function listOfMissingFields(existingFields: FieldDefinition[]): Promise { + const requiredFields = await requiredFieldDefinitions(); + const requiredFieldNames = requiredFields.map((f) => f.name); const existingFieldNames = existingFields.map((f) => f.name); const missingFieldNames = requiredFieldNames.filter((f) => !existingFieldNames.includes(f)); - const missingFields = customTypeDefinition.orderCustomTypeFieldDefinitions.filter((f) => - missingFieldNames.includes(f.name) - ); + const missingFields = requiredFields.filter((f) => missingFieldNames.includes(f.name)); return missingFields as FieldDefinition[]; } From a5ce254fcb091bbd471e24b0aa5adf097cf65f23 Mon Sep 17 00:00:00 2001 From: Arno Erpenbeck <88486704+arnoerpenbeck@users.noreply.github.com> Date: Fri, 24 May 2024 08:27:27 +0200 Subject: [PATCH 2/2] fix: Sonar issue --- shared/src/commercetools/customTypeService.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/shared/src/commercetools/customTypeService.ts b/shared/src/commercetools/customTypeService.ts index 35ee377..a11b5c9 100644 --- a/shared/src/commercetools/customTypeService.ts +++ b/shared/src/commercetools/customTypeService.ts @@ -163,6 +163,5 @@ async function listOfMissingFields(existingFields: FieldDefinition[]): Promise f.name); const existingFieldNames = existingFields.map((f) => f.name); const missingFieldNames = requiredFieldNames.filter((f) => !existingFieldNames.includes(f)); - const missingFields = requiredFields.filter((f) => missingFieldNames.includes(f.name)); - return missingFields as FieldDefinition[]; + return requiredFields.filter((f) => missingFieldNames.includes(f.name)); }