diff --git a/processor/src/commercetools/customFields.commercetools.ts b/processor/src/commercetools/customFields.commercetools.ts index 3ee0586..7bd9e4e 100644 --- a/processor/src/commercetools/customFields.commercetools.ts +++ b/processor/src/commercetools/customFields.commercetools.ts @@ -299,3 +299,83 @@ export async function createCustomPaymentTransactionCancelReasonType(): Promise< .execute(); } } + +export async function createTransactionSurchargeCustomType(): Promise { + const apiRoot = createApiRoot(); + const customFields: FieldDefinition[] = [ + { + name: 'surchargeAmountInCent', + label: { + en: 'Total surcharge amount in cent', + de: 'Gesamtbetrag des Zuschlags in Cent', + }, + required: false, + type: { + name: 'Number', + }, + inputHint: 'MultiLine', + }, + ]; + + const { + body: { results: types }, + } = await apiRoot + .types() + .get({ + queryArgs: { + where: `key = "${CustomFields.transactionSurchargeCost}"`, + }, + }) + .execute(); + + if (types.length <= 0) { + await apiRoot + .types() + .post({ + body: { + key: CustomFields.createPayment.interfaceInteraction.key, + name: { + en: 'SCTM - Transaction surcharge amount', + de: 'SCTM - Betrag des Transaktionszuschlags', + }, + resourceTypeIds: ['transaction'], + fieldDefinitions: customFields, + }, + }) + .execute(); + + return; + } + + const type = types[0]; + const definitions = type.fieldDefinitions; + + if (definitions.length > 0) { + const actions: TypeUpdateAction[] = []; + definitions.forEach((definition) => { + actions.push({ + action: 'removeFieldDefinition', + fieldName: definition.name, + }); + }); + customFields.forEach((field) => { + actions.push({ + action: 'addFieldDefinition', + fieldDefinition: field, + }); + }); + + await apiRoot + .types() + .withKey({ key: CustomFields.transactionSurchargeCost }) + .post({ + body: { + version: type.version, + actions, + }, + }) + .execute(); + + return; + } +} diff --git a/processor/src/service/connector.service.ts b/processor/src/service/connector.service.ts index 9e9fd8d..f611771 100644 --- a/processor/src/service/connector.service.ts +++ b/processor/src/service/connector.service.ts @@ -3,12 +3,14 @@ import { createCustomPaymentType, createCustomPaymentInterfaceInteractionType, createCustomPaymentTransactionCancelReasonType, + createTransactionSurchargeCustomType, } from '../commercetools/customFields.commercetools'; export const createExtensionAndCustomFields = async (extensionUrl: string): Promise => { await createPaymentExtension(extensionUrl); await createCustomPaymentType(); await createCustomPaymentInterfaceInteractionType(); await createCustomPaymentTransactionCancelReasonType(); + await createTransactionSurchargeCustomType(); }; export const removeExtension = async (): Promise => { diff --git a/processor/src/service/payment.service.ts b/processor/src/service/payment.service.ts index ccdfad5..7ff0d01 100644 --- a/processor/src/service/payment.service.ts +++ b/processor/src/service/payment.service.ts @@ -54,7 +54,6 @@ import { changeTransactionState, changeTransactionTimestamp, setCustomFields, - setTransactionCustomField, setTransactionCustomType, } from '../commercetools/action.commercetools'; import { readConfiguration } from '../utils/config.utils'; @@ -503,11 +502,9 @@ export const getCreatePaymentUpdateAction = async ( if (surchargeAmountInCent > 0) { // Add surcharge amount to the custom field of the transaction actions.push( - setTransactionCustomField( - CustomFields.transactionSurchargeCost, - JSON.stringify({ surchargeAmountInCent }), - originalTransaction.id, - ), + setTransactionCustomType(originalTransaction.id, CustomFields.transactionSurchargeCost, { + surchargeAmountInCent, + }), ); } diff --git a/processor/tests/commercetools/action.commercetools.spec.ts b/processor/tests/commercetools/action.commercetools.spec.ts index 3035ef1..1d2d9e5 100644 --- a/processor/tests/commercetools/action.commercetools.spec.ts +++ b/processor/tests/commercetools/action.commercetools.spec.ts @@ -1,4 +1,4 @@ -import { ConnectorActions, CustomFields, MOLLIE_SURCHARGE_CUSTOM_LINE_ITEM } from '../../src/utils/constant.utils'; +import { ConnectorActions, MOLLIE_SURCHARGE_CUSTOM_LINE_ITEM } from '../../src/utils/constant.utils'; import { describe, test, expect, jest } from '@jest/globals'; import { addCustomLineItem, @@ -8,7 +8,6 @@ import { changeTransactionTimestamp, removeCustomLineItem, setCustomFields, - setTransactionCustomField, setTransactionCustomType, } from '../../src/commercetools/action.commercetools'; import { CTTransactionState, CreateInterfaceInteractionParams } from '../../src/types/commercetools.types'; @@ -183,19 +182,4 @@ describe('Test actions.utils.ts', () => { slug, }); }); - - test('should be able to return the correct setTransactionCustomField action', () => { - const name = CustomFields.transactionSurchargeCost; - const surchargeInCentAmount = { - surchargeInCentAmount: 12345, - }; - const transactionId = 'test'; - - expect(setTransactionCustomField(name, JSON.stringify(surchargeInCentAmount), transactionId)).toStrictEqual({ - action: 'setTransactionCustomField', - name, - value: JSON.stringify(surchargeInCentAmount), - transactionId, - }); - }); }); diff --git a/processor/tests/routes/processor.route.spec.ts b/processor/tests/routes/processor.route.spec.ts index 768eb0d..d01c1d5 100644 --- a/processor/tests/routes/processor.route.spec.ts +++ b/processor/tests/routes/processor.route.spec.ts @@ -7,6 +7,7 @@ import { createCustomPaymentType, createCustomPaymentInterfaceInteractionType, createCustomPaymentTransactionCancelReasonType, + createTransactionSurchargeCustomType, } from '../../src/commercetools/customFields.commercetools'; jest.mock('../../src/commercetools/extensions.commercetools', () => ({ @@ -18,6 +19,7 @@ jest.mock('../../src/commercetools/customFields.commercetools', () => ({ createCustomPaymentType: jest.fn(), createCustomPaymentInterfaceInteractionType: jest.fn(), createCustomPaymentTransactionCancelReasonType: jest.fn(), + createTransactionSurchargeCustomType: jest.fn(), })); describe('Test src/route/processor.route.ts', () => { @@ -109,6 +111,7 @@ describe('Test src/route/processor.route.ts', () => { (createCustomPaymentType as jest.Mock).mockReturnValueOnce(Promise.resolve()); (createCustomPaymentInterfaceInteractionType as jest.Mock).mockReturnValueOnce(Promise.resolve()); (createCustomPaymentTransactionCancelReasonType as jest.Mock).mockReturnValueOnce(Promise.resolve()); + (createTransactionSurchargeCustomType as jest.Mock).mockReturnValueOnce(Promise.resolve()); req = { hostname: 'test.com', diff --git a/processor/tests/service/payment.service.spec.ts b/processor/tests/service/payment.service.spec.ts index 27a5147..e51aeb9 100644 --- a/processor/tests/service/payment.service.spec.ts +++ b/processor/tests/service/payment.service.spec.ts @@ -1206,11 +1206,13 @@ describe('Test getCreatePaymentUpdateAction', () => { }); expect(actual[4]).toEqual({ - action: 'setTransactionCustomField', - name: CustomFieldName.transactionSurchargeCost, - value: JSON.stringify({ + action: 'setTransactionCustomType', + type: { + key: 'sctm_transaction_surcharge_cost', + }, + fields: { surchargeAmountInCent: 1000, - }), + }, transactionId: CTPayment.transactions[0].id, }); }); @@ -1421,12 +1423,14 @@ describe('Test handleCreatePayment', () => { state: 'Pending', }, { - action: 'setTransactionCustomField', - name: 'sctm_transaction_surcharge_cost', - transactionId: '5c8b0375-305a-4f19-ae8e-07806b101999', - value: JSON.stringify({ - surchargeAmountInCent: totalSurchargeAmount, - }), + action: 'setTransactionCustomType', + type: { + key: 'sctm_transaction_surcharge_cost', + }, + fields: { + surchargeAmountInCent: 1020, + }, + transactionId: CTPayment.transactions[0].id, }, ]; @@ -1590,12 +1594,14 @@ describe('Test handleCreatePayment', () => { state: 'Pending', }, { - action: 'setTransactionCustomField', - name: 'sctm_transaction_surcharge_cost', - transactionId: '5c8b0375-305a-4f19-ae8e-07806b101999', - value: JSON.stringify({ + action: 'setTransactionCustomType', + type: { + key: 'sctm_transaction_surcharge_cost', + }, + fields: { surchargeAmountInCent: totalSurchargeAmount, - }), + }, + transactionId: CTPayment.transactions[0].id, }, ];