Skip to content

Commit

Permalink
[1.2.0] Custom application (cont.) - update custom type
Browse files Browse the repository at this point in the history
  • Loading branch information
tdang1-shopmacher committed Dec 3, 2024
1 parent 5411c8d commit 9ff4c55
Showing 1 changed file with 80 additions and 0 deletions.
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;
}
}

0 comments on commit 9ff4c55

Please sign in to comment.