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

MOL-451/Bugfix: Add Transaction Custom Type #100

Merged
merged 2 commits into from
Nov 20, 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
80 changes: 80 additions & 0 deletions processor/src/commercetools/customFields.commercetools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,83 @@ export async function createCustomPaymentTransactionCancelReasonType(): Promise<
.execute();
}
}

export async function createTransactionSurchargeCustomType(): Promise<void> {
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;
}
}
2 changes: 2 additions & 0 deletions processor/src/service/connector.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import {
createCustomPaymentType,
createCustomPaymentInterfaceInteractionType,
createCustomPaymentTransactionCancelReasonType,
createTransactionSurchargeCustomType,
} from '../commercetools/customFields.commercetools';
export const createExtensionAndCustomFields = async (extensionUrl: string): Promise<void> => {
await createPaymentExtension(extensionUrl);
await createCustomPaymentType();
await createCustomPaymentInterfaceInteractionType();
await createCustomPaymentTransactionCancelReasonType();
await createTransactionSurchargeCustomType();
};

export const removeExtension = async (): Promise<void> => {
Expand Down
9 changes: 3 additions & 6 deletions processor/src/service/payment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ import {
changeTransactionState,
changeTransactionTimestamp,
setCustomFields,
setTransactionCustomField,
setTransactionCustomType,
} from '../commercetools/action.commercetools';
import { readConfiguration } from '../utils/config.utils';
Expand Down Expand Up @@ -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,
}),
);
}

Expand Down
18 changes: 1 addition & 17 deletions processor/tests/commercetools/action.commercetools.spec.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -8,7 +8,6 @@ import {
changeTransactionTimestamp,
removeCustomLineItem,
setCustomFields,
setTransactionCustomField,
setTransactionCustomType,
} from '../../src/commercetools/action.commercetools';
import { CTTransactionState, CreateInterfaceInteractionParams } from '../../src/types/commercetools.types';
Expand Down Expand Up @@ -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,
});
});
});
3 changes: 3 additions & 0 deletions processor/tests/routes/processor.route.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
createCustomPaymentType,
createCustomPaymentInterfaceInteractionType,
createCustomPaymentTransactionCancelReasonType,
createTransactionSurchargeCustomType,
} from '../../src/commercetools/customFields.commercetools';

jest.mock('../../src/commercetools/extensions.commercetools', () => ({
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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',
Expand Down
36 changes: 21 additions & 15 deletions processor/tests/service/payment.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
});
Expand Down Expand Up @@ -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,
},
];

Expand Down Expand Up @@ -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,
},
];

Expand Down